Exemplo n.º 1
0
 def build_image_object(self):
     image_object = image.Image()
     api = Api(None, username="******", password="******", headers=None,
               disable_ssl_certificate_validation=True, timeout=constants.HTTP_TIMEOUT)
     image_object.api = api
     image_object.login = "******"
     return image_object
Exemplo n.º 2
0
    def test_do_list_succeed_when_appliance_list_with_UTC_Timezone(
            self, mock_table_add_row, mock_api_images_getall,
            mock_api_appliance_getall):
        # given
        t = template.Template()
        t.api = Api("url",
                    username="******",
                    password="******",
                    headers=None,
                    disable_ssl_certificate_validation=False,
                    timeout=constants.HTTP_TIMEOUT)
        t.login = "******"
        t.password = "******"
        timeZone = "UTC"
        self.create_appliance_list(timeZone, mock_api_appliance_getall)
        new_images = Images()
        new_images.Images = pyxb.BIND()
        mock_api_images_getall.return_value = new_images

        # when
        t.do_list("")

        # then
        self.assertEquals(mock_table_add_row.call_count, 1)
        mock_table_add_row.assert_called_with([
            1, "ApplianceWithInstallProfileUTCTimezone", "1.0",
            "Ubuntu 14.x x86_64", ANY, ANY, 0, None, ANY, ANY
        ])
Exemplo n.º 3
0
    def test_retrieve_image_raise_exception_when_format_name_not_found(
            self, mock_remove_special_chars):
        # given
        api = Api("url",
                  username="******",
                  password="******",
                  headers=None,
                  disable_ssl_certificate_validation=False,
                  timeout=constants.HTTP_TIMEOUT)
        target_format = uforge.TargetFormat()
        image_format = uforge.ImageFormat()
        image_format.name = "vcenter"
        target_format.format = image_format

        builder = {"hardwareSettings": {"memory": 512, "hwType": 4}}

        mock_remove_special_chars.return_value = "vcenternotfound"

        # when
        with self.assertRaises(Exception) as e:
            migration_utils.retrieve_image(builder, target_format, api,
                                           "login")

        # then
        self.assertTrue(
            "TargetFormat type is unsupported: vcenter" in e.exception)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    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"], "", ""))
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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")
Exemplo n.º 8
0
    def prepare_image(self):
        i = image.Image()
        i.api = Api("url", username="******", password="******", headers=None,
                    disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT)
        i.login = "******"
        i.password = "******"

        return i
Exemplo n.º 9
0
    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
Exemplo n.º 10
0
    def prepare_scan_run_command(self, name, overlay_option):
        s = scan.Scan()
        s.api = Api("url", username="******", password="******", headers=None,
                    disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT)
        s.login = "******"
        s.password = "******"

        args = "--ip 10.0.0.1 --scan-login login --scan-password password %s --name '%s'" % (overlay_option, name)
        return s, args
Exemplo n.º 11
0
class TestFilesYam(unittest.TestCase):
        global api
        api = Api(url, username=login, password=password, headers=None, disable_ssl_certificate_validation=True, timeout=constants.HTTP_TIMEOUT)

        def test_template_create_with_yaml(self):
                template = hammr.commands.template.Template()
                template.set_globals(api, login, password)
                r = template.do_create("--file tests/integration/data/test-parsing.yml --force")
                self.assertEqual(r, 0)
Exemplo n.º 12
0
class TestCLI(unittest.TestCase):

        global api
        api = Api(url, username=login, password=password, headers=None, disable_ssl_certificate_validation=True, timeout=constants.HTTP_TIMEOUT)

        def test_01_help_list(self):
                image = hammr.commands.image.Image()
                image.set_globals(api, login, password)
                r = image.help_list()
                self.assertEqual(r, None)
Exemplo n.º 13
0
    def test_download_binary_in_local_temp_dir_download_with_good_url_and_directory(
            self, mock_mkdir, mock_get_uforge_url, mock_download):
        # Given
        api = Api("url",
                  username="******",
                  password="******",
                  headers=None,
                  disable_ssl_certificate_validation=False,
                  timeout=constants.HTTP_TIMEOUT)
        mock_get_uforge_url.return_value = "/url"
        # When
        local_binary_path = hammr_utils.download_binary_in_local_temp_dir(
            api, "/tmp/local/temp/dir", "/uri/binary", "binaryName")

        # Then
        mock_download.assert_called_with(
            "/url/uri/binary", "/tmp/local/temp/dir/binaryName",
            not api.getDisableSslCertificateValidation())
        self.assertEqual(local_binary_path, "/tmp/local/temp/dir/binaryName")
Exemplo n.º 14
0
class TestUser(unittest.TestCase):

        global api
        api = Api(url, username=login, password=password, headers=None, disable_ssl_certificate_validation=True, timeout=constants.HTTP_TIMEOUT)

        def test_01_info(self):
                user = hammr.commands.user.User()
                user.set_globals(api, login, password)
                r = user.do_info(None)
                self.assertEqual(r, 0)
