Exemple #1
0
    def get_requested_machine(self):
        """Return the `RequestedMachine`."""
        block_devices = []

        storage_constraints = get_storage_constraints_from_string(
            self.get_value_for('storage'))
        for _, size, tags in storage_constraints:
            if tags is None:
                tags = []
            block_devices.append(
                RequestedMachineBlockDevice(size=size, tags=tags))
        interfaces_label_map = self.get_value_for('interfaces')
        if interfaces_label_map is not None:
            requested_machine_interfaces = (
                self._get_requested_machine_interfaces_via_constraints(
                    interfaces_label_map))
        else:
            requested_machine_interfaces = [RequestedMachineInterface()]

        return RequestedMachine(
            hostname=self.get_value_for('hostname'),
            architecture=self.get_value_for('architecture'),
            cores=self.get_value_for('cores'),
            memory=self.get_value_for('memory'),
            cpu_speed=self.get_value_for('cpu_speed'),
            block_devices=block_devices,
            interfaces=requested_machine_interfaces)
Exemple #2
0
 def test_machine(self):
     hostname = factory.make_name("hostname")
     cores = random.randint(1, 8)
     cpu_speed = random.randint(1000, 2000)
     memory = random.randint(4096, 8192)
     interfaces = [RequestedMachineInterface() for _ in range(3)]
     block_devices = [
         RequestedMachineBlockDevice(
             size=random.randint(512, 1024),
             tags=[factory.make_name("tag") for _ in range(3)],
         ) for _ in range(3)
     ]
     machine = RequestedMachine(
         hostname=hostname,
         architecture="amd64/generic",
         cores=cores,
         cpu_speed=cpu_speed,
         memory=memory,
         interfaces=interfaces,
         block_devices=block_devices,
     )
     self.assertEquals(hostname, machine.hostname)
     self.assertEquals(cores, machine.cores)
     self.assertEquals(cpu_speed, machine.cpu_speed)
     self.assertEquals(memory, machine.memory)
     self.assertEquals(interfaces, machine.interfaces)
     self.assertEquals(block_devices, machine.block_devices)
Exemple #3
0
    def get_requested_machine(self, known_host_interfaces):
        """Return the `RequestedMachine`."""
        block_devices = []

        storage_constraints = get_storage_constraints_from_string(
            self.get_value_for("storage"))
        for _, size, tags in storage_constraints:
            if tags is None:
                tags = []
            block_devices.append(
                RequestedMachineBlockDevice(size=size, tags=tags))
        interfaces_label_map = self.get_value_for("interfaces")
        if interfaces_label_map is not None:
            requested_machine_interfaces = self._get_requested_machine_interfaces_via_constraints(
                interfaces_label_map)
        else:
            requested_machine_interfaces = [RequestedMachineInterface()]

        return RequestedMachine(
            hostname=self.get_value_for("hostname"),
            architecture=self.get_value_for("architecture"),
            cores=self.get_value_for("cores"),
            memory=self.get_value_for("memory"),
            cpu_speed=self.get_value_for("cpu_speed"),
            block_devices=block_devices,
            interfaces=requested_machine_interfaces,
            known_host_interfaces=known_host_interfaces,
        )
 def test_get_usable_storage_pool_filters_on_default_pool_name_raises_invalid(
     self, ):
     driver = lxd_module.LXDPodDriver()
     pools = [
         Mock(
             **{
                 "resources.get.return_value":
                 Mock(space={
                     "total": 2**i * 2048,
                     "used": 2 * i * 1500
                 })
             }) for i in range(3)
     ]
     # Override name attribute on Mock and calculate the available
     for pool in pools:
         type(pool).name = PropertyMock(
             return_value=factory.make_name("pool_name"))
     disk = RequestedMachineBlockDevice(size=2048 + 1, tags=[])
     self.assertRaises(
         PodInvalidResources,
         driver.get_usable_storage_pool,
         disk,
         pools,
         pools[0].name,
     )
Exemple #5
0
    def get_requested_machine(self, known_host_interfaces):
        """Return the `RequestedMachine`."""
        block_devices = []

        storage_constraints = get_storage_constraints_from_string(
            self.get_value_for("storage"))
        # LXD Pods currently only support one block device.
        if self.pod.power_type == "lxd" and len(storage_constraints) > 1:
            raise PodProblem(
                "LXD Pod virtual machines currently only support one block device."
            )
        for _, size, tags in storage_constraints:
            if tags is None:
                tags = []
            block_devices.append(
                RequestedMachineBlockDevice(size=size, tags=tags))
        interfaces_label_map = self.get_value_for("interfaces")
        if interfaces_label_map is not None:
            requested_machine_interfaces = self._get_requested_machine_interfaces_via_constraints(
                interfaces_label_map)
        else:
            requested_machine_interfaces = [RequestedMachineInterface()]

        return RequestedMachine(
            hostname=self.get_value_for("hostname"),
            architecture=self.get_value_for("architecture"),
            cores=self.get_value_for("cores"),
            memory=self.get_value_for("memory"),
            cpu_speed=self.get_value_for("cpu_speed"),
            block_devices=block_devices,
            interfaces=requested_machine_interfaces,
            known_host_interfaces=known_host_interfaces,
        )
