Beispiel #1
0
    def test_mount_single_device(self):

        device1 = Device(
            name="TestSwitch1",
            device_type=DeviceType.objects.get(manufacturer__slug="acme",
                                               slug="ff2048"),
            device_role=DeviceRole.objects.get(slug="switch"),
            site=self.site1,
            rack=self.rack,
            position=10,
            face=DeviceFaceChoices.FACE_REAR,
        )
        device1.save()

        # Validate rack height
        self.assertEqual(list(self.rack.units), list(reversed(range(1, 43))))

        # Validate inventory (front face)
        rack1_inventory_front = self.rack.get_rack_units(
            face=DeviceFaceChoices.FACE_FRONT)
        self.assertEqual(rack1_inventory_front[-10]["device"], device1)
        del rack1_inventory_front[-10]
        for u in rack1_inventory_front:
            self.assertIsNone(u["device"])

        # Validate inventory (rear face)
        rack1_inventory_rear = self.rack.get_rack_units(
            face=DeviceFaceChoices.FACE_REAR)
        self.assertEqual(rack1_inventory_rear[-10]["device"], device1)
        del rack1_inventory_rear[-10]
        for u in rack1_inventory_rear:
            self.assertIsNone(u["device"])
Beispiel #2
0
    def test_rack_device_outside_height(self):

        rack1 = Rack(
            name="TestRack2",
            facility_id="A102",
            site=self.site1,
            status=self.status,
            u_height=42,
        )
        rack1.save()

        device1 = Device(
            name="TestSwitch1",
            device_type=DeviceType.objects.get(manufacturer__slug="acme",
                                               slug="ff2048"),
            device_role=DeviceRole.objects.get(slug="switch"),
            site=self.site1,
            rack=rack1,
            position=43,
            face=DeviceFaceChoices.FACE_FRONT,
        )
        device1.save()

        with self.assertRaises(ValidationError):
            rack1.clean()
Beispiel #3
0
    def test_device_creation(self):
        """
        Ensure that all Device components are copied automatically from the DeviceType.
        """
        d = Device(
            site=self.site,
            device_type=self.device_type,
            device_role=self.device_role,
            name="Test Device 1",
        )
        d.save()

        ConsolePort.objects.get(device=d, name="Console Port 1")

        ConsoleServerPort.objects.get(device=d, name="Console Server Port 1")

        pp = PowerPort.objects.get(device=d,
                                   name="Power Port 1",
                                   maximum_draw=1000,
                                   allocated_draw=500)

        PowerOutlet.objects.get(
            device=d,
            name="Power Outlet 1",
            power_port=pp,
            feed_leg=PowerOutletFeedLegChoices.FEED_LEG_A,
        )

        Interface.objects.get(
            device=d,
            name="Interface 1",
            type=InterfaceTypeChoices.TYPE_1GE_FIXED,
            mgmt_only=True,
        )

        rp = RearPort.objects.get(device=d,
                                  name="Rear Port 1",
                                  type=PortTypeChoices.TYPE_8P8C,
                                  positions=8)

        FrontPort.objects.get(
            device=d,
            name="Front Port 1",
            type=PortTypeChoices.TYPE_8P8C,
            rear_port=rp,
            rear_port_position=2,
        )

        DeviceBay.objects.get(device=d, name="Device Bay 1")
Beispiel #4
0
    def run(self, data, commit):
        # Create access switches
        switch_role = DeviceRole.objects.get(name='Access Switch')
        for i in range(1, data['switch_count'] + 1):
            switch = Device(device_type=data['switch_model'],
                            name=f'{switch{i}',
                            status=DeviceStatusChoices.STATUS_PLANNED,
                            device_role=switch_role)
            switch.validated_save()
            self.log_success(obj=switch, message="Created new switch")

        # Generate a CSV table of new devices
        output = ['name,make,model']
        for switch in Device.objects.filter(site=site):
            attrs = [
                switch.name, switch.device_type.manufacturer.name,
                switch.device_type.model
            ]
            output.append(','.join(attrs))

        return '\n'.join(output)
Beispiel #5
0
    def test_multiple_unnamed_devices(self):

        device1 = Device(
            site=self.site,
            device_type=self.device_type,
            device_role=self.device_role,
            status=self.device_status,
            name="",
        )
        device1.save()

        device2 = Device(
            site=device1.site,
            device_type=device1.device_type,
            device_role=device1.device_role,
            status=self.device_status,
            name="",
        )
        device2.full_clean()
        device2.save()

        self.assertEqual(Device.objects.filter(name="").count(), 2)
