def test_create_migration_create_a_migration_with_all_stages(self): # given m = migration.Migration() data = {"migration": {"name": "myMigrationTest", "os": "linux"}} target_format = self.create_targetFormat("nameTargetFormat") cred_account = uforge.CredAccountVSphere() install_profile = uforge.InstallProfile() image = uforge.Image() image.installProfile = install_profile publish_image = uforge.PublishImageVSphere() publish_image.credAccount = cred_account # when my_migration = m.create_migration(data["migration"]["name"], data["migration"]["os"], target_format.name, image, publish_image) # then self.assertEqual(my_migration.name, "myMigrationTest") self.assertEqual(my_migration.stages.stage[0].family, data["migration"]["os"]) self.assertEqual(my_migration.stages.stage[1].image.targetFormat.name, target_format.name) self.assertEqual(my_migration.stages.stage[1].image.installProfile, install_profile) self.assertEqual( my_migration.stages.stage[2].publishImage.targetFormat.name, target_format.name) self.assertEqual(my_migration.stages.stage[2].publishImage.credAccount, cred_account)
def test_migration_table_return_the_list_of_migrations_when_there_are_migrations( self, mock_api_getall, mock_migration_table): # given m = migration.Migration() m.api = Api("url", username="******", password="******", headers=None, disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT) m.login = "******" m.password = "******" migrations = uforge.migrations() migrations.migrations = pyxb.BIND() migration1 = self.create_migration(1, "a migration", 50, "In Progress", False, False, False) migrations.migrations.append(migration1) mock_api_getall.return_value = migrations # when m.do_list("") # then mock_migration_table.assert_called_with( migrations.migrations.migration)
def test_upload_and_launch_migration_binary_succeed_overlay_false( self, mock_upload_binary, mock_launch_binary): # given m = migration.Migration() m.api = Api("url", username="******", password="******", headers=None, disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT) m.login = "******" m.password = "******" migration_config = self.get_migration_config(overlay=False) # when m.upload_and_launch_migration_binary(m.login, m.password, migration_config, "local_uforge_migration_path", m.api.getUrl()) # then mock_upload_binary.assert_called_with( migration_config["source"]["host"], migration_config["source"]["ssh-port"], migration_config["source"]["user"], migration_config["source"]["password"], "local_uforge_migration_path", "/tmp/" + constants.MIGRATION_BINARY_NAME, None) mock_launch_binary.assert_called_with( ANY, self.get_command_launch(migration_config["name"], "", ""))
def test_do_launch_succeed_when_all_parameters_are_ok(self, mock_rmtree, mock_out, mock_api_create, mock_upload_and_launch_migration_binary, mock_create_migration, mock_retrieve_account, mock_retrieve_publish_image, mock_retrieve_image, mock_retrieve_target_format, mock_retrieve_migration_configuration, mock_download_binary): # given m = migration.Migration() m.api = Api("url", username="******", password="******", headers=None, disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT) m.login = "******" m.password = "******" mock_retrieve_migration_configuration.return_value = self.get_migration_config() mock_retrieve_target_format.return_value = self.create_targetFormat("targetFormatRetrieved") mock_retrieve_image.return_value = uforge.Image() mock_retrieve_publish_image.return_value = uforge.PublishImageVSphere() mock_retrieve_account.return_value = uforge.CredAccountVSphere() migration_to_create = uforge.migration() mock_create_migration.return_value = migration_to_create mock_upload_and_launch_migration_binary.return_value(0) # when m.do_launch("--file config.json") # then self.assertEquals(mock_api_create.call_count, 1) self.assertEquals(mock_download_binary.call_count, 1) self.assertEquals(mock_upload_and_launch_migration_binary.call_count, 1) self.assertEquals(mock_rmtree.call_count, 1) mock_out.assert_called_with("Migration launched successfully, please go to the platform to follow steps of the migration.", "OK")
def test_upload_and_launch_migration_binary_succeed_empty_exclude( self, mock_upload_binary, mock_launch_binary): # given m = migration.Migration() m.api = Api("url", username="******", password="******", headers=None, disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT) m.login = "******" m.password = "******" migration_config = self.get_migration_config() # when m.upload_and_launch_migration_binary(m.login, m.password, migration_config, "local_uforge_migration_path", m.api.getUrl()) # then mock_upload_binary.assert_called_with( migration_config["source"]["host"], migration_config["source"]["ssh-port"], migration_config["source"]["user"], migration_config["source"]["password"], "local_uforge_migration_path", "/tmp/" + constants.MIGRATION_BINARY_NAME) command_launch = 'chmod +x ' + '/tmp/' + constants.MIGRATION_BINARY_NAME + '; nohup ' + '/tmp/' + constants.MIGRATION_BINARY_NAME + ' -u login -p password -U url -n \'' + migration_config[ "name"] + '\' ' + ' >/dev/null 2>&1 &' mock_launch_binary.assert_called_with(ANY, command_launch)
def prepare_migrate_command(self): m = migration.Migration() m.api = Api("url", username="******", password="******", headers=None, disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT) m.login = "******" m.password = "******" return m
def test_do_launch_display_message_error_when_file_not_given(self, mock_out): # given m = migration.Migration() # when m.do_launch("--file") # then mock_out.assert_called_with("ERROR: In Arguments: argument --file: expected one argument", "ERROR")
def test_do_launch_display_message_error_when_apikeys_given(self, mock_out): # given m = migration.Migration() m.api = Api("url", username="******", password="******", headers=None, disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT) m.login = "******" m.apikeys = {"publickey": "the_public_key", "secretkey": "the_secret_key"} # when m.do_launch("--file config.json") # then mock_out.assert_called_with("Using API keys with command 'hammr migration launch' is not yet supported. Please use password.", "ERROR")
def test_do_list_return_no_migration_message_when_there_is_no_migration(self, mock_api_getall, mock_message, mock_migration_table): # given m = migration.Migration() m.api = Api("url", username="******", password="******", headers=None, disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT) m.login = "******" m.password = "******" migrations = uforge.migrations() migrations.migrations = pyxb.BIND() mock_api_getall.return_value = migrations # when m.do_list("") # then mock_message.assert_called_with("No migrations available") mock_migration_table(ANY).assert_not_called()
def test_upload_and_launch_migration_binary_succeed_with_exclude(self, mock_upload_binary, mock_launch_binary): # given m = migration.Migration() m.api = Api("url", username="******", password="******", headers=None, disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT) m.login = "******" m.password = "******" migration_config = self.get_migration_config(exclude_list=["/folder_to_exclude", "/folder/file_to_exclude.txt", "/folder/file to exclude with space.txt"]) # when m.upload_and_launch_migration_binary(m.login, m.password, migration_config, "local_uforge_migration_path", m.api.getUrl()) # then mock_upload_binary.assert_called_with(migration_config["source"]["host"], migration_config["source"]["ssh-port"], migration_config["source"]["user"], migration_config["source"]["password"], "local_uforge_migration_path", "/tmp/" + constants.MIGRATION_BINARY_NAME) mock_launch_binary.assert_called_with(ANY, self.get_command_launch(migration_config["name"], "-o ", "-e '/folder_to_exclude' -e '/folder/file_to_exclude.txt' -e '/folder/file to exclude with space.txt' "))