From aca7b668c30e1de991783c6730faea278d5dc4b0 Mon Sep 17 00:00:00 2001 From: James Page Date: Mon, 16 Dec 2013 13:07:26 +0000 Subject: [PATCH] Add test for import of keys with prefix --- unit_tests/test_nova_compute_utils.py | 37 +++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/unit_tests/test_nova_compute_utils.py b/unit_tests/test_nova_compute_utils.py index 6f5fe1aa..6990e4e4 100644 --- a/unit_tests/test_nova_compute_utils.py +++ b/unit_tests/test_nova_compute_utils.py @@ -179,7 +179,7 @@ class NovaComputeUtilsTests(CharmTestCase): self.assertFalse(_open.called) @patch('pwd.getpwnam') - def test_import_authorized_keys(self, getpwnam): + def _test_import_authorized_keys_base(self, getpwnam, prefix=None): getpwnam.return_value = self.fake_user('foo') self.relation_get.side_effect = [ 'Zm9vX2tleQo=', # relation_get('known_hosts') @@ -200,6 +200,38 @@ class NovaComputeUtilsTests(CharmTestCase): self.assertEquals(ex_open, _open.call_args_list) self.assertEquals(ex_write, _file.write.call_args_list) + self.relation_get.assert_has_called([ + call('known_hosts'). + call('authorized_keys') + ]) + + @patch('pwd.getpwnam') + def test_import_authorized_keys_prefix(self, getpwnam): + getpwnam.return_value = self.fake_user('foo') + self.relation_get.side_effect = [ + 'Zm9vX2tleQo=', # relation_get('known_hosts') + 'Zm9vX2hvc3QK', # relation_get('authorized_keys') + ] + + ex_open = [ + call('/home/foo/.ssh/authorized_keys', 'wb'), + call('/home/foo/.ssh/known_hosts', 'wb') + ] + ex_write = [ + call('foo_host\n'), + call('foo_key\n'), + ] + + with patch_open() as (_open, _file): + utils.import_authorized_keys(user='foo', prefix='bar') + self.assertEquals(ex_open, _open.call_args_list) + self.assertEquals(ex_write, _file.write.call_args_list) + + self.relation_get.assert_has_called([ + call('bar_known_hosts'). + call('bar_authorized_keys') + ]) + @patch('subprocess.check_call') def test_import_keystone_cert_missing_data(self, check_call): self.relation_get.return_value = None @@ -256,4 +288,5 @@ class NovaComputeUtilsTests(CharmTestCase): @patch.object(utils, 'check_call') def test_disable_shell(self, _check_call): utils.disable_shell('dummy') - _check_call.assert_called_with(['usermod', '-s', '/bin/false', 'dummy']) + _check_call.assert_called_with(['usermod', '-s', '/bin/false', + 'dummy'])