def test_normal_migration(self): selected = [MountPoint("C:\\", 1024), MountPoint("E:\\", 2048)] source_credentials = Credentials("username", "password", "domain") storage = [ MountPoint("C:\\", 1024), MountPoint("E:\\", 2048), MountPoint("D:\\", 2048) ] source = Workload("127.0.0.1", source_credentials, storage) cloud_credentials = Credentials("username", "password", "cloud") target = MigrationTarget(CloudType.vSphere, cloud_credentials, None) migration = Migration(selected, source, target) migration.run() self.assertEqual(migration.migration_target.target_vm.ip, "127.0.0.1") self.assertEqual( migration.migration_target.target_vm.credentials.username, "username") self.assertEqual( migration.migration_target.target_vm.credentials.password, "password") self.assertEqual( migration.migration_target.target_vm.credentials.domain, "domain") self.assertEqual( [(s.mount_point_name, s.size) for s in migration.migration_target.target_vm.storage], [('C:\\', 1024), ('E:\\', 2048)]) self.assertEqual(migration.migration_state, MigrationState.SUCCESS)
def test_extract_wsp(monkeypatch): worker = Migration('/opt/graphite/storage/whisper/zon', '127.0.0.1', 2003) def mock_return(path): yield ('/opt/graphite/storage/whisper/zon', [], ['where.wsp']) monkeypatch.setattr(os, 'walk', mock_return) relative_path, full_path = next(worker._extract_wsp()) assert relative_path == '/where.wsp' assert full_path == '/opt/graphite/storage/whisper/zon/where.wsp'
def test_no_selected_storages_in_source(self): selected = [MountPoint("C:\\", 1024), MountPoint("E:\\", 2048)] source_credentials = Credentials("username", "password", "domain") storage = [MountPoint("D:\\", 2048), MountPoint("F:\\", 2048)] source = Workload("localhost", source_credentials, storage) cloud_credentials = Credentials("username", "password", "cloud") target = MigrationTarget(CloudType.vSphere, cloud_credentials, None) with self.assertRaises(Exception): migration = Migration(selected, source, target) migration.run()
def test_update(self): credentials = Credentials("new_user", "password", None) migration_target = MigrationTarget(CloudType.Azure, credentials, None) workload = Workload("127.0.0.1", credentials, []) mount_points = [MountPoint("C:\\", 1024)] migration = Migration(mount_points, workload, migration_target) migration_pickler.create(migration) mount_points = [MountPoint("E:\\", 1024)] new_migration = Migration(mount_points, workload, migration_target) migration_pickler.update(migration, new_migration) ret = migration_pickler.read(new_migration) self.assertEqual(len(ret), 1) self.assertEqual(ret[0].mount_points[0].mount_point_name, "E:\\")
async def test_send_one_wsp(monkeypatch): loop = asyncio.get_event_loop() host = '127.0.0.1' port = 2003 server = await asyncio.start_server(handler, host, port) worker = Migration('/opt/graphite/storage/whisper/zon', host, port, loop=loop) await worker.connect_to_graphite() def fetch_mock_return(path, i): return ((1483668388, 1483668390, 2), [7]) monkeypatch.setattr(whisper, 'fetch', fetch_mock_return) def exist_mock_return(path): return True monkeypatch.setattr(os.path, 'exists', exist_mock_return) storage = '/zon/where' metric = 'velocity' new_metric = 'mondev' await worker.send_one_wsp(storage, metric, new_metric) server.close()
async def go(): """ Use context manager """ async with Migration(directory, host, port, loop=loop, debug=True) as migration_worker: await migration_worker.run()
async def test_read_from_wsps(monkeypatch): loop = asyncio.get_event_loop() worker = Migration('/opt/graphite/storage/whisper/zon', '127.0.0.1', 2003, loop=loop) def fetch_mock_return(path, i): return ((1483668388, 1483668392, 2), [7, 8]) monkeypatch.setattr(whisper, 'fetch', fetch_mock_return) def walk_mock_return(path): yield ('/opt/graphite/storage/whisper/zon', [], ['where.wsp']) monkeypatch.setattr(os, 'walk', walk_mock_return) await worker.read_from_wsps() num = worker.queue.qsize() # two datapoints and one terminator assert num == 3 data1 = await worker.queue.get() data2 = await worker.queue.get() terminator = await worker.queue.get() assert data1 == ('zon.where', 7, 1483668388) assert data2 == ('zon.where', 8, 1483668390) assert terminator == None
def test_delete(self): credentials = Credentials("username", "new_password", None) migration_target = MigrationTarget( CloudType.AWS, credentials, Workload("127.0.0.1", credentials, [])) workload = Workload("127.0.0.1", credentials, []) mount_points = [MountPoint("C:\\", 1024)] migration = Migration(mount_points, workload, migration_target) migration_pickler.create(migration) migration_pickler.delete(migration) self.assertEqual(migration_pickler.read(migration_target), [])
def test_create_and_read_several_records(self): credentials1 = Credentials("username", "password", None) migration_target1 = MigrationTarget( CloudType.Azure, credentials1, Workload("127.0.0.1", credentials1, [])) workload1 = Workload("127.0.0.1", credentials1, []) mount_points1 = [MountPoint("C:\\", 1024)] migration1 = Migration(mount_points1, workload1, migration_target1) migration_pickler.create(migration1) credentials2 = Credentials("username", "password", None) migration_target2 = MigrationTarget(CloudType.Azure, credentials2, None) workload2 = Workload("127.0.0.1", credentials2, []) mount_points2 = [MountPoint("C:\\", 1024)] migration2 = Migration(mount_points2, workload2, migration_target2) migration_pickler.create(migration2) ret = migration_pickler.read(migration2) self.assertEqual(len(ret), 2)
def status_updater(self, migration: Migration): migration_template = Migration(migration.mount_points, migration.source, None) while migration.migration_state not in [ MigrationState.SUCCESS, MigrationState.ERROR ]: self.migration_pickler.update(migration_template, migration) sleep(5) self.migration_pickler.update(migration_template, migration)
def test_json_enc(self): credentials = Credentials("username", "password", None) self.assertEqual( json.dumps(credentials.repr_json(), cls=ComplexEncoder), '{"username": "******", "password": "******", "domain": null}') mount_point = MountPoint('C:\\', 1024) self.assertEqual( json.dumps(mount_point.repr_json(), cls=ComplexEncoder), r'{"mount_point_name": "C:\\", "size": 1024}') workload = Workload("127.0.0.1", credentials, [mount_point]) self.assertEqual( json.dumps(workload.repr_json(), cls=ComplexEncoder), '{"ip": "127.0.0.1", ' '"credentials": {"username": "******", "password": "******", "domain": null}, ' r'"storage": [{"mount_point_name": "C:\\", "size": 1024}]}') migration_target = MigrationTarget(CloudType.AWS, credentials, workload) self.assertEqual( json.dumps(migration_target.repr_json(), cls=ComplexEncoder), '{"cloud_type": "AWS", ' '"cloud_credentials": {"username": "******", "password": "******", "domain": null}, ' '"target_vm": {"ip": "127.0.0.1", "credentials":' ' {"username": "******", "password": "******", "domain": null}, ' r'"storage": [{"mount_point_name": "C:\\", "size": 1024}]}}') migration = Migration([mount_point], workload, migration_target) self.assertEqual( json.dumps(migration.repr_json(), cls=ComplexEncoder), r'{"mount_points": [{"mount_point_name": "C:\\", "size": 1024}], ' '"source": {"ip": "127.0.0.1", ' '"credentials": {"username": "******", "password": "******", "domain": null}, ' r'"storage": [{"mount_point_name": "C:\\", "size": 1024}]}, ' '"migration_target": {"cloud_type": "AWS", ' '"cloud_credentials": {"username": "******", "password": "******", "domain": null}, ' '"target_vm": {"ip": "127.0.0.1", ' '"credentials": {"username": "******", "password": "******", "domain": null}, ' r'"storage": [{"mount_point_name": "C:\\", "size": 1024}]}}, ' '"migration_state": "NOT_STARTED"}')
def test_create_and_read(self): credentials = Credentials("username", "password", None) migration_target = MigrationTarget(CloudType.Azure, credentials, None) workload = Workload("127.0.0.1", credentials, []) mount_points = [MountPoint("C:\\", 1024)] migration = Migration(mount_points, workload, migration_target) migration_pickler.create(migration) ret = migration_pickler.read(migration) self.assertEqual(len(ret), 1) self.assertEqual(ret[0].migration_target.cloud_type, CloudType.Azure) self.assertEqual(ret[0].source.credentials.username, "username")
async def test_write_to_graphite(): loop = asyncio.get_event_loop() worker = Migration('/opt/graphite/storage/whisper/zon', '127.0.0.1', 2003, loop=loop) await worker.connect_to_graphite() # prefill some data into queue data = ('zon.test.metric', 7, 1483668388) await worker.queue.put(data) await worker.queue.put(None) # send data await worker.write_to_graphite() await worker.close_conn_to_graphite()
async def test_graphite_connect(): loop = asyncio.get_event_loop() worker = Migration('/opt/graphite/storage/whisper/zon', '127.0.0.1', 2003, loop=loop) await worker.connect_to_graphite() message = "pytest-lamp" reader = worker.graphite_conn._reader writer = worker.graphite_conn._writer writer.write(message.encode("ascii")) await writer.drain() writer.write_eof() await writer.drain() data = (await reader.read()).decode("utf-8") writer.close() assert message == data await worker.close_conn_to_graphite()
async def test_run_with_context_manager(monkeypatch): loop = asyncio.get_event_loop() host = '127.0.0.1' port = 2003 server = await asyncio.start_server(handler, host, port) def fetch_mock_return(path, i): return ((1483668388, 1483668390, 2), [7]) monkeypatch.setattr(whisper, 'fetch', fetch_mock_return) def walk_mock_return(path): yield ('/opt/graphite/storage/whisper/zon', [], ['where.wsp']) monkeypatch.setattr(os, 'walk', walk_mock_return) async with Migration('/opt/graphite/storage/whisper/zon', host, port, loop=loop) as worker: await worker.run() server.close()
async def go(): migration_worker = Migration(directory, host, port, loop=loop) await migration_worker.connect_to_graphite() await migration_worker.run() await migration_worker.close_conn_to_graphite()
async def go(): migration_worker = Migration(directory, host, port, loop=loop, debug=True) await migration_worker.connect_to_graphite() await migration_worker.send_one_wsp(storage_dir, metric, new_metric) await migration_worker.close_conn_to_graphite()