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 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)
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)
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, )
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 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()])
def test_machine_fromdict(self): hostname = factory.make_name("hostname") cores = random.randint(1, 8) cpu_speed = random.randint(1000, 2000) memory = random.randint(4096, 8192) interfaces = [dict() for _ in range(3)] block_devices = [ dict( size=random.randint(512, 1024), tags=[factory.make_name("tag") for _ in range(3)], ) for _ in range(3) ] pinned_cores = random.sample(range(10), 3) hugepages_backed = factory.pick_bool() machine_data = dict( hostname=hostname, architecture="amd64/generic", cores=cores, cpu_speed=cpu_speed, pinned_cores=pinned_cores, hugepages_backed=hugepages_backed, memory=memory, interfaces=interfaces, block_devices=block_devices, ) machine = RequestedMachine.fromdict(machine_data) self.assertThat(machine, IsInstance(RequestedMachine)) self.assertThat( machine, MatchesStructure( hostname=Equals(hostname), architecture=Equals("amd64/generic"), cores=Equals(cores), cpu_speed=Equals(cpu_speed), memory=Equals(memory), pinned_cores=Equals(pinned_cores), hugepages_backed=Equals(hugepages_backed), interfaces=MatchesListwise([ IsInstance(RequestedMachineInterface) for interface in interfaces ]), block_devices=MatchesListwise([ MatchesAll( IsInstance(RequestedMachineBlockDevice), MatchesStructure( 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, )
def test_machine_fromdict(self): hostname = factory.make_name('hostname') cores = random.randint(1, 8) cpu_speed = random.randint(1000, 2000) memory = random.randint(4096, 8192) interfaces = [ dict() for _ in range(3) ] block_devices = [ dict(size=random.randint(512, 1024), tags=[ factory.make_name('tag') for _ in range(3) ]) for _ in range(3) ] machine_data = dict( hostname=hostname, architecture='amd64/generic', cores=cores, cpu_speed=cpu_speed, memory=memory, interfaces=interfaces, block_devices=block_devices) machine = RequestedMachine.fromdict(machine_data) self.assertThat(machine, IsInstance(RequestedMachine)) self.assertThat(machine, MatchesStructure( hostname=Equals(hostname), architecture=Equals('amd64/generic'), cores=Equals(cores), cpu_speed=Equals(cpu_speed), memory=Equals(memory), interfaces=MatchesListwise([ IsInstance(RequestedMachineInterface) for interface in interfaces ]), block_devices=MatchesListwise([ MatchesAll( IsInstance(RequestedMachineBlockDevice), MatchesStructure( size=Equals(block_device['size']), tags=Equals(block_device['tags']), ), ) for block_device in block_devices ]), ))
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()])
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))
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)
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 ]), }), )
def fromString(self, inString): # Circular imports. from provisioningserver.drivers.pod import RequestedMachine data = super(AmpRequestedMachine, self).fromString(inString) return RequestedMachine.fromdict(data)