Ejemplo n.º 1
0
    def test_autofix_eol(self, project_loader):
        project_loader("autofix_eol")

        history = (
            PropertyMigration("docker.compose.network_name",
                              "jsonnet.docker.compose.network_name",
                              since="v1.6.0"),
            PropertyMigration("docker.port_prefix",
                              "jsonnet.docker.expose.port_prefix",
                              since="v1.6.0"),
        )

        migrations.set_history(history)

        features.register(FileFeature())
        features.register(JinjaFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        with open('docker-compose.properties', 'r') as f:
            rendered = f.read()

        with open('docker-compose.expected.properties', 'r') as f:
            expected = f.read()

        assert rendered == expected
Ejemplo n.º 2
0
    def test_project_2_fallback(self, project_loader):
        project_loader("project2")

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(SymlinksFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.path.islink('test')
        assert os.readlink('test') == 'test.stage'
        assert os.path.exists('test')

        assert os.path.islink('test.yml')
        assert os.readlink('test.yml') == 'test.dev.yml'
        assert os.path.exists('test.yml')

        assert os.path.islink('test2')
        assert os.readlink('test2') == 'test2.prod'
        assert os.path.exists('test2')

        assert not os.path.islink('test3')
Ejemplo n.º 3
0
    def test_run_docker_binary_workdir_outside(self, project_loader,
                                               capsys: CaptureFixture):
        project_loader("outside")
        config.cwd = os.path.join(config.paths.project_home, "../outside")

        features.register(CoreFeature())
        features.register(RunFeature())
        features.register(ShellFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        binaries.register(DockerBinary("bin", "service", "/app"))

        action = RunAction()
        action.run("bin")

        read = capsys.readouterr()

        cwd = config.cwd if config.cwd else os.getcwd()
        real_cwd = os.path.realpath(cwd)

        assert read.out == docker_compose_bin + " -f ../project/docker-compose.yml " \
                                                "run --rm "\
                                                f"--volume={real_cwd}:/app " \
                                                "--workdir=/app " \
                                                "service\n"
Ejemplo n.º 4
0
    def test_from_mysql_fixuid_removed(self, project_loader):
        project_loader("from-mysql-fixuid-removed")
        register_default_caches()

        features.register(FixuidFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        with open('docker-compose.yml', 'r') as config_file:
            config = yaml.load(config_file, yaml.SafeLoader)

        events.docker.docker_compose_config(config)

        with open(os.path.join("docker", "Dockerfile.expected"), "r") as f:
            expected = f.read()
        with open(os.path.join("docker", "Dockerfile"), "r") as f:
            content = f.read()
        assert content == expected

        assert os.path.exists(os.path.join("docker", "fixuid.tar.gz"))

        os.remove(os.path.join("docker", "fixuid.yml"))
        events.file.deleted(os.path.join("docker", "fixuid.yml"))

        with open(os.path.join("docker", "Dockerfile.removed.expected"),
                  "r") as f:
            expected = f.read()
        with open(os.path.join("docker", "Dockerfile"), "r") as f:
            content = f.read()
        assert content == expected

        assert not os.path.exists(os.path.join("docker", "fixuid.tar.gz"))
Ejemplo n.º 5
0
    def test_run_binary_condition(self, project_loader,
                                  capsys: CaptureFixture):
        project_loader("empty")

        features.register(CoreFeature())
        features.register(RunFeature())
        features.register(ShellFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        binary1 = DockerBinary("npm", "node1")
        binary2 = DockerBinary("npm", "node2")
        default_binary = DefaultBinary("npm", ["npm"])
        binary4 = DockerBinary("npm", "node4")
        binary_invalid_condition = DockerBinary(
            "npm", "node5", condition="'another-value' in args")
        binary_valid_condition = DockerBinary("npm",
                                              "node6",
                                              condition="'some-value' in args")

        for bin in (binary1, binary2, default_binary, binary4,
                    binary_invalid_condition, binary_valid_condition):
            binaries.register(bin)

        action = RunAction()
        action.run("npm", "some-value")

        read = capsys.readouterr()
        assert "node6" in read.out
Ejemplo n.º 6
0
    def test_run_docker_binary_exe(self, project_loader,
                                   capsys: CaptureFixture):
        project_loader("exe")

        features.register(CoreFeature())
        features.register(RunFeature())
        features.register(ShellFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        binaries.register(
            DockerBinary("echo",
                         docker_compose_service="web",
                         args="echo",
                         exe=True))

        DockerUtils.service_stop('web')
        assert not DockerUtils.is_container_up('web')

        action = RunAction()
        action.run("echo")

        read = capsys.readouterr()

        assert read.out == docker_compose_bin + " exec web echo\n"

        assert DockerUtils.is_container_up('web')
Ejemplo n.º 7
0
    def test_empty_configuration_without_core(self, project_loader):
        project_loader("empty")

        with pytest.raises(FeatureConfigurationError):
            features.register(SymlinksFeature())
            load_registered_features()
            register_actions_in_event_bus(True)
Ejemplo n.º 8
0
    def test_example1_yaml(self, project_loader):
        project_loader("example1.yaml")

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(JsonnetFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.path.exists('example1.another')
        with open('example1.another', 'r') as f:
            example_another = f.read()

        with open('example1.expected.another', 'r') as f:
            example_another_expected = f.read()

        assert example_another == example_another_expected

        assert os.path.exists('example1.yaml')
        with open('example1.yaml', 'r') as f:
            example_yaml = f.read()

        with open('example1.expected.yaml', 'r') as f:
            example_yaml_expected = f.read()

        assert example_yaml == example_yaml_expected
Ejemplo n.º 9
0
    def test_empty_project_without_core(self, project_loader):
        project_loader("empty")

        features.register(RunFeature())
        features.register(ShellFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = RunAction()
        action.execute()
Ejemplo n.º 10
0
    def test_binary_options(self, project_loader):
        project_loader("binary-options")

        features.register(DockerFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = actions.get('docker:emit-docker-compose-config'
                             )  # type:EmitDockerComposeConfigAction
        action.execute()

        assert len(list(binaries.all())) == 3
        assert binaries.has("npm-simple")
        assert binaries.has("npm-conditions")
        assert binaries.has("mysql")

        npm_simple_set = binaries.get("npm-simple")
        assert len(npm_simple_set) == 1
        npm_simple = list(npm_simple_set)[0]
        assert npm_simple.command() == (
            ''.join(effective_command("docker-compose")) +
            " run --rm --workdir=/app/. --label traefik.enable=false node"
        ).split()
        assert npm_simple.command("serve") == (
            ''.join(effective_command("docker-compose")) +
            ' run --rm --workdir=/app/. --label traefik.enable=false node'
        ).split()
        assert npm_simple.command("run serve") == (
            ''.join(effective_command("docker-compose")) +
            ' run --rm --workdir=/app/. --label traefik.enable=false node'
        ).split()

        npm_conditions_set = binaries.get("npm-conditions")
        assert len(npm_conditions_set) == 1
        npm_conditions = list(npm_conditions_set)[0]
        assert npm_conditions.command() == (
            ''.join(effective_command("docker-compose")) +
            " run --rm --workdir=/app/. --label traefik.enable=false node"
        ).split()
        assert npm_conditions.command("serve") == (
            ''.join(effective_command("docker-compose")) +
            ' run --rm --workdir=/app/. --label traefik.enable=false node'
        ).split()
        assert npm_conditions.command("run serve") == (
            ''.join(effective_command("docker-compose")) +
            ' run --rm --workdir=/app/. node').split()

        mysql_set = binaries.get("mysql")
        assert len(mysql_set) == 1
        mysql = list(mysql_set)[0]
        assert mysql.command() == (
            ''.join(effective_command("docker-compose")) +
            ' run --rm --workdir=/app/. db mysql -hdb -uproject-management-tool -pproject-management-tool'
        ).split()
Ejemplo n.º 11
0
    def test_empty_project_without_core(self, project_loader):
        project_loader("empty")

        features.register(FileFeature())
        features.register(JsonnetFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()
Ejemplo n.º 12
0
    def test_empty_configuration_with_core(self, project_loader):
        project_loader("empty")

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(SymlinksFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()
Ejemplo n.º 13
0
    def test_run_missing_binary(self, project_loader):
        project_loader("empty")

        features.register(CoreFeature())
        features.register(RunFeature())
        features.register(ShellFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = RunAction()

        with pytest.raises(ValueError):
            action.run("missing")
Ejemplo n.º 14
0
    def test_binary_workdir(self, project_loader):
        project_loader("binary-workdir")

        features.register(DockerFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = actions.get('docker:emit-docker-compose-config'
                             )  # type:EmitDockerComposeConfigAction
        action.execute()

        assert len(list(binaries.all())) == 2
        assert binaries.has("npm")
        assert binaries.has("node")
Ejemplo n.º 15
0
    def test_local_volume_related(self, project_loader):
        project_loader("local-volume-related")

        features.register(DockerFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = actions.get('docker:emit-docker-compose-config'
                             )  # type:EmitDockerComposeConfigAction
        action.execute()

        assert not os.path.exists('node-path')
        assert os.path.isdir('new_directory')
        assert os.path.isdir('child')
        assert os.path.isdir(os.path.join('new_directory', 'some', 'child'))
Ejemplo n.º 16
0
    def test_ignore_invalid_extension(self, project_loader):
        project_loader("ignore_invalid_extension")

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(YttFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert not os.path.exists('yaml.txt')
        assert not os.path.exists('yaml.yml')
Ejemplo n.º 17
0
    def test_project_many_prod(self, project_loader):
        project_loader("project_many_prod")

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(SymlinksFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.path.islink('test.yml')
        assert os.readlink('test.yml') == 'test.prod.yml'
Ejemplo n.º 18
0
    def test_project2(self, project_loader):
        project_loader("project2")
        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(PermissionsFeature())

        load_registered_features()
        register_actions_in_event_bus()

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.access("script.sh", os.X_OK)
        assert os.access(os.path.join("subdirectory", "another-script.sh"),
                         os.X_OK)
Ejemplo n.º 19
0
    def test_inherit_from_template(self, project_loader):
        project_loader("inherit_from_template")
        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(JinjaFeature())
        features.register(PermissionsFeature())

        load_registered_features()
        register_actions_in_event_bus()

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.path.exists("script.sh")
        assert os.access("script.sh", os.X_OK)
Ejemplo n.º 20
0
    def test_docker_compose_variants(self, project_loader, variant):
        project_loader("docker_compose" + variant)

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(DockerFeature())
        features.register(JsonnetFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.path.exists('docker-compose.yml')
        with open('docker-compose.yml', 'r') as f:
            rendered = yaml.load(f.read(), yaml.SafeLoader)

        with open('docker-compose.expected.yml', 'r') as f:
            expected_data = f.read()

            if os.name == 'nt':
                mapped_cwd = re.sub(r"^([a-zA-Z]):", r"/\1", os.getcwd())
                mapped_cwd = pathlib.Path(mapped_cwd).as_posix()

                expected_data = expected_data.replace("%ddb.path.project%",
                                                      mapped_cwd)
            else:
                expected_data = expected_data.replace("%ddb.path.project%",
                                                      os.getcwd())
            expected_data = expected_data.replace(
                "%uid%", str(config.data.get('docker.user.uid')))
            expected_data = expected_data.replace(
                "%gid%", str(config.data.get('docker.user.gid')))
            expected_data = expected_data.replace(
                "%docker.debug.host%",
                str(config.data.get('docker.debug.host')))

            expected = yaml.load(expected_data, yaml.SafeLoader)

        assert rendered == expected

        if variant == '_mount_single_volume':
            assert os.path.isdir('volumes/shared-volume')

        if variant == '_mount_single_volume_with_default':
            assert os.path.isdir('shared-volume')
Ejemplo n.º 21
0
    def test_run_existing_binary(self, project_loader, capsys: CaptureFixture):
        project_loader("empty")

        features.register(CoreFeature())
        features.register(RunFeature())
        features.register(ShellFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        binaries.register(DefaultBinary("test", ["some", "command"]))

        action = RunAction()
        action.run("test")

        read = capsys.readouterr()

        assert read.out == "some command\n"
Ejemplo n.º 22
0
    def test_docker_compose_traefik_defaults(self, project_loader, variant):
        def before_load_config():
            os.rename("ddb.%s.yml" % variant, "ddb.yml")
            os.rename("docker-compose.expected.%s.yml" % variant,
                      "docker-compose.expected.yml")

        project_loader("docker_compose_traefik_defaults", before_load_config)

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(DockerFeature())
        features.register(JsonnetFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.path.exists('docker-compose.yml')
        with open('docker-compose.yml', 'r') as f:
            rendered = yaml.load(f.read(), yaml.SafeLoader)

        with open('docker-compose.expected.yml', 'r') as f:
            expected_data = f.read()

            if os.name == 'nt':
                mapped_cwd = re.sub(r"^([a-zA-Z]):", r"/\1", os.getcwd())
                mapped_cwd = pathlib.Path(mapped_cwd).as_posix()

                expected_data = expected_data.replace("%ddb.path.project%",
                                                      mapped_cwd)
            else:
                expected_data = expected_data.replace("%ddb.path.project%",
                                                      os.getcwd())
            expected_data = expected_data.replace(
                "%uid%", str(config.data.get('docker.user.uid')))
            expected_data = expected_data.replace(
                "%gid%", str(config.data.get('docker.user.gid')))
            expected_data = expected_data.replace(
                "%docker.debug.host%",
                str(config.data.get('docker.debug.host')))

            expected = yaml.load(expected_data, yaml.SafeLoader)

        assert rendered == expected
Ejemplo n.º 23
0
    def test_run_docker_binary(self, project_loader, capsys: CaptureFixture):
        project_loader("empty")
        config.cwd = config.paths.project_home

        features.register(CoreFeature())
        features.register(RunFeature())
        features.register(ShellFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        binaries.register(DockerBinary("bin", "service", "/app"))

        action = RunAction()
        action.run("bin")

        read = capsys.readouterr()

        assert read.out == docker_compose_bin + " run --rm --workdir=/app/. service\n"
Ejemplo n.º 24
0
    def test_project_keep_trailing_lines_default(self, project_loader):
        project_loader("trailing-newline")

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(JinjaFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.path.exists('foo.yml')
        with open('foo.yml', 'r') as f:
            foo = f.read()

        assert foo == 'test: trailing\n'
Ejemplo n.º 25
0
    def test_project4(self, project_loader):
        project_loader("project4")

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(JinjaFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.path.exists('foo')
        with open('foo', 'r') as f:
            foo = f.read()

        assert foo == 'env=dev'
Ejemplo n.º 26
0
    def test_local_volume_simple(self, project_loader):
        project_loader("local-volume-simple")

        features.register(DockerFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = actions.get('docker:emit-docker-compose-config'
                             )  # type:EmitDockerComposeConfigAction
        action.execute()

        assert not os.path.exists('node-path')
        assert os.path.isdir('existing-directory')
        assert os.path.isdir('new_directory')
        assert os.path.isfile('existing-file.txt')
        assert os.path.isfile('new_file')
        assert os.path.isfile('another_new_file.txt')

        with open('existing-file.txt', 'r') as f:
            assert f.read() == 'existing-file.txt'
Ejemplo n.º 27
0
    def test_project_4_subdirectory(self, project_loader):
        project_loader("project4")

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(SymlinksFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.path.islink(os.path.join('subdirectory', 'test.yml'))
        assert os.readlink(os.path.join('subdirectory',
                                        'test.yml')) == 'test.dev.yml'

        assert os.path.exists(os.path.join('subdirectory', 'test.yml'))

        assert not os.path.islink('no.yml')
Ejemplo n.º 28
0
    def test_fixuid_manual_entrypoint(self, project_loader):
        project_loader("fixuid-manual-entrypoint")
        register_default_caches()

        features.register(FixuidFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        with open('docker-compose.yml', 'r') as config_file:
            config = yaml.load(config_file, yaml.SafeLoader)

        events.docker.docker_compose_config(config)

        with open(os.path.join("docker", "Dockerfile.expected"), "r") as f:
            expected = f.read()
        with open(os.path.join("docker", "Dockerfile"), "r") as f:
            content = f.read()
        assert content == expected

        assert os.path.exists(os.path.join("docker", "fixuid.tar.gz"))
Ejemplo n.º 29
0
    def test_autofix_variables_only(self, project_loader):
        project_loader("autofix_variables_only")

        config.args.autofix = True

        history = (
            PropertyMigration("old_property", "new_property", since="v1.1.0"),
            PropertyMigration("some.deep.old.property",
                              "some.another.new.property",
                              since="v1.1.0"),
        )

        migrations.set_history(history)

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(DockerFeature())
        features.register(JsonnetFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.path.exists('variables.json')
        with open('variables.json', 'r') as f:
            rendered = f.read()

        with open('variables.expected.json', 'r') as f:
            expected = f.read()

        assert expected == rendered

        with open('variables.json.jsonnet', 'r') as f:
            source = f.read()

        with open('variables.json.autofix', 'r') as f:
            fixed = f.read()

        assert source == fixed
Ejemplo n.º 30
0
    def test_config_variables(self, project_loader):
        project_loader("config_variables")

        features.register(CoreFeature())
        features.register(FileFeature())
        features.register(JsonnetFeature())
        load_registered_features()
        register_actions_in_event_bus(True)

        action = FileWalkAction()
        action.initialize()
        action.execute()

        assert os.path.exists('variables.json')
        with open('variables.json', 'r') as f:
            variables = f.read()

        with open('variables.expected.json', 'r') as f:
            variables_expected = f.read()

        assert variables == variables_expected