Esempio n. 1
0
def test_target_configure(config_files, monkeypatch, capsys):
    def fakerun(*popenargs, **kwargs):
        if get_command(popenargs) == "ansible-playbook":
            return subprocess.CompletedProcess("fakerun", 0, '')
        else:
            print('Passthrough: {}'.format(get_command(popenargs)))
            return subprocess.run(*popenargs, **kwargs)

    monkeypatch.setattr(mockablerun, 'run_mockable', fakerun)

    suppress_chown_during_debuild(monkeypatch)

    with workspace():
        edi_exec = os.path.join(get_project_root(), 'bin', 'edi')
        project_name = 'pytest-{}'.format(get_random_string(6))
        config_command = [
            edi_exec, 'config', 'init', project_name, 'debian-jessie-amd64'
        ]
        run(config_command)  # run as non root

        parser = edi._setup_command_line_interface()
        cli_args = parser.parse_args([
            'target', 'configure', 'remote-target',
            '{}-develop.yml'.format(project_name)
        ])

        Configure().run_cli(cli_args)

        out, err = capsys.readouterr()
        print(out)
        assert not err or 'is shallow and may cause errors' in err
Esempio n. 2
0
def test_run_and_clean(config_files, monkeypatch):
    def intercept_command_run(*popenargs, **kwargs):
        if "command" in get_command(popenargs):
            print(popenargs)
            with open(get_command(popenargs), encoding='utf-8') as f:
                print(f.read())
            return subprocess.run(*popenargs, **kwargs)
        else:
            print("passthrough")
            return subprocess.run(*popenargs, **kwargs)

    monkeypatch.setattr(mockablerun, 'run_mockable', intercept_command_run)

    suppress_chown_during_debuild(monkeypatch)

    with workspace() as workdir:
        with open(config_files, "r") as main_file:
            parser = ConfigurationParser(main_file)

            input_file = os.path.join(workdir, 'input.txt')
            with open(input_file, mode='w', encoding='utf-8') as i:
                i.write("*input file*\n")

            runner = CommandRunner(parser, 'postprocessing_commands',
                                   input_file)

            artifacts = runner.run()

            assert os.path.isfile(os.path.join('artifacts', 'first.txt'))
            assert os.path.isfile(os.path.join('artifacts', 'last.txt'))

            def verify_last_artifact(artifact):
                assert str(workdir) in str(artifact)
                assert 'artifacts/last.txt' in str(artifact)
                assert 'last.txt' in str(artifact)

                with open(artifact, mode='r') as result_file:
                    content = result_file.read()
                    assert "*input file*" in content
                    assert "*first step*" in content
                    assert "*second step*" in content
                    assert "*last step*" in content

            verify_last_artifact(artifacts[-1])

            first_file = os.path.join('artifacts', 'first.txt')
            first_folder = os.path.join('artifacts', 'first_folder')
            os.remove(first_file)
            os.rmdir(first_folder)

            runner.run()
            assert os.path.isfile(first_file)
            assert os.path.isdir(first_folder)
            runner.clean()
            assert not os.path.isfile(os.path.join('artifacts', 'last.txt'))
            assert not os.path.isfile(first_file)
            assert not os.path.isdir(first_folder)