Exemplo n.º 15
0
class TestFormat(unittest.TestCase):

        global api
        api = Api(url, username=login, password=password, headers=None, disable_ssl_certificate_validation=True, timeout=constants.HTTP_TIMEOUT)

        def test_01_list(self):
                format = hammr.commands.format.Format()
                format.set_globals(api, login, password)
                r = format.do_list(None)
                self.assertEqual(r, 0)
Exemplo n.º 16
0
class TestBundle(unittest.TestCase):

    global api
    api = Api(url,
              username=login,
              password=password,
              headers=None,
              disable_ssl_certificate_validation=True,
              timeout=constants.HTTP_TIMEOUT)

    def test_01_list(self):
        bundle = hammr.commands.bundle.Bundle()
        bundle.set_globals(api, login, password)
        r = bundle.do_list(None)
        self.assertEqual(r, 0)

    def test_02_validate(self):
        bundle = hammr.commands.bundle.Bundle()
        bundle.set_globals(api, login, password)
        r = bundle.do_validate(
            "--file tests/integration/data/bundle/bundleFull.json")
        self.assertEqual(r, 0)

    def test_03_create(self):
        bundle = hammr.commands.bundle.Bundle()
        bundle.set_globals(api, login, password)
        r = bundle.do_create(
            "--file tests/integration/data/bundle/bundleFull.json")
        self.assertEqual(r, 0)

    def test_04_export(self):
        bundle = hammr.commands.bundle.Bundle()
        bundle.set_globals(api, login, password)
        id = get_bundle_id(bundle, "Bundle")
        r = bundle.do_export("--id " + id)
        self.assertEqual(r, 0)

    def test_05_import(self):
        bundle = hammr.commands.bundle.Bundle()
        bundle.set_globals(api, login, password)
        id = get_bundle_id(bundle, "Bundle")
        bundle.do_delete("--id " + id)
        r = bundle.do_import("--file Bundle.tar.gz")
        os.remove("Bundle.tar.gz")
        self.assertEqual(r, 0)

    def test_06_delete(self):
        bundle = hammr.commands.bundle.Bundle()
        bundle.set_globals(api, login, password)
        id = get_bundle_id(bundle, "Bundle")
        if id is not None and id != "":
            r = bundle.do_delete("--id " + id)
            self.assertEqual(r, None)
        else:
            raise unittest.SkipTest("No bundle to delete")
Exemplo n.º 17
0
 def create_template(self, url, username, password, template_login,
                     template_password):
     templ = template.Template()
     templ.api = Api(url,
                     username=username,
                     password=password,
                     headers=None,
                     disable_ssl_certificate_validation=False,
                     timeout=constants.HTTP_TIMEOUT)
     templ.login = template_login
     templ.password = template_password
     return templ
Exemplo n.º 18
0
    def test_retrieve_target_format_raise_exception_when_the_target_format_not_found(self, mock_get_target_format_object):
        # given
        api = Api("url", username="******", password="******", headers=None,
                  disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT)
        mock_get_target_format_object.return_value = None

        # when
        with self.assertRaises(Exception) as e:
            migration_utils.retrieve_target_format(api, "login", "targetFormatName")

        # then
        self.assertTrue("TargetFormat type unknown: targetFormatName" in e.exception)
Exemplo n.º 19
0
    def test_retrieve_target_format_return_the_target_format_found(self, mock_get_target_format_object):
        # given
        api = Api("url", username="******", password="******", headers=None,
                  disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT)
        target_format = uforge.TargetFormat()
        mock_get_target_format_object.return_value = target_format


        # when
        target_format_retrieved = migration_utils.retrieve_target_format(api, "login", "targetFormatName")

        # then
        self.assertEqual(target_format_retrieved, target_format)
Exemplo n.º 20
0
    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")
Exemplo n.º 21
0
    def test_retrieve_account_from_platform_raise_exception_when_no_accounts(self, mock_api_get_all):
        # given
        api = Api("url", username="******", password="******", headers=None,
                  disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT)
        accounts = uforge.CredAccounts()
        accounts.credAccounts = pyxb.BIND()
        mock_api_get_all.return_value = accounts

        # when
        with self.assertRaises(Exception) as e:
            migration_utils.retrieve_account(api, "login", "account")

        # then
        self.assertTrue("No CredAccounts available.\n You can use the command 'hammr account create' to create an account." in e.exception)
Exemplo n.º 22
0
    def test_do_list_gives_correct_number_of_deployments(self, mock_table_add_row, mock_api_deployments_getall):
        # given
        i = deploy.Deploy()
        i.api = Api("url", username="******", password="******", headers=None,
                    disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT)
        i.login = "******"
        i.password = "******"
        self.create_deployments(mock_api_deployments_getall)

        # when
        i.do_list(None)

        # then
        self.assertEquals(mock_table_add_row.call_count, 1)
