~oddbloke/cloud-init/lp1506187

« back to all changes in this revision

Viewing changes to tests/unittests/test_datasource/test_azure.py

  • Committer: Daniel Watkins
  • Date: 2015-10-30 16:26:31 UTC
  • Revision ID: daniel.watkins@canonical.com-20151030162631-xmsz2bdgf25zb6d1
Use DMI data to find Azure instance IDs.

This replaces the use of SharedConfig.xml in both the walinuxagent case,
and the case where we communicate with the Azure fabric ourselves.

Show diffs side-by-side

added added

removed removed

Lines of Context:
115
115
            data['pubkey_files'] = flist
116
116
            return ["pubkey_from: %s" % f for f in flist]
117
117
 
118
 
        def _iid_from_shared_config(path):
119
 
            data['iid_from_shared_cfg'] = path
120
 
            return 'i-my-azure-id'
121
 
 
122
118
        if data.get('ovfcontent') is not None:
123
119
            populate_dir(os.path.join(self.paths.seed_dir, "azure"),
124
120
                         {'ovf-env.xml': data['ovfcontent']})
127
123
        mod.BUILTIN_DS_CONFIG['data_dir'] = self.waagent_d
128
124
 
129
125
        self.get_metadata_from_fabric = mock.MagicMock(return_value={
130
 
            'instance-id': 'i-my-azure-id',
131
126
            'public-keys': [],
132
127
        })
133
128
 
 
129
        self.instance_id = 'test-instance-id'
 
130
 
134
131
        self.apply_patches([
135
132
            (mod, 'list_possible_azure_ds_devs', dsdevs),
136
133
            (mod, 'invoke_agent', _invoke_agent),
137
134
            (mod, 'wait_for_files', _wait_for_files),
138
135
            (mod, 'pubkeys_from_crt_files', _pubkeys_from_crt_files),
139
 
            (mod, 'iid_from_shared_config', _iid_from_shared_config),
140
136
            (mod, 'perform_hostname_bounce', mock.MagicMock()),
141
137
            (mod, 'get_hostname', mock.MagicMock()),
142
138
            (mod, 'set_hostname', mock.MagicMock()),
143
139
            (mod, 'get_metadata_from_fabric', self.get_metadata_from_fabric),
 
140
            (mod.util, 'read_dmi_data', mock.MagicMock(
 
141
                return_value=self.instance_id)),
144
142
        ])
145
143
 
