def test_install_ca_cert_old_cert(self, _retrieve_ca_cert): _retrieve_ca_cert.return_value = cert with patch_open() as (_open, _file): apache_utils.install_ca_cert(cert) self.assertFalse(_open.called) self.assertFalse(_file.called) self.assertFalse(self.subprocess.check_call.called)
def test_ssh_compute_remove_missing_key(self): self.setup_mocks_ssh_compute_remove( isfile=False, authorized_keys_lines=[UNIT2_PUBKEY_1]) with patch_open() as (mock_open, mock_file): ssh_migrations.ssh_compute_remove(UNIT1_PUBKEY_1, 'nova-compute-lxd') self.assertFalse(mock_file.write.called)
def test_benchmark_init(self, in_relation_hook, relation_ids, relation_set, relation_get): in_relation_hook.return_value = True relation_data = FAKE_RELATION['benchmark:0']['benchmark/0'] relation_ids.return_value = FAKE_RELATION.keys() relation_get.side_effect = lambda k: relation_data.get(k) actions = ['asdf', 'foobar'] with patch_open() as (_open, _file): b = Benchmark(actions) self.assertIsInstance(b, Benchmark) relation_ids.assert_called_once_with('benchmark') for key in b.required_keys: relation_get.assert_any_call(key) relation_set.assert_called_once_with( relation_id='benchmark:0', relation_settings={'benchmarks': ",".join(actions)}) # Test benchmark.conf _open.assert_called_with('/etc/benchmark.conf', 'w') for key, val in relation_data.items(): _file.write.assert_any_call("%s=%s\n" % (key, val))
def test_create_public_key(self, pwnam, isfile, chown): fake_user = MagicMock() fake_user.pw_uid = 3133 pwnam.return_value = fake_user create_cmd = ['ssh-keygen', '-y', '-f', '/home/foo/.ssh/id_rsa'] def _ensure_perms(): chown.assert_called_with('/home/foo/.ssh/id_rsa.pub', 3133, -1) isfile.return_value = True unison.create_public_key(user='******', priv_key_path='/home/foo/.ssh/id_rsa', pub_key_path='/home/foo/.ssh/id_rsa.pub') self.assertNotIn(call(create_cmd), self.check_output.call_args_list) _ensure_perms() isfile.return_value = False with patch_open() as (_open, _file): self.check_output.return_value = b'fookey' unison.create_public_key(user='******', priv_key_path='/home/foo/.ssh/id_rsa', pub_key_path='/home/foo/.ssh/id_rsa.pub') self.assertIn(call(create_cmd), self.check_output.call_args_list) _ensure_perms() _open.assert_called_with('/home/foo/.ssh/id_rsa.pub', 'wb') _file.write.assert_called_with(b'fookey')
def test_file_hash_missing(self, exists): filename = '/etc/missing.conf' exists.side_effect = [False] with patch_open() as (mock_open, mock_file): mock_file.read.return_value = self._hash_files[filename] result = host.file_hash(filename) self.assertEqual(result, None)
def test_writes_content_to_a_file(self, os_, log, getgrnam, getpwnam): # Curly brackets here demonstrate that we are *not* rendering # these strings with Python's string formatting. This is a # change from the original behavior per Bug #1195634. uid = 123 gid = 234 owner = 'some-user-{foo}' group = 'some-group-{bar}' path = '/some/path/{baz}' contents = b'what is {juju}' perms = 0o644 fileno = 'some-fileno' getpwnam.return_value.pw_uid = uid getgrnam.return_value.gr_gid = gid with patch_open() as (mock_open, mock_file): mock_file.fileno.return_value = fileno host.write_file(path, contents, owner=owner, group=group, perms=perms) getpwnam.assert_called_with('some-user-{foo}') getgrnam.assert_called_with('some-group-{bar}') mock_open.assert_called_with('/some/path/{baz}', 'wb') os_.fchown.assert_called_with(fileno, uid, gid) os_.fchmod.assert_called_with(fileno, perms) mock_file.write.assert_called_with(b'what is {juju}')
def test_create_keyfile(self, _exists): '''It creates a new ceph keyfile''' _exists.return_value = False with patch_open() as (_open, _file): ceph_utils.create_key_file('cinder', 'cephkey') _file.write.assert_called_with('cephkey') self.log.assert_called()
def test_add_authorized_key(self): self.setup_mocks_add_known_host() with patch_open() as (mock_open, mock_file): ssh_migrations.add_authorized_key(UNIT1_PUBKEY_1, 'nova-compute-lxd') mock_file.write.assert_called_with(UNIT1_PUBKEY_1 + '\n') mock_open.assert_called_with('/somedir/authorized_keys', 'a')
def test_benchmark_init(self, in_relation_hook, relation_ids, relation_set, relation_get): in_relation_hook.return_value = True relation_ids.return_value = ['benchmark:0'] actions = ['asdf', 'foobar'] with patch_open() as (_open, _file): b = Benchmark(actions) self.assertIsInstance(b, Benchmark) self.assertTrue(relation_get.called) self.assertTrue(relation_set.called) relation_ids.assert_called_once_with('benchmark') for key in b.required_keys: relation_get.assert_any_call(key) relation_set.assert_called_once_with( relation_id='benchmark:0', relation_settings={'benchmarks': ",".join(actions)}) _open.assert_called_with('/etc/benchmark.conf', 'w') for key, val in iter( FAKE_RELATION['benchmark:0']['benchmark/0'].items()): _file.write.assert_any_called("%s=%s\n" % (key, val))
def test_modprobe(self): with patch_open() as (_open, _file): _file.read.return_value = 'anothermod\n' ceph_utils.modprobe('mymod') _open.assert_called_with('/etc/modules', 'r+') _file.read.assert_called() _file.write.assert_called_with('mymod') self.check_call.assert_called_with(['modprobe', 'mymod'])
def test_add_ovsbridge_linuxbridge(self, check_call, add_bridge_port, port_to_br): port_to_br.return_value = None with patch_open() as (mock_open, mock_file): ovs.add_ovsbridge_linuxbridge('br-ex', 'br-eno1') check_call.assert_called_with(['ifup', 'veth-br-eno1']) add_bridge_port.assert_called_with('br-ex', 'veth-br-eno1')
def test_install_ca_cert(self): with patch_open() as (_open, _file): apache_utils.install_ca_cert(cert) _open.assert_called_with( '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt', 'w') _file.write.assert_called_with(cert) self.subprocess.assertCalledWith(['update-ca-certificates', '--fresh'])
def test_ssh_compute_remove(self): self.setup_mocks_ssh_compute_remove(isfile=True, authorized_keys_lines=PUB_KEYS) with patch_open() as (mock_open, mock_file): ssh_migrations.ssh_compute_remove(UNIT1_PUBKEY_1, 'nova-compute-lxd') mock_file.write.assert_called_with(UNIT2_PUBKEY_1 + '\n') mock_open.assert_called_with('/somedir/authorized_keys', 'w')
def test_add_source_proposed(self, lsb_release): source = "proposed" result = """# Proposed deb http://archive.ubuntu.com/ubuntu precise-proposed main universe multiverse restricted """ lsb_release.return_value = {'DISTRIB_CODENAME': 'precise'} with patch_open() as (mock_open, mock_file): fetch.add_source(source=source) mock_file.write.assert_called_with(result)
def test_lists_the_mount_points(self): with patch_open() as (mock_open, mock_file): mock_file.readlines.return_value = MOUNT_LINES result = host.mounts() self.assertEqual( result, [['/', 'rootfs'], ['/sys', 'sysfs'], ['/proc', 'proc'], ['/dev', 'udev'], ['/dev/pts', 'devpts']]) mock_open.assert_called_with('/proc/mounts')
def test_file_hash_sha1(self, exists, sha1): filename = '/etc/exists.conf' exists.side_effect = [True] m = sha1() m.hexdigest.return_value = self._hash_files[filename] with patch_open() as (mock_open, mock_file): mock_file.read.return_value = self._hash_files[filename] result = host.file_hash(filename, hash_type='sha1') self.assertEqual(result, self._hash_files[filename])
def test_add_known_host_existing_valid_key(self): self.setup_mocks_add_known_host() self.check_output.return_value = UNIT2_HOST_KEY_1 self.ssh_known_host_key.return_value = UNIT2_HOST_KEY_1 with patch_open() as (mock_open, mock_file): ssh_migrations.add_known_host('juju-4665be-20180716142533-8', 'nova-compute-lxd') self.assertFalse(mock_open.called) self.assertFalse(self.remove_known_host.called)
def test_add_source_cloud_distroless_style(self, apt_install, filter_pkg): source = "cloud:havana" result = '''# Ubuntu Cloud Archive deb http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/havana main ''' with patch_open() as (mock_open, mock_file): fetch.add_source(source=source) mock_file.write.assert_called_with(result) filter_pkg.assert_called_with(['ubuntu-cloud-keyring'])
def test_modprobe_not_persistent_centos(self, platform, check_call): platform.return_value = 'centos' imp.reload(kernel) with patch_open() as (_open, _file): _file.read.return_value = 'anothermod\n' with patch("charmhelpers.core.kernel.log"): kernel.modprobe('mymod', persist=False) assert not _open.called check_call.assert_called_with(['modprobe', 'mymod'])
def test_install_ca_cert_new_cert(self, _retrieve_ca_cert): _retrieve_ca_cert.return_value = None with patch_open() as (_open, _file): apache_utils.install_ca_cert(cert) _open.assert_called_once_with( '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt', 'wb') _file.write.assert_called_with(cert) self.subprocess.check_call.assert_called_with( ['update-ca-certificates', '--fresh'])
def test_add_known_host(self): self.setup_mocks_add_known_host() self.check_output.return_value = UNIT1_HOST_KEY_1 self.ssh_known_host_key.return_value = '' with patch_open() as (mock_open, mock_file): ssh_migrations.add_known_host('juju-4665be-20180716142533-8', 'nova-compute-lxd') mock_file.write.assert_called_with(UNIT1_HOST_KEY_1 + '\n') mock_open.assert_called_with('/somedir/known_hosts', 'a') self.assertFalse(self.remove_known_host.called)
def test_add_source_proposed_x86_64(self, _machine, lsb_release, log): source = "proposed" result = ('# Proposed\n' 'deb http://archive.ubuntu.com/ubuntu precise-proposed' ' main universe multiverse restricted\n') lsb_release.return_value = {'DISTRIB_CODENAME': 'precise'} _machine.return_value = 'x86_64' with patch_open() as (mock_open, mock_file): fetch.add_source(source=source) mock_file.write.assert_called_with(result)
def test_get_keypair(self, get_homedir, isdir, mkdir): get_homedir.return_value = '/home/foo' isdir.return_value = False with patch_open() as (_open, _file): _file.read.side_effect = ['foopriv', 'foopub'] priv, pub = unison.get_keypair('adam') for f in ['/home/foo/.ssh/id_rsa', '/home/foo/.ssh/id_rsa.pub']: self.assertIn(call(f, 'r'), _open.call_args_list) self.assertEquals(priv, 'foopriv') self.assertEquals(pub, 'foopub')
def test_setup_eni_sources_eni_folder(self, exists, lsb_release): exists.return_value = True lsb_release.return_value = {'DISTRIB_CODENAME': 'bionic'} with patch_open() as (_, mock_file): # Mocked initial /etc/network/interfaces file content: mock_file.__iter__.return_value = ['some line', 'some other line'] ovs.setup_eni() mock_file.write.assert_called_once_with( '\nsource /etc/network/interfaces.d/*')
def test_add_ovsbridge_linuxbridge_longname(self, check_call, add_bridge_port, port_to_br): port_to_br.return_value = None mock_hasher = MagicMock() mock_hasher.hexdigest.return_value = '12345678901234578910' self.hashlib.sha256.return_value = mock_hasher with patch_open() as (mock_open, mock_file): ovs.add_ovsbridge_linuxbridge('br-ex', 'br-reallylongname') check_call.assert_called_with(['ifup', 'cvb12345678-10']) add_bridge_port.assert_called_with('br-ex', 'cvb12345678-10')
def test_write_auth_keys(self, get_homedir): get_homedir.return_value = '/home/foo' keys = [ 'ssh-rsa AAAB3Nz adam', 'ssh-rsa ALKJFz [email protected]', ] with patch_open() as (_open, _file): unison.write_authorized_keys('foo', keys) _open.assert_called_with('/home/foo/.ssh/authorized_keys', 'w') for k in keys: self.assertIn(call('%s\n' % k), _file.write.call_args_list)
def test_add_source_proposed_x86_64(self, _machine, get_distrib_codename, log): source = "proposed" result = ('# Proposed\n' 'deb http://archive.ubuntu.com/ubuntu precise-proposed' ' main universe multiverse restricted\n') get_distrib_codename.return_value = 'precise' _machine.return_value = 'x86_64' with patch_open() as (mock_open, mock_file): fetch.add_source(source=source) mock_file.write.assert_called_with(result)
def test_add_source_cloud_os_style(self, get_distrib_codename, apt_install, filter_pkg, log): source = "cloud:precise-havana" get_distrib_codename.return_value = 'precise' result = ('# Ubuntu Cloud Archive\n' 'deb http://ubuntu-cloud.archive.canonical.com/ubuntu' ' precise-updates/havana main\n') with patch_open() as (mock_open, mock_file): fetch.add_source(source=source) mock_file.write.assert_called_with(result) filter_pkg.assert_called_with(['ubuntu-cloud-keyring'])
def test_add_source_proposed_ppc64le(self, _machine, lsb_release, log): source = "proposed" result = ( "# Proposed\n" "deb http://ports.ubuntu.com/ubuntu-ports precise-proposed main " "universe multiverse restricted\n") lsb_release.return_value = {'DISTRIB_CODENAME': 'precise'} _machine.return_value = 'ppc64le' with patch_open() as (mock_open, mock_file): fetch.add_source(source=source) mock_file.write.assert_called_with(result)
def test_add_source_http_centos(self, listdir, log): source = "http://archive.ubuntu.com/ubuntu raring-backports main" with patch_open() as (mock_open, mock_file): fetch.add_source(source=source) listdir.assert_called_with('/etc/yum.repos.d/') mock_file.write.assert_has_calls([ call("[archive.ubuntu.com_ubuntu raring-backports main]\n"), call("name=archive.ubuntu.com/ubuntu raring-backports main\n"), call("baseurl=http://archive.ubuntu.com/ubuntu raring" "-backports main\n\n") ])