Esempio n. 1
0
    def test_empty_project_with_activate_deactivate_commands(
            self, project_loader):
        project_loader("empty")

        features.register(CoreFeature())
        features.register(ShellFeature())
        features.register(SmartcdFeature())
        load_registered_features()

        action = WindowsProjectActivate()
        action.execute()

        assert os.path.exists("ddb_activate.bat")
        assert os.path.exists("ddb_deactivate.bat")

        with open("ddb_activate.bat") as f:
            assert "set command=(ddb activate)" in f.read()

        with open("ddb_deactivate.bat") as f:
            assert "set command=(ddb deactivate)" in f.read()
Esempio n. 2
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
Esempio n. 3
0
    def test_docker_compose_variables(self, project_loader):
        project_loader("docker_compose_variables")

        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
Esempio n. 4
0
    def test_example1(self, project_loader):
        project_loader("example1")

        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.json')
        with open('example1.json', 'r') as f:
            example = f.read()

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

        assert example == example_expected
Esempio n. 5
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
Esempio n. 6
0
    def test_depends_suffixes(self, project_loader):
        project_loader("depends_suffixes")

        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 os.path.exists('variables.yaml')
        with open('variables.yaml', 'r') as f:
            rendered = f.read()

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

        assert rendered == expected
Esempio n. 7
0
    def test_project2(self, project_loader):
        project_loader("project2")

        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 == 'env: dev\nincluded: True'

        assert not os.path.exists(os.path.join("partial", "_partial.yml"))
        assert not os.path.exists(os.path.join("partial", "partial.yml"))
Esempio n. 8
0
    def test_autofix_eol2(self, project_loader):
        project_loader("autofix_eol2")

        config.args.autofix = True

        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('msmtprc', 'r') as f:
            rendered = f.read()

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

        assert rendered == expected

        with open('msmtprc.jinja', 'r') as f:
            template = f.read()

        with open('msmtprc.autofix', 'r') as f:
            autofix = f.read()

        assert autofix == template
