def test_init(self): ipm = ipmanager.NetBlock('192.168.1.0/24') self.assertEqual( { ipaddress.IPv4Address('192.168.1.0'): True, ipaddress.IPv4Address('192.168.1.255'): True }, ipm.in_use)
def allocate_network(netblock, provide_dhcp=True, provide_nat=False, name=None, namespace=None): net_id = str(uuid.uuid4()) ipm = ipmanager.NetBlock(netblock) etcd.put('ipmanager', None, net_id, ipm.save()) vxid = 1 while not etcd.create('vxlan', None, vxid, {'network_uuid': net_id}): vxid += 1 d = { 'uuid': net_id, 'vxid': vxid, 'netblock': netblock, 'provide_dhcp': provide_dhcp, 'provide_nat': provide_nat, 'namespace': namespace, 'floating_gateway': None, 'name': name, 'state': 'initial', 'state_updated': time.time() } etcd.put('network', None, net_id, d) return d
def __init__(self): self.db_entry = {'uuid': 'notauuid'} self.ipmanager = ipmanager.NetBlock('10.0.0.0/8') self.router = self.ipmanager.get_address_at_index(1) self.dhcp_start = '10.0.0.2' self.netmask = '255.0.0.0' self.broadcast = '10.255.255.255'
def __init__(self, uuid=None, vxlan_id=1, provide_dhcp=False, provide_nat=False, physical_nic='eth0', ipblock=None, ipmanager_state=None, floating_gateway=None): self.uuid = uuid self.vxlan_id = vxlan_id self.provide_dhcp = provide_dhcp self.provide_nat = provide_nat self.physical_nic = physical_nic self.floating_gateway = floating_gateway if ipmanager_state: self.ipmanager = ipmanager.from_db(ipmanager_state.decode('utf-8')) self.router = self.ipmanager.get_address_at_index(1) else: self.ipmanager = ipmanager.NetBlock(ipblock) self.router = self.ipmanager.get_address_at_index(1) self.ipmanager.reserve(self.router) self.persist_ipmanager() self.netmask = self.ipmanager.netmask
def allocate_network(netblock, provide_dhcp=True, provide_nat=False, name=None, namespace=None): see_this_node() netid = str(uuid.uuid4()) ipm = ipmanager.NetBlock(netblock) etcd.put('ipmanager', None, netid, ipm.save()) with etcd.get_lock('sf/vxlan') as _: vxid = 1 while etcd.get('vxlan', None, vxid): vxid += 1 etcd.put('vxlan', None, vxid, {'network_uuid': netid}) d = { 'uuid': netid, 'vxid': vxid, 'netblock': netblock, 'provide_dhcp': provide_dhcp, 'provide_nat': provide_nat, 'namespace': namespace, 'floating_gateway': None, 'name': name, 'state': 'initial', 'state_updated': time.time() } etcd.put('network', None, netid, d) return d
def test_get_free_random_ip(self): ipm = ipmanager.NetBlock('10.0.0.0/8') for i in range(65025): ipm.get_random_free_address() # The extra two are the reserved network and broadcast # addresses self.assertEqual(65025 + 2, len(ipm.in_use))
def test_is_free_and_reserve(self): ipm = ipmanager.NetBlock('192.168.1.0/24') self.assertEqual(True, ipm.is_free('192.168.1.24')) ipm.reserve('192.168.1.24') self.assertEqual(False, ipm.is_free('192.168.1.24')) self.assertEqual(False, ipm.reserve_if_free('192.168.1.24')) self.assertEqual(True, ipm.is_free('192.168.1.42')) self.assertEqual(True, ipm.reserve_if_free('192.168.1.42')) self.assertEqual(False, ipm.is_free('192.168.1.42')) ipm.release('192.168.1.42') self.assertEqual(True, ipm.is_free('192.168.1.42'))
def create_floating_network(netblock): ipm = ipmanager.NetBlock(netblock) etcd.put('ipmanager', None, 'floating', ipm.save()) etcd.put( 'network', None, 'floating', { 'uuid': 'floating', 'vxid': 0, 'netblock': netblock, 'provide_dhcp': False, 'provide_nat': False, 'namespace': None, 'floating_gateway': None, 'name': 'floating', 'state': 'initial', 'state_updated': time.time() })
class ExternalApiNetworkTestCase(ExternalApiTestCase): def setUp(self): super(ExternalApiNetworkTestCase, self).setUp() class FakeConfig(SFConfigBase): NODE_NAME: str = 'seriously' NODE_IP: str = '127.0.0.1' NETWORK_NODE_IP = '127.0.0.1' LOG_METHOD_TRACE: int = 1 NODE_EGRESS_NIC: str = 'eth0' fake_config_network = FakeConfig() self.config = mock.patch('shakenfist.config.config', fake_config_network) self.mock_config = self.config.start() # Without this cleanup, other test classes will have # 'shakenfist.config.config.get' mocked during parallel testing # by stestr. self.addCleanup(self.config.stop) @mock.patch('shakenfist.db.get_network', return_value={ 'uuid': '30f6da44-look-i-am-uuid', 'state': 'created', }) @mock.patch('shakenfist.db.get_networks', return_value=[{ 'floating_gateway': '10.10.0.150', 'name': 'bob', 'state': 'created', 'uuid': '30f6da44-look-i-am-uuid', }]) @mock.patch('shakenfist.db.get_network_interfaces', return_value=[]) @mock.patch('shakenfist.db.get_ipmanager', return_value=ipmanager.NetBlock('10.0.0.0/24')) @mock.patch('shakenfist.net.Network.remove_dhcp') @mock.patch('shakenfist.net.Network.delete') @mock.patch('shakenfist.db.update_network_state') @mock.patch('shakenfist.etcd.put') @mock.patch('shakenfist.db.get_lock') def test_delete_all_networks(self, mock_db_get_lock, mock_etcd_put, mock_update_network_state, mock_delete, mock_remove_dhcp, mock_get_ipmanager, mock_db_get_network_interfaces, mock_db_get_networks, mock_db_get_network): resp = self.client.delete('/networks', headers={'Authorization': self.auth_header}, data=json.dumps({ 'confirm': True, 'namespace': 'foo' })) self.assertEqual(['30f6da44-look-i-am-uuid'], resp.get_json()) self.assertEqual(200, resp.status_code) @mock.patch('shakenfist.db.get_networks', return_value=[{ 'floating_gateway': '10.10.0.150', 'name': 'bob', 'state': 'deleted', 'uuid': '30f6da44-look-i-am-uuid', }]) @mock.patch('shakenfist.db.get_network_interfaces', return_value=[]) @mock.patch('shakenfist.db.get_ipmanager', return_value=ipmanager.NetBlock('10.0.0.0/24')) @mock.patch('shakenfist.net.Network.remove_dhcp') @mock.patch('shakenfist.etcd.put') @mock.patch('shakenfist.db.get_lock') def test_delete_all_networks_none_to_delete(self, mock_db_get_lock, mock_etcd_put, mock_remove_dhcp, mock_get_ipmanager, mock_db_get_network_interfaces, mock_db_get_networks): resp = self.client.delete('/networks', headers={'Authorization': self.auth_header}, data=json.dumps({ 'confirm': True, 'namespace': 'foo' })) self.assertEqual([], resp.get_json())
def __init__(self): self.ipmanager = ipmanager.NetBlock('127.0.0.0/8') self.router = self.ipmanager.get_address_at_index(1) self.netmask = '255.0.0.0' self.dhcp_start = '127.0.0.2' self.broadcast = '127.255.255.255'
class ExternalApiNetworkTestCase(ExternalApiTestCase): def setUp(self): super(ExternalApiNetworkTestCase, self).setUp() def fake_config_network(key): fc = { 'NODE_NAME': 'seriously', 'NODE_IP': '127.0.0.1', 'NETWORK_NODE_IP': '127.0.0.1', 'LOG_METHOD_TRACE': 1, 'NODE_EGRESS_NIC': 'eth0' } if key in fc: return fc[key] raise Exception('fake_config_network() Unknown config key') self.config = mock.patch('shakenfist.config.parsed.get', fake_config_network) self.mock_config = self.config.start() # Without this cleanup, other test classes will have 'config.parsed.get' # mocked during parallel testing by stestr. self.addCleanup(self.config.stop) @mock.patch('shakenfist.db.get_networks', return_value=[{ 'floating_gateway': '10.10.0.150', 'name': 'bob', 'state': 'created', 'uuid': '30f6da44-look-i-am-uuid', }]) @mock.patch('shakenfist.db.get_network_interfaces', return_value=[]) @mock.patch('shakenfist.db.get_ipmanager', return_value=ipmanager.NetBlock('10.0.0.0/24')) @mock.patch('shakenfist.net.Network.remove_dhcp') @mock.patch('shakenfist.net.Network.delete') @mock.patch('shakenfist.db.update_network_state') @mock.patch('shakenfist.etcd.put') @mock.patch('shakenfist.db.get_lock') def test_delete_all_networks(self, mock_db_get_lock, mock_etcd_put, mock_update_network_state, mock_delete, mock_remove_dhcp, mock_get_ipmanager, mock_db_get_network_interfaces, mock_db_get_networks): mock_network = mock.patch('shakenfist.net.from_db', return_value=net.Network({'uuid': 'foo'})) mock_network.start() self.addCleanup(mock_network.stop) resp = self.client.delete('/networks', headers={'Authorization': self.auth_header}, data=json.dumps({ 'confirm': True, 'namespace': 'foo' })) self.assertEqual(['30f6da44-look-i-am-uuid'], resp.get_json()) self.assertEqual(200, resp.status_code) mock_network.stop() @mock.patch('shakenfist.db.get_networks', return_value=[{ 'floating_gateway': '10.10.0.150', 'name': 'bob', 'state': 'deleted', 'uuid': '30f6da44-look-i-am-uuid', }]) @mock.patch('shakenfist.db.get_network_interfaces', return_value=[]) @mock.patch('shakenfist.db.get_ipmanager', return_value=ipmanager.NetBlock('10.0.0.0/24')) @mock.patch('shakenfist.net.Network.remove_dhcp') @mock.patch('shakenfist.etcd.put') @mock.patch('shakenfist.db.get_lock') def test_delete_all_networks_none_to_delete(self, mock_db_get_lock, mock_etcd_put, mock_remove_dhcp, mock_get_ipmanager, mock_db_get_network_interfaces, mock_db_get_networks): resp = self.client.delete('/networks', headers={'Authorization': self.auth_header}, data=json.dumps({ 'confirm': True, 'namespace': 'foo' })) self.assertEqual([], resp.get_json())
def test_get_address_at_index(self): ipm = ipmanager.NetBlock('192.168.1.0/24') self.assertEqual('192.168.1.1', str(ipm.get_address_at_index(1))) self.assertEqual('192.168.1.254', str(ipm.get_address_at_index(-2)))
def __init__(self): self.ipmanager = ipmanager.NetBlock('127.0.0.0/8') self.router = self.ipmanager.get_address_at_index(1)