~cloud-init-dev/cloud-init/trunk

« back to all changes in this revision

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

  • Committer: Scott Moser
  • Date: 2015-10-15 00:27:07 UTC
  • mfrom: (1147.2.1 cloud-init)
  • Revision ID: smoser@ubuntu.com-20151015002707-lplv0v7qzzga9li1
azure: support extracting SSH key values from ovf-env.xml

Azure has or will be offering shortly the ability to directly define the SSH
key value instead of a fingerprint in the ovf-env.xml file. This patch
favors defined SSH keys over the fingerprint method (LP: #1506244).

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
 
55
55
    if pubkeys:
56
56
        content += "<SSH><PublicKeys>\n"
57
 
        for fp, path in pubkeys:
 
57
        for fp, path, value in pubkeys:
58
58
            content += " <PublicKey>"
59
 
            content += ("<Fingerprint>%s</Fingerprint><Path>%s</Path>" %
60
 
                        (fp, path))
 
59
            if fp and path:
 
60
                content += ("<Fingerprint>%s</Fingerprint><Path>%s</Path>" %
 
61
                            (fp, path))
 
62
            if value:
 
63
                content += "<Value>%s</Value>" % value
61
64
            content += "</PublicKey>\n"
62
65
        content += "</PublicKeys></SSH>"
63
66
    content += """
297
300
        self.assertFalse(ret)
298
301
        self.assertFalse('agent_invoked' in data)
299
302
 
300
 
    def test_cfg_has_pubkeys(self):
301
 
        odata = {'HostName': "myhost", 'UserName': "myuser"}
302
 
        mypklist = [{'fingerprint': 'fp1', 'path': 'path1'}]
303
 
        pubkeys = [(x['fingerprint'], x['path']) for x in mypklist]
304
 
        data = {'ovfcontent': construct_valid_ovf_env(data=odata,
305
 
                                                      pubkeys=pubkeys)}
306
 
 
307
 
        dsrc = self._get_ds(data)
308
 
        ret = dsrc.get_data()
309
 
        self.assertTrue(ret)
310
 
        for mypk in mypklist:
311
 
            self.assertIn(mypk, dsrc.cfg['_pubkeys'])
 
303
    def test_cfg_has_pubkeys_fingerprint(self):
 
304
        odata = {'HostName': "myhost", 'UserName': "myuser"}
 
305
        mypklist = [{'fingerprint': 'fp1', 'path': 'path1', 'value': ''}]
 
306
        pubkeys = [(x['fingerprint'], x['path'], x['value']) for x in mypklist]
 
307
        data = {'ovfcontent': construct_valid_ovf_env(data=odata,
 
308
                                                      pubkeys=pubkeys)}
 
309
 
 
310
        dsrc = self._get_ds(data)
 
311
        ret = dsrc.get_data()
 
312
        self.assertTrue(ret)
 
313
        for mypk in mypklist:
 
314
            self.assertIn(mypk, dsrc.cfg['_pubkeys'])
 
315
            self.assertIn('pubkey_from', dsrc.metadata['public-keys'][-1])
 
316
 
 
317
    def test_cfg_has_pubkeys_value(self):
 
318
        # make sure that provided key is used over fingerprint
 
319
        odata = {'HostName': "myhost", 'UserName': "myuser"}
 
320
        mypklist = [{'fingerprint': 'fp1', 'path': 'path1', 'value': 'value1'}]
 
321
        pubkeys = [(x['fingerprint'], x['path'], x['value']) for x in mypklist]
 
322
        data = {'ovfcontent': construct_valid_ovf_env(data=odata,
 
323
                                                      pubkeys=pubkeys)}
 
324
 
 
325
        dsrc = self._get_ds(data)
 
326
        ret = dsrc.get_data()
 
327
        self.assertTrue(ret)
 
328
 
 
329
        for mypk in mypklist:
 
330
            self.assertIn(mypk, dsrc.cfg['_pubkeys'])
 
331
            self.assertIn(mypk['value'], dsrc.metadata['public-keys'])
 
332
 
 
333
    def test_cfg_has_no_fingerprint_has_value(self):
 
334
        # test value is used when fingerprint not provided
 
335
        odata = {'HostName': "myhost", 'UserName': "myuser"}
 
336
        mypklist = [{'fingerprint': None, 'path': 'path1', 'value': 'value1'}]
 
337
        pubkeys = [(x['fingerprint'], x['path'], x['value']) for x in mypklist]
 
338
        data = {'ovfcontent': construct_valid_ovf_env(data=odata,
 
339
                                                      pubkeys=pubkeys)}
 
340
 
 
341
        dsrc = self._get_ds(data)
 
342
        ret = dsrc.get_data()
 
343
        self.assertTrue(ret)
 
344
 
 
345
        for mypk in mypklist:
 
346
            self.assertIn(mypk['value'], dsrc.metadata['public-keys'])
 
347
 
312
348
 
313
349
    def test_default_ephemeral(self):
314
350
        # make sure the ephemeral device works
642
678
            DataSourceAzure.read_azure_ovf, invalid_xml)
643
679
 
644
680
    def test_load_with_pubkeys(self):
645
 
        mypklist = [{'fingerprint': 'fp1', 'path': 'path1'}]
646
 
        pubkeys = [(x['fingerprint'], x['path']) for x in mypklist]
 
681
        mypklist = [{'fingerprint': 'fp1', 'path': 'path1', 'value': ''}]
 
682
        pubkeys = [(x['fingerprint'], x['path'], x['value']) for x in mypklist]
647
683
        content = construct_valid_ovf_env(pubkeys=pubkeys)
648
684
        (_md, _ud, cfg) = DataSourceAzure.read_azure_ovf(content)
649
685
        for mypk in mypklist: