def test_add_known_host_exists_outdated( self, check_output, host_key, rm, known_hosts): check_output.return_value = '|1|= fookey' host_key.return_value = '|1|= fookey_old' with patch_open() as (_open, _file): utils.add_known_host('foohost', None, None) rm.assert_called_with('foohost', None, None)
def test_secret_retrieved(self, _path): _path.exists.return_value = True with patch_open() as (_open, _file): _file.read.return_value = 'secret_thing\n' self.assertEqual(context.get_shared_secret(), 'secret_thing') _open.assert_called_with(context.SHARED_SECRET.format('quantum'), 'r')
def test_import_keystone_cert(self, check_call): self.relation_get.return_value = 'Zm9vX2NlcnQK' with patch_open() as (_open, _file): utils.import_keystone_ca_cert() _open.assert_called_with(utils.CA_CERT_PATH, 'wb') _file.write.assert_called_with('foo_cert\n') check_call.assert_called_with(['update-ca-certificates'])
def test_add_known_host_exists(self, check_output, host_key, rm): check_output.return_value = '|1|= fookey' host_key.return_value = '|1|= fookey' with patch_open() as (_open, _file): utils.add_known_host('foohost') self.assertFalse(rm.called) self.assertFalse(_file.write.called)
def test_numa_node_cores(self, _parse_cpu_list): self.glob.glob.return_value = ['/sys/devices/system/node/node0'] with patch_open() as (_, mock_file): mock_file.read.return_value = TEST_CPULIST_1 self.assertEqual(context.numa_node_cores(), {'0': [0, 1, 2, 3]}) self.glob.glob.assert_called_with('/sys/devices/system/node/node*') _parse_cpu_list.assert_called_with(TEST_CPULIST_1)
def test__set_sriov_numvfs(self, mock_sysnet_ints): mock_sysnet_ints.side_effect = [{ 'interface': 'eth2', 'mac_address': 'a8:9d:21:cf:93:fc', 'pci_address': '0000:10:00.0', 'state': 'up', 'sriov': True, 'sriov_totalvfs': 7, 'sriov_numvfs': 0 }], [{ 'interface': 'eth2', 'mac_address': 'a8:9d:21:cf:93:fc', 'pci_address': '0000:10:00.0', 'state': 'up', 'sriov': True, 'sriov_totalvfs': 7, 'sriov_numvfs': 4 }] dev = pci.PCINetDevice('0000:10:00.0') self.assertEqual('eth2', dev.interface_name) self.assertTrue(dev.sriov) self.assertEqual(7, dev.sriov_totalvfs) self.assertEqual(0, dev.sriov_numvfs) with patch_open() as (mock_open, mock_file): dev._set_sriov_numvfs(4) mock_open.assert_called_with( '/sys/class/net/eth2/device/sriov_numvfs', 'w') mock_file.write.assert_called_with("4") self.assertTrue(dev.sriov) self.assertEqual(7, dev.sriov_totalvfs) self.assertEqual(4, dev.sriov_numvfs)
def test_secret_retrieved(self, _path): _path.exists.return_value = True with patch_open() as (_open, _file): _file.read.return_value = 'secret_thing\n' self.assertEquals(context.get_shared_secret(), 'secret_thing') _open.assert_called_with( context.SHARED_SECRET.format('quantum'), 'r')
def test_get_sysnet_device_state(self, _update): with patch_open() as (_open, _file): super_fh = mocked_filehandle() _file.readlines = MagicMock() _open.side_effect = super_fh._setfilename _file.read.side_effect = super_fh._getfilecontents_read state = pci.get_sysnet_device_state('/sys/class/net/eth3') self.assertEqual(state, 'down')
def test_get_sysnet_mac(self, _update): with patch_open() as (_open, _file): super_fh = mocked_filehandle() _file.readlines = MagicMock() _open.side_effect = super_fh._setfilename _file.read.side_effect = super_fh._getfilecontents_read macaddr = pci.get_sysnet_mac('/sys/class/net/eth3') self.assertEqual(macaddr, 'a8:9d:21:cf:93:fd')
def test_config_changed_upgrade_available(self): self.openstack_upgrade_available.return_value = True self.relations_of_type.return_value = False with patch_open() as (_open, _file): _file.read.return_value = "foo" hooks.config_changed() self.assertTrue(self.do_openstack_upgrade.called) self.assertTrue(self.CONFIGS.write_all.called)
def test_secret_created_stored(self, _uuid4, _path): _path.exists.return_value = False _uuid4.return_value = 'secret_thing' with patch_open() as (_open, _file): self.assertEqual(context.get_shared_secret(), 'secret_thing') _open.assert_called_with(context.SHARED_SECRET.format('quantum'), 'w') _file.write.assert_called_with('secret_thing')
def test_config_changed_nrpe_master(self): self.openstack_upgrade_available.return_value = False self.relations_of_type.return_value = True with patch_open() as (_open, _file): _file.read.return_value = "foo" hooks.config_changed() self.assertTrue(self.CONFIGS.write_all.called) self.assertTrue(self.setup_rsync.called) self.assertTrue(self.update_nrpe_config.called)
def test_config_changed_with_openstack_upgrade_action(self): self.openstack_upgrade_available.return_value = True self.test_config.set('action-managed-upgrade', True) with patch_open() as (_open, _file): _file.read.return_value = "foo" hooks.config_changed() self.assertFalse(self.do_openstack_upgrade.called)
def test_config_changed_ipv6(self, mock_assert_charm_supports_ipv6): self.test_config.set('prefer-ipv6', True) self.openstack_upgrade_available.return_value = False self.relations_of_type.return_value = False with patch_open() as (_open, _file): _file.read.return_value = "foo" hooks.config_changed() self.assertTrue(self.CONFIGS.write_all.called) self.assertTrue(self.setup_rsync.called)
def test_secret_created_stored(self, _uuid4, _path): _path.exists.return_value = False _uuid4.return_value = 'secret_thing' with patch_open() as (_open, _file): self.assertEquals(context.get_shared_secret(), 'secret_thing') _open.assert_called_with( context.SHARED_SECRET.format('quantum'), 'w') _file.write.assert_called_with('secret_thing')
def test_find_block_devices(self): self.is_block_device.return_value = True with patch_open() as (_open, _file): _file.read.return_value = PROC_PARTITIONS _file.readlines = MagicMock() _file.readlines.return_value = PROC_PARTITIONS.split('\n') result = swift_utils.find_block_devices() ex = ['/dev/sdb', '/dev/vdb', '/dev/cciss/c1d0'] self.assertEquals(ex, result)
def test_add_known_host_exists_added( self, check_output, host_key, rm, known_hosts): check_output.return_value = '|1|= fookey' host_key.return_value = None with patch_open() as (_open, _file): _file.write = MagicMock() utils.add_known_host('foohost') self.assertFalse(rm.called) _file.write.assert_called_with('|1|= fookey\n')
def test_rsync_enable_rsync(self): with patch_open() as (_open, _file): ctxt = swift_context.RsyncContext() _file.read.return_value = 'RSYNC_ENABLE=false' ctxt.enable_rsyncd() _file.write.assert_called_with('RSYNC_ENABLE=true') _file.read.return_value = '#foo' ctxt.enable_rsyncd() _file.write.assert_called_with('RSYNC_ENABLE=true\n')
def test_find_block_devices(self): self.is_block_device.return_value = True self.is_device_mounted.side_effect = self._fake_is_device_mounted with patch_open() as (_open, _file): _file.read.return_value = PROC_PARTITIONS _file.readlines = MagicMock() _file.readlines.return_value = PROC_PARTITIONS.split('\n') result = swift_utils.find_block_devices() ex = ['/dev/sdb', '/dev/vdb', '/dev/cciss/c1d0'] self.assertEqual(ex, result)
def test_numa_node_cores(self, _parse_cpu_list): self.glob.glob.return_value = [ '/sys/devices/system/node/node0' ] with patch_open() as (_, mock_file): mock_file.read.return_value = TEST_CPULIST_1 self.assertEqual(context.numa_node_cores(), {'0': [0, 1, 2, 3]}) self.glob.glob.assert_called_with('/sys/devices/system/node/node*') _parse_cpu_list.assert_called_with(TEST_CPULIST_1)
def test_upgrade_havana_icehouse_source(self, _do_openstack_upgrade): "Verify get_step_upgrade_source() for havana->icehouse" self.config.side_effect = None self.config.return_value = 'cloud:precise-icehouse' with patch_open() as (_open, _file): _file.read = MagicMock() _file.readline.return_value = "deb url precise-updates/havana main" utils.do_openstack_upgrade(self.register_configs()) expected = [call('cloud:precise-icehouse')] self.assertEquals(_do_openstack_upgrade.call_args_list, expected)
def test_find_block_devices_real_world(self): self.is_block_device.return_value = True side_effect = lambda x: x in ["/dev/sdb", "/dev/sdb1"] # flake8: noqa self.is_device_mounted.side_effect = side_effect with patch_open() as (_open, _file): _file.read.return_value = REAL_WORLD_PARTITIONS _file.readlines = MagicMock() _file.readlines.return_value = REAL_WORLD_PARTITIONS.split('\n') result = swift_utils.find_block_devices() expected = ["/dev/sda"] self.assertEquals(expected, result)
def test_find_block_devices_real_world(self): self.is_block_device.return_value = True side_effect = lambda x: x in ["/dev/sdb", "/dev/sdb1"] # flake8: noqa self.is_device_mounted.side_effect = side_effect with patch_open() as (_open, _file): _file.read.return_value = REAL_WORLD_PARTITIONS _file.readlines = MagicMock() _file.readlines.return_value = REAL_WORLD_PARTITIONS.split('\n') result = swift_utils.find_block_devices() expected = ["/dev/sda"] self.assertEqual(expected, result)
def test_send_rndckey_info_no_info(self): self.patch_br('conversations') conv_mock = mock.MagicMock() self.conversations.return_value = [conv_mock] with test_utils.patch_open() as (_open, _file): _file.readlines.return_value = ['nothing useful'] self.br.send_rndckey_info() conv_mock.set_remote.assert_has_calls([ mock.call('rndckey', None), mock.call('algorithm', None), ])
def _test_import_authorized_keys_base(self, getpwnam, prefix=None, auth_key_path='/home/foo/.ssh/' 'authorized_keys'): getpwnam.return_value = self.fake_user('foo') self.relation_get.side_effect = [ 3, # relation_get('known_hosts_max_index') 'k_h_0', # relation_get_('known_hosts_0') 'k_h_1', # relation_get_('known_hosts_1') 'k_h_2', # relation_get_('known_hosts_2') 3, # relation_get('authorized_keys_max_index') 'auth_0', # relation_get('authorized_keys_0') 'auth_1', # relation_get('authorized_keys_1') 'auth_2', # relation_get('authorized_keys_2') ] ex_open = [ call('/home/foo/.ssh/known_hosts', 'wb'), call(auth_key_path, 'wb') ] ex_write = [ call('k_h_0\n'), call('k_h_1\n'), call('k_h_2\n'), call('auth_0\n'), call('auth_1\n'), call('auth_2\n') ] with patch_open() as (_open, _file): utils.import_authorized_keys(user='******', prefix=prefix) self.assertEqual(ex_open, _open.call_args_list) self.assertEqual(ex_write, _file.write.call_args_list) authkey_root = 'authorized_keys_' known_hosts_root = 'known_hosts_' if prefix: authkey_root = prefix + '_authorized_keys_' known_hosts_root = prefix + '_known_hosts_' expected_relations = [ call(known_hosts_root + 'max_index'), call(known_hosts_root + '0'), call(known_hosts_root + '1'), call(known_hosts_root + '2'), call(authkey_root + 'max_index'), call(authkey_root + '0'), call(authkey_root + '1'), call(authkey_root + '2') ] self.assertEqual(sorted(self.relation_get.call_args_list), sorted(expected_relations))
def eth_int(self, pci_address, _osrealpath, _osislink, subproc_map=None): self.glob.glob.side_effect = mocked_globs _osislink.side_effect = mocked_islink _osrealpath.side_effect = mocked_realpath self.subprocess.check_output.side_effect = mocked_subprocess( subproc_map=subproc_map) with patch_open() as (_open, _file): super_fh = mocked_filehandle() _file.readlines = MagicMock() _open.side_effect = super_fh._setfilename _file.read.side_effect = super_fh._getfilecontents_read _file.readlines.side_effect = super_fh._getfilecontents_readlines netint = pci.PCINetDevice(pci_address) return netint
def test_get_step_upgrade_source_target_liberty(self): self.get_os_codename_install_source.return_value = 'kilo' self.os_release.return_value = 'icehouse' self.assertEquals( utils.get_step_upgrade_source('cloud:trusty-liberty'), 'cloud:trusty-kilo') self.os_release.return_value = 'juno' self.assertEquals( utils.get_step_upgrade_source('cloud:trusty-liberty'), 'cloud:trusty-kilo') self.os_release.return_value = 'kilo' with patch_open() as (_open, _file): self.assertEquals( utils.get_step_upgrade_source('cloud:trusty-liberty'), None)
def pci_devs(_osislink, _glob, _check_output, _Popen, _log, subproc_map=None): _glob.side_effect = mocked_globs _osislink.side_effect = mocked_islink _check_output.side_effect = mocked_subprocess( subproc_map=subproc_map) with patch_open() as (_open, _file), \ patch('pci.os.path.realpath') as _realpath: super_fh = mocked_filehandle() _file.readlines = MagicMock() _open.side_effect = super_fh._setfilename _file.read.side_effect = super_fh._getfilecontents_read _file.readlines.side_effect = super_fh._getfilecontents_readlines _realpath.side_effect = mocked_realpath devices = pci.PCINetDevices() return devices
def _test_import_authorized_keys_base(self, getpwnam, prefix=None, auth_key_path='/home/foo/.ssh/' 'authorized_keys'): getpwnam.return_value = self.fake_user('foo') d = { 'known_hosts_max_index': 3, 'known_hosts_0': 'k_h_0', 'known_hosts_1': 'k_h_1', 'known_hosts_2': 'k_h_2', 'authorized_keys_max_index': 3, 'authorized_keys_0': 'auth_0', 'authorized_keys_1': 'auth_1', 'authorized_keys_2': 'auth_2', } if prefix: for k, v in d.copy().items(): d["{}_{}".format(prefix, k)] = v def _relation_get(scope=None, *args, **kwargs): if scope is not None: return d.get(scope, None) return d self.relation_get.side_effect = _relation_get ex_open = [ call('/home/foo/.ssh/known_hosts', 'wt'), call(auth_key_path, 'wt') ] ex_write = [ call('k_h_0\n'), call('k_h_1\n'), call('k_h_2\n'), call('auth_0\n'), call('auth_1\n'), call('auth_2\n') ] # we only have to verify that the files are writen as expected as this # implicitly checks that the relation_get calls have occurred. with patch_open() as (_open, _file): utils.import_authorized_keys(user='******', prefix=prefix) self.assertEqual(ex_open, _open.call_args_list) self.assertEqual(ex_write, _file.write.call_args_list)
def pci_devs(self, _osislink, subproc_map=None): self.glob.glob.side_effect = mocked_globs rp_patcher = patch('os.path.realpath') rp_mock = rp_patcher.start() rp_mock.side_effect = mocked_realpath _osislink.side_effect = mocked_islink self.subprocess.check_output.side_effect = mocked_subprocess( subproc_map=subproc_map) with patch_open() as (_open, _file): super_fh = mocked_filehandle() _file.readlines = MagicMock() _open.side_effect = super_fh._setfilename _file.read.side_effect = super_fh._getfilecontents_read _file.readlines.side_effect = super_fh._getfilecontents_readlines devices = pci.PCINetDevices() rp_patcher.stop() return devices
def test_ssh_compute_remove(self, isfile, auth_key, known_host): isfile.return_value = False removed_key = AUTHORIZED_KEYS.split('\n')[2] keys_removed = ( "\nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC27Us7lSjCpa7bumXAgc " "nova-compute-1\n" "ssh-rsa CCCCB3NzaC1yc2ECCCCDCQCBCCCBCQC27Us7lSjCpa7bumXCgc " "nova-compute-3\n" ) isfile.return_value = True self.remote_unit.return_value = 'nova-compute/2' with patch_open() as (_open, _file): _file.readlines = MagicMock() _file.write = MagicMock() _file.readlines.return_value = AUTHORIZED_KEYS.split('\n') utils.ssh_compute_remove(removed_key) _file.write.assert_called_with(keys_removed)
def test_neutron_api_relation_joined(self, get_cell_type, configs, isfile, rename, _canonical_url): neutron_conf = '/etc/neutron/neutron.conf' nova_url = 'http://novaurl:8774/v2' isfile.return_value = True self.service_running.return_value = True _identity_joined = self.patch('identity_joined') self.relation_ids.return_value = ['relid'] _canonical_url.return_value = 'http://novaurl' get_cell_type.return_value = 'parent' self.uuid.uuid4.return_value = 'bob' with patch_open() as (_open, _file): hooks.neutron_api_relation_joined(remote_restart=True) self.service_stop.assert_called_with('neutron-server') rename.assert_called_with(neutron_conf, neutron_conf + '_unused') self.assertTrue(_identity_joined.called) self.relation_set.assert_called_with(relation_id=None, cell_type='parent', nova_url=nova_url, restart_trigger='bob')
def test_import_authorized_keys(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='******') self.assertEquals(ex_open, _open.call_args_list) self.assertEquals(ex_write, _file.write.call_args_list)
def test_upgrade_charm(self, leader_elected, os_remove, os_path_isfile): os_path_isfile.return_value = False self.is_leader.return_value = False relations.upgrade_charm() leader_elected.assert_called_once_with() os_path_isfile.assert_not_called() # now say we are the leader self.is_leader.return_value = True os_path_isfile.return_value = False relations.upgrade_charm() self.leader_set.assert_not_called() os_path_isfile.return_value = True with patch_open() as (mock_open, mock_file): mock_file.read.return_value = "abc" relations.upgrade_charm() filename = os.path.join(relations.HEAT_PATH, 'encryption-key') mock_open.assert_called_once_with(filename, 'r') self.leader_set.assert_called_once_with( {'heat-auth-encryption-key': 'abc'}) os_remove.assert_called_once_with(filename)
def test_neutron_netns_cleanup(self, _check_call, _unlink, _exists): _exists.return_value = True with test_utils.patch_open() as (_open, _file): actions.neutron_netns_cleanup() _open.assert_called_once_with( '/etc/neutron/rootwrap.d/charm-n-ovs.filters', 'w') _file.write.assert_called_once_with( '[Filters]\n' 'neutron.cmd.netns_cleanup: CommandFilter, ip, root\n') _check_call.assert_has_calls([ # FIXME: remove once package deps have been backported mock.call(('apt', '-y', 'install', 'net-tools')), mock.call(( 'neutron-netns-cleanup', '--force', '--config-file=/etc/neutron/neutron.conf', '--config-file=/etc/neutron/l3_agent.ini', '--config-file=/etc/neutron/fwaas_driver.ini', '--config-file=/etc/neutron/dhcp_agent.ini', )), ]) _unlink.assert_called_once_with( '/etc/neutron/rootwrap.d/charm-n-ovs.filters') # Confirm behaviour when a config does not exist _exists.reset_mock() _exists.side_effect = [True, True, True, False] _check_call.reset_mock() actions.neutron_netns_cleanup() _check_call.assert_has_calls([ # FIXME: remove once package deps have been backported mock.call(('apt', '-y', 'install', 'net-tools')), mock.call(( 'neutron-netns-cleanup', '--force', '--config-file=/etc/neutron/neutron.conf', '--config-file=/etc/neutron/l3_agent.ini', '--config-file=/etc/neutron/fwaas_driver.ini', )), ])
def test_get_step_upgrade_source_target_liberty(self, lsb_release): self.lsb_release.return_value = {'DISTRIB_CODENAME': 'trusty'} lsb_release.return_value = {'DISTRIB_CODENAME': 'trusty'} self.get_os_codename_install_source.side_effect = self.originals[ 'get_os_codename_install_source'] # icehouse -> liberty self.os_release.return_value = 'icehouse' self.assertEqual( utils.get_step_upgrade_source('cloud:trusty-liberty'), 'cloud:trusty-kilo') # juno -> liberty self.os_release.return_value = 'juno' self.assertEqual( utils.get_step_upgrade_source('cloud:trusty-liberty'), 'cloud:trusty-kilo') # kilo -> liberty self.os_release.return_value = 'kilo' with patch_open() as (_open, _file): self.assertEqual( utils.get_step_upgrade_source('cloud:trusty-liberty'), None)
def test_import_keystone_cert_missing_data(self, check_call): self.relation_get.return_value = None with patch_open() as (_open, _file): utils.import_keystone_ca_cert() self.assertFalse(_open.called) self.assertFalse(check_call.called)
def test_import_authorized_keys_missing_data(self): self.relation_get.return_value = None with patch_open() as (_open, _file): utils.import_authorized_keys(user='******') self.assertFalse(_open.called)
def test_keystone_ca_cert_b64(self, _isfile): _isfile.return_value = True with patch_open() as (_open, _file): nutils.keystone_ca_cert_b64() self.assertTrue(self.b64encode.called)
def test_ssh_authorized_key_exists(self, keys): key = 'BBBBB3NzaC1yc2EBBBBDBQBBBBBBBQC27Us7lSjCpa7bumXBgc' with patch_open() as (_open, _file): _file.read.return_value = AUTHORIZED_KEYS self.assertTrue(utils.ssh_authorized_key_exists(key))
def test_ssh_authorized_key_doesnt_exist(self, keys): key = ('xxxx') with patch_open() as (_open, _file): _file.read = MagicMock() _file.readreturn_value = AUTHORIZED_KEYS self.assertFalse(utils.ssh_authorized_key_exists(key))