Example #1
0
 def test_full_dehydrate_for_list_no_details(self):
     admin = factory.make_admin()
     handler = PodHandler(admin, {}, None)
     vlan = factory.make_VLAN(dhcp_on=True)
     subnet = factory.make_Subnet(vlan=vlan)
     node = factory.make_Node_with_Interface_on_Subnet(subnet=subnet)
     ip = factory.make_StaticIPAddress(interface=node.boot_interface,
                                       subnet=subnet)
     numa_node0 = node.default_numanode
     numa_node0.cores = [0, 3]
     numa_node0.memory = 4096
     numa_node0.save()
     factory.make_NUMANode(node=node, cores=[1, 4], memory=1024)
     factory.make_NUMANode(node=node, cores=[2, 5], memory=2048)
     project = factory.make_string()
     pod = self.make_pod_with_hints(
         pod_type="lxd",
         parameters={"project": project},
         host=node,
         ip_address=ip,
     )
     factory.make_VirtualMachine(bmc=pod, project=project)
     result = handler.full_dehydrate(pod, for_list=True)
     self.assertEqual(result["resources"]["numa"], [])
     self.assertEqual(result["resources"]["vms"], [])
Example #2
0
 def test_get_resources_no_detailed(self):
     pod = factory.make_Pod(pod_type="lxd", host=factory.make_Node())
     factory.make_VirtualMachine(bmc=pod)
     resources = get_vm_host_resources(pod, detailed=False)
     # NUMA info and VMs list are not included when not in detailed mode
     self.assertEqual(resources.numa, [])
     self.assertEqual(resources.vms, [])
Example #3
0
 def test_get_resources_hugepages_round(self):
     node = factory.make_Node()
     numa_node0 = node.default_numanode
     numa_node0.cores = [0, 1]
     numa_node0.memory = 4096
     numa_node0.save()
     numa_node1 = factory.make_NUMANode(
         node=node, cores=[2, 3], memory=8192
     )
     factory.make_NUMANodeHugepages(
         numa_node=numa_node0, page_size=2048 * MB, total=4096 * MB
     )
     factory.make_NUMANodeHugepages(
         numa_node=numa_node1, page_size=4096 * MB, total=8192 * MB
     )
     pod = factory.make_Pod(pod_type="lxd")
     pod.hints.nodes.add(node)
     factory.make_VirtualMachine(
         memory=2048,
         pinned_cores=[0, 2],
         hugepages_backed=True,
         bmc=pod,
         machine=factory.make_Node(system_id="vm0"),
     )
     resources = get_vm_host_resources(pod)
     self.assertEqual(
         [asdict(r) for r in resources],
         [
             {
                 "cores": {"allocated": [0], "free": [1]},
                 "memory": {
                     "general": {"allocated": 0, "free": 0},
                     "hugepages": [
                         {
                             "allocated": 2048 * MB,
                             "free": 2048 * MB,
                             "page_size": 2048 * MB,
                         }
                     ],
                 },
                 "node_id": 0,
                 "vms": [{"pinned_cores": [0], "system_id": "vm0"}],
             },
             {
                 "cores": {"allocated": [2], "free": [3]},
                 "memory": {
                     "general": {"allocated": 0, "free": 0},
                     "hugepages": [
                         {
                             "allocated": 4096 * MB,
                             "free": 4096 * MB,
                             "page_size": 4096 * MB,
                         }
                     ],
                 },
                 "node_id": 1,
                 "vms": [{"pinned_cores": [2], "system_id": "vm0"}],
             },
         ],
     )
Example #4
0
    def test_get_vm_hosts_stats_machine_usage(self):
        lxd_vm_host = self.make_pod(cpu=10, mem=100, pod_type="lxd")
        lxd_machine = factory.make_Machine(bmc=lxd_vm_host,
                                           cpu_count=1,
                                           memory=10)
        factory.make_VirtualMachine(bmc=lxd_vm_host, machine=lxd_machine)
        virsh_vm_host = self.make_pod(cpu=20, mem=200, pod_type="virsh")
        virsh_machine = factory.make_Machine(bmc=virsh_vm_host,
                                             cpu_count=2,
                                             memory=20)
        factory.make_VirtualMachine(bmc=virsh_vm_host, machine=virsh_machine)

        stats = get_vm_hosts_stats(power_type="lxd")
        compare = {
            "vm_hosts": 1,
            "vms": 1,
            "available_resources": {
                "cores": 10,
                "memory": 100,
                "over_cores": 10.0,
                "over_memory": 100.0,
                "storage": 0,
            },
            "utilized_resources": {
                "cores": 1,
                "memory": 10,
                "storage": 0
            },
        }
        self.assertEqual(compare, stats)
 def test_get_resources_no_host(self):
     pod = factory.make_Pod(pod_type="lxd", host=None)
     factory.make_VirtualMachine(
         memory=1024,
         pinned_cores=[0, 2],
         bmc=pod,
     )
     self.assertEqual(get_vm_host_resources(pod), [])
 def test_get_resources_unaligned(self):
     node = factory.make_Node()
     numa_node0 = node.default_numanode
     numa_node0.cores = [0, 1]
     numa_node0.memory = 4096
     numa_node0.save()
     factory.make_NUMANode(node=node, cores=[2, 3], memory=2048)
     pod = factory.make_Pod(pod_type="lxd")
     pod.hints.nodes.add(node)
     factory.make_VirtualMachine(
         memory=2048,
         pinned_cores=[0, 2],
         hugepages_backed=False,
         bmc=pod,
         machine=factory.make_Node(system_id="vm0"),
     )
     resources = get_vm_host_resources(pod)
     self.assertEqual(
         [asdict(r) for r in resources],
         [
             {
                 "cores": {
                     "allocated": [0],
                     "free": [1]
                 },
                 "memory": {
                     "general": {
                         "allocated": 1024 * MB,
                         "free": 3072 * MB
                     },
                     "hugepages": [],
                 },
                 "node_id": 0,
                 "vms": [{
                     "pinned_cores": [0],
                     "system_id": "vm0"
                 }],
             },
             {
                 "cores": {
                     "allocated": [2],
                     "free": [3]
                 },
                 "memory": {
                     "general": {
                         "allocated": 1024 * MB,
                         "free": 1024 * MB
                     },
                     "hugepages": [],
                 },
                 "node_id": 1,
                 "vms": [{
                     "pinned_cores": [2],
                     "system_id": "vm0"
                 }],
             },
         ],
     )
Example #7
0
 def test_get_resources_numa_aligned(self):
     node = factory.make_Node()
     numa_node0 = node.default_numanode
     numa_node0.cores = [0, 3]
     numa_node0.memory = 4096
     numa_node0.save()
     factory.make_NUMANode(node=node, cores=[1, 4], memory=1024)
     factory.make_NUMANode(node=node, cores=[2, 5], memory=2048)
     pod = factory.make_Pod(pod_type="lxd", host=node)
     vm0 = factory.make_VirtualMachine(
         memory=1024,
         pinned_cores=[0],
         hugepages_backed=False,
         bmc=pod,
     )
     vm1 = factory.make_VirtualMachine(
         memory=1024,
         pinned_cores=[2, 5],
         hugepages_backed=False,
         bmc=pod,
     )
     resources = get_vm_host_resources(pod)
     self.assertEqual(
         [asdict(r) for r in resources.numa],
         [
             {
                 "cores": {"allocated": [0], "free": [3]},
                 "memory": {
                     "general": {"allocated": 1024 * MB, "free": 3072 * MB},
                     "hugepages": [],
                 },
                 "interfaces": [],
                 "node_id": 0,
                 "vms": [vm0.id],
             },
             {
                 "cores": {"allocated": [], "free": [1, 4]},
                 "memory": {
                     "general": {"allocated": 0, "free": 1024 * MB},
                     "hugepages": [],
                 },
                 "interfaces": [],
                 "node_id": 1,
                 "vms": [],
             },
             {
                 "cores": {"allocated": [2, 5], "free": []},
                 "memory": {
                     "general": {"allocated": 1024 * MB, "free": 1024 * MB},
                     "hugepages": [],
                 },
                 "interfaces": [],
                 "node_id": 2,
                 "vms": [vm1.id],
             },
         ],
     )
