Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
sources/azure: ensure instance id is always correct (#1727)
Currently, get_instance_id() assumes that the instance ID is in the metadata.
If not found, it falls back to a hardcoded string "iid-datasource".

Override this behavior to query the instance id as needed.

Signed-off-by: Chris Patterson cpatterson@microsoft.com
  • Loading branch information
cjp256 committed Sep 13, 2022
1 parent 51351f2 commit b861ea8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cloudinit/sources/DataSourceAzure.py
Expand Up @@ -825,6 +825,11 @@ def get_imds_data_with_api_fallback(
)
return {}

def get_instance_id(self):
if not self.metadata or "instance-id" not in self.metadata:
return self._iid()
return str(self.metadata["instance-id"])

def device_name_to_device(self, name):
return self.ds_cfg["disk_aliases"].get(name)

Expand Down
14 changes: 14 additions & 0 deletions tests/unittests/sources/test_azure.py
Expand Up @@ -4030,6 +4030,20 @@ def test_will_not_retry_errors(
]


class TestInstanceId:
def test_metadata(self, azure_ds, mock_dmi_read_dmi_data):
azure_ds.metadata = {"instance-id": "test-id"}

id = azure_ds.get_instance_id()

assert id == "test-id"

def test_fallback(self, azure_ds, mock_dmi_read_dmi_data):
id = azure_ds.get_instance_id()

assert id == "fake-system-uuid"


class TestProvisioning:
@pytest.fixture(autouse=True)
def provisioning_setup(
Expand Down

0 comments on commit b861ea8

Please sign in to comment.