def test_gen_conf_dns(self, m_get_phys_by_mac): """Test rendering with/without DNS server, search domain""" self.maxDiff = None # empty DNS, ETH0_DNS, ETH0_SEARCH_DOMAIN context = { 'ETH0_MAC': '02:00:0a:12:01:01', 'DNS': '', 'ETH0_DNS': '', 'ETH0_SEARCH_DOMAIN': '', } for nic in self.system_nics: expected = { 'version': 2, 'ethernets': { nic: { 'match': { 'macaddress': MACADDR }, 'addresses': [IP_BY_MACADDR + '/' + IP4_PREFIX] } } } m_get_phys_by_mac.return_value = {MACADDR: nic} net = ds.OpenNebulaNetwork(context, mock.Mock()) self.assertEqual(net.gen_conf(), expected) # set DNS, ETH0_DNS, ETH0_SEARCH_DOMAIN context = { 'ETH0_MAC': '02:00:0a:12:01:01', 'DNS': '1.2.3.8', 'ETH0_DNS': '1.2.3.6 1.2.3.7', 'ETH0_SEARCH_DOMAIN': 'example.com example.org', } for nic in self.system_nics: expected = { 'version': 2, 'ethernets': { nic: { 'nameservers': { 'addresses': ['1.2.3.6', '1.2.3.7', '1.2.3.8'], 'search': ['example.com', 'example.org'] }, 'match': { 'macaddress': MACADDR }, 'addresses': [IP_BY_MACADDR + '/' + IP4_PREFIX] } } } m_get_phys_by_mac.return_value = {MACADDR: nic} net = ds.OpenNebulaNetwork(context, mock.Mock()) self.assertEqual(net.gen_conf(), expected)
def test_gen_conf_ipv6address(self, m_get_phys_by_mac): """Test rendering with/without IPv6 address""" self.maxDiff = None # empty ETH0_IP6, ETH0_IP6_ULA, ETH0_IP6_PREFIX_LENGTH context = { 'ETH0_MAC': '02:00:0a:12:01:01', 'ETH0_IP6': '', 'ETH0_IP6_ULA': '', 'ETH0_IP6_PREFIX_LENGTH': '', } for nic in self.system_nics: expected = { 'version': 2, 'ethernets': { nic: { 'match': { 'macaddress': MACADDR }, 'addresses': [IP_BY_MACADDR + '/' + IP4_PREFIX] } } } m_get_phys_by_mac.return_value = {MACADDR: nic} net = ds.OpenNebulaNetwork(context, mock.Mock()) self.assertEqual(net.gen_conf(), expected) # set ETH0_IP6, ETH0_IP6_ULA, ETH0_IP6_PREFIX_LENGTH context = { 'ETH0_MAC': '02:00:0a:12:01:01', 'ETH0_IP6': IP6_GLOBAL, 'ETH0_IP6_PREFIX_LENGTH': IP6_PREFIX, 'ETH0_IP6_ULA': IP6_ULA, } for nic in self.system_nics: expected = { 'version': 2, 'ethernets': { nic: { 'match': { 'macaddress': MACADDR }, 'addresses': [ IP_BY_MACADDR + '/' + IP4_PREFIX, IP6_GLOBAL + '/' + IP6_PREFIX, IP6_ULA + '/' + IP6_PREFIX ] } } } m_get_phys_by_mac.return_value = {MACADDR: nic} net = ds.OpenNebulaNetwork(context, mock.Mock()) self.assertEqual(net.gen_conf(), expected)
def test_normalize_mtype_on_bsd(self, m_tmpdir, m_subp, m_is_BSD, m_is_Linux, mtype, expected): m_is_BSD.return_value = True m_is_Linux.return_value = False m_tmpdir.return_value.__enter__ = mock.Mock(autospec=True, return_value="/tmp/fake") m_tmpdir.return_value.__exit__ = mock.Mock(autospec=True, return_value=True) callback = mock.Mock(autospec=True) util.mount_cb('/dev/fake0', callback, mtype=mtype) assert mock.call( ["mount", "-o", "ro", "-t", expected, "/dev/fake0", "/tmp/fake"], update_env=None) in m_subp.call_args_list
def _apply_network_setup(self, m_macs): old_instance_id = os.path.join(self.init.paths.get_cpath('data'), 'instance-id') write_file(old_instance_id, TEST_INSTANCE_ID) net_cfg = { 'version': 1, 'config': [{ 'subnets': [{ 'type': 'dhcp' }], 'type': 'physical', 'name': 'eth9', 'mac_address': '42:42:42:42:42:42' }] } def fake_network_config(): return net_cfg, NetworkConfigSource.fallback m_macs.return_value = {'42:42:42:42:42:42': 'eth9'} self.init._find_networking_config = fake_network_config self.init.datasource = FakeDataSource(paths=self.init.paths) self.init.is_new_instance = mock.Mock(return_value=False) return net_cfg
def test_handle_publish_hostkeys_empty_blacklist(self, m_path_exists, m_nug, m_glob, m_setup_keys): """Test handle with various configs for ssh_publish_hostkeys.""" self._publish_hostkey_test_setup() cc_ssh.PUBLISH_HOST_KEYS = True keys = ["key1"] user = "******" # Return no matching keys for first glob, test keys for second. m_glob.side_effect = iter([ [], self.test_hostkey_files, ]) # Mock os.path.exits to True to short-circuit the key writing logic m_path_exists.return_value = True m_nug.return_value = ({user: {"default": user}}, {}) cloud = self.tmp_cloud(distro='ubuntu', metadata={'public-keys': keys}) cloud.datasource.publish_host_keys = mock.Mock() cfg = {'ssh_publish_hostkeys': {'enabled': True, 'blacklist': []}} expected_call = [ self.test_hostkeys[key_type] for key_type in ['dsa', 'ecdsa', 'ed25519', 'rsa'] ] cc_ssh.handle("name", cfg, cloud, LOG, None) self.assertEqual([mock.call(expected_call)], cloud.datasource.publish_host_keys.call_args_list)
def test_no_handle_when_modfreq_once(self): """C{handle_part} is not called if frequency is once.""" self.frequency = "once" mod_mock = mock.Mock(frequency=settings.PER_ONCE) handlers.run_part(mod_mock, self.data, self.filename, self.payload, self.frequency, self.headers) self.assertEqual(0, mod_mock.handle_part.call_count)
def test_user_data_plain(self): for k in ('USER_DATA', 'USERDATA'): my_d = os.path.join(self.tmp, k) populate_context_dir(my_d, {k: USER_DATA, 'USERDATA_ENCODING': ''}) results = ds.read_context_disk_dir(my_d, mock.Mock()) self.assertTrue('userdata' in results) self.assertEqual(USER_DATA, results['userdata'])
def test_get_ip6_prefix(self): """ Verify get_ip6_prefix('device') correctly returns IPv6 prefix. """ context = {'ETH0_IP6_PREFIX_LENGTH': IP6_PREFIX} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_ip6_prefix('eth0') self.assertEqual(IP6_PREFIX, val)
def test_get_mask(self): """ Verify get_mask('device') correctly returns IPv4 subnet mask. """ context = {'ETH0_MASK': '255.255.0.0'} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_mask('eth0') self.assertEqual('255.255.0.0', val)
def test_get_network(self): """ Verify get_network('device') correctly returns IPv4 network address. """ context = {'ETH0_NETWORK': '1.2.3.0'} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_network('eth0', MACADDR) self.assertEqual('1.2.3.0', val)
def test_get_field(self): """ Verify get_field('device', 'name') returns *context* value. """ context = {'ETH9_DUMMY': 'DUMMY_VALUE'} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_field('eth9', 'dummy') self.assertEqual('DUMMY_VALUE', val)
def test_gen_conf_mtu(self, m_get_phys_by_mac): """Test rendering with/without MTU""" self.maxDiff = None # empty ETH0_MTU context = { 'ETH0_MAC': '02:00:0a:12:01:01', 'ETH0_MTU': '', } for nic in self.system_nics: expected = { 'version': 2, 'ethernets': { nic: { 'match': { 'macaddress': MACADDR }, 'addresses': [IP_BY_MACADDR + '/' + IP4_PREFIX] } } } m_get_phys_by_mac.return_value = {MACADDR: nic} net = ds.OpenNebulaNetwork(context, mock.Mock()) self.assertEqual(net.gen_conf(), expected) # set ETH0_MTU context = { 'ETH0_MAC': '02:00:0a:12:01:01', 'ETH0_MTU': '1280', } for nic in self.system_nics: expected = { 'version': 2, 'ethernets': { nic: { 'mtu': '1280', 'match': { 'macaddress': MACADDR }, 'addresses': [IP_BY_MACADDR + '/' + IP4_PREFIX] } } } m_get_phys_by_mac.return_value = {MACADDR: nic} net = ds.OpenNebulaNetwork(context, mock.Mock()) self.assertEqual(net.gen_conf(), expected)
def test_get_mask_emptystring(self): """ Verify get_mask('device') correctly returns IPv4 subnet mask. It returns default value '255.255.255.0' if ETH0_MASK has empty string. """ context = {'ETH0_MASK': ''} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_mask('eth0') self.assertEqual('255.255.255.0', val)
def test_already_mounted_calls_callback_with_data(self, already_mounted_device): callback = mock.Mock() util.mount_cb(already_mounted_device, callback, data=mock.sentinel.data) assert [mock.call(mock.ANY, mock.sentinel.data)] == callback.call_args_list
def test_get_gateway6(self): """ Verify get_gateway6('device') correctly returns IPv6 default gateway address. """ context = {'ETH0_GATEWAY6': IP6_GW} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_gateway6('eth0') self.assertEqual(IP6_GW, val)
def test(self, interfaces_by_mac, physical_devs, expected_return): distro = mock.Mock() distro.networking.is_physical.side_effect = ( lambda devname: devname in physical_devs) with mock.patch( DS_PATH + ".net.get_interfaces_by_mac", return_value=interfaces_by_mac, ): assert expected_return == ds.get_physical_nics_by_mac(distro)
def test_get_gateway(self): """ Verify get_gateway('device') correctly returns IPv4 default gateway address. """ context = {'ETH0_GATEWAY': '1.2.3.5'} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_gateway('eth0') self.assertEqual('1.2.3.5', val)
def test_get_field_withdefaultvalue_emptycontext(self): """ Verify get_field('device', 'name', 'default value') returns *default* value if context value is empty string. """ context = {'ETH9_DUMMY': ''} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_field('eth9', 'dummy', 'DEFAULT_VALUE') self.assertEqual('DEFAULT_VALUE', val)
def test_get_field_nonecontext(self): """ Verify get_field('device', 'name') returns None if context value is None. """ context = {'ETH9_DUMMY': None} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_field('eth9', 'dummy') self.assertEqual(None, val)
def test_user_data_encoding_required_for_decode(self): b64userdata = util.b64e(USER_DATA) for k in ('USER_DATA', 'USERDATA'): my_d = os.path.join(self.tmp, k) populate_context_dir(my_d, {k: b64userdata}) results = ds.read_context_disk_dir(my_d, mock.Mock()) self.assertTrue('userdata' in results) self.assertEqual(b64userdata, results['userdata'])
def test_get_network_emptystring(self): """ Verify get_network('device') correctly returns IPv4 network address. It returns network address created by MAC address if ETH0_NETWORK has empty string. """ context = {'ETH0_NETWORK': ''} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_network('eth0', MACADDR) self.assertEqual('10.18.1.0', val)
def test_preexec_fn_sets_group_id_if_adm_group_present( self, _m_os_umask, m_setgid, m_getgrnam, preexec_fn): """We should setgrp to adm if present, so files are owned by them.""" fake_group = mock.Mock(gr_gid=mock.sentinel.gr_gid) m_getgrnam.return_value = fake_group preexec_fn() assert [mock.call("adm")] == m_getgrnam.call_args_list assert [mock.call(mock.sentinel.gr_gid)] == m_setgid.call_args_list
def test_get_ip6_prefix_emptystring(self): """ Verify get_ip6_prefix('device') correctly returns IPv6 prefix. It returns default value '64' if ETH0_IP6_PREFIX_LENGTH has empty string. """ context = {'ETH0_IP6_PREFIX_LENGTH': ''} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_ip6_prefix('eth0') self.assertEqual('64', val)
def test_get_ip_emptystring(self): """ Verify get_ip('device') correctly returns IPv4 address. It returns IP address created by MAC address if ETH0_IP has empty string. """ context = {'ETH0_IP': ''} net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_ip('eth0', MACADDR) self.assertEqual(IP_BY_MACADDR, val)
def setUp(self): super(TestInit, self).setUp() self.tmpdir = self.tmp_dir() self.init = stages.Init() # Setup fake Paths for Init to reference self.init._cfg = {'system_info': { 'distro': 'ubuntu', 'paths': {'cloud_dir': self.tmpdir, 'run_dir': self.tmpdir}}} self.init.datasource = FakeDataSource(paths=self.init.paths) self._real_is_new_instance = self.init.is_new_instance self.init.is_new_instance = mock.Mock(return_value=True)
def test_context_devname(self): """Verify context_devname correctly returns mac and name.""" context = { 'ETH0_MAC': '02:00:0a:12:01:01', 'ETH1_MAC': '02:00:0a:12:0f:0f', } expected = { '02:00:0a:12:01:01': 'ETH0', '02:00:0a:12:0f:0f': 'ETH1', } net = ds.OpenNebulaNetwork(context, mock.Mock()) self.assertEqual(expected, net.context_devname)
def test_hostname(self, m_get_phys_by_mac): for dev in ('eth0', 'ens3'): m_get_phys_by_mac.return_value = {MACADDR: dev} for k in ('HOSTNAME', 'PUBLIC_IP', 'IP_PUBLIC', 'ETH0_IP'): my_d = os.path.join(self.tmp, k) populate_context_dir(my_d, {k: PUBLIC_IP}) results = ds.read_context_disk_dir(my_d, mock.Mock()) self.assertTrue('metadata' in results) self.assertTrue('local-hostname' in results['metadata']) self.assertEqual(PUBLIC_IP, results['metadata']['local-hostname'])
def test_get_ip6_dual(self): """ Verify get_ip6('device') correctly returns IPv6 address. In this case, IPv6 addresses are Given by ETH0_IP6 and ETH0_IP6_ULA. """ context = { 'ETH0_IP6': IP6_GLOBAL, 'ETH0_IP6_ULA': IP6_ULA, } expected = [IP6_GLOBAL, IP6_ULA] net = ds.OpenNebulaNetwork(context, mock.Mock()) val = net.get_ip6('eth0') self.assertEqual(expected, val)
def test_already_mounted_calls_callback( self, trailing_slash_in_mounts, already_mounted_device_and_mountdict): device, mount_dict = already_mounted_device_and_mountdict mountpoint = mount_dict["mountpoint"] mount_dict["mountpoint"] += trailing_slash_in_mounts callback = mock.Mock() util.mount_cb(device, callback) # The mountpoint passed to callback should always have a trailing # slash, regardless of the input assert [mock.call(mountpoint + "/")] == callback.call_args_list
def test_modfreq_per_always(self): """ C{handle_part} is called regardless of frequency if nofreq is always. """ self.frequency = "once" mod_mock = mock.Mock(frequency=settings.PER_ALWAYS, handler_version=1) handlers.run_part(mod_mock, self.data, self.filename, self.payload, self.frequency, self.headers) # Assert that the handle_part() method of the mock object got # called with the expected arguments. mod_mock.handle_part.assert_called_once_with(self.data, self.ctype, self.filename, self.payload)