summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Watkins <daniel.watkins@canonical.com>2015-05-08 12:59:57 +0100
committerDaniel Watkins <daniel.watkins@canonical.com>2015-05-08 12:59:57 +0100
commit7ca682408f857fcfd04bfc026ea6c697c1fd4b86 (patch)
tree9920074fe549900a24a6d0b0ce081448a1c26a82
parent4a2b6ef37578b13d7240dc1447bbb715b8a0a077 (diff)
downloadvyos-cloud-init-7ca682408f857fcfd04bfc026ea6c697c1fd4b86.tar.gz
vyos-cloud-init-7ca682408f857fcfd04bfc026ea6c697c1fd4b86.zip
Make find_endpoint a staticmethod to clean up top-level namespace.
-rw-r--r--cloudinit/sources/DataSourceAzure.py84
-rw-r--r--tests/unittests/test_datasource/test_azure.py21
2 files changed, 57 insertions, 48 deletions
diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
index c783732d..ba4afa5f 100644
--- a/cloudinit/sources/DataSourceAzure.py
+++ b/cloudinit/sources/DataSourceAzure.py
@@ -70,22 +70,6 @@ BUILTIN_CLOUD_CONFIG = {
DS_CFG_PATH = ['datasource', DS_NAME]
DEF_EPHEMERAL_LABEL = 'Temporary Storage'
-REPORT_READY_XML_TEMPLATE = """\
-<?xml version=\"1.0\" encoding=\"utf-8\"?>
-<Health xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
- <GoalStateIncarnation>{incarnation}</GoalStateIncarnation>
- <Container>
- <ContainerId>{container_id}</ContainerId>
- <RoleInstanceList>
- <Role>
- <InstanceId>{instance_id}</InstanceId>
- <Health>
- <State>Ready</State>
- </Health>
- </Role>
- </RoleInstanceList>
- </Container>
-</Health>"""
@contextmanager
@@ -126,29 +110,6 @@ class AzureEndpointHttpClient(object):
return util.read_file_or_url(url, data=data, headers=headers)
-def find_endpoint():
- LOG.debug('Finding Azure endpoint...')
- content = util.load_file('/var/lib/dhcp/dhclient.eth0.leases')
- value = None
- for line in content.splitlines():
- if 'unknown-245' in line:
- value = line.strip(' ').split(' ', 2)[-1].strip(';\n"')
- if value is None:
- raise Exception('No endpoint found in DHCP config.')
- if ':' in value:
- hex_string = ''
- for hex_pair in value.split(':'):
- if len(hex_pair) == 1:
- hex_pair = '0' + hex_pair
- hex_string += hex_pair
- value = struct.pack('>L', int(hex_string.replace(':', ''), 16))
- else:
- value = value.encode('utf-8')
- endpoint_ip_address = socket.inet_ntoa(value)
- LOG.debug('Azure endpoint found at %s', endpoint_ip_address)
- return endpoint_ip_address
-
-
class GoalState(object):
def __init__(self, xml, http_client):
@@ -268,14 +229,55 @@ class OpenSSLManager(object):
class WALinuxAgentShim(object):
+ REPORT_READY_XML_TEMPLATE = '\n'.join([
+ '<?xml version="1.0" encoding="utf-8"?>',
+ '<Health xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
+ ' xmlns:xsd="http://www.w3.org/2001/XMLSchema">',
+ ' <GoalStateIncarnation>{incarnation}</GoalStateIncarnation>',
+ ' <Container>',
+ ' <ContainerId>{container_id}</ContainerId>',
+ ' <RoleInstanceList>',
+ ' <Role>',
+ ' <InstanceId>{instance_id}</InstanceId>',
+ ' <Health>',
+ ' <State>Ready</State>',
+ ' </Health>',
+ ' </Role>',
+ ' </RoleInstanceList>',
+ ' </Container>',
+ '</Health>'])
+
def __init__(self):
LOG.debug('WALinuxAgentShim instantiated...')
- self.endpoint = find_endpoint()
+ self.endpoint = self.find_endpoint()
self.openssl_manager = OpenSSLManager()
self.http_client = AzureEndpointHttpClient(
self.openssl_manager.certificate)
self.values = {}
+ @staticmethod
+ def find_endpoint():
+ LOG.debug('Finding Azure endpoint...')
+ content = util.load_file('/var/lib/dhcp/dhclient.eth0.leases')
+ value = None
+ for line in content.splitlines():
+ if 'unknown-245' in line:
+ value = line.strip(' ').split(' ', 2)[-1].strip(';\n"')
+ if value is None:
+ raise Exception('No endpoint found in DHCP config.')
+ if ':' in value:
+ hex_string = ''
+ for hex_pair in value.split(':'):
+ if len(hex_pair) == 1:
+ hex_pair = '0' + hex_pair
+ hex_string += hex_pair
+ value = struct.pack('>L', int(hex_string.replace(':', ''), 16))
+ else:
+ value = value.encode('utf-8')
+ endpoint_ip_address = socket.inet_ntoa(value)
+ LOG.debug('Azure endpoint found at %s', endpoint_ip_address)
+ return endpoint_ip_address
+
def register_with_azure_and_fetch_data(self):
LOG.info('Registering with Azure...')
for i in range(10):
@@ -303,7 +305,7 @@ class WALinuxAgentShim(object):
def _report_ready(self, goal_state):
LOG.debug('Reporting ready to Azure fabric.')
- document = REPORT_READY_XML_TEMPLATE.format(
+ document = self.REPORT_READY_XML_TEMPLATE.format(
incarnation=goal_state.incarnation,
container_id=goal_state.container_id,
instance_id=goal_state.instance_id,
diff --git a/tests/unittests/test_datasource/test_azure.py b/tests/unittests/test_datasource/test_azure.py
index fd5b24f8..28703029 100644
--- a/tests/unittests/test_datasource/test_azure.py
+++ b/tests/unittests/test_datasource/test_azure.py
@@ -637,11 +637,13 @@ class TestFindEndpoint(TestCase):
def test_missing_file(self):
self.load_file.side_effect = IOError
- self.assertRaises(IOError, DataSourceAzure.find_endpoint)
+ self.assertRaises(IOError,
+ DataSourceAzure.WALinuxAgentShim.find_endpoint)
def test_missing_special_azure_line(self):
self.load_file.return_value = ''
- self.assertRaises(Exception, DataSourceAzure.find_endpoint)
+ self.assertRaises(Exception,
+ DataSourceAzure.WALinuxAgentShim.find_endpoint)
def _build_lease_content(self, ip_address, use_hex=True):
ip_address_repr = ':'.join(
@@ -661,26 +663,30 @@ class TestFindEndpoint(TestCase):
ip_address = '98.76.54.32'
file_content = self._build_lease_content(ip_address)
self.load_file.return_value = file_content
- self.assertEqual(ip_address, DataSourceAzure.find_endpoint())
+ self.assertEqual(ip_address,
+ DataSourceAzure.WALinuxAgentShim.find_endpoint())
def test_hex_string_with_single_character_part(self):
ip_address = '4.3.2.1'
file_content = self._build_lease_content(ip_address)
self.load_file.return_value = file_content
- self.assertEqual(ip_address, DataSourceAzure.find_endpoint())
+ self.assertEqual(ip_address,
+ DataSourceAzure.WALinuxAgentShim.find_endpoint())
def test_packed_string(self):
ip_address = '98.76.54.32'
file_content = self._build_lease_content(ip_address, use_hex=False)
self.load_file.return_value = file_content
- self.assertEqual(ip_address, DataSourceAzure.find_endpoint())
+ self.assertEqual(ip_address,
+ DataSourceAzure.WALinuxAgentShim.find_endpoint())
def test_latest_lease_used(self):
ip_addresses = ['4.3.2.1', '98.76.54.32']
file_content = '\n'.join([self._build_lease_content(ip_address)
for ip_address in ip_addresses])
self.load_file.return_value = file_content
- self.assertEqual(ip_addresses[-1], DataSourceAzure.find_endpoint())
+ self.assertEqual(ip_addresses[-1],
+ DataSourceAzure.WALinuxAgentShim.find_endpoint())
class TestGoalStateParsing(TestCase):
@@ -856,7 +862,8 @@ class TestWALinuxAgentShim(TestCase):
self.AzureEndpointHttpClient = patches.enter_context(
mock.patch.object(DataSourceAzure, 'AzureEndpointHttpClient'))
self.find_endpoint = patches.enter_context(
- mock.patch.object(DataSourceAzure, 'find_endpoint'))
+ mock.patch.object(
+ DataSourceAzure.WALinuxAgentShim, 'find_endpoint'))
self.GoalState = patches.enter_context(
mock.patch.object(DataSourceAzure, 'GoalState'))
self.iid_from_shared_config_content = patches.enter_context(