~vila/u1-test-utils/sudo-opt-in

« back to all changes in this revision

Viewing changes to setup_vm/tests/test_setup_vm.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2013-08-08 16:37:16 UTC
  • mfrom: (91.1.4 pep8)
  • Revision ID: tarmac-20130808163716-lgpyrzxei2aizk46
[r=elopio] Add pep8/pyflakes for setup_vm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
        super(TestVmStores, self).setUp()
78
78
        self.conf = sm.VmStack('foo')
79
79
 
80
 
 
81
80
    def test_default_in_empty_stack(self):
82
81
        self.assertEqual('1024', self.conf.get('vm.ram_size'))
83
82
 
84
 
 
85
83
    def test_system_overrides_internal(self):
86
84
        self.conf.system_store._load_from_string('vm.ram_size = 42')
87
85
        self.assertEqual('42', self.conf.get('vm.ram_size'))
113
111
 
114
112
    def test_raring_iso_url(self):
115
113
        self.assertValue('http://cdimage.ubuntu.com/daily-live/current/',
116
 
                         'vm.iso_url' )
 
114
                         'vm.iso_url')
117
115
 
118
116
    def test_raring_iso_name(self):
119
 
        self.assertValue( 'raring-desktop-amd64.iso', 'vm.iso_name')
 
117
        self.assertValue('raring-desktop-amd64.iso', 'vm.iso_name')
120
118
 
121
119
    def test_raring_cloud_image_url(self):
122
120
        self.assertValue('http://cloud-images.ubuntu.com/raring/current/',
256
254
        return sm.yaml.safe_dump(*args, **kwargs)
257
255
 
258
256
    def test_load_scalar(self):
259
 
        self.assertEqual({'foo': 'bar'}, self.yaml_load(StringIO('{foo: bar}')))
 
257
        self.assertEqual(
 
258
            {'foo': 'bar'}, self.yaml_load(StringIO('{foo: bar}')))
260
259
        # Surprisingly the enclosing braces are not needed, probably a special
261
260
        # case for the highest level
262
261
        self.assertEqual({'foo': 'bar'}, self.yaml_load(StringIO('foo: bar')))
276
275
 
277
276
    def test_dump_list(self):
278
277
        # No more enclosing braces... yeah for consistency :-/
279
 
        self.assertEqual('foo: [a, b, c]\n',
280
 
                         self.yaml_dump(dict(foo=['a', 'b', 'c'])))
 
278
        self.assertEqual(
 
279
            'foo: [a, b, c]\n', self.yaml_dump(dict(foo=['a', 'b', 'c'])))
281
280
 
282
281
    def test_load_dict(self):
283
282
        self.assertEqual({'foo': {'bar': 'baz'}},
284
 
                          self.yaml_load(StringIO('{foo: {bar: baz}}')))
 
283
                         self.yaml_load(StringIO('{foo: {bar: baz}}')))
285
284
        multiple_lines = '''\
286
285
foo: {bar: multiple
287
286
  lines}
288
287
'''
289
 
        self.assertEqual({'foo': {'bar': 'multiple lines'}},
290
 
                          self.yaml_load(StringIO(multiple_lines)))
291
 
 
 
288
        self.assertEqual(
 
289
            {'foo': {'bar': 'multiple lines'}},
 
290
            self.yaml_load(StringIO(multiple_lines)))
292
291
 
293
292
 
294
293
class TestLaunchpadAccess(TestCaseWithHome):
305
304
                              self.ci_data.set_launchpad_access)
306
305
        key_path = '~/.ssh/I-dont-exist@setup_vm'
307
306
        self.assertEqual(key_path, e.path)
308
 
        self.assertTrue(unicode(e).startswith(
309
 
                'You need to create the {p} keypair'.format(p=key_path)))
 
307
        msg_prefix = 'You need to create the {p} keypair'.format(p=key_path)
 
308
        self.assertTrue(unicode(e).startswith(msg_prefix))
310
309
 
311
310
    def test_id_with_key(self):
312
311
        ssh_dir = os.path.join(self.home_dir, '.ssh')
324
323
chmod 0700 ~ubuntu
325
324
chown ubuntu:ubuntu /home/ubuntu/.ssh
326
325
chmod 0700 /home/ubuntu/.ssh
327
 
cat >/home/ubuntu/.ssh/id_rsa <<'EOSETUPVMCONTENTREALLYUNIQUEDONTBREAKFORFUN'
 
326
cat >/home/ubuntu/.ssh/id_rsa <<'EOSETUPVMUNIQUECONTENTDONTBREAKFORFUN'
328
327
key content
329
 
EOSETUPVMCONTENTREALLYUNIQUEDONTBREAKFORFUN
 