Exemplo n.º 23
0
class TestImage(unittest.TestCase):

    global api
    api = Api(url,
              username=login,
              password=password,
              headers=None,
              disable_ssl_certificate_validation=True)

    def test_01_list(self):
        image = hammr.commands.image.Image()
        image.set_globals(api, login, password)
        r = image.do_list(None)
        self.assertEqual(r, 0)
Exemplo n.º 24
0
    def test_retrieve_account_return_the_cred_account_found(self, mock_api_get_all):
        # given
        api = Api("url", username="******", password="******", headers=None, disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT)
        cred_account = uforge.CredAccountVSphere()
        cred_account.name = "accountName"
        cred_account.uri = "/uri/credAccount"
        cred_accounts = self.create_accounts(cred_account, "vsphere")
        mock_api_get_all.return_value = cred_accounts

        # when
        cred_account_retrieved = migration_utils.retrieve_account(api, "login", cred_account.name)

        # then
        self.assertEqual(cred_account_retrieved.name, cred_account.name)
        self.assertEqual(cred_account_retrieved.uri, cred_account.uri)
Exemplo n.º 25
0
    def test_retrieve_account_from_platform_raise_exception_when_account_not_found(self, mock_api_get_all):
        # given
        api = Api("url", username="******", password="******", headers=None, disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT)
        cred_account = uforge.CredAccountVSphere()
        cred_account.name = "accountName"
        cred_account.uri = "/uri/credAccount"
        cred_accounts = self.create_accounts(cred_account, "vsphere")
        mock_api_get_all.return_value = cred_accounts

        # when
        with self.assertRaises(Exception) as e:
            migration_utils.retrieve_account(api, "login", "accountNotFound")

        # then
        self.assertTrue("CredAccount unknown: accountNotFound\n You can use the command 'hammr account create' to create an account." in e.exception)
Exemplo n.º 26
0
    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()
Exemplo n.º 27
0
    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' "))
Exemplo n.º 28
0
    def test_retrieve_image_return_the_image_created(self):
        # given
        api = Api("url",
                  username="******",
                  password="******",
                  headers=None,
                  disable_ssl_certificate_validation=False,
                  timeout=constants.HTTP_TIMEOUT)
        target_format = uforge.TargetFormat()
        image_format = uforge.ImageFormat()
        image_format.name = "vcenter"
        target_format.format = image_format

        builder = {"hardwareSettings": {"memory": 512, "hwType": 4}}

        # when
        image_retrieved = migration_utils.retrieve_image(
            builder, target_format, api, "login")

        # then
        self.assertEqual(image_retrieved.installProfile.memorySize, 512)
        self.assertEqual(image_retrieved.installProfile.hwType, "4")
        self.assertFalse(image_retrieved.compress)
Exemplo n.º 29
0
    def test_do_list_check_size(self, mock_table_add_row, mock_api_pimg_getall,
                                mock_api_getall):
        # given
        i = image.Image()
        i.api = Api("url",
                    username="******",
                    password="******",
                    headers=None,
                    disable_ssl_certificate_validation=False,
                    timeout=constants.HTTP_TIMEOUT)
        i.login = "******"
        i.password = "******"
        self.create_image(6000, mock_api_getall)
        new_pimages = uforge.publishImages()
        new_pimages.publishImages = pyxb.BIND()
        mock_api_pimg_getall.return_value = new_pimages
        # when
        i.do_list("")

        # then
        self.assertEquals(mock_table_add_row.call_count, 1)
        mock_table_add_row.assert_called_with(
            [ANY, ANY, ANY, ANY, ANY, ANY,
             size(6000), ANY, ANY])
Exemplo n.º 30
0
            sslAutosigned = data["acceptAutoSigned"]
        else:
            sslAutosigned = True
    except ValueError as e:
        printer.out("parsing error in credentials file: " + str(e),
                    printer.ERROR)
    except IOError as e:
        printer.out("File error in credentials file: " + str(e), printer.ERROR)
    except Exception as e:
        hammr_utils.print_uforge_exception(e)
        exit(1)

#UForge API instanciation
api = Api(url,
          username=username,
          password=password,
          headers=None,
          disable_ssl_certificate_validation=sslAutosigned,
          timeout=constants.HTTP_TIMEOUT)

if generics_utils.is_superviser_mode(username):
    login = generics_utils.get_target_username(username)
else:
    login = username

set_globals_cmds(app.subCmds)

if mainArgs.help and len(mainArgs.cmds) >= 1:
    argList = mainArgs.cmds + unknown
    argList.insert(len(mainArgs.cmds) - 1, "help")
    app.cmdloop(argList)
elif mainArgs.help: