-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Eliminate redundant configuration reads (#5536)
When instance id hasn't changed and datasource hasn't changed, don't forcibly reload the configuration.
- Loading branch information
Showing
2 changed files
with
115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from typing import cast | ||
|
||
import pytest | ||
from pycloudlib.lxd.instance import LXDInstance | ||
|
||
from cloudinit import subp | ||
from tests.integration_tests.instances import IntegrationInstance | ||
from tests.integration_tests.integration_settings import PLATFORM | ||
|
||
_INSTANCE_ID = 0 | ||
|
||
|
||
def setup_meta_data(instance: LXDInstance): | ||
"""Increment the instance id and apply it to the instance.""" | ||
global _INSTANCE_ID | ||
_INSTANCE_ID += 1 | ||
command = [ | ||
"lxc", | ||
"config", | ||
"set", | ||
instance.name, | ||
f"user.meta-data=instance-id: test_{_INSTANCE_ID}", | ||
] | ||
subp.subp(command) | ||
|
||
|
||
# class TestInstanceID: | ||
@pytest.mark.skipif( | ||
PLATFORM not in ["lxd_container", "lxd_vm"], | ||
reason="Uses lxd-specific behavior.", | ||
) | ||
@pytest.mark.lxd_setup.with_args(setup_meta_data) | ||
@pytest.mark.lxd_use_exec | ||
def test_instance_id_changes(client: IntegrationInstance): | ||
"""Verify instance id change behavior | ||
If the id from the datasource changes, cloud-init should update the | ||
instance id link. | ||
""" | ||
client.execute("cloud-init status --wait") | ||
# check that instance id is the one we set | ||
assert ( | ||
"test_1" | ||
== client.execute("cloud-init query instance-id").stdout.rstrip() | ||
) | ||
assert ( | ||
"/var/lib/cloud/instances/test_1" | ||
== client.execute( | ||
"readlink -f /var/lib/cloud/instance" | ||
).stdout.rstrip() | ||
) | ||
|
||
instance = cast(LXDInstance, client.instance) | ||
setup_meta_data(instance) | ||
client.restart() | ||
client.execute("cloud-init status --wait") | ||
# check that instance id is the one we reset | ||
assert ( | ||
"test_2" | ||
== client.execute("cloud-init query instance-id").stdout.rstrip() | ||
) | ||
assert ( | ||
"/var/lib/cloud/instances/test_2" | ||
== client.execute( | ||
"readlink -f /var/lib/cloud/instance" | ||
).stdout.rstrip() | ||
) | ||
|
||
|
||
@pytest.mark.lxd_use_exec | ||
def test_instance_id_no_changes(client: IntegrationInstance): | ||
"""Verify instance id no change behavior | ||
If the id from the datasource does not change, cloud-init should not | ||
update the instance id link. | ||
""" | ||
instance_id = client.execute( | ||
"cloud-init query instance-id" | ||
).stdout.rstrip() | ||
assert ( | ||
f"/var/lib/cloud/instances/{instance_id}" | ||
== client.execute( | ||
"readlink -f /var/lib/cloud/instance" | ||
).stdout.rstrip() | ||
) | ||
client.restart() | ||
client.execute("cloud-init status --wait") | ||
assert ( | ||
instance_id | ||
== client.execute("cloud-init query instance-id").stdout.rstrip() | ||
) | ||
assert ( | ||
f"/var/lib/cloud/instances/{instance_id}" | ||
== client.execute( | ||
"readlink -f /var/lib/cloud/instance" | ||
).stdout.rstrip() | ||
) |