def test_update_interface_updates_non_admin(self): user = factory.make_User() node = factory.make_Node(owner=user, interface=False, node_type=NODE_TYPE.DEVICE) handler = DeviceHandler(user, {}, None) name = factory.make_name("eth") mac_address = factory.make_mac_address() ip_assignment = DEVICE_IP_ASSIGNMENT_TYPE.DYNAMIC params = { "system_id": node.system_id, "name": name, "mac_address": mac_address, "ip_assignment": ip_assignment, } handler.create_interface(params) interface = node.interface_set.first() self.assertIsNotNone(interface) new_mac_address = factory.make_mac_address() new_params = { "system_id": node.system_id, "interface_id": interface.id, "name": name, "mac_address": new_mac_address, "ip_assignment": ip_assignment, } handler.update_interface(new_params) data = self.dehydrate_device(node, user)["interfaces"] self.assertEqual(1, len(data)) self.assertEqual(data[0]["mac_address"], new_mac_address)
def test_create_interface_creates_static(self): user = factory.make_admin() node = factory.make_Node(interface=False, node_type=NODE_TYPE.DEVICE) handler = DeviceHandler(user, {}, None) name = factory.make_name("eth") mac_address = factory.make_mac_address() fabric = factory.make_Fabric() vlan = fabric.get_default_vlan() subnet = factory.make_Subnet(vlan=vlan) handler.create_interface( { "system_id": node.system_id, "name": name, "mac_address": mac_address, "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.STATIC, "subnet": subnet.id, } ) new_interface = node.interface_set.first() self.assertIsNotNone(new_interface) auto_ip = new_interface.ip_addresses.filter( alloc_type=IPADDRESS_TYPE.STICKY, subnet=subnet ) self.assertIsNotNone(auto_ip) self.assertEqual(1, len(auto_ip))
def test_create_interface_creates_interface(self): user = factory.make_admin() node = factory.make_Node(interface=False, node_type=NODE_TYPE.DEVICE) handler = DeviceHandler(user, {}) name = factory.make_name("eth") mac_address = factory.make_mac_address() handler.create_interface({ "system_id": node.system_id, "name": name, "mac_address": mac_address, "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.DYNAMIC, }) self.assertEqual( 1, node.interface_set.count(), "Should have one interface on the node.")
def test_create_interface_creates_static_ip_assignment_explicit(self): user = factory.make_User() handler = DeviceHandler(user, {}) device = factory.make_Device(owner=user) mac = factory.make_mac_address() subnet = factory.make_Subnet() ip_address = factory.pick_ip_in_Subnet(subnet) updated_device = handler.create_interface({ "system_id": device.system_id, "mac_address": mac, "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.STATIC, "subnet": subnet.id, "ip_address": ip_address, }) self.expectThat(updated_device["primary_mac"], Equals(mac)) self.expectThat( updated_device["ip_assignment"], Equals(DEVICE_IP_ASSIGNMENT_TYPE.STATIC)) self.expectThat(updated_device["ip_address"], Equals(ip_address)) static_interface = Interface.objects.get(mac_address=MAC(mac)) observed_subnet = static_interface.ip_addresses.first().subnet self.expectThat( observed_subnet, Equals(subnet), "Static assignment to the subnet was not created.") self.expectThat( StaticIPAddress.objects.filter(ip=ip_address).count(), Equals(1), "StaticIPAddress was not created.")
def test_create_interface_creates_with_external_ip_assignment(self): user = factory.make_User() request = HttpRequest() request.user = user handler = DeviceHandler(user, {}, request) device = factory.make_Device(owner=user) mac = factory.make_mac_address() ip_address = factory.make_ipv4_address() updated_device = handler.create_interface({ "system_id": device.system_id, "mac_address": mac, "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.EXTERNAL, "ip_address": ip_address, }) self.expectThat(updated_device["primary_mac"], Equals(mac)) self.expectThat( updated_device["ip_assignment"], Equals(DEVICE_IP_ASSIGNMENT_TYPE.EXTERNAL), ) self.expectThat( StaticIPAddress.objects.filter(ip=ip_address).count(), Equals(1), "StaticIPAddress was not created.", )
def test_update_interface_updates(self): user = factory.make_admin() node = factory.make_Node(interface=False, node_type=NODE_TYPE.DEVICE) handler = DeviceHandler(user, {}) name = factory.make_name("eth") mac_address = factory.make_mac_address() ip_assignment = factory.pick_enum(DEVICE_IP_ASSIGNMENT_TYPE) params = { "system_id": node.system_id, "name": name, "mac_address": mac_address, "ip_assignment": ip_assignment, } if ip_assignment == DEVICE_IP_ASSIGNMENT_TYPE.STATIC: subnet = factory.make_Subnet() params['subnet'] = subnet.id ip_address = str(IPAddress(IPNetwork(subnet.cidr).first)) params['ip_address'] = ip_address elif ip_assignment == DEVICE_IP_ASSIGNMENT_TYPE.EXTERNAL: ip_address = factory.make_ip_address() params['ip_address'] = ip_address handler.create_interface(params) interface = node.interface_set.first() self.assertIsNotNone(interface) new_name = factory.make_name("eth") new_ip_assignment = factory.pick_enum(DEVICE_IP_ASSIGNMENT_TYPE) new_params = { "system_id": node.system_id, "interface_id": interface.id, "name": new_name, "mac_address": mac_address, "ip_assignment": new_ip_assignment, } if new_ip_assignment == DEVICE_IP_ASSIGNMENT_TYPE.STATIC: new_subnet = factory.make_Subnet() new_params['subnet'] = new_subnet.id new_ip_address = str(IPAddress(IPNetwork(new_subnet.cidr).first)) new_params['ip_address'] = new_ip_address elif new_ip_assignment == DEVICE_IP_ASSIGNMENT_TYPE.EXTERNAL: new_ip_address = factory.make_ip_address() new_params['ip_address'] = new_ip_address handler.update_interface(new_params) data = self.dehydrate_device(node, user)['interfaces'] self.assertEqual(1, len(data)) self.assertEqual(data[0]['ip_assignment'], new_ip_assignment) if new_ip_assignment != DEVICE_IP_ASSIGNMENT_TYPE.DYNAMIC: self.assertEqual(data[0]['ip_address'], new_ip_address)
def test_create_interface_creates_external(self): user = factory.make_admin() node = factory.make_Node(interface=False, node_type=NODE_TYPE.DEVICE) handler = DeviceHandler(user, {}) name = factory.make_name("eth") mac_address = factory.make_mac_address() ip_address = factory.make_ip_address() handler.create_interface({ "system_id": node.system_id, "name": name, "mac_address": mac_address, "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.EXTERNAL, "ip_address": ip_address, }) new_interface = node.interface_set.first() self.assertIsNotNone(new_interface) auto_ip = new_interface.ip_addresses.filter( alloc_type=IPADDRESS_TYPE.USER_RESERVED) self.assertIsNotNone(auto_ip) self.assertEqual(1, len(auto_ip))
def test_create_interface_creates_with_dynamic_ip_assignment(self): user = factory.make_User() handler = DeviceHandler(user, {}) device = factory.make_Device(owner=user) mac = factory.make_mac_address() updated_device = handler.create_interface({ "system_id": device.system_id, "mac_address": mac, "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.DYNAMIC, }) self.expectThat(updated_device["primary_mac"], Equals(mac)) self.expectThat( updated_device["ip_assignment"], Equals(DEVICE_IP_ASSIGNMENT_TYPE.DYNAMIC))