Esempio n. 3
0
def test_create_jessie_image(capsys):
    print(os.getcwd())
    with workspace():
        edi_exec = os.path.join(get_project_root(), 'bin', 'edi')
        project_name = 'pytest-{}'.format(get_random_string(6))
        config_command = [edi_exec, 'config', 'init', project_name, 'debian-jessie-amd64']
        run(config_command)  # run as non root

        parser = edi._setup_command_line_interface()
        cli_args = parser.parse_args(['image', 'create', '{}-develop.yml'.format(project_name)])

        Create().run_cli(cli_args)
        out, err = capsys.readouterr()
        print(out)
        assert not err

        lxc_compression_algo = get_server_image_compression_algorithm()
        lxc_export_extension = get_file_extension_from_image_compression_algorithm(lxc_compression_algo)

        images = [
            os.path.join(get_artifact_dir(), '{}-develop_edicommand_image_bootstrap_di.tar.gz'.format(project_name)),
            os.path.join(get_artifact_dir(), '{}-develop_edicommand_image_lxc_di.tar.gz'.format(project_name)),
            os.path.join(get_artifact_dir(), '{}-develop_edicommand_lxc_export{}'.format(project_name,
                                                                                         lxc_export_extension)),
            os.path.join(get_artifact_dir(), '{}-develop.result'.format(project_name)),
        ]
        for image in images:
            assert os.path.isfile(image)

        image_store_items = [
            "{}-develop_edicommand_lxc_import_di".format(project_name),
            "{}-develop_edicommand_lxc_publish".format(project_name)
        ]
        lxc_image_list_cmd = ['lxc', 'image', 'list']
        result = run(lxc_image_list_cmd, stdout=subprocess.PIPE)
        for image_store_item in image_store_items:
            assert image_store_item in result.stdout

        parser = edi._setup_command_line_interface()
        cli_args = parser.parse_args(['image', 'create', '--clean', '{}-develop.yml'.format(project_name)])
        Create().run_cli(cli_args)

        parser = edi._setup_command_line_interface()
        cli_args = parser.parse_args(['image', 'create', '--recursive-clean', '8',
                                      '{}-develop.yml'.format(project_name)])
        Create().run_cli(cli_args)

        for image in images:
            assert not os.path.isfile(image)

        result = run(lxc_image_list_cmd, stdout=subprocess.PIPE)
        for image_store_item in image_store_items:
            assert image_store_item not in result.stdout
Esempio n. 4
0
def test_artifacts_folder_removal(monkeypatch):
    suppress_chown_during_debuild(monkeypatch)

    with workspace() as workdir:
        create_artifact_dir()
        artifacts_dir = get_artifact_dir()
        assert str(workdir) in str(artifacts_dir)
        random_dir_name = get_random_string(20)
        abs_dir_name = os.path.join(artifacts_dir, random_dir_name)
        run(['mkdir', '-p', abs_dir_name])
        assert os.path.isdir(abs_dir_name)
        safely_remove_artifacts_folder(abs_dir_name)
        assert not os.path.isdir(abs_dir_name)
Esempio n. 5
0
def test_build_stretch_container(capsys):
    print(os.getcwd())
    with workspace():
        edi_exec = os.path.join(get_project_root(), 'bin', 'edi')
        project_name = 'pytest-{}'.format(get_random_string(6))
        config_command = [edi_exec, 'config', 'init', project_name, 'debian-stretch-amd64']
        run(config_command)  # run as non root

        container_name = 'pytest-{}'.format(get_random_string(6))
        parser = edi._setup_command_line_interface()
        cli_args = parser.parse_args(['-v', 'lxc', 'configure', container_name, '{}-develop.yml'.format(project_name)])

        Configure().run_cli(cli_args)
        out, err = capsys.readouterr()
        print(out)
        assert not err

        images = [
            '{}-develop_edicommand_image_bootstrap.tar.gz'.format(project_name),
            '{}-develop_edicommand_image_lxc.tar.gz'.format(project_name)
        ]
        for image in images:
            assert os.path.isfile(image)

        lxc_image_list_cmd = ['lxc', 'image', 'list']
        result = run(lxc_image_list_cmd, stdout=subprocess.PIPE)
        assert project_name in result.stdout

        parser = edi._setup_command_line_interface()
        cli_args = parser.parse_args(['-v', 'clean', '{}-develop.yml'.format(project_name)])
        Clean().run_cli(cli_args)

        for image in images:
            assert not os.path.isfile(image)

        result = run(lxc_image_list_cmd, stdout=subprocess.PIPE)
        assert project_name not in result.stdout

        verification_command = ['lxc', 'exec', container_name, '--', 'cat', '/etc/os-release']
        result = run(verification_command, stdout=subprocess.PIPE)
        assert '''VERSION_ID="9"''' in result.stdout
        assert 'ID=debian' in result.stdout

        stop_command = ['lxc', 'stop', container_name]
        run(stop_command)

        delete_command = ['lxc', 'delete', container_name]
        run(delete_command)