Exemple #6
0
 def test_block_device_size(self):
     size = random.randint(512, 512 * 1024)
     tags = [
         factory.make_name('tag')
         for _ in range(3)
     ]
     device = RequestedMachineBlockDevice(size=size, tags=tags)
     self.assertEquals(size, device.size)
     self.assertEquals(tags, device.tags)
Exemple #7
0
 def make_requested_machine(self):
     return RequestedMachine(
         hostname=factory.make_name('hostname'),
         architecture='amd64/generic',
         cores=random.randint(1, 8),
         cpu_speed=random.randint(1000, 3000),
         memory=random.randint(1024, 8192),
         block_devices=[
             RequestedMachineBlockDevice(size=random.randint(8, 16))
         ],
         interfaces=[RequestedMachineInterface()])
Exemple #8
0
 def test_machine_asdict(self):
     hostname = factory.make_name('hostname')
     cores = random.randint(1, 8)
     cpu_speed = random.randint(1000, 2000)
     memory = random.randint(4096, 8192)
     interfaces = [RequestedMachineInterface() for _ in range(3)]
     block_devices = [
         RequestedMachineBlockDevice(
             size=random.randint(512, 1024),
             tags=[factory.make_name('tag') for _ in range(3)])
         for _ in range(3)
     ]
     machine = RequestedMachine(hostname=hostname,
                                architecture='amd64/generic',
                                cores=cores,
                                cpu_speed=cpu_speed,
                                memory=memory,
                                interfaces=interfaces,
                                block_devices=block_devices)
     self.assertThat(
         machine.asdict(),
         MatchesDict({
             "hostname":
             Equals(hostname),
             "architecture":
             Equals("amd64/generic"),
             "cores":
             Equals(cores),
             "cpu_speed":
             Equals(cpu_speed),
             "memory":
             Equals(memory),
             "interfaces":
             MatchesListwise([
                 MatchesDict({
                     "attach_name":
                     Equals(interface.attach_name),
                     "attach_type":
                     Equals(interface.attach_type),
                     "attach_options":
                     Equals(interface.attach_options),
                 }) for interface in interfaces
             ]),
             "block_devices":
             MatchesListwise([
                 MatchesDict({
                     "size": Equals(block_device.size),
                     "tags": Equals(block_device.tags),
                 }) for block_device in block_devices
             ]),
         }))
def make_requested_machine():
    block_devices = [
        RequestedMachineBlockDevice(size=random.randint(1024**3, 4 * 1024**3))
    ]
    interfaces = [RequestedMachineInterface()]
    return RequestedMachine(
        hostname=factory.make_name("hostname"),
        architecture="amd64/generic",
        cores=random.randint(2, 4),
        memory=random.randint(1024, 4096),
        cpu_speed=random.randint(2000, 3000),
        block_devices=block_devices,
        interfaces=interfaces,
    )
Exemple #10
0
 def get_requested_machine(self):
     """Return the `RequestedMachine`."""
     # XXX blake_r 2017-04-04: Interfaces are hard coded at the
     # moment. Will be extended later.
     block_devices = []
     constraints = get_storage_constraints_from_string(
         self.get_value_for('storage'))
     for _, size, tags in constraints:
         if tags is None:
             tags = []
         block_devices.append(
             RequestedMachineBlockDevice(size=size, tags=tags))
     return RequestedMachine(
         hostname=self.get_value_for('hostname'),
         architecture=self.get_value_for('architecture'),
         cores=self.get_value_for('cores'),
         memory=self.get_value_for('memory'),
         cpu_speed=self.get_value_for('cpu_speed'),
         block_devices=block_devices,
         interfaces=[RequestedMachineInterface()])
Exemple #11
0
class TestRequestedMachine(MAASTestCase):

    example = RequestedMachine(
        hostname=factory.make_name("hostname"),
        architecture="amd64/generic",
        cores=random.randint(1, 8),
        cpu_speed=random.randint(1000, 2000),
        memory=random.randint(1024, 8192),
        interfaces=[RequestedMachineInterface() for _ in range(3)],
        block_devices=[
            RequestedMachineBlockDevice(size=random.randint(512, 1024))
            for _ in range(3)
        ],
    )

    def test_round_trip(self):
        argument = arguments.AmpRequestedMachine()
        encoded = argument.toString(self.example)
        self.assertThat(encoded, IsInstance(bytes))
        decoded = argument.fromString(encoded)
        self.assertThat(decoded, Equals(self.example))