Example #8
0
 def test_get_resources_vms(self):
     node = factory.make_Node()
     numa_node0 = node.default_numanode
     numa_node0.cores = [0, 1, 2, 3]
     numa_node0.memory = 4096
     numa_node0.save()
     project = factory.make_string()
     pod = factory.make_Pod(pod_type="lxd",
                            parameters={"project": project},
                            host=node)
     node = factory.make_Node(bmc=pod)
     vm0 = factory.make_VirtualMachine(
         machine=node,
         memory=1024,
         pinned_cores=[0, 1],
         hugepages_backed=False,
         bmc=pod,
         project=project,
     )
     vm1 = factory.make_VirtualMachine(
         memory=1024,
         unpinned_cores=2,
         hugepages_backed=True,
         bmc=pod,
         project=project,
     )
     # another VM, in a different project
     factory.make_VirtualMachine(
         memory=1024,
         unpinned_cores=2,
         bmc=pod,
         project=factory.make_string(),
     )
     resources = get_vm_host_resources(pod)
     self.assertCountEqual(
         resources.vms,
         [
             VMHostVirtualMachineResources(
                 id=vm0.id,
                 system_id=node.system_id,
                 pinned_cores=[0, 1],
                 unpinned_cores=0,
                 memory=1024 * MB,
                 hugepages_backed=False,
             ),
             VMHostVirtualMachineResources(
                 id=vm1.id,
                 system_id=None,
                 pinned_cores=[],
                 unpinned_cores=2,
                 memory=1024 * MB,
                 hugepages_backed=True,
             ),
         ],
     )
Example #9
0
 def test_get_used_storage(self):
     project = factory.make_string()
     pod = factory.make_Pod(parameters={"project": project})
     pool = factory.make_PodStoragePool(pod=pod)
     size = 0
     for _ in range(3):
         vm = factory.make_VirtualMachine(project=project)
         disk = factory.make_VirtualMachineDisk(vm=vm, backing_pool=pool)
         size += disk.size
     # disks for VMs in other projects are not counted
     for _ in range(4):
         vm = factory.make_VirtualMachine(project=factory.make_string())
         factory.make_VirtualMachineDisk(vm=vm, backing_pool=pool)
     self.assertEqual(size, pool.get_used_storage())
Example #10
0
 def test_get_resources_no_host(self):
     pod = factory.make_Pod(pod_type="lxd", host=None)
     factory.make_VirtualMachine(
         memory=1024,
         pinned_cores=[0, 2],
         bmc=pod,
     )
     resources = get_vm_host_resources(pod)
     self.assertEqual(resources.cores.free, 0)
     self.assertEqual(resources.cores.allocated, 0)
     self.assertEqual(resources.memory.general.free, 0)
     self.assertEqual(resources.memory.general.allocated, 0)
     self.assertEqual(resources.memory.hugepages.free, 0)
     self.assertEqual(resources.memory.hugepages.allocated, 0)
     self.assertEqual(resources.numa, [])
     self.assertEqual(resources.vms, [])
Example #11
0
 def test_get_resources_interfaces_not_sriov(self):
     node = factory.make_Node()
     iface = factory.make_Interface(
         INTERFACE_TYPE.PHYSICAL,
         name="eth0",
         numa_node=node.default_numanode,
         sriov_max_vf=8,
     )
     project = factory.make_string()
     pod = factory.make_Pod(
         pod_type="lxd",
         parameters={"project": project},
         host=node,
     )
     VirtualMachineInterface.objects.create(
         vm=factory.make_VirtualMachine(bmc=pod, project=project),
         host_interface=iface,
         attachment_type=InterfaceAttachType.BRIDGE,
     )
     resources = get_vm_host_resources(pod)
     self.assertEqual(
         resources.interfaces,
         [
             VMHostNetworkInterface(
                 id=iface.id,
                 name="eth0",
                 numa_index=0,
                 virtual_functions=VMHostResource(
                     allocated_tracked=0,
                     allocated_other=0,
                     free=8,
                 ),
             ),
         ],
     )
Example #12
0
 def test_get_resources_global_resources_pinned_cores_overlap(self):
     node = factory.make_Node()
     numa_node0 = node.default_numanode
     numa_node0.cores = [0, 1]
     numa_node0.memory = 4096
     numa_node0.save()
     factory.make_NUMANode(node=node, cores=[2, 3], memory=2048)
     pod = factory.make_Pod(pod_type="lxd", host=node)
     factory.make_VirtualMachine(
         pinned_cores=[0, 1],
         bmc=pod,
     )
     factory.make_VirtualMachine(
         pinned_cores=[1, 2],
         bmc=pod,
     )
     resources = get_vm_host_resources(pod)
     self.assertEqual(resources.cores.free, 0)
     self.assertEqual(resources.cores.allocated, 4)
Example #13
0
 def test_GET_reads_vms(self):
     vm1 = factory.make_VirtualMachine(
         memory=1024, unpinned_cores=4, hugepages_backed=True
     )
     vm2 = factory.make_VirtualMachine(
         memory=2048,
         pinned_cores=[0, 1, 2, 3],
         hugepages_backed=False,
     )
     response = self.client.get("/MAAS/api/2.0/virtual-machines/")
     self.assertEqual(
         http.client.OK, response.status_code, response.content
     )
     self.assertEqual(
         json_load_bytes(response.content),
         [
             {
                 "id": vm1.id,
                 "identifier": vm1.identifier,
                 "bmc_id": vm1.bmc_id,
                 "project": vm1.project,
                 "hugepages_backed": True,
                 "machine_id": None,
                 "memory": 1024,
                 "pinned_cores": [],
                 "resource_uri": f"/MAAS/api/2.0/virtual-machines/{vm1.id}",
                 "unpinned_cores": 4,
             },
             {
                 "id": vm2.id,
                 "identifier": vm2.identifier,
                 "bmc_id": vm2.bmc_id,
                 "project": vm2.project,
                 "hugepages_backed": False,
                 "machine_id": None,
                 "memory": 2048,
                 "pinned_cores": [0, 1, 2, 3],
                 "resource_uri": f"/MAAS/api/2.0/virtual-machines/{vm2.id}",
                 "unpinned_cores": 0,
             },
         ],
     )
Example #14
0
    def test_get_used_resources(self):
        project = factory.make_string()
        vmhost = factory.make_Pod(
            pod_type="lxd",
            parameters={"project": project},
        )
        pool1 = factory.make_PodStoragePool(pod=vmhost)
        pool2 = factory.make_PodStoragePool(pod=vmhost)

        vm1 = factory.make_VirtualMachine(
            bmc=vmhost,
            project=project,
            memory=1024,
            pinned_cores=[0, 1, 2],
            hugepages_backed=False,
        )
        factory.make_VirtualMachineDisk(vm=vm1, backing_pool=pool1, size=1000)
        vm2 = factory.make_VirtualMachine(
            bmc=vmhost,
            project=project,
            memory=2048,
            unpinned_cores=2,
            hugepages_backed=False,
        )
        factory.make_VirtualMachineDisk(vm=vm2, backing_pool=pool2, size=2000)
        vm3 = factory.make_VirtualMachine(
            bmc=vmhost,
            project=project,
            memory=4096,
            unpinned_cores=4,
            hugepages_backed=True,
        )
        factory.make_VirtualMachineDisk(vm=vm3, backing_pool=pool1, size=500)

        used_resources = get_vm_host_used_resources(vmhost)
        self.assertEqual(used_resources.cores, 9)
        self.assertEqual(used_resources.memory, 3072)
        self.assertEqual(used_resources.hugepages_memory, 4096)
        self.assertEqual(used_resources.total_memory, 7168)
        self.assertEqual(used_resources.storage, 3500)
Example #15
0
    def test_read_returns_storage_pool(self):
        node = factory.make_Node(with_boot_disk=False)
        vm = factory.make_VirtualMachine(machine=node)
        pool = factory.make_PodStoragePool()
        block_device = factory.make_PhysicalBlockDevice(node=node)
        factory.make_VirtualMachineDisk(vm,
                                        backing_pool=pool,
                                        block_device=block_device)

        uri = get_blockdevices_uri(node)
        response = self.client.get(uri)

        self.assertEqual(http.client.OK, response.status_code,
                         response.content)
        parsed_devices = json_load_bytes(response.content)
        self.assertEqual(parsed_devices[0]["storage_pool"], pool.pool_id)
Example #16
0
    def test_get_used_resources_only_tracked_project(self):
        project = factory.make_string()
        vmhost = factory.make_Pod(
            pod_type="lxd",
            parameters={"project": project},
        )
        pool = factory.make_PodStoragePool(pod=vmhost)

        vm = factory.make_VirtualMachine(
            bmc=vmhost,
            project=factory.make_string(),
            memory=1024,
            pinned_cores=[0, 1, 2],
            hugepages_backed=False,
        )
        factory.make_VirtualMachineDisk(vm=vm, backing_pool=pool, size=1000)
        used_resources = get_vm_host_used_resources(vmhost)
        self.assertEqual(used_resources.cores, 0)
        self.assertEqual(used_resources.memory, 0)
        self.assertEqual(used_resources.hugepages_memory, 0)
        self.assertEqual(used_resources.storage, 0)
Example #17
0
 def test_GET_reads_vm(self):
     vm = factory.make_VirtualMachine(memory=1024,
                                      unpinned_cores=4,
                                      hugepages_backed=True)
     response = self.client.get(get_vm_uri(vm.id))
     self.assertEqual(http.client.OK, response.status_code,
                      response.content)
     self.assertEqual(
         json_load_bytes(response.content),
         {
             "id": vm.id,
             "identifier": vm.identifier,
             "bmc_id": vm.bmc_id,
             "hugepages_backed": True,
             "machine_id": None,
             "memory": 1024,
             "pinned_cores": [],
             "resource_uri": f"/MAAS/api/2.0/virtual-machines/{vm.id}",
             "unpinned_cores": 4,
         },
     )
Example #18
0
 def test_get_resources_no_host(self):
     pod = factory.make_Pod(
         pod_type="lxd",
         host=None,
         cores=8,
         memory=4096,
     )
     node = factory.make_Node(bmc=pod)
     vm = factory.make_VirtualMachine(
         machine=node,
         memory=1024,
         pinned_cores=[0, 2],
         hugepages_backed=False,
         bmc=pod,
     )
     resources = get_vm_host_resources(pod)
     self.assertEqual(resources.cores.free, 6)
     self.assertEqual(resources.cores.allocated, 2)
     self.assertEqual(resources.memory.general.free, 3072 * MB)
     self.assertEqual(resources.memory.general.allocated, 1024 * MB)
     self.assertEqual(resources.memory.hugepages.free, 0)
     self.assertEqual(resources.memory.hugepages.allocated, 0)
     self.assertEqual(resources.numa, [])
     self.assertEqual(
         resources.vms,
         [
             VMHostVirtualMachineResources(
                 id=vm.id,
                 system_id=node.system_id,
                 pinned_cores=[0, 2],
                 unpinned_cores=0,
                 memory=1024 * MB,
                 hugepages_backed=False,
             ),
         ],
     )
Example #19
0
 def test_get_resources_interfaces(self):
     node = factory.make_Node()
     if0 = factory.make_Interface(
         INTERFACE_TYPE.PHYSICAL,
         name="eth0",
         numa_node=node.default_numanode,
         sriov_max_vf=8,
     )
     if1 = factory.make_Interface(
         INTERFACE_TYPE.PHYSICAL,
         name="eth1",
         numa_node=factory.make_NUMANode(node=node),
         sriov_max_vf=4,
     )
     project = factory.make_string()
     pod = factory.make_Pod(
         pod_type="lxd",
         parameters={"project": project},
         host=node,
     )
     vm0 = factory.make_VirtualMachine(bmc=pod, project=project)
     for _ in range(3):
         VirtualMachineInterface.objects.create(
             vm=vm0,
             host_interface=if0,
             attachment_type=InterfaceAttachType.SRIOV,
         )
     vm1 = factory.make_VirtualMachine(bmc=pod,
                                       project=factory.make_string())
     for _ in range(2):
         VirtualMachineInterface.objects.create(
             vm=vm1,
             host_interface=if0,
             attachment_type=InterfaceAttachType.SRIOV,
         )
     vm2 = factory.make_VirtualMachine(bmc=pod)
     for _ in range(2):
         VirtualMachineInterface.objects.create(
             vm=vm2,
             host_interface=if1,
             attachment_type=InterfaceAttachType.SRIOV,
         )
     resources = get_vm_host_resources(pod)
     self.assertCountEqual(
         resources.interfaces,
         [
             VMHostNetworkInterface(
                 id=if0.id,
                 name="eth0",
                 numa_index=0,
                 virtual_functions=VMHostResource(
                     allocated_tracked=3,
                     allocated_other=2,
                     free=3,
                 ),
             ),
             VMHostNetworkInterface(
                 id=if1.id,
                 name="eth1",
                 numa_index=1,
                 virtual_functions=VMHostResource(
                     allocated_tracked=0,
                     allocated_other=2,
                     free=2,
                 ),
             ),
         ],
     )
Example #20
0
 def test_GET_with_machine(self):
     machine = factory.make_Machine()
     vm = factory.make_VirtualMachine(machine=machine)
     response = self.client.get(get_vm_uri(vm.id))
     details = json_load_bytes(response.content)
     self.assertEqual(details["machine_id"], machine.id)
Example #21
0
    def test_get_host_interfaces_sriov(self):
        admin = factory.make_admin()
        handler = PodHandler(admin, {}, None)
        node = factory.make_Machine()
        numa_node0 = node.default_numanode
        numa_node1 = factory.make_NUMANode(node=node)
        iface1 = factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            name="eth0",
            numa_node=numa_node0,
            sriov_max_vf=8,
        )
        iface2 = factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            name="eth1",
            numa_node=numa_node1,
            sriov_max_vf=16,
        )
        pod = self.make_pod_with_hints(
            pod_type="lxd",
            host=node,
        )
        vm_machine0 = factory.make_Node(system_id="vm0")
        factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            node=vm_machine0,
            mac_address="11:11:11:11:11:11",
        )
        vm_machine1 = factory.make_Node(system_id="vm1")
        factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            node=vm_machine1,
            mac_address="aa:aa:aa:aa:aa:aa",
        )
        factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            node=vm_machine1,
            mac_address="bb:bb:bb:bb:bb:bb",
        )
        vm0 = factory.make_VirtualMachine(
            bmc=pod,
            machine=vm_machine0,
        )
        VirtualMachineInterface.objects.create(
            vm=vm0,
            mac_address="11:11:11:11:11:11",
            host_interface=iface1,
            attachment_type=InterfaceAttachType.SRIOV,
        )
        vm1 = factory.make_VirtualMachine(bmc=pod, machine=vm_machine1)
        VirtualMachineInterface.objects.create(
            vm=vm1,
            mac_address="aa:aa:aa:aa:aa:aa",
            host_interface=iface1,
            attachment_type=InterfaceAttachType.SRIOV,
        )
        VirtualMachineInterface.objects.create(
            vm=vm1,
            mac_address="bb:bb:bb:bb:bb:bb",
            host_interface=iface2,
            attachment_type=InterfaceAttachType.SRIOV,
        )

        result = handler.get({"id": pod.id})
        numa1, numa2 = result["resources"]["numa"]
        self.assertCountEqual(numa1["interfaces"], [iface1.id])
        self.assertCountEqual(numa2["interfaces"], [iface2.id])
        self.assertCountEqual(
            result["resources"]["interfaces"],
            [
                {
                    "id": iface1.id,
                    "name": "eth0",
                    "numa_index": 0,
                    "virtual_functions": {
                        "allocated_tracked": 2,
                        "allocated_other": 0,
                        "free": 6,
                    },
                },
                {
                    "id": iface2.id,
                    "name": "eth1",
                    "numa_index": 1,
                    "virtual_functions": {
                        "allocated_tracked": 1,
                        "allocated_other": 0,
                        "free": 15,
                    },
                },
            ],
        )
Example #22
0
    def test_get_with_pod_host(self):
        admin = factory.make_admin()
        handler = PodHandler(admin, {}, None)
        vlan = factory.make_VLAN(dhcp_on=True)
        subnet = factory.make_Subnet(vlan=vlan)
        node = factory.make_Node_with_Interface_on_Subnet(subnet=subnet)
        ip = factory.make_StaticIPAddress(interface=node.boot_interface,
                                          subnet=subnet)
        numa_node0 = node.default_numanode
        numa_node0.cores = [0, 3]
        numa_node0.memory = 4096
        numa_node0.save()
        factory.make_NUMANode(node=node, cores=[1, 4], memory=1024)
        factory.make_NUMANode(node=node, cores=[2, 5], memory=2048)
        project = factory.make_string()
        pod = self.make_pod_with_hints(
            pod_type="lxd",
            parameters={"project": project},
            host=node,
            ip_address=ip,
        )
        vm0 = factory.make_VirtualMachine(
            project=project,
            memory=1024,
            pinned_cores=[0],
            hugepages_backed=False,
            bmc=pod,
            machine=factory.make_Node(system_id="vm0"),
        )
        vm1 = factory.make_VirtualMachine(
            project=project,
            memory=1024,
            pinned_cores=[2, 5],
            hugepages_backed=False,
            bmc=pod,
            machine=factory.make_Node(system_id="vm1"),
        )

        expected_numa_details = [
            {
                "cores": {
                    "allocated": [0],
                    "free": [3]
                },
                "interfaces": [node.boot_interface.id],
                "memory": {
                    "general": {
                        "allocated": 1024 * MB,
                        "free": 3072 * MB
                    },
                    "hugepages": [],
                },
                "node_id": 0,
                "vms": [vm0.id],
            },
            {
                "cores": {
                    "allocated": [],
                    "free": [1, 4]
                },
                "interfaces": [],
                "memory": {
                    "general": {
                        "allocated": 0,
                        "free": 1024 * MB
                    },
                    "hugepages": [],
                },
                "node_id": 1,
                "vms": [],
            },
            {
                "cores": {
                    "allocated": [2, 5],
                    "free": []
                },
                "interfaces": [],
                "memory": {
                    "general": {
                        "allocated": 1024 * MB,
                        "free": 1024 * MB
                    },
                    "hugepages": [],
                },
                "node_id": 2,
                "vms": [vm1.id],
            },
        ]
        result = handler.get({"id": pod.id})
        self.assertThat(result["host"], Equals(node.system_id))
        self.assertThat(result["attached_vlans"], Equals([subnet.vlan_id]))
        self.assertThat(result["boot_vlans"], Equals([subnet.vlan_id]))
        resources = result["resources"]
        self.assertEqual(
            resources["cores"],
            {
                "allocated_other": 0,
                "allocated_tracked": 3,
                "free": 3
            },
        )
        self.assertEqual(
            resources["interfaces"],
            [{
                "id": node.boot_interface.id,
                "name": node.boot_interface.name,
                "numa_index": 0,
                "virtual_functions": {
                    "allocated_other": 0,
                    "allocated_tracked": 0,
                    "free": 0,
                },
            }],
        )
        self.assertEqual(
            resources["memory"],
            {
                "general": {
                    "allocated_other": 0,
                    "allocated_tracked": 2147483648,
                    "free": 5368709120,
                },
                "hugepages": {
                    "allocated_other": 0,
                    "allocated_tracked": 0,
                    "free": 0,
                },
            },
        )
        self.assertEqual(resources["numa"], expected_numa_details)
        self.assertEqual(resources["vm_count"], {"other": 0, "tracked": 2})
        self.assertCountEqual(
            resources["vms"],
            [
                {
                    "id": vm0.id,
                    "system_id": "vm0",
                    "memory": 1024 * MB,
                    "hugepages_backed": False,
                    "pinned_cores": [0],
                    "unpinned_cores": 0,
                },
                {
                    "id": vm1.id,
                    "system_id": "vm1",
                    "memory": 1024 * MB,
                    "hugepages_backed": False,
                    "pinned_cores": [2, 5],
                    "unpinned_cores": 0,
                },
            ],
        )
Example #23
0
    def test_get_with_pod_host(self):
        admin = factory.make_admin()
        handler = PodHandler(admin, {}, None)
        vlan = factory.make_VLAN(dhcp_on=True)
        subnet = factory.make_Subnet(vlan=vlan)
        node = factory.make_Node_with_Interface_on_Subnet(subnet=subnet)
        ip = factory.make_StaticIPAddress(interface=node.boot_interface,
                                          subnet=subnet)
        numa_node0 = node.default_numanode
        numa_node0.cores = [0, 3]
        numa_node0.memory = 4096
        numa_node0.save()
        factory.make_NUMANode(node=node, cores=[1, 4], memory=1024)
        factory.make_NUMANode(node=node, cores=[2, 5], memory=2048)
        pod = self.make_pod_with_hints(pod_type="lxd",
                                       host=node,
                                       ip_address=ip)
        factory.make_VirtualMachine(
            memory=1024,
            pinned_cores=[0],
            hugepages_backed=False,
            bmc=pod,
            machine=factory.make_Node(system_id="vm0"),
        )
        factory.make_VirtualMachine(
            memory=1024,
            pinned_cores=[2, 5],
            hugepages_backed=False,
            bmc=pod,
            machine=factory.make_Node(system_id="vm1"),
        )

        expected_data = handler.full_dehydrate(pod)
        result = handler.get({"id": pod.id})
        self.assertItemsEqual(expected_data.keys(), result.keys())
        for key in expected_data:
            self.assertEqual(expected_data[key], result[key], key)
        self.assertThat(result, Equals(expected_data))
        self.assertThat(result["host"], Equals(node.system_id))
        self.assertThat(result["attached_vlans"], Equals([subnet.vlan_id]))
        self.assertThat(result["boot_vlans"], Equals([subnet.vlan_id]))
        self.assertEqual(
            result["numa_pinning"],
            [
                {
                    "cores": {
                        "allocated": [0],
                        "free": [3]
                    },
                    "interfaces": [
                        {
                            "id": 100,
                            "name": "eth4",
                            "virtual_functions": {
                                "allocated": 4,
                                "free": 12
                            },
                        },
                        {
                            "id": 200,
                            "name": "eth5",
                            "virtual_functions": {
                                "allocated": 14,
                                "free": 2
                            },
                        },
                    ],
                    "memory": {
                        "general": {
                            "allocated": 1024 * MB,
                            "free": 3072 * MB
                        },
                        "hugepages": [],
                    },
                    "node_id":
                    0,
                    "vms": [
                        {
                            "pinned_cores": [0],
                            "system_id":
                            "vm0",
                            "networks": [
                                {
                                    "guest_nic_id": 101,
                                    "host_nic_id": 100
                                },
                                {
                                    "guest_nic_id": 102,
                                    "host_nic_id": 100
                                },
                            ],
                        },
                    ],
                },
                {
                    "cores": {
                        "allocated": [],
                        "free": [1, 4]
                    },
                    "interfaces": [
                        {
                            "id": 100,
                            "name": "eth4",
                            "virtual_functions": {
                                "allocated": 4,
                                "free": 12
                            },
                        },
                        {
                            "id": 200,
                            "name": "eth5",
                            "virtual_functions": {
                                "allocated": 14,
                                "free": 2
                            },
                        },
                    ],
                    "memory": {
                        "general": {
                            "allocated": 0,
                            "free": 1024 * MB
                        },
                        "hugepages": [],
                    },
                    "node_id":
                    1,
                    "vms": [],
                },
                {
                    "cores": {
                        "allocated": [2, 5],
                        "free": []
                    },
                    "interfaces": [
                        {
                            "id": 100,
                            "name": "eth4",
                            "virtual_functions": {
                                "allocated": 4,
                                "free": 12
                            },
                        },
                        {
                            "id": 200,
                            "name": "eth5",
                            "virtual_functions": {
                                "allocated": 14,
                                "free": 2
                            },
                        },
                    ],
                    "memory": {
                        "general": {
                            "allocated": 1024 * MB,
                            "free": 1024 * MB
                        },
                        "hugepages": [],
                    },
                    "node_id":
                    2,
                    "vms": [
                        {
                            "pinned_cores": [2, 5],
                            "system_id":
                            "vm1",
                            "networks": [
                                {
                                    "guest_nic_id": 101,
                                    "host_nic_id": 100
                                },
                                {
                                    "guest_nic_id": 102,
                                    "host_nic_id": 100
                                },
                            ],
                        },
                    ],
                },
            ],
        )
Example #24
0
    def test_get_host_interfaces_no_sriov(self):
        admin = factory.make_admin()
        handler = PodHandler(admin, {}, None)
        node = factory.make_Machine()
        numa_node0 = node.default_numanode
        numa_node1 = factory.make_NUMANode(node=node)
        iface1 = factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL, name="eth0", numa_node=numa_node0
        )
        br1 = factory.make_Interface(
            INTERFACE_TYPE.BRIDGE,
            name="br0",
            numa_node=numa_node0,
            parents=[iface1],
        )
        iface2 = factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL, name="eth1", numa_node=numa_node1
        )
        pod = self.make_pod_with_hints(
            pod_type="lxd",
            host=node,
        )
        vm_machine0 = factory.make_Node(system_id="vm0")
        factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            node=vm_machine0,
            mac_address="11:11:11:11:11:11",
        )
        vm_machine1 = factory.make_Node(system_id="vm1")
        factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            node=vm_machine1,
            mac_address="aa:aa:aa:aa:aa:aa",
        )
        factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            node=vm_machine1,
            mac_address="bb:bb:bb:bb:bb:bb",
        )
        vm0 = factory.make_VirtualMachine(
            bmc=pod,
            machine=vm_machine0,
        )
        VirtualMachineInterface.objects.create(
            vm=vm0,
            mac_address="11:11:11:11:11:11",
            host_interface=br1,
            attachment_type=InterfaceAttachType.BRIDGE,
        )
        vm1 = factory.make_VirtualMachine(bmc=pod, machine=vm_machine1)
        VirtualMachineInterface.objects.create(
            vm=vm1,
            mac_address="aa:aa:aa:aa:aa:aa",
            host_interface=br1,
            attachment_type=InterfaceAttachType.BRIDGE,
        )
        VirtualMachineInterface.objects.create(
            vm=vm1,
            mac_address="bb:bb:bb:bb:bb:bb",
            host_interface=iface2,
            attachment_type=InterfaceAttachType.MACVLAN,
        )

        result = handler.get({"id": pod.id})
        numa1, numa2 = result["numa_pinning"]
        self.assertEqual(
            [
                {
                    "id": iface1.id,
                    "name": "eth0",
                    "virtual_functions": {"allocated": 0, "free": 0},
                },
                {
                    "id": br1.id,
                    "name": "br0",
                    "virtual_functions": {"allocated": 0, "free": 0},
                },
            ],
            numa1["interfaces"],
        )
        self.assertEqual(
            [
                [
                    {
                        "guest_nic_id": None,
                        "host_nic_id": br1.id,
                    },
                ],
                [
                    {
                        "guest_nic_id": None,
                        "host_nic_id": br1.id,
                    },
                ],
            ],
            [vm["networks"] for vm in numa1["vms"]],
        )
        self.assertEqual(
            [
                {
                    "id": iface2.id,
                    "name": "eth1",
                    "virtual_functions": {"allocated": 0, "free": 0},
                }
            ],
            numa2["interfaces"],
        )
        self.assertEqual(
            [
                [
                    {
                        "guest_nic_id": None,
                        "host_nic_id": iface2.id,
                    },
                ],
            ],
            [vm["networks"] for vm in numa2["vms"]],
        )
 def test_instantiate_unpinned_cores(self):
     vm = factory.make_VirtualMachine(unpinned_cores=4)
     self.assertEqual(vm.unpinned_cores, 4)
 def test_instantiate_pinned_cores(self):
     vm = factory.make_VirtualMachine(pinned_cores=[1, 2, 3])
     self.assertEqual(vm.pinned_cores, [1, 2, 3])
Example #27
0
def populate_main():
    """Populate the main data all in one transaction."""
    admin = factory.make_admin(username="******",
                               password="******",
                               completed_intro=False)  # noqa
    user1, _ = factory.make_user_with_keys(username="******",
                                           password="******",
                                           completed_intro=False)
    user2, _ = factory.make_user_with_keys(username="******",
                                           password="******",
                                           completed_intro=False)

    # Physical zones.
    zones = [
        factory.make_Zone(name="zone-north"),
        factory.make_Zone(name="zone-south"),
    ]

    # DNS domains.
    domains = [
        Domain.objects.get_default_domain(),
        factory.make_Domain("sample"),
        factory.make_Domain("ubnt"),
    ]

    # Create the fabrics that will be used by the regions, racks,
    # machines, and devices.
    fabric0 = Fabric.objects.get_default_fabric()
    fabric0_untagged = fabric0.get_default_vlan()
    fabric0_vlan10 = factory.make_VLAN(fabric=fabric0, vid=10)
    fabric1 = factory.make_Fabric()
    fabric1_untagged = fabric1.get_default_vlan()
    fabric1_vlan42 = factory.make_VLAN(fabric=fabric1, vid=42)
    empty_fabric = factory.make_Fabric()  # noqa

    # Create some spaces.
    space_mgmt = factory.make_Space("management")
    space_storage = factory.make_Space("storage")
    space_internal = factory.make_Space("internal")
    space_ipv6_testbed = factory.make_Space("ipv6-testbed")

    # Subnets used by regions, racks, machines, and devices.
    subnet_1 = factory.make_Subnet(
        cidr="172.16.1.0/24",
        gateway_ip="172.16.1.1",
        vlan=fabric0_untagged,
        space=space_mgmt,
    )
    subnet_2 = factory.make_Subnet(
        cidr="172.16.2.0/24",
        gateway_ip="172.16.2.1",
        vlan=fabric1_untagged,
        space=space_mgmt,
    )
    subnet_3 = factory.make_Subnet(
        cidr="172.16.3.0/24",
        gateway_ip="172.16.3.1",
        vlan=fabric0_vlan10,
        space=space_storage,
    )
    subnet_4 = factory.make_Subnet(  # noqa
        cidr="172.16.4.0/24",
        gateway_ip="172.16.4.1",
        vlan=fabric0_vlan10,
        space=space_internal,
    )
    subnet_2001_db8_42 = factory.make_Subnet(  # noqa
        cidr="2001:db8:42::/64",
        gateway_ip="",
        vlan=fabric1_vlan42,
        space=space_ipv6_testbed,
    )
    ipv4_subnets = [subnet_1, subnet_2, subnet_3, subnet_4]

    # Static routes on subnets.
    factory.make_StaticRoute(source=subnet_1, destination=subnet_2)
    factory.make_StaticRoute(source=subnet_1, destination=subnet_3)
    factory.make_StaticRoute(source=subnet_1, destination=subnet_4)
    factory.make_StaticRoute(source=subnet_2, destination=subnet_1)
    factory.make_StaticRoute(source=subnet_2, destination=subnet_3)
    factory.make_StaticRoute(source=subnet_2, destination=subnet_4)
    factory.make_StaticRoute(source=subnet_3, destination=subnet_1)
    factory.make_StaticRoute(source=subnet_3, destination=subnet_2)
    factory.make_StaticRoute(source=subnet_3, destination=subnet_4)
    factory.make_StaticRoute(source=subnet_4, destination=subnet_1)
    factory.make_StaticRoute(source=subnet_4, destination=subnet_2)
    factory.make_StaticRoute(source=subnet_4, destination=subnet_3)

    # Load builtin scripts in the database so we can generate fake results
    # below.
    load_builtin_scripts()

    hostname = gethostname()
    region_rack = get_one(
        Node.objects.filter(node_type=NODE_TYPE.REGION_AND_RACK_CONTROLLER,
                            hostname=hostname))
    # If "make run" executes before "make sampledata", the rack may have
    # already registered.
    if region_rack is None:
        region_rack = factory.make_Node(
            node_type=NODE_TYPE.REGION_AND_RACK_CONTROLLER,
            hostname=hostname,
            interface=False,
        )

        # Get list of mac addresses that should be used for the region
        # rack controller. This will make sure the RegionAdvertisingService
        # picks the correct region on first start-up and doesn't get multiple.
        mac_addresses = get_mac_addresses()

        def get_next_mac():
            try:
                return mac_addresses.pop()
            except IndexError:
                return factory.make_mac_address()

        # Region and rack controller (hostname of dev machine)
        #   eth0     - fabric 0 - untagged
        #   eth1     - fabric 0 - untagged
        #   eth2     - fabric 1 - untagged - 172.16.2.2/24 - static
        #   bond0    - fabric 0 - untagged - 172.16.1.2/24 - static
        #   bond0.10 - fabric 0 - 10       - 172.16.3.2/24 - static
        eth0 = factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            name="eth0",
            node=region_rack,
            vlan=fabric0_untagged,
            mac_address=get_next_mac(),
        )
        eth1 = factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            name="eth1",
            node=region_rack,
            vlan=fabric0_untagged,
            mac_address=get_next_mac(),
        )
        eth2 = factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            name="eth2",
            node=region_rack,
            vlan=fabric1_untagged,
            mac_address=get_next_mac(),
        )
        bond0 = factory.make_Interface(
            INTERFACE_TYPE.BOND,
            name="bond0",
            node=region_rack,
            vlan=fabric0_untagged,
            parents=[eth0, eth1],
            mac_address=eth0.mac_address,
        )
        bond0_10 = factory.make_Interface(
            INTERFACE_TYPE.VLAN,
            node=region_rack,
            vlan=fabric0_vlan10,
            parents=[bond0],
        )
        factory.make_StaticIPAddress(
            alloc_type=IPADDRESS_TYPE.STICKY,
            ip="172.16.1.2",
            subnet=subnet_1,
            interface=bond0,
        )
        factory.make_StaticIPAddress(
            alloc_type=IPADDRESS_TYPE.STICKY,
            ip="172.16.2.2",
            subnet=subnet_2,
            interface=eth2,
        )
        factory.make_StaticIPAddress(
            alloc_type=IPADDRESS_TYPE.STICKY,
            ip="172.16.3.2",
            subnet=subnet_3,
            interface=bond0_10,
        )
        fabric0_untagged.primary_rack = region_rack
        fabric0_untagged.save()
        fabric1_untagged.primary_rack = region_rack
        fabric1_untagged.save()
        fabric0_vlan10.primary_rack = region_rack
        fabric0_vlan10.save()

    # Rack controller (happy-rack)
    #   eth0     - fabric 0 - untagged
    #   eth1     - fabric 0 - untagged
    #   eth2     - fabric 1 - untagged - 172.16.2.3/24 - static
    #   bond0    - fabric 0 - untagged - 172.16.1.3/24 - static
    #   bond0.10 - fabric 0 - 10       - 172.16.3.3/24 - static
    rack = factory.make_Node(
        node_type=NODE_TYPE.RACK_CONTROLLER,
        hostname="happy-rack",
        interface=False,
    )
    eth0 = factory.make_Interface(INTERFACE_TYPE.PHYSICAL,
                                  name="eth0",
                                  node=rack,
                                  vlan=fabric0_untagged)
    eth1 = factory.make_Interface(INTERFACE_TYPE.PHYSICAL,
                                  name="eth1",
                                  node=rack,
                                  vlan=fabric0_untagged)
    eth2 = factory.make_Interface(INTERFACE_TYPE.PHYSICAL,
                                  name="eth2",
                                  node=rack,
                                  vlan=fabric1_untagged)
    bond0 = factory.make_Interface(
        INTERFACE_TYPE.BOND,
        name="bond0",
        node=rack,
        vlan=fabric0_untagged,
        parents=[eth0, eth1],
    )
    bond0_10 = factory.make_Interface(INTERFACE_TYPE.VLAN,
                                      node=rack,
                                      vlan=fabric0_vlan10,
                                      parents=[bond0])
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.1.3",
        subnet=subnet_1,
        interface=bond0,
    )
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.2.3",
        subnet=subnet_2,
        interface=eth2,
    )
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.3.3",
        subnet=subnet_3,
        interface=bond0_10,
    )
    fabric0_untagged.secondary_rack = rack
    fabric0_untagged.save()
    fabric1_untagged.secondary_rack = rack
    fabric1_untagged.save()
    fabric0_vlan10.secondary_rack = rack
    fabric0_vlan10.save()

    # Region controller (happy-region)
    #   eth0     - fabric 0 - untagged
    #   eth1     - fabric 0 - untagged
    #   eth2     - fabric 1 - untagged - 172.16.2.4/24 - static
    #   bond0    - fabric 0 - untagged - 172.16.1.4/24 - static
    #   bond0.10 - fabric 0 - 10       - 172.16.3.4/24 - static
    region = factory.make_Node(
        node_type=NODE_TYPE.REGION_CONTROLLER,
        hostname="happy-region",
        interface=False,
    )
    eth0 = factory.make_Interface(
        INTERFACE_TYPE.PHYSICAL,
        name="eth0",
        node=region,
        vlan=fabric0_untagged,
    )
    eth1 = factory.make_Interface(
        INTERFACE_TYPE.PHYSICAL,
        name="eth1",
        node=region,
        vlan=fabric0_untagged,
    )
    eth2 = factory.make_Interface(
        INTERFACE_TYPE.PHYSICAL,
        name="eth2",
        node=region,
        vlan=fabric1_untagged,
    )
    bond0 = factory.make_Interface(
        INTERFACE_TYPE.BOND,
        name="bond0",
        node=region,
        vlan=fabric0_untagged,
        parents=[eth0, eth1],
    )
    bond0_10 = factory.make_Interface(INTERFACE_TYPE.VLAN,
                                      node=region,
                                      vlan=fabric0_vlan10,
                                      parents=[bond0])
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.1.4",
        subnet=subnet_1,
        interface=bond0,
    )
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.2.4",
        subnet=subnet_2,
        interface=eth2,
    )
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.3.4",
        subnet=subnet_3,
        interface=bond0_10,
    )

    # Create one machine for every status. Each machine has a random interface
    # and storage configration.
    node_statuses = [
        status for status in map_enum(NODE_STATUS).items() if status not in
        [NODE_STATUS.MISSING, NODE_STATUS.RESERVED, NODE_STATUS.RETIRED]
    ]
    machines = []
    test_scripts = [
        script.name
        for script in Script.objects.filter(script_type=SCRIPT_TYPE.TESTING)
    ]
    for _, status in node_statuses:
        owner = None
        if status in ALLOCATED_NODE_STATUSES:
            owner = random.choice([admin, user1, user2])
        elif status in [
                NODE_STATUS.COMMISSIONING,
                NODE_STATUS.FAILED_RELEASING,
        ]:
            owner = admin

        machine = factory.make_Node(
            status=status,
            owner=owner,
            zone=random.choice(zones),
            interface=False,
            with_boot_disk=False,
            power_type="manual",
            domain=random.choice(domains),
            memory=random.choice([1024, 4096, 8192]),
            description=random.choice([
                "",
                "Scheduled for removeal",
                "Firmware old",
                "Earmarked for Project Fuse in April",
            ]),
            cpu_count=random.randint(2, 8),
        )
        machine.set_random_hostname()
        machines.append(machine)

        # Create random network configuration.
        RandomInterfaceFactory.create_random(machine)

        # Add random storage devices and set a random layout.
        for _ in range(random.randint(1, 5)):
            factory.make_PhysicalBlockDevice(
                node=machine,
                size=random.randint(LARGE_BLOCK_DEVICE,
                                    LARGE_BLOCK_DEVICE * 10),
            )
        if status in [
                NODE_STATUS.READY,
                NODE_STATUS.ALLOCATED,
                NODE_STATUS.DEPLOYING,
                NODE_STATUS.DEPLOYED,
                NODE_STATUS.FAILED_DEPLOYMENT,
                NODE_STATUS.RELEASING,
                NODE_STATUS.FAILED_RELEASING,
        ]:
            machine.set_storage_layout(
                random.choice([
                    layout for layout in STORAGE_LAYOUTS.keys()
                    if layout != "vmfs6"
                ]))
            if status != NODE_STATUS.READY:
                machine._create_acquired_filesystems()

        # Add a random amount of events.
        for _ in range(random.randint(25, 100)):
            factory.make_Event(node=machine)

        # Add in commissioning and testing results.
        if status != NODE_STATUS.NEW:
            for _ in range(0, random.randint(1, 10)):
                css = ScriptSet.objects.create_commissioning_script_set(
                    machine)
                scripts = set()
                for __ in range(1, len(test_scripts)):
                    scripts.add(random.choice(test_scripts))
                tss = ScriptSet.objects.create_testing_script_set(
                    machine, list(scripts))
            machine.current_commissioning_script_set = css
            machine.current_testing_script_set = tss
            machine.save()

        # Fill in historic results
        for script_set in machine.scriptset_set.all():
            if script_set in [css, tss]:
                continue
            for script_result in script_set:
                # Can't use script_result.store_result as it will try to
                # process the result and fail on the fake data.
                script_result.exit_status = random.randint(0, 255)
                if script_result.exit_status == 0:
                    script_result.status = SCRIPT_STATUS.PASSED
                else:
                    script_result.status = random.choice(
                        list(SCRIPT_STATUS_FAILED))
                script_result.started = factory.make_date()
                script_result.ended = script_result.started + timedelta(
                    seconds=random.randint(0, 10000))
                script_result.stdout = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.stderr = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.output = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.save()

        # Only add in results in states where commissiong should be completed.
        if status not in [NODE_STATUS.NEW, NODE_STATUS.COMMISSIONING]:
            if status == NODE_STATUS.FAILED_COMMISSIONING:
                exit_status = random.randint(1, 255)
                script_status = random.choice(list(SCRIPT_STATUS_FAILED))
            else:
                exit_status = 0
                script_status = SCRIPT_STATUS.PASSED
            for script_result in css:
                # Can't use script_result.store_result as it will try to
                # process the result and fail on the fake data.
                script_result.status = script_status
                script_result.exit_status = exit_status
                script_result.started = factory.make_date()
                script_result.ended = script_result.started + timedelta(
                    seconds=random.randint(0, 10000))
                script_result.stdout = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.stderr = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.output = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.save()
        elif status == NODE_STATUS.COMMISSIONING:
            for script_result in css:
                script_result.status = random.choice(
                    list(SCRIPT_STATUS_RUNNING_OR_PENDING))
                if script_result.status != SCRIPT_STATUS.PENDING:
                    script_result.started = factory.make_date()
                script_result.save()

        # Only add in results in states where testing should be completed.
        if status not in [NODE_STATUS.NEW, NODE_STATUS.TESTING]:
            if status == NODE_STATUS.FAILED_TESTING:
                exit_status = random.randint(1, 255)
                script_status = random.choice(list(SCRIPT_STATUS_FAILED))
            else:
                exit_status = 0
                script_status = SCRIPT_STATUS.PASSED
            for script_result in tss:
                # Can't use script_result.store_result as it will try to
                # process the result and fail on the fake data.
                script_result.status = script_status
                script_result.exit_status = exit_status
                script_result.started = factory.make_date()
                script_result.ended = script_result.started + timedelta(
                    seconds=random.randint(0, 10000))
                script_result.stdout = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.stderr = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.output = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.save()
        elif status == NODE_STATUS.TESTING:
            for script_result in tss:
                script_result.status = random.choice(
                    list(SCRIPT_STATUS_RUNNING_OR_PENDING))
                if script_result.status != SCRIPT_STATUS.PENDING:
                    script_result.started = factory.make_date()
                script_result.save()

        # Add installation results.
        if status in [
                NODE_STATUS.DEPLOYING,
                NODE_STATUS.DEPLOYED,
                NODE_STATUS.FAILED_DEPLOYMENT,
        ]:
            script_set = ScriptSet.objects.create_installation_script_set(
                machine)
            machine.current_installation_script_set = script_set
            machine.save()

        if status == NODE_STATUS.DEPLOYED:
            for script_result in machine.current_installation_script_set:
                stdout = factory.make_string().encode("utf-8")
                script_result.store_result(0, stdout)
        elif status == NODE_STATUS.FAILED_DEPLOYMENT:
            for script_result in machine.current_installation_script_set:
                exit_status = random.randint(1, 255)
                stdout = factory.make_string().encode("utf-8")
                stderr = factory.make_string().encode("utf-8")
                script_result.store_result(exit_status, stdout, stderr)

        # Add children devices to the deployed machine.
        if status == NODE_STATUS.DEPLOYED:
            boot_interface = machine.get_boot_interface()
            for _ in range(5):
                device = factory.make_Device(
                    interface=True,
                    domain=machine.domain,
                    parent=machine,
                    vlan=boot_interface.vlan,
                )
                device.set_random_hostname()
                RandomInterfaceFactory.assign_ip(
                    device.get_boot_interface(),
                    alloc_type=IPADDRESS_TYPE.STICKY,
                )

    # Create a few pods to and assign a random set of the machines to the pods.
    pods = [None]
    pod_storage_pools = defaultdict(list)
    machines_in_pods = defaultdict(list)
    for _ in range(3):
        subnet = random.choice(ipv4_subnets)
        ip = factory.pick_ip_in_Subnet(subnet)
        ip_address = factory.make_StaticIPAddress(
            alloc_type=IPADDRESS_TYPE.STICKY, ip=ip, subnet=subnet)
        power_address = "qemu+ssh://ubuntu@%s/system" % ip
        pod = factory.make_Pod(
            pod_type="virsh",
            parameters={"power_address": power_address},
            ip_address=ip_address,
            capabilities=[
                Capabilities.DYNAMIC_LOCAL_STORAGE,
                Capabilities.COMPOSABLE,
            ],
        )
        for _ in range(3):
            pool = factory.make_PodStoragePool(pod)
            pod_storage_pools[pod].append(pool)
        pod.default_storage_pool = pool
        pod.save()
        pods.append(pod)
    for machine in machines:
        # Add the machine to the pod if its lucky day!
        pod = random.choice(pods)
        if pod is not None:
            machine.bmc = pod
            machine.instance_power_parameters = {"power_id": machine.hostname}
            machine.save()
            machines_in_pods[pod].append(machine)

            vm = factory.make_VirtualMachine(
                identifier=machine.hostname,
                bmc=pod,
                machine=machine,
                unpinned_cores=machine.cpu_count,
            )

            # Assign the block devices on the machine to a storage pool.
            for block_device in machine.physicalblockdevice_set.all():
                factory.make_VirtualMachineDisk(
                    vm=vm,
                    name=block_device.name,
                    size=block_device.size,
                    backing_pool=random.choice(pod_storage_pools[pod]),
                )

    # Update the pod attributes so that it has more available then used.
    for pod in pods[1:]:
        used_resources = get_vm_host_used_resources(pod)
        pod.cores = used_resources.cores + random.randint(4, 8)
        pod.memory = used_resources.total_memory + random.choice(
            [1024, 2048, 4096, 4096 * 4, 4096 * 8])
        pod.local_storage = sum(pool.storage
                                for pool in pod_storage_pools[pod])
        pod.save()

    # Create a few devices.
    for _ in range(10):
        device = factory.make_Device(interface=True)
        device.set_random_hostname()

    # Add some DHCP snippets.
    # - Global
    factory.make_DHCPSnippet(
        name="foo class",
        description="adds class for vender 'foo'",
        value=VersionedTextFile.objects.create(data=dedent("""\
            class "foo" {
                match if substring (
                    option vendor-class-identifier, 0, 3) = "foo";
            }
        """)),
    )
    factory.make_DHCPSnippet(
        name="bar class",
        description="adds class for vender 'bar'",
        value=VersionedTextFile.objects.create(data=dedent("""\
            class "bar" {
                match if substring (
                    option vendor-class-identifier, 0, 3) = "bar";
            }
        """)),
        enabled=False,
    )
    # - Subnet
    factory.make_DHCPSnippet(
        name="600 lease time",
        description="changes lease time to 600 secs.",
        value=VersionedTextFile.objects.create(data="default-lease-time 600;"),
        subnet=subnet_1,
    )
    factory.make_DHCPSnippet(
        name="7200 max lease time",
        description="changes max lease time to 7200 secs.",
        value=VersionedTextFile.objects.create(data="max-lease-time 7200;"),
        subnet=subnet_2,
        enabled=False,
    )
    # - Node
    factory.make_DHCPSnippet(
        name="boot from other server",
        description="instructs device to boot from other server",
        value=VersionedTextFile.objects.create(data=dedent("""\
            filename "test-boot";
            server-name "boot.from.me";
        """)),
        node=device,
    )

    # Add notifications for admins, users, and each individual user, and for
    # each notification category.
    factory.make_Notification(
        "Attention admins! Core critical! Meltdown imminent! Evacuate "
        "habitat immediately!",
        admins=True,
        category="error",
    )
    factory.make_Notification(
        "Dear users, rumours of a core meltdown are unfounded. Please "
        "return to your home-pods and places of business.",
        users=True,
        category="warning",
    )
    factory.make_Notification(
        "FREE! For the next 2 hours get FREE blueberry and iodine pellets "
        "at the nutri-dispensers.",
        users=True,
        category="success",
    )
    for user in User.objects.all():
        context = {"name": user.username.capitalize()}
        factory.make_Notification(
            "Greetings, {name}! Get away from the habitat for the weekend and "
            "visit the Mare Nubium with MAAS Tours. Use the code METAL to "
            "claim a special gift!",
            user=user,
            context=context,
            category="info",
        )
Example #28
0
 def test_get_resources_global_resources(self):
     node = factory.make_Node()
     numa_node0 = node.default_numanode
     numa_node0.cores = [0, 1]
     numa_node0.memory = 4096
     numa_node0.save()
     factory.make_NUMANode(node=node, cores=[2, 3], memory=2048)
     factory.make_NUMANode(node=node, cores=[4, 5], memory=2048)
     factory.make_NUMANode(node=node, cores=[6, 7], memory=2048)
     factory.make_NUMANodeHugepages(numa_node=numa_node0,
                                    page_size=1024 * MB,
                                    total=2048 * MB)
     project = factory.make_string()
     pod = factory.make_Pod(pod_type="lxd",
                            parameters={"project": project},
                            host=node)
     pool1 = factory.make_PodStoragePool(pod=pod)
     pool2 = factory.make_PodStoragePool(pod=pod)
     vm1 = factory.make_VirtualMachine(
         memory=1024,
         pinned_cores=[0, 1],
         hugepages_backed=False,
         bmc=pod,
         project=project,
     )
     disk1 = factory.make_VirtualMachineDisk(vm=vm1, backing_pool=pool1)
     vm2 = factory.make_VirtualMachine(
         memory=1024,
         pinned_cores=[2],
         hugepages_backed=False,
         bmc=pod,
     )
     disk2 = factory.make_VirtualMachineDisk(vm=vm2, backing_pool=pool2)
     factory.make_VirtualMachine(
         memory=1024,
         unpinned_cores=2,
         hugepages_backed=True,
         project=project,
         bmc=pod,
     )
     factory.make_VirtualMachine(memory=2048,
                                 unpinned_cores=1,
                                 hugepages_backed=False,
                                 bmc=pod)
     resources = get_vm_host_resources(pod)
     self.assertEqual(resources.vm_count.tracked, 2)
     self.assertEqual(resources.vm_count.other, 2)
     self.assertEqual(resources.cores.free, 2)
     self.assertEqual(resources.cores.allocated, 6)
     self.assertEqual(resources.cores.allocated_tracked, 4)
     self.assertEqual(resources.cores.allocated_other, 2)
     self.assertEqual(resources.memory.general.free, 6144 * MB)
     self.assertEqual(resources.memory.general.allocated, 4096 * MB)
     self.assertEqual(resources.memory.general.allocated_tracked, 1024 * MB)
     self.assertEqual(resources.memory.general.allocated_other, 3072 * MB)
     self.assertEqual(resources.memory.hugepages.free, 1024 * MB)
     self.assertEqual(resources.memory.hugepages.allocated, 1024 * MB)
     self.assertEqual(resources.memory.hugepages.allocated_tracked,
                      1024 * MB)
     self.assertEqual(resources.memory.hugepages.allocated_other, 0)
     self.assertEqual(resources.storage.allocated_tracked, disk1.size)
     self.assertEqual(resources.storage.allocated_other, disk2.size)
     self.assertEqual(
         resources.storage.free,
         pool1.storage + pool2.storage - disk1.size - disk2.size,
     )
