def starter_device(data: dict, user: str) -> dict: """ Creates a device for starters :param data: The given data :param user: The user uuid. :return: the response """ count: int = wrapper.session.query(func.count( Device.uuid)).filter_by(owner=user).scalar() if count > 0: return already_own_a_device performance: tuple = calculate_power(hardware["start_pc"]) device: Device = Device.create_starter_device(user, True) Workload.create(device.uuid, performance) create_hardware(hardware["start_pc"], device.uuid) m.contact_microservice("service", ["device_init"], { "device_uuid": device.uuid, "user": device.owner }) return device.serialize
def test__model__workload__serialize(self): workload = Workload( uuid="device-uuid", performance_cpu=2.1, performance_ram=2.2, performance_gpu=2.3, performance_disk=2.4, performance_network=2.5, usage_cpu=1.1, usage_ram=1.2, usage_gpu=1.3, usage_disk=1.4, usage_network=1.5, ) expected_result = { "uuid": "device-uuid", "performance_cpu": 2.1, "performance_ram": 2.2, "performance_gpu": 2.3, "performance_disk": 2.4, "performance_network": 2.5, "usage_cpu": 1.1, "usage_ram": 1.2, "usage_gpu": 1.3, "usage_disk": 1.4, "usage_network": 1.5, } serialized = workload.serialize self.assertEqual(expected_result, serialized) serialized["usage_cpu"] = 13.37 self.assertEqual(expected_result, workload.serialize)
def setUpClass(cls): """Makes default values""" cls._mount_points = [MountPoint(name='C:\\', size=42)] cls._credentials = Credentials( user_name='User', password='******', domain='xxx.com' ) cls._test_workload = Workload( id=None, ip='111.11.11', credentials=cls._credentials, storage=cls._mount_points ) cls._test_migration_target = MigrationTarget( cloud_type=CloudType.VSPHERE, cloud_credentials=cls._credentials, target_vm=cls._test_workload ) cls._test_migration = Migration( mount_points=cls._mount_points, source=cls._test_workload, migration_target=cls._test_migration_target, migration_state=MigrationState.NOT_STARTED ) cls.results_mock = CollectionResults()
def test_C_dir_not_selected(self): mp1 = MountPoint("D:", 10) mps = [mp1] cr1 = Credentials('usr', 'psw', 'aws.com') source = Workload('10.1.1.1', cr1, mps) # Migration Target mp1_t = MountPoint("E:", 10) mps_t = [mp1_t] cr2 = Credentials('usr', 'psw', 'azure.com') target = MigrationTarget(CloudType.AZURE, cr2, Workload('10.0.0.0', cr2, mps_t)) # Start migration sel_mnt_pnt = [mp1] migration = Migration(sel_mnt_pnt, source, target) self.assertRaises(Exception, migration.run())
def test_invalid_credentials(self): mount_points = [MountPoint(name='C:\\', size=42)] with self.assertRaises(Exception): Workload(id=None, ip="1.1.1.1", credentials=None, storage=mount_points)
def setUpClass(cls): """Describe default models needs testing""" cls._mount_points = [MountPoint(name='C:\\', size=42)] cls._credentials = Credentials(user_name='User', password='******', domain='xxx.com') cls._test_workload = Workload(id=None, ip='111.11.11', credentials=cls._credentials, storage=cls._mount_points)
def test_invalid_storage(self): credentials = Credentials(user_name='User', password='******', domain='xxx.com') with self.assertRaises(Exception): Workload(id=None, ip="1.1.1.1", credentials=credentials, storage=None)
def get_workload(self) -> Workload: if not self.credentials: raise Exception("Credentials should be defined") if self.storage is None: raise Exception("Storage should be defined") return Workload(id=None, ip=self.ip, credentials=self.credentials, storage=self.storage)
def test__model__workload__service(self): workload = Workload.create("the-device", (3.1, 3.2, 3.3, 3.4, 3.5)) workload.service((1.01, 1.02, 1.03, 1.04, 1.05)) self.assertEqual(1.01, workload.usage_cpu) self.assertEqual(1.02, workload.usage_ram) self.assertEqual(1.03, workload.usage_gpu) self.assertEqual(1.04, workload.usage_disk) self.assertEqual(1.05, workload.usage_network) mock.wrapper.session.commit.assert_called_with()
def test_invalid_ip(self): mount_points = [MountPoint(name='C:\\', size=42)] credentials = Credentials(user_name='User', password='******', domain='xxx.com') with self.assertRaises(Exception): Workload(id=None, ip=None, credentials=credentials, storage=mount_points)
def serialize(self, data: dict) -> None: self.__data: dict = data self.__id: str = data.get('id') self.__name: str = data.get('name') self.__workloads: list = [] workloads_data: list = data.get('workloads') for i in range(len(workloads_data)): workload_data: dict = workloads_data[i] workload: Workload = Workload() workload.serialize(workload_data) self.__workloads.append(workload)
def test_valid_ip_credentials_storage(self): mount_points = [MountPoint(name='C:\\', size=42)] credentials = Credentials(user_name='User', password='******', domain='xxx.com') test_workload = Workload(id=None, ip='111.11.11', credentials=credentials, storage=mount_points) self.assertEqual(test_workload.ip, '111.11.11') self.assertEqual(test_workload.credentials, credentials) self.assertEqual(test_workload.storage, mount_points)
def create_device(data: dict, user: str) -> dict: """ Create a device. :param data: The given data. :param user: The user uuid. :return: The response """ count: int = wrapper.session.query(func.count( Device.uuid)).filter_by(owner=user).scalar() if count >= 3: return maximum_devices_reached comp, message = check_compatible(data) if not comp: return message comp, message = check_exists(user, data) if not comp: return message performance: tuple = calculate_power(data) device: Device = Device.create(user, True) Workload.create(device.uuid, performance) create_hardware(data, device.uuid) delete_items(user, data) m.contact_microservice("service", ["device_init"], { "device_uuid": device.uuid, "user": device.owner }) return device.serialize
def setUpClass(cls): """Setup general models for each test""" cls._mount_points = [MountPoint(name='C:\\', size=42)] cls._credentials = Credentials(user_name='User', password='******', domain='xxx.com') cls._test_workload = Workload(id=None, ip='111.11.11', credentials=cls._credentials, storage=cls._mount_points) cls._test_migration_target = MigrationTarget( cloud_type=CloudType.VSPHERE, cloud_credentials=cls._credentials, target_vm=cls._test_workload)
def test__model__workload__create(self): actual_result = Workload.create("the-device", (3.1, 3.2, 3.3, 3.4, 3.5)) self.assertEqual("the-device", actual_result.uuid) self.assertEqual(3.1, actual_result.performance_cpu) self.assertEqual(3.2, actual_result.performance_ram) self.assertEqual(3.3, actual_result.performance_gpu) self.assertEqual(3.4, actual_result.performance_disk) self.assertEqual(3.5, actual_result.performance_network) self.assertEqual(0, actual_result.usage_cpu) self.assertEqual(0, actual_result.usage_ram) self.assertEqual(0, actual_result.usage_gpu) self.assertEqual(0, actual_result.usage_disk) self.assertEqual(0, actual_result.usage_network) mock.wrapper.session.add.assert_called_with(actual_result) mock.wrapper.session.commit.assert_called_with()
def test__model__workload__display(self): workload = Workload.create("the-device", (3.1, 3.2, 3.3, 3.4, 3.5)) workload.usage_cpu = 1.4 workload.usage_ram = 3 workload.usage_disk = 1000 workload.usage_network = 2.7 expected_result = { "cpu": 1.4 / 3.1, "ram": 3 / 3.2, "gpu": 0.0, "disk": 1, "network": 2.7 / 3.5 } actual_result = workload.display() self.assertEqual(expected_result, actual_result)
def test__model__workload__workload_notification(self): workload = Workload.create("the-device", (3.1, 3.2, 3.3, 3.4, 3.5)) workload.usage_cpu = 1.4 workload.usage_ram = 3 workload.usage_disk = 1000 workload.usage_network = 2.7 expected_result = { "notify-id": "resource-usage", "origin": "test", "device_uuid": "the-device", "data": { "cpu": 1.4 / 3.1, "ram": 3 / 3.2, "gpu": 0.0, "disk": 1, "network": 2.7 / 3.5 }, } actual_result = workload.workload_notification("test") self.assertEqual(expected_result, actual_result)
from models.migration_target import MigrationTarget, CloudType from models.mount_point import MountPoint from models.credentials import Credentials from models.workload import Workload from service.migration import Migration mp1 = MountPoint("D:", 10) mp2 = MountPoint("C:", 20) mp3 = MountPoint("E:", 30) mps = [mp1, mp2, mp3] # Migration Source cr1 = Credentials('usr', 'psw', 'aws.com') source = Workload('10.1.1.1', cr1, mps) cr1.save() source.save() # Migration Target mp1_t = MountPoint("E:", 10) mp2_t = MountPoint("C:", 20) mps_t = [mp1_t, mp2_t] cr2 = Credentials('usr', 'psw', 'azure.com') target = MigrationTarget(CloudType.AZURE, cr2, Workload('10.0.0.0', cr2, mps_t)) cr2.save() target.save() # Start migration sel_mnt_pnt = [mp2] migration = Migration(sel_mnt_pnt, source, target) migration.run()
def create_model_from_dict(self, d: dict, obj_id: str) -> Workload: d["id"] = obj_id return Workload(**d)