146
144
        dsrc = mod.DataSourceAzureNet(
193
191
        self.assertEqual(dsrc.metadata['local-hostname'], odata['HostName'])
194
192
        self.assertTrue(os.path.isfile(
195
193
            os.path.join(self.waagent_d, 'ovf-env.xml')))
196
 
        self.assertEqual(dsrc.metadata['instance-id'], 'i-my-azure-id')
197
194
 
198
195
    def test_waagent_d_has_0700_perms(self):
199
196
        # we expect /var/lib/waagent to be created 0700
345
342
        for mypk in mypklist:
346
343
            self.assertIn(mypk['value'], dsrc.metadata['public-keys'])
347
344
 
348
 
 
349
345
    def test_default_ephemeral(self):
350
346
        # make sure the ephemeral device works
351
347
        odata = {}
434
430
        dsrc = self._get_ds({'ovfcontent': xml})
435
431
        dsrc.get_data()
436
432
 
437
 
    def test_existing_ovf_same(self):
438
 
        # waagent/SharedConfig left alone if found ovf-env.xml same as cached
439
 
        odata = {'UserData': b64e("SOMEUSERDATA")}
440
 
        data = {'ovfcontent': construct_valid_ovf_env(data=odata)}
441
 
 
442
 
        populate_dir(self.waagent_d,
443
 
            {'ovf-env.xml': data['ovfcontent'],
444
 
             'otherfile': 'otherfile-content',
445
 
             'SharedConfig.xml': 'mysharedconfig'})
446
 
 
447
 
        dsrc = self._get_ds(data)
448
 
        ret = dsrc.get_data()
449
 
        self.assertTrue(ret)
450
 
        self.assertTrue(os.path.exists(
451
 
            os.path.join(self.waagent_d, 'ovf-env.xml')))
452
 
        self.assertTrue(os.path.exists(
453
 
            os.path.join(self.waagent_d, 'otherfile')))
454
 
        self.assertTrue(os.path.exists(
455
 
            os.path.join(self.waagent_d, 'SharedConfig.xml')))
456
 
 
457
 
    def test_existing_ovf_diff(self):
458
 
        # waagent/SharedConfig must be removed if ovfenv is found elsewhere
459
 
 
460
 
        # 'get_data' should remove SharedConfig.xml in /var/lib/waagent
461
 
        # if ovf-env.xml differs.
462
 
        cached_ovfenv = construct_valid_ovf_env(
463
 
            {'userdata': b64e("FOO_USERDATA")})
464
 
        new_ovfenv = construct_valid_ovf_env(
465
 
            {'userdata': b64e("NEW_USERDATA")})
466
 
 
467
 
        populate_dir(self.waagent_d,
468
 
            {'ovf-env.xml': cached_ovfenv,
469
 
             'SharedConfig.xml': "mysharedconfigxml",
470
 
             'otherfile': 'otherfilecontent'})
471
 
 
472
 
        dsrc = self._get_ds({'ovfcontent': new_ovfenv})
473
 
        ret = dsrc.get_data()
474
 
        self.assertTrue(ret)
475
 
        self.assertEqual(dsrc.userdata_raw, b"NEW_USERDATA")
476
 
        self.assertTrue(os.path.exists(
477
 
            os.path.join(self.waagent_d, 'otherfile')))
478
 
        self.assertFalse(os.path.exists(
479
 
                        os.path.join(self.waagent_d, 'SharedConfig.xml')))
480
 
        self.assertTrue(os.path.exists(
481
 
                        os.path.join(self.waagent_d, 'ovf-env.xml')))
482
 
        new_xml = load_file(os.path.join(self.waagent_d, 'ovf-env.xml'))
483
 
        self.xml_equals(new_ovfenv, new_xml)
484
 
 
485
433
    def test_exception_fetching_fabric_data_doesnt_propagate(self):
486
434
        ds = self._get_ds({'ovfcontent': construct_valid_ovf_env()})
487
435
        ds.ds_cfg['agent_command'] = '__builtin__'
496
444
        self.assertTrue(ret)
497
445
        self.assertEqual('value', ds.metadata['test'])
498
446
 
 
447
    def test_instance_id_from_dmidecode_used(self):
 
448
        ds = self._get_ds({'ovfcontent': construct_valid_ovf_env()})
 
449
        ds.get_data()
 
450
        self.assertEqual(self.instance_id, ds.metadata['instance-id'])
 
451
 
 
452
    def test_instance_id_from_dmidecode_used_for_builtin(self):
 
453
        ds = self._get_ds({'ovfcontent': construct_valid_ovf_env()})
 
454
        ds.ds_cfg['agent_command'] = '__builtin__'
 
455
        ds.get_data()
 
456
        self.assertEqual(self.instance_id, ds.metadata['instance-id'])
 
457
 
499
458
 
500
459
class TestAzureBounce(TestCase):
501
460
 
505
464
        self.patches.enter_context(
506
465
            mock.patch.object(DataSourceAzure, 'wait_for_files'))
507
466
        self.patches.enter_context(
508
 
            mock.patch.object(DataSourceAzure, 'iid_from_shared_config',
509
 
                              mock.MagicMock(return_value='i-my-azure-id')))
510
 
        self.patches.enter_context(
511
467
            mock.patch.object(DataSourceAzure, 'list_possible_azure_ds_devs',
512
468
                              mock.MagicMock(return_value=[])))
513
469
        self.patches.enter_context(
521
477
        self.patches.enter_context(
522
478
            mock.patch.object(DataSourceAzure, 'get_metadata_from_fabric',
523
479
                              mock.MagicMock(return_value={})))
 
480
        self.patches.enter_context(
 
481
            mock.patch.object(DataSourceAzure.util, 'read_dmi_data',
 
482
                              mock.MagicMock(return_value='test-instance-id')))
524
483
 
525
484
    def setUp(self):
526
485
        super(TestAzureBounce, self).setUp()