Beispiel #6
0
    def test_device_duplicate_names(self):

        device1 = Device(
            site=self.site,
            device_type=self.device_type,
            device_role=self.device_role,
            status=self.device_status,
            name="Test Device 1",
        )
        device1.save()

        device2 = Device(
            site=device1.site,
            device_type=device1.device_type,
            device_role=device1.device_role,
            status=self.device_status,
            name=device1.name,
        )

        # Two devices assigned to the same Site and no Tenant should fail validation
        with self.assertRaises(ValidationError):
            device2.full_clean()

        tenant = Tenant.objects.create(name="Test Tenant 1",
                                       slug="test-tenant-1")
        device1.tenant = tenant
        device1.save()
        device2.tenant = tenant

        # Two devices assigned to the same Site and the same Tenant should fail validation
        with self.assertRaises(ValidationError):
            device2.full_clean()

        device2.tenant = None

        # Two devices assigned to the same Site and different Tenants should pass validation
        device2.full_clean()
        device2.save()
Beispiel #7
0
    def setUpTestData(cls):

        manufacturers = (
            Manufacturer(name="Manufacturer 1", slug="manufacturer-1"),
            Manufacturer(name="Manufacturer 2", slug="manufacturer-2"),
            Manufacturer(name="Manufacturer 3", slug="manufacturer-3"),
        )
        Manufacturer.objects.bulk_create(manufacturers)

        device_types = (
            DeviceType(
                manufacturer=manufacturers[0],
                model="Model 1",
                slug="model-1",
                is_full_depth=True,
            ),
            DeviceType(
                manufacturer=manufacturers[1],
                model="Model 2",
                slug="model-2",
                is_full_depth=True,
            ),
            DeviceType(
                manufacturer=manufacturers[2],
                model="Model 3",
                slug="model-3",
                is_full_depth=False,
            ),
        )
        DeviceType.objects.bulk_create(device_types)

        device_roles = (
            DeviceRole(name="Device Role 1", slug="device-role-1"),
            DeviceRole(name="Device Role 2", slug="device-role-2"),
            DeviceRole(name="Device Role 3", slug="device-role-3"),
        )
        DeviceRole.objects.bulk_create(device_roles)

        device_statuses = Status.objects.get_for_model(Device)
        device_status_map = {ds.slug: ds for ds in device_statuses.all()}

        platforms = (
            Platform(name="Platform 1", slug="platform-1"),
            Platform(name="Platform 2", slug="platform-2"),
            Platform(name="Platform 3", slug="platform-3"),
        )
        Platform.objects.bulk_create(platforms)

        regions = (
            Region(name="Region 1", slug="region-1"),
            Region(name="Region 2", slug="region-2"),
            Region(name="Region 3", slug="region-3"),
        )
        for region in regions:
            region.save()

        sites = (
            Site(name="Site 1",
                 slug="abc-site-1",
                 region=regions[0],
                 asn=65001),
            Site(name="Site 2",
                 slug="def-site-2",
                 region=regions[1],
                 asn=65101),
            Site(name="Site 3",
                 slug="ghi-site-3",
                 region=regions[2],
                 asn=65201),
        )
        Site.objects.bulk_create(sites)

        racks = (
            Rack(name="Rack 1", site=sites[0]),
            Rack(name="Rack 2", site=sites[1]),
            Rack(name="Rack 3", site=sites[2]),
        )
        Rack.objects.bulk_create(racks)

        devices = (
            Device(
                name="Device 1",
                device_type=device_types[0],
                device_role=device_roles[0],
                platform=platforms[0],
                serial="ABC",
                asset_tag="1001",
                site=sites[0],
                rack=racks[0],
                position=1,
                face=DeviceFaceChoices.FACE_FRONT,
                status=device_status_map["active"],
                local_context_data={"foo": 123},
                comments="Device 1 comments",
            ),
            Device(
                name="Device 2",
                device_type=device_types[1],
                device_role=device_roles[1],
                platform=platforms[1],
                serial="DEF",
                asset_tag="1002",
                site=sites[1],
                rack=racks[1],
                position=2,
                face=DeviceFaceChoices.FACE_FRONT,
                status=device_status_map["staged"],
                comments="Device 2 comments",
            ),
            Device(
                name="Device 3",
                device_type=device_types[2],
                device_role=device_roles[2],
                platform=platforms[2],
                serial="GHI",
                asset_tag="1003",
                site=sites[2],
                rack=racks[2],
                position=3,
                face=DeviceFaceChoices.FACE_REAR,
                status=device_status_map["failed"],
                comments="Device 3 comments",
            ),
        )
        Device.objects.bulk_create(devices)

        interfaces = (
            Interface(device=devices[0],
                      name="Interface 1",
                      mac_address="00-00-00-00-00-01"),
            Interface(device=devices[0],
                      name="Interface 2",
                      mac_address="aa-00-00-00-00-01"),
            Interface(device=devices[1],
                      name="Interface 3",
                      mac_address="00-00-00-00-00-02"),
            Interface(device=devices[1],
                      name="Interface 4",
                      mac_address="bb-00-00-00-00-02"),
            Interface(device=devices[2],
                      name="Interface 5",
                      mac_address="00-00-00-00-00-03"),
            Interface(device=devices[2],
                      name="Interface 6",
                      mac_address="cc-00-00-00-00-03"),
        )
        Interface.objects.bulk_create(interfaces)