Exemple #12
0
 def test_get_usable_storage_pool_filters_on_disk_tags(self):
     driver = lxd_module.LXDPodDriver()
     pools = [
         Mock(
             **{
                 "resources.get.return_value":
                 Mock(space={
                     "total": 2**i * 2048,
                     "used": 2 * i * 1500
                 })
             }) for i in range(3)
     ]
     # Override name attribute on Mock and calculate the available
     for pool in pools:
         type(pool).name = PropertyMock(
             return_value=factory.make_name("pool_name"))
     selected_pool = pools[1]
     disk = RequestedMachineBlockDevice(size=1024,
                                        tags=[selected_pool.name])
     self.assertEqual(pools[1].name,
                      driver.get_usable_storage_pool(disk, pools))
Exemple #13
0
 def test_get_usable_storage_pool(self):
     driver = lxd_module.LXDPodDriver()
     pools = [
         Mock(
             **{
                 "resources.get.return_value":
                 Mock(space={
                     "total": 2**i * 2048,
                     "used": 2 * i * 1500
                 })
             }) for i in range(3)
     ]
     # Override name attribute on Mock and calculate the available
     for pool in pools:
         type(pool).name = PropertyMock(
             return_value=factory.make_name("pool_name"))
     disk = RequestedMachineBlockDevice(
         size=2048,  # Only the first pool will have this availability.
         tags=[],
     )
     self.assertEqual(pools[0].name,
                      driver.get_usable_storage_pool(disk, pools))
Exemple #14
0
 def test_machine_without_cpu_speed(self):
     hostname = factory.make_name('hostname')
     cores = random.randint(1, 8)
     memory = random.randint(4096, 8192)
     interfaces = [RequestedMachineInterface() for _ in range(3)]
     block_devices = [
         RequestedMachineBlockDevice(size=random.randint(512, 1024))
         for _ in range(3)
     ]
     machine = RequestedMachine(hostname=hostname,
                                architecture='amd64/generic',
                                cores=cores,
                                cpu_speed=None,
                                memory=memory,
                                interfaces=interfaces,
                                block_devices=block_devices)
     self.assertEquals(hostname, machine.hostname)
     self.assertEquals(cores, machine.cores)
     self.assertIsNone(machine.cpu_speed)
     self.assertEquals(memory, machine.memory)
     self.assertEquals(interfaces, machine.interfaces)
     self.assertEquals(block_devices, machine.block_devices)
Exemple #15
0
 def test_machine_asdict(self):
     hostname = factory.make_name("hostname")
     cores = random.randint(1, 8)
     cpu_speed = random.randint(1000, 2000)
     memory = random.randint(4096, 8192)
     interfaces = [RequestedMachineInterface() for _ in range(3)]
     known_host_interfaces = [KnownHostInterface() for _ in range(3)]
     block_devices = [
         RequestedMachineBlockDevice(
             size=random.randint(512, 1024),
             tags=[factory.make_name("tag") for _ in range(3)],
         ) for _ in range(3)
     ]
     machine = RequestedMachine(
         hostname=hostname,
         architecture="amd64/generic",
         cores=cores,
         cpu_speed=cpu_speed,
         memory=memory,
         interfaces=interfaces,
         known_host_interfaces=known_host_interfaces,
         block_devices=block_devices,
     )
     self.assertThat(
         machine.asdict(),
         MatchesDict({
             "hostname":
             Equals(hostname),
             "architecture":
             Equals("amd64/generic"),
             "cores":
             Equals(cores),
             "cpu_speed":
             Equals(cpu_speed),
             "memory":
             Equals(memory),
             "interfaces":
             MatchesListwise([
                 MatchesDict({
                     "ifname":
                     Is(None),
                     "requested_ips":
                     Equals([]),
                     "ip_mode":
                     Is(None),
                     "attach_name":
                     Equals(interface.attach_name),
                     "attach_type":
                     Equals(interface.attach_type),
                     "attach_options":
                     Equals(interface.attach_options),
                 }) for interface in interfaces
             ]),
             "known_host_interfaces":
             MatchesListwise([
                 MatchesDict({
                     "ifname":
                     Equals(known_host_interface.ifname),
                     "attach_type":
                     Equals(known_host_interface.attach_type),
                     "dhcp_enabled":
                     Equals(False),
                 }) for known_host_interface in known_host_interfaces
             ]),
             "block_devices":
             MatchesListwise([
                 MatchesDict({
                     "size": Equals(block_device.size),
                     "tags": Equals(block_device.tags),
                 }) for block_device in block_devices
             ]),
         }),
     )