328
EOSETUPVMUNIQUECONTENTDONTBREAKFORFUN
330
329
chown ubuntu:ubuntu /home/ubuntu/.ssh/id_rsa
331
330
chmod 0400 /home/ubuntu/.ssh/id_rsa
332
331
''',
388
387
    def test_poweroff_other(self):
389
388
        self.conf.store._load_from_string('vm.release = raring')
390
389
        self.ci_data.populate()
391
 
        self.assertEqual({'mode': 'poweroff'},
392
 
                          self.ci_data.cloud_config['power_state'])
 
390
        self.assertEqual(
 
391
            {'mode': 'poweroff'}, self.ci_data.cloud_config['power_state'])
393
392
        self.assertIs(None, self.ci_data.cloud_config.get('runcmd'))
394
393
 
395
394
    def test_update_true(self):
420
419
            [{'source': 'deb http://archive.ubuntu.com/ubuntu raring partner'},
421
420
             {'source': 'deb http://archive.ubuntu.com/ubuntu raring main'},
422
421
             {'source':
423
 
                  'deb https://u:p@ppa.lp.net/user/ppa/ubuntu raring main',
 
422
              'deb https://u:p@ppa.lp.net/user/ppa/ubuntu raring main',
424
423
              'keyid': 'ABCDEF'}],
425
424
            self.ci_data.cloud_config['apt_sources'])
426
425
 
492
491
        self.conf.store._load_from_string(
493
492
            '''vm.uploaded_scripts = I-dont-exist ''')
494
493
        self.assertRaises(sm.ConfigPathNotFound,
495
 
                              self.ci_data.populate)
 
494
                          self.ci_data.populate)
496
495
 
497
496
    def test_root_script(self):
498
497
        with open('root_script.sh', 'w') as f:
507
506
        # The additional newline after the script is expected
508
507
        self.assertEqual('''\
509
508
#!/bin/sh
510
 
cat >~root/setup_vm_post_install <<'EOSETUPVMCONTENTREALLYUNIQUEDONTBREAKFORFUN'
 
509
cat >~root/setup_vm_post_install <<'EOSETUPVMUNIQUECONTENTDONTBREAKFORFUN'
511
510
#!/bin/sh
512
511
echo Hello root
513
512
 
514
 
EOSETUPVMCONTENTREALLYUNIQUEDONTBREAKFORFUN
 
513
EOSETUPVMUNIQUECONTENTDONTBREAKFORFUN
515
514
chown root:root ~root/setup_vm_post_install
516
515
chmod 0700 ~root/setup_vm_post_install
517
516
''', self.ci_data.root_hook)
531
530
        # The additional newline after the script is expected
532
531
        self.assertEqual('''\
533
532
#!/bin/sh
534
 
cat >~ubuntu/setup_vm_post_install <<'EOSETUPVMCONTENTREALLYUNIQUEDONTBREAKFORFUN'
 
533
cat >~ubuntu/setup_vm_post_install <<'EOSETUPVMUNIQUECONTENTDONTBREAKFORFUN'
535
534
#!/bin/sh
536
535
echo Hello ubuntu
537
536
 
538
 
EOSETUPVMCONTENTREALLYUNIQUEDONTBREAKFORFUN
 
537
EOSETUPVMUNIQUECONTENTDONTBREAKFORFUN
539
538
chown ubuntu:ubuntu ~ubuntu/setup_vm_post_install
540
539
chmod 0700 ~ubuntu/setup_vm_post_install
541
540
''', self.ci_data.ubuntu_hook)
555
554
        self.ci_data.populate()
556
555
        self.assertEqual('''\
557
556
#!/bin/sh
558
 
cat >~ubuntu/setup_vm_uploads <<'EOSETUPVMCONTENTREALLYUNIQUEDONTBREAKFORFUN'
 
557
cat >~ubuntu/setup_vm_uploads <<'EOSETUPVMUNIQUECONTENTDONTBREAKFORFUN'
559
558
mkdir -p ~ubuntu/bin
560
559
cd ~ubuntu/bin
561
560
cat >foo <<'EOFfoo'
570
569
 
571
570
EOFbar
572
571
chmod 0755 bar
573
 
EOSETUPVMCONTENTREALLYUNIQUEDONTBREAKFORFUN
 
572
EOSETUPVMUNIQUECONTENTDONTBREAKFORFUN
574
573
chown ubuntu:ubuntu ~ubuntu/setup_vm_uploads
575
574
chmod 0700 ~ubuntu/setup_vm_uploads
576
575
''',
717
716
        self.assertEqual(expected, sm.vm_states(lines))
718
717
 
719
718
    def test_empty(self):
720
 
        self.assertStates({},[])
 
719
        self.assertStates({}, [])
721
720
 
722
721
    def test_garbage(self):
723
722
        self.assertRaises(ValueError, self.assertStates, None, [''])
941
940
        self.assertTrue(os.path.exists(public_path))
942
941
        public = file(public_path).read()
943
942
        private = file(private_path).read()
944
 
        self.assertTrue(private.startswith(
945
 
                '-----BEGIN %s PRIVATE KEY-----\n' % (upper_type,)))
946
 
        self.assertTrue(private.endswith(
947
 
                '-----END %s PRIVATE KEY-----\n' % (upper_type,)))
 
943
        self.assertTrue(
 
944
            private.startswith('-----BEGIN %s PRIVATE KEY-----\n'
 
945
                               % (upper_type,)))
 
946
        self.assertTrue(
 
947
            private.endswith('-----END %s PRIVATE KEY-----\n' % (upper_type,)))
948
948
        return private, public
949
949
 
950
950
    def test_dsa(self):
990
990
        self.assertFalse(ns.install)
991
991
        self.assertTrue(ns.download)
992
992
 
 
993
 
993
994
class TestBuildCommands(testtools.TestCase):
994
995
 
995
996
    def setUp(self):
1061
1062
            def install(self):
1062
1063
                self.install_called = True
1063
1064
 
1064
 
 
1065
1065
        self.vm = FakeKvm(self.conf)
1066
1066
        cmd = sm.Install(self.vm)
1067
1067
        cmd.run()