Пример #1
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
Пример #2
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)
Пример #3
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
Пример #4
0
def test_documentation_all(datadir):
    parser = edi._setup_command_line_interface()
    raw_input = os.path.join(str(datadir), 'raw_input')
    cli_args = parser.parse_args(['--log', 'WARNING', 'documentation', 'render', raw_input, str(datadir),
                                  os.path.join(str(datadir), 'all.yml')])

    Render().run_cli(cli_args)

    output_files = ['changelog.rst', 'index.rst', 'setup.rst', 'versions.rst']

    for file in output_files:
        generated = os.path.join(str(datadir), file)
        reference = os.path.join(str(datadir), 'expected', 'all', file)
        assert os.path.isfile(generated)
        assert cmp(generated, reference)

    with pytest.raises(FatalError) as error:
        Render().run_cli(cli_args)

    assert 'already exists' in error.value.message

    cli_args = parser.parse_args(['documentation', 'render', '--clean', raw_input, str(datadir),
                                  os.path.join(str(datadir), 'all.yml')])

    Render().run_cli(cli_args)

    for file in output_files:
        generated = os.path.join(str(datadir), file)
        assert not os.path.isfile(generated)
Пример #5
0
def test_merge(config_files, capsys):
    parser = edi._setup_command_line_interface()
    cli_args = parser.parse_args(['config', 'merge', config_files])

    Merge().run_cli(cli_args)
    out, err = capsys.readouterr()

    assert err == ''
    merged_config = yaml.load(out)
    assert merged_config.get('bootstrap').get('architecture') == 'i386'
Пример #6
0
def test_command_line_interface_setup(empty_config_file):
    parser = edi._setup_command_line_interface()
    assert 'embedded development infrastructure' in parser.description
    args = parser.parse_args(
        ['-v', 'lxc', 'configure', 'some-container', empty_config_file])
    assert args.command_name == 'lxc'
    assert str(args.config_file.name) == str(empty_config_file)
    assert args.container_name == 'some-container'
    assert args.sub_command_name == 'configure'
    assert args.verbose == True
Пример #7
0
def test_plugins(monkeypatch, config_files, capsys, command, command_args,
                 has_templates, has_profiles, has_playbooks,
                 has_postprocessing_commands):
    with mocked_executable('lxc'):
        with mocked_lxd_version_check():

            def fake_lxc_config_command(*popenargs, **kwargs):
                if 'images.compression_algorithm' in popenargs[0]:
                    return subprocess.CompletedProcess("fakerun", 0, '')
                else:
                    return subprocess.run(*popenargs, **kwargs)

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

            parser = edi._setup_command_line_interface()
            command_args.append(config_files)
            cli_args = parser.parse_args(command_args)

            command().run_cli(cli_args)
            out, err = capsys.readouterr()

            assert err == ''
            result = yaml.safe_load(out)

            if has_templates:
                assert result.get('lxc_templates')
            else:
                assert not result.get('lxc_templates')

            if has_profiles:
                assert result.get('lxc_profiles')
            else:
                assert not result.get('lxc_profiles')

            if has_playbooks:
                assert len(result.get('playbooks')) == 3
                base_system = result.get('playbooks')[0].get('10_base_system')
                assert 'plugins/playbooks/foo.yml' in base_system.get('path')
                assert base_system.get('dictionary').get(
                    'kernel_package') == 'linux-image-amd64-rt'
                assert base_system.get('dictionary').get(
                    'edi_project_directory') == os.path.dirname(config_files)
                assert base_system.get('dictionary').get("param1") == "keep"
                assert base_system.get('dictionary').get(
                    "param2") == "overwritten"
                assert base_system.get('dictionary').get(
                    "param3") == "customized"
            else:
                assert not result.get('playbooks')

            if has_postprocessing_commands:
                assert result.get('postprocessing_commands')
            else:
                assert not result.get('postprocessing_commands')
Пример #8
0
def test_config(config_files, capsys):
    # TODO: apply to all introspection aware commands
    parser = edi._setup_command_line_interface()
    cli_args = parser.parse_args(['lxc', 'configure', '--config', 'cname', config_files])

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

    assert err == ''
    merged_config = yaml.load(out)
    assert merged_config.get('bootstrap').get('architecture') == 'i386'