Esempio n. 9
0
    def test_docker_compose_xdebug(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_xdebug", before_load_config)

        print(os.getcwd())

        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()

            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
    def test_emit_complex(self, project_loader):
        project_loader("emit-complex")

        features.register(DockerFeature())
        load_registered_features()

        some_events = []
        another_events = []

        def someListener(*args, **kwargs):
            some_events.append({"args": args, "kwargs": kwargs})

        def anotherListener(*args, **kwargs):
            another_events.append({"args": args, "kwargs": kwargs})

        bus.on("some:test", someListener)
        bus.on("another:test", anotherListener)

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

        assert len(some_events) == 3
        assert {"args": ("emit-one-arg", ), "kwargs": {}} in some_events
        assert {"args": ("emit-one-arg-2", ), "kwargs": {}} in some_events
        # docker-compose >= 1.27.0 returns major version only. see https://github.com/docker/compose/issues/7730
        assert {"args": ("emit-some-arg",),
                "kwargs": {'image': 'ubuntu', 'kw1': 'emit-one-kwarg', 'kw2': 7, 'version': '3.7'}} in some_events or \
               {"args": ("emit-some-arg",),
                "kwargs": {'image': 'ubuntu', 'kw1': 'emit-one-kwarg', 'kw2': 7, 'version': '3'}} in some_events

        assert len(another_events) == 2
        assert {"args": ("emit-another-arg", ), "kwargs": {}} in another_events
        assert {
            "args": (),
            "kwargs": {
                "kw1": "emit-another-kwarg"
            }
        } in another_events
Esempio n. 11
0
    def test_example3_with_dir(self, project_loader):
        project_loader("example3.with_dir")

        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('./target/uwsgi.ini')
        with open('./target/uwsgi.ini', 'r') as f:
            iwsgi = f.read()

        with open('uwsgi.expected.ini', 'r') as f:
            iwsgi_expected = f.read()

        assert iwsgi == iwsgi_expected

        assert os.path.exists('./target/init.sh')
        with open('./target/init.sh', 'r') as f:
            init = f.read()

        with open('init.expected.sh', 'r') as f:
            init_expected = f.read()

        assert init == init_expected

        assert os.path.exists('./target/cassandra.conf')
        with open('./target/cassandra.conf', 'r') as f:
            cassandra = f.read()

        with open('cassandra.expected.conf', 'r') as f:
            cassandra_expected = f.read()

        assert cassandra == cassandra_expected
Esempio n. 12
0
    def test_get_file_content(self, data_dir: str, project_loader):
        project_loader()
        features.register(CoreFeature())
        load_registered_features()

        url = 'https://raw.githubusercontent.com/inetum-orleans/docker-devbox-ddb/b4f11276a37a4e4b1142f6b54b3d0763ccf5639e/ddb/__init__.py'
        path = 'test_file_content.txt'

        expected_file_content = '\n'.join([
            '# -*- coding: utf-8 -*-',
            '',
            'from .__version__ import __version__',
            ''
        ])

        url_content = FileUtils.get_file_content(url)
        assert expected_file_content == url_content

        url_content = FileUtils.get_file_content('file://' + os.path.join(config.path.project_home, path))
        assert url_content == 'this is a file for test_file_content'

        url_content = FileUtils.get_file_content('file://' + path)
        assert url_content == 'this is a file for test_file_content'
Esempio n. 13
0
    def test_docker_compose_included_services(self, project_loader):
        project_loader("docker_compose_included_services")

        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()
            expected = yaml.load(expected_data, yaml.SafeLoader)

        assert rendered == expected
Esempio n. 14
0
    def test_project5(self, project_loader):
        project_loader("project5")

        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'

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

        assert foo == 'env: dev'
    def test_ubuntu(self, project_loader):
        project_loader("ubuntu")

        features.register(DockerFeature())
        load_registered_features()

        custom_event_listeners = []

        def listener(docker_compose_config):
            custom_event_listeners.append(docker_compose_config)

        bus.on("docker:docker-compose-config", listener)

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

        assert len(custom_event_listeners) == 1
        assert custom_event_listeners[0] in [
            # docker-compose >= 1.27.0 returns major version only. see https://github.com/docker/compose/issues/7730
            {
                "version": "3",
                "services": {
                    "docker": {
                        "image": "ubuntu"
                    }
                }
            },
            {
                "version": "3.7",
                "services": {
                    "docker": {
                        "image": "ubuntu"
                    }
                }
            }
        ]
Esempio n. 16
0
    def test_empty_project_with_activate_deactivate_commands(
            self, project_loader, mocker: MockerFixture):
        mocker.patch('ddb.feature.smartcd.actions.is_smartcd_installed',
                     lambda: True)

        project_loader("empty")

        features.register(CoreFeature())
        features.register(ShellFeature())
        features.register(SmartcdFeature())
        load_registered_features()

        action = SmartcdAction()
        action.execute()

        assert os.path.exists(".bash_enter")
        assert os.path.exists(".bash_leave")

        with open(".bash_enter") as f:
            content = f.read()
            assert "$(ddb activate)" in content

        with open(".bash_leave") as f:
            assert "$(ddb deactivate)" in f.read()
    def test_install_certs(self, project_loader):
        project_loader("install-certs")

        features.register(CoreFeature())
        features.register(TraefikFeature())
        load_registered_features()

        install_action = TraefikInstalllCertsAction()
        install_action.execute(
            domain="dummy.tld",
            private_key=os.path.join(".certs", "some-dummy.tld.key"),
            certificate=os.path.join(".certs", "some-dummy.tld.crt"))

        assert os.path.exists(
            os.path.join(config.paths.home, "certs", "dummy.tld.key"))
        assert os.path.exists(
            os.path.join(config.paths.home, "certs", "dummy.tld.crt"))

        with open(os.path.join(config.paths.home, "certs",
                               "dummy.tld.crt")) as crt_file:
            assert 'crt' == crt_file.read()

        with open(os.path.join(config.paths.home, "certs",
                               "dummy.tld.key")) as key_file:
            assert 'key' == key_file.read()

        assert os.path.exists(
            os.path.join(config.data['traefik']['config_directory'],
                         "dummy.tld.ssl.toml"))

        uninstall_action = TraefikUninstalllCertsAction()
        uninstall_action.execute(domain="dummy.tld")

        assert not os.path.exists(
            os.path.join(config.data['traefik']['config_directory'],
                         "dummy.tld.ssl.toml"))
    def test_output_data(self, project_loader):
        features.register(DockerFeature())
        load_registered_features()
        action = actions.get(
            'docker:display-info')  # type:DockerDisplayInfoAction

        config.args.type = None

        assert '' == action._output_data('test', dict(), [], [], [])

        test_vhosts = action._retrieve_vhosts_data(
            Dotty({
                'labels': {
                    'traefik.http.routers.defaults-project.rule':
                    'Host(`web.domain.tld`)',
                    'traefik.http.routers.defaults-project-tls.rule':
                    'Host(`web.domain.tld`)',
                    'traefik.http.routers.test-project.rule':
                    'Host(`test.tld`)',
                    'ddb.emit.docker:binary[npm-simple](workdir)': '/app'
                }
            }))

        assert ('\n'.join([
            '+-------------------------+', '| test                    |',
            '+-------------------------+', '| http://test.tld/        |',
            '| https://web.domain.tld/ |', '+-------------------------+'
        ])) == action._output_data('test', dict(), [], [], test_vhosts)

        test_binaries = action._retrieve_binaries_data(
            Dotty({
                'labels': {
                    'ddb.emit.docker:binary[npm](name)': 'npm',
                    'ddb.emit.docker:binary[npm-simple](name)': 'npm-simple',
                    'ddb.emit.docker:binary[npm-simple](workdir)': '/app'
                }
            }))

        assert ('\n'.join([
            '+-------------------------+', '| test                    |',
            '+-------------------------+', '| npm                     |',
            '| npm-simple              |', '+-------------------------+',
            '| http://test.tld/        |', '| https://web.domain.tld/ |',
            '+-------------------------+'
        ])) == action._output_data('test', dict(), [], test_binaries,
                                   test_vhosts)

        test_envs = action._retrieve_environment_data(
            Dotty({'environment': {
                'AZERTY': '123'
            }}))
        assert ('\n'.join([
            '+-------------------------+', '| test                    |',
            '+-------------------------+', '| AZERTY: 123             |',
            '+-------------------------+', '| npm                     |',
            '| npm-simple              |', '+-------------------------+',
            '| http://test.tld/        |', '| https://web.domain.tld/ |',
            '+-------------------------+'
        ])) == action._output_data('test', test_envs, [], test_binaries,
                                   test_vhosts)

        test_ports = action._retrieve_service_ports(
            Dotty({'ports': [{
                'published': '12123',
                'target': '123'
            }]}))
        assert ('\n'.join([
            '+-------------------------+', '| test                    |',
            '+-------------------------+', '| AZERTY: 123             |',
            '+-------------------------+', '| 12123:123/tcp           |',
            '+-------------------------+', '| npm                     |',
            '| npm-simple              |', '+-------------------------+',
            '| http://test.tld/        |', '| https://web.domain.tld/ |',
            '+-------------------------+'
        ])) == action._output_data('test', test_envs, test_ports,
                                   test_binaries, test_vhosts)

        config.args.type = 'nothing'
        assert '' == action._output_data('test', dict(), [], [], test_vhosts)

        config.args.type = 'vhost'
        assert ('\n'.join([
            '+-------------------------+', '| test                    |',
            '+-------------------------+', '| http://test.tld/        |',
            '| https://web.domain.tld/ |', '+-------------------------+'
        ])) == action._output_data('test', dict(), [], [], test_vhosts)
    def test_traefik_cert(self, project_loader,
                          module_scoped_container_getter):
        project_loader("traefik-cert")

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

        setup_cfssl(module_scoped_container_getter)

        shutil.copyfile("docker-compose.yml", "docker-compose.original.yml")

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

        assert os.path.exists(
            os.path.join(config.paths.project_home, ".certs",
                         "web.domain.tld.key"))
        assert os.path.exists(
            os.path.join(config.paths.project_home, ".certs",
                         "web.domain.tld.crt"))
        assert os.path.exists(
            os.path.join(config.paths.home, "certs", "web.domain.tld.key"))
        assert os.path.exists(
            os.path.join(config.paths.home, "certs", "web.domain.tld.crt"))
        assert os.path.exists(
            os.path.join(config.paths.home, "traefik", "config",
                         "web.domain.tld.ssl.toml"))

        shutil.copyfile("docker-compose.removed.yml", "docker-compose.yml")
        action.execute()

        assert not os.path.exists(
            os.path.join(config.paths.project_home, ".certs",
                         "web.domain.tld.key"))
        assert not os.path.exists(
            os.path.join(config.paths.project_home, ".certs",
                         "web.domain.tld.crt"))
        assert not os.path.exists(
            os.path.join(config.paths.home, "certs", "web.domain.tld.key"))
        assert not os.path.exists(
            os.path.join(config.paths.home, "certs", "web.domain.tld.crt"))
        assert not os.path.exists(
            os.path.join(config.paths.home, "traefik", "config",
                         "web.domain.tld.ssl.toml"))

        shutil.copyfile("docker-compose.original.yml", "docker-compose.yml")
        action.execute()

        assert os.path.exists(
            os.path.join(config.paths.project_home, ".certs",
                         "web.domain.tld.key"))
        assert os.path.exists(
            os.path.join(config.paths.project_home, ".certs",
                         "web.domain.tld.crt"))
        assert os.path.exists(
            os.path.join(config.paths.home, "certs", "web.domain.tld.key"))
        assert os.path.exists(
            os.path.join(config.paths.home, "certs", "web.domain.tld.crt"))
        assert os.path.exists(
            os.path.join(config.paths.home, "traefik", "config",
                         "web.domain.tld.ssl.toml"))

        shutil.copyfile("docker-compose.changed.yml", "docker-compose.yml")
        action.execute()

        assert not os.path.exists(
            os.path.join(config.paths.project_home, ".certs",
                         "web.domain.tld.key"))
        assert not os.path.exists(
            os.path.join(config.paths.project_home, ".certs",
                         "web.domain.tld.crt"))
        assert not os.path.exists(
            os.path.join(config.paths.home, "certs", "web.domain.tld.key"))
        assert not os.path.exists(
            os.path.join(config.paths.home, "certs", "web.domain.tld.crt"))
        assert not os.path.exists(
            os.path.join(config.paths.home, "traefik", "config",
                         "web.domain.tld.ssl.toml"))

        assert os.path.exists(
            os.path.join(config.paths.project_home, ".certs",
                         "web-changed.domain.tld.key"))
        assert os.path.exists(
            os.path.join(config.paths.project_home, ".certs",
                         "web-changed.domain.tld.crt"))
        assert os.path.exists(
            os.path.join(config.paths.home, "certs",
                         "web-changed.domain.tld.key"))
        assert os.path.exists(
            os.path.join(config.paths.home, "certs",
                         "web-changed.domain.tld.crt"))
        assert os.path.exists(
            os.path.join(config.paths.home, "traefik", "config",
                         "web-changed.domain.tld.ssl.toml"))