def test_secondary_nic(self): self.m_readurl.return_value = OPC_VM_SECONDARY_VNIC_RESPONSE mac_addr, nic_name = '00:00:17:02:2b:b1', 'ens3' self.m_get_interfaces_by_mac.return_value = { mac_addr: nic_name, } network_config = {'version': 1, 'config': [{'primary': 'nic'}]} oracle._add_network_config_from_opc_imds(network_config) # The input is mutated self.assertEqual(2, len(network_config['config'])) secondary_nic_cfg = network_config['config'][1] self.assertEqual(nic_name, secondary_nic_cfg['name']) self.assertEqual('physical', secondary_nic_cfg['type']) self.assertEqual(mac_addr, secondary_nic_cfg['mac_address']) self.assertEqual(9000, secondary_nic_cfg['mtu']) self.assertEqual(1, len(secondary_nic_cfg['subnets'])) subnet_cfg = secondary_nic_cfg['subnets'][0] # These values are hard-coded in OPC_VM_SECONDARY_VNIC_RESPONSE self.assertEqual('10.0.0.231', subnet_cfg['address']) self.assertEqual('24', subnet_cfg['netmask']) self.assertEqual('10.0.0.1', subnet_cfg['gateway']) self.assertEqual('manual', subnet_cfg['control'])
def test_missing_mac_skipped_v2(self): self.m_readurl.return_value = OPC_VM_SECONDARY_VNIC_RESPONSE self.m_get_interfaces_by_mac.return_value = {} network_config = {'version': 2, 'ethernets': {'primary': {'nic': {}}}} oracle._add_network_config_from_opc_imds(network_config) self.assertEqual(1, len(network_config['ethernets'])) self.assertIn( 'Interface with MAC 00:00:17:02:2b:b1 not found; skipping', self.logs.getvalue())
def test_bare_metal_machine_skipped(self): # nicIndex in the first entry indicates a bare metal machine self.m_readurl.return_value = OPC_BM_SECONDARY_VNIC_RESPONSE # We test this by passing in a non-dict to ensure that no dict # operations are used self.assertFalse(oracle._add_network_config_from_opc_imds(object())) self.assertIn('bare metal machine', self.logs.getvalue())
def test_secondary_nic_v2(self): self.m_readurl.return_value = OPC_VM_SECONDARY_VNIC_RESPONSE mac_addr, nic_name = '00:00:17:02:2b:b1', 'ens3' self.m_get_interfaces_by_mac.return_value = { mac_addr: nic_name, } network_config = {'version': 2, 'ethernets': {'primary': {'nic': {}}}} oracle._add_network_config_from_opc_imds(network_config) # The input is mutated self.assertEqual(2, len(network_config['ethernets'])) secondary_nic_cfg = network_config['ethernets']['ens3'] self.assertFalse(secondary_nic_cfg['dhcp4']) self.assertFalse(secondary_nic_cfg['dhcp6']) self.assertEqual(mac_addr, secondary_nic_cfg['match']['macaddress']) self.assertEqual(9000, secondary_nic_cfg['mtu']) self.assertEqual(1, len(secondary_nic_cfg['addresses'])) # These values are hard-coded in OPC_VM_SECONDARY_VNIC_RESPONSE self.assertEqual('10.0.0.231', secondary_nic_cfg['addresses'][0])
def test_no_secondary_nics_does_not_mutate_input(self): self.m_readurl.return_value = json.dumps([{}]) # We test this by passing in a non-dict to ensure that no dict # operations are used; failure would be seen as exceptions oracle._add_network_config_from_opc_imds(object())
def test_invalid_json(self): # invalid JSON error should just bubble out to the caller self.m_readurl.return_value = '{' with self.assertRaises(Exception): oracle._add_network_config_from_opc_imds([])
def test_empty_response(self): # empty response error should just bubble out to the caller self.m_readurl.return_value = '' with self.assertRaises(Exception): oracle._add_network_config_from_opc_imds([])
def test_failure_to_readurl(self): # readurl failures should just bubble out to the caller self.m_readurl.side_effect = Exception('oh no') with self.assertRaises(Exception) as excinfo: oracle._add_network_config_from_opc_imds({}) self.assertEqual(str(excinfo.exception), 'oh no')