Example #29
0
 def test_get_resources_numa_unaligned_hugepages(self):
     node = factory.make_Node()
     numa_node0 = node.default_numanode
     numa_node0.cores = [0, 1]
     numa_node0.memory = 4096
     numa_node0.save()
     numa_node1 = factory.make_NUMANode(node=node,
                                        cores=[2, 3],
                                        memory=4096)
     factory.make_NUMANodeHugepages(numa_node=numa_node0,
                                    page_size=1024 * MB,
                                    total=1024 * MB)
     factory.make_NUMANodeHugepages(numa_node=numa_node1,
                                    page_size=1024 * MB,
                                    total=4096 * MB)
     pod = factory.make_Pod(pod_type="lxd")
     pod.hints.nodes.add(node)
     vm = factory.make_VirtualMachine(
         memory=2048,
         pinned_cores=[0, 2],
         hugepages_backed=True,
         bmc=pod,
     )
     resources = get_vm_host_resources(pod)
     self.assertEqual(
         [asdict(r) for r in resources.numa],
         [
             {
                 "cores": {
                     "allocated": [0],
                     "free": [1]
                 },
                 "memory": {
                     "general": {
                         "allocated": 0,
                         "free": 3072 * MB
                     },
                     "hugepages": [{
                         "allocated": 1024 * MB,
                         "free": 0,
                         "page_size": 1024 * MB,
                     }],
                 },
                 "interfaces": [],
                 "node_id": 0,
                 "vms": [vm.id],
             },
             {
                 "cores": {
                     "allocated": [2],
                     "free": [3]
                 },
                 "memory": {
                     "general": {
                         "allocated": 0,
                         "free": 0
                     },
                     "hugepages": [{
                         "allocated": 1024 * MB,
                         "free": 3072 * MB,
                         "page_size": 1024 * MB,
                     }],
                 },
                 "interfaces": [],
                 "node_id": 1,
                 "vms": [vm.id],
             },
         ],
     )