Esempio n. 1
0
 def test_create_creates_device_with_static_ip_assignment_explicit(self):
     user = factory.make_User()
     handler = DeviceHandler(user, {})
     mac = factory.make_mac_address()
     hostname = factory.make_name("hostname")
     subnet = factory.make_Subnet()
     ip_address = factory.pick_ip_in_Subnet(subnet)
     created_device = handler.create({
         "hostname": hostname,
         "primary_mac": mac,
         "interfaces": [{
             "mac": mac,
             "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.STATIC,
             "subnet": subnet.id,
             "ip_address": ip_address,
         }],
     })
     self.expectThat(
         created_device["ip_assignment"],
         Equals(DEVICE_IP_ASSIGNMENT_TYPE.STATIC))
     self.expectThat(created_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.")
Esempio n. 2
0
 def test_create_creates_device_with_external_ip_assignment(self):
     user = factory.make_User()
     request = HttpRequest()
     request.user = user
     handler = DeviceHandler(user, {}, request)
     mac = factory.make_mac_address()
     hostname = factory.make_name("hostname")
     ip_address = factory.make_ipv4_address()
     created_device = handler.create({
         "hostname":
         hostname,
         "primary_mac":
         mac,
         "interfaces": [{
             "mac": mac,
             "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.EXTERNAL,
             "ip_address": ip_address,
         }],
     })
     self.expectThat(
         created_device["ip_assignment"],
         Equals(DEVICE_IP_ASSIGNMENT_TYPE.EXTERNAL),
     )
     self.expectThat(created_device["ip_address"], Equals(ip_address))
     self.expectThat(
         StaticIPAddress.objects.filter(ip=ip_address).count(),
         Equals(1),
         "StaticIPAddress was not created.",
     )
Esempio n. 3
0
 def test_create_creates_device_with_dynamic_ip_assignment(self):
     user = factory.make_User()
     request = HttpRequest()
     request.user = user
     handler = DeviceHandler(user, {}, request)
     mac = factory.make_mac_address()
     hostname = factory.make_name("hostname")
     description = factory.make_name("description")
     created_device = handler.create({
         "hostname":
         hostname,
         "description":
         description,
         "primary_mac":
         mac,
         "interfaces": [{
             "mac": mac,
             "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.DYNAMIC,
         }],
     })
     self.expectThat(created_device["hostname"], Equals(hostname))
     self.expectThat(created_device["description"], Equals(description))
     self.expectThat(created_device["primary_mac"], Equals(mac))
     self.expectThat(created_device["extra_macs"], Equals([]))
     self.expectThat(
         created_device["ip_assignment"],
         Equals(DEVICE_IP_ASSIGNMENT_TYPE.DYNAMIC),
     )
     self.expectThat(created_device["ip_address"], Is(None))
     self.expectThat(created_device["owner"], Equals(user.username))
Esempio n. 4
0
 def test_create_creates_device_with_static_and_external_ip(self):
     user = factory.make_User()
     request = HttpRequest()
     request.user = user
     handler = DeviceHandler(user, {}, request)
     hostname = factory.make_name("hostname")
     subnet = factory.make_Subnet()
     mac_static = factory.make_mac_address()
     static_ip_address = factory.pick_ip_in_Subnet(subnet)
     mac_external = factory.make_mac_address()
     external_ip_address = factory.make_ipv4_address()
     created_device = handler.create(
         {
             "hostname": hostname,
             "primary_mac": mac_static,
             "extra_macs": [mac_external],
             "interfaces": [
                 {
                     "mac": mac_static,
                     "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.STATIC,
                     "subnet": subnet.id,
                     "ip_address": static_ip_address,
                 },
                 {
                     "mac": mac_external,
                     "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.EXTERNAL,
                     "ip_address": external_ip_address,
                 },
             ],
         }
     )
     self.expectThat(created_device["primary_mac"], Equals(mac_static))
     self.expectThat(created_device["extra_macs"], Equals([mac_external]))
     self.expectThat(
         created_device["ip_assignment"],
         Equals(DEVICE_IP_ASSIGNMENT_TYPE.STATIC),
     )
     self.expectThat(
         created_device["ip_address"], Equals(static_ip_address)
     )
     static_interface = Interface.objects.get(mac_address=MAC(mac_static))
     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=static_ip_address).count(),
         Equals(1),
         "Static StaticIPAddress was not created.",
     )
     self.expectThat(
         StaticIPAddress.objects.filter(ip=external_ip_address).count(),
         Equals(1),
         "External StaticIPAddress was not created.",
     )
Esempio n. 5
0
 def test_create_copes_with_mac_addresses_of_different_forms(self):
     user = factory.make_User()
     handler = DeviceHandler(user, {})
     mac = factory.make_mac_address(delimiter=":")
     created_device = handler.create({
         "hostname": factory.make_name("hostname"),
         "primary_mac": mac,  # Colons.
         "interfaces": [{
             "mac": mac.replace(":", "-"),  # Hyphens.
             "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.DYNAMIC,
         }],
     })
     self.assertThat(created_device["primary_mac"], Equals(mac))
Esempio n. 6
0
 def test_create_copes_with_mac_addresses_of_different_case(self):
     user = factory.make_User()
     handler = DeviceHandler(user, {})
     mac = factory.make_mac_address()
     created_device = handler.create({
         "hostname": factory.make_name("hostname"),
         "primary_mac": mac.lower(),  # Lowercase.
         "interfaces": [{
             "mac": mac.upper(),  # Uppercase.
             "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.DYNAMIC,
         }],
     })
     self.assertThat(created_device["primary_mac"], Equals(mac))