Пример #9
0
def test_plugins(config_files, capsys):
    parser = edi._setup_command_line_interface()
    cli_args = parser.parse_args(['lxc', 'configure', '--plugins', 'cname', config_files])

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

    assert err == ''
    result = yaml.load(out)
    assert len(result.get('playbooks')) == 3
    base_system = result.get('playbooks')[0].get('10_base_system')
    assert 'plugins/playbooks/foo.yml' in base_system.get('path')
    assert base_system.get('dictionary').get('kernel_package') == 'linux-image-amd64-rt'
    assert base_system.get('dictionary').get('edi_config_directory') == os.path.dirname(config_files)
Пример #10
0
def test_dictionary(config_files, capsys):
    parser = edi._setup_command_line_interface()
    cli_args = parser.parse_args(
        ['lxc', 'configure', '--dictionary', 'cname', config_files])

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

    assert err == ''
    dictionary = yaml.load(out)
    assert dictionary.get('edi_config_directory') == os.path.dirname(
        config_files)
    assert dictionary.get('edi_project_plugin_directory') == os.path.join(
        os.path.dirname(config_files), 'plugins')
Пример #11
0
def test_config(monkeypatch, config_files, capsys, command, command_args):
    def fake_lxc_config_command(*popenargs, **kwargs):
        if 'images.compression_algorithm' in popenargs[0]:
            return subprocess.CompletedProcess("fakerun", 0, '')
        else:
            return subprocess.run(*popenargs, **kwargs)

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

    parser = edi._setup_command_line_interface()
    command_args.append(config_files)
    cli_args = parser.parse_args(command_args)

    command().run_cli(cli_args)
    out, err = capsys.readouterr()

    assert err == ''
    merged_config = yaml.safe_load(out)
    assert merged_config.get('bootstrap').get('architecture') == 'i386'
Пример #12
0
def test_dictionary(monkeypatch, config_files, capsys, command, command_args):
    def fake_lxc_config_command(*popenargs, **kwargs):
        if 'images.compression_algorithm' in popenargs[0]:
            return subprocess.CompletedProcess("fakerun", 0, '')
        else:
            return subprocess.run(*popenargs, **kwargs)

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

    parser = edi._setup_command_line_interface()
    command_args.append(config_files)
    cli_args = parser.parse_args(command_args)

    command().run_cli(cli_args)
    out, err = capsys.readouterr()

    assert err == ''
    dictionary = yaml.safe_load(out)
    assert dictionary.get('edi_project_directory') == os.path.dirname(
        config_files)
    assert dictionary.get('edi_project_plugin_directory') == os.path.join(
        os.path.dirname(config_files), 'plugins')
Пример #13
0
def test_output_node(datadir, capsys):
    parser = edi._setup_command_line_interface()
    # without overlay
    cli_args = parser.parse_args([
        'image', 'create', '--config',
        os.path.join(str(datadir), 'output-node-base.yml')
    ])
    Create().run_cli(cli_args)
    out, err = capsys.readouterr()
    config = yaml.safe_load(out)
    second_command = config.get('postprocessing_commands').get(
        '120_second_command')
    assert second_command.get('output').get('output1') == 'foo.result'
    assert second_command.get('output').get('output2') == 'bar.result'
    assert second_command.get('parameters').get('bingo') == 'bongo'
    assert second_command.get('parameters').get(
        'message') == 'Foo, bar or baz?'
    assert second_command.get(
        'path') == 'postprocessing_commands/sample_command/some_command'
    assert not err or 'is shallow and may cause errors' in err

    # with overlay
    cli_args = parser.parse_args([
        'image', 'create', '--config',
        os.path.join(str(datadir), 'output-node.yml')
    ])
    Create().run_cli(cli_args)
    out, err = capsys.readouterr()
    config = yaml.safe_load(out)
    second_command = config.get('postprocessing_commands').get(
        '120_second_command')
    assert second_command.get('output').get('output1') == 'foo.result'
    assert second_command.get('output').get('output2') == 'baz.result'
    assert second_command.get('parameters').get('bingo') == 'bongo'
    assert second_command.get('parameters').get('message') == 'Foo and baz!'
    assert second_command.get(
        'path') == 'postprocessing_commands/sample_command/custom_command'
    assert not err or 'is shallow and may cause errors' in err
Пример #14
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)