Esempio n. 6
0
def test_artifacts_folder_removal_as_sudo():
    with workspace() as workdir:
        create_artifact_dir()
        artifacts_dir = get_artifact_dir()
        assert str(workdir) in str(artifacts_dir)
        random_dir_name = get_random_string(20)
        abs_dir_name = os.path.join(artifacts_dir, random_dir_name)
        mount_folder = os.path.join(workdir, get_random_string(20))
        mount_point = os.path.join(abs_dir_name, 'mnt')

        for folder in [abs_dir_name, mount_folder, mount_point]:
            run(['mkdir', '-p', folder])
            assert os.path.isdir(folder)

        run(['mount', '--bind', mount_folder, mount_point], sudo=True)

        with pytest.raises(FatalError) as error:
            safely_remove_artifacts_folder(abs_dir_name, sudo=True)
        assert abs_dir_name in error.value.message

        run(['umount', mount_point], sudo=True)
        safely_remove_artifacts_folder(abs_dir_name, sudo=True)

        assert not os.path.isdir(abs_dir_name)
Esempio n. 7
0
def test_clean_empty_config(empty_config_file, monkeypatch):
    suppress_chown_during_debuild(monkeypatch)
    with workspace():
        with open(empty_config_file, "r") as main_file:
            clean_cmd = Clean()
            clean_cmd.run(main_file)
Esempio n. 8
0
def test_build_stretch_container(capsys, datadir):
    with workspace():
        edi_exec = os.path.join(get_project_root(), 'bin', 'edi')
        project_name = 'pytest-{}'.format(get_random_string(6))
        config_command = [
            edi_exec, 'config', 'init', project_name,
            'debian-stretch-{}'.format(get_debian_architecture())
        ]
        run(config_command)  # run as non root

        # enable ssh server and create a default user
        modify_develop_overlay(project_name)
        # copy pub key into default pub key folder
        prepare_pub_key(datadir)

        container_name = 'pytest-{}'.format(get_random_string(6))
        parser = edi._setup_command_line_interface()
        cli_args = parser.parse_args([
            '-v', 'lxc', 'configure', container_name,
            '{}-develop.yml'.format(project_name)
        ])

        Configure().run_cli(cli_args)
        out, err = capsys.readouterr()
        print(out)
        assert not err

        images = [
            os.path.join(
                get_artifact_dir(),
                '{}-develop_edicommand_image_bootstrap.tar.gz'.format(
                    project_name)),
            os.path.join(
                get_artifact_dir(),
                '{}-develop_edicommand_lxc_prepare.tar.gz'.format(
                    project_name))
        ]
        for image in images:
            assert os.path.isfile(image)

        lxc_image_list_cmd = [lxc_exec(), 'image', 'list']
        result = run(lxc_image_list_cmd, stdout=subprocess.PIPE)
        assert project_name in result.stdout

        parser = edi._setup_command_line_interface()
        cli_args = parser.parse_args(
            ['-v', 'clean', '{}-develop.yml'.format(project_name)])
        Clean().run_cli(cli_args)

        for image in images:
            assert not os.path.isfile(image)

        result = run(lxc_image_list_cmd, stdout=subprocess.PIPE)
        assert project_name not in result.stdout

        verification_command = [
            lxc_exec(), 'exec', container_name, '--', 'cat', '/etc/os-release'
        ]
        result = run(verification_command, stdout=subprocess.PIPE)
        assert '''VERSION_ID="9"''' in result.stdout
        assert 'ID=debian' in result.stdout

        os.chmod(os.path.join(str(datadir), 'keys'), 0o700)
        os.chmod(os.path.join(str(datadir), 'keys', 'test_id_rsa'), 0o600)
        container_ip = get_container_ip_addr(container_name, 'lxcif0')
        ssh_cmd = [
            'ssh', '-i',
            str(os.path.join(str(datadir), 'keys', 'test_id_rsa')), '-o',
            'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no',
            'testuser@{}'.format(container_ip), 'true'
        ]
        # ssh command should work without password due to proper ssh key setup!
        run(ssh_cmd, sudo=True, timeout=5)

        verify_shared_folder(container_name)

        stop_command = [lxc_exec(), 'stop', container_name]
        run(stop_command)

        delete_command = [lxc_exec(), 'delete', container_name]
        run(delete_command)