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
def test_empty_project_with_core(self, project_loader, module_scoped_container_getter): project_loader("empty") features.register(CoreFeature()) features.register(CertsFeature()) load_registered_features() setup_cfssl(module_scoped_container_getter) generate_action = GenerateCertAction() generate_action.execute(domain="testing.test") assert os.path.exists(os.path.join(".certs", "testing.test.crt")) assert os.path.exists(os.path.join(".certs", "testing.test.key")) with open(os.path.join(".certs", "testing.test.crt")) as crt_file: assert 'CERTIFICATE' in crt_file.read() with open(os.path.join(".certs", "testing.test.key")) as key_file: assert 'PRIVATE KEY' in key_file.read() remove_action = RemoveCertAction() remove_action.execute(domain="testing.test") assert not os.path.exists(os.path.join(".certs", "testing.test.crt")) assert not os.path.exists(os.path.join(".certs", "testing.test.key"))
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')
def test_update_files_with_submodule(self, project_loader): project_loader("with_submodules") features.register(CoreFeature()) features.register(GitFeature()) load_registered_features() path_main = config.path.project_home path_submodule = os.path.join(path_main, 'submodule') submodule = Repo.init(path_submodule) submodule.create_remote( 'origin', 'https://github.com/inetum-orleans/docker-devbox-ddb') submodule.git.add('.') submodule.git.update_index('.gitignore', chmod='+x') submodule.git.commit('-m "Initial commit"') repo = Repo.init(path_main) repo.git.add('.') repo.create_submodule('a-sub-module', 'submodule', no_checkout=True) repo.git.update_index('.gitignore', chmod='+x') repo.git.commit('-m "Initial commit"') action = FixFilePermissionsAction() action.execute() assert os.access(os.path.join(config.path.project_home, '.gitignore'), os.X_OK) assert os.access( os.path.join(config.path.project_home, 'submodule', '.gitignore'), os.X_OK) assert not os.access( os.path.join(config.path.project_home, '.gitmodules'), os.X_OK)
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"
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')
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
def test_existing_does_nothing(self, project_loader, module_scoped_container_getter): project_loader("existing") features.register(CoreFeature()) features.register(CertsFeature()) load_registered_features() setup_cfssl(module_scoped_container_getter) assert os.path.exists(os.path.join(".certs", "testing.test.crt")) assert os.path.exists(os.path.join(".certs", "testing.test.key")) with open(os.path.join(".certs", "testing.test.crt")) as crt_file: crt = crt_file.read() with open(os.path.join(".certs", "testing.test.key")) as key_file: key = key_file.read() generate_action = GenerateCertAction() generate_action.execute(domain="testing.test") with open(os.path.join(".certs", "testing.test.crt")) as crt_file: assert crt == crt_file.read() with open(os.path.join(".certs", "testing.test.key")) as key_file: assert key == key_file.read() remove_action = RemoveCertAction() remove_action.execute(domain="testing.test") assert not os.path.exists(os.path.join(".certs", "testing.test.crt")) assert not os.path.exists(os.path.join(".certs", "testing.test.key"))
def test_extra_services(self, project_loader): project_loader("extra-services") features.register(CoreFeature()) features.register(TraefikFeature()) features.register(DockerFeature()) features.register(JsonnetFeature()) load_registered_features() install_action = TraefikExtraServicesAction() install_action.initialize() install_action.execute() files = { "api": { "generated": "sub.project.test.extra-service.api.toml", "expected": "sub.project.test.extra-service.api.expected.toml" }, "rule": { "generated": "rule.project.test.extra-service.web.toml", "expected": "rule.project.test.extra-service.web.expected.toml", "replaces": { "{{ip}}": config.data.get('docker.debug.host') } }, "secured": { "generated": "secured.project.test.extra-service.redirect.toml", "expected": "secured.project.test.extra-service.redirect.expected.toml" }, "secured_path_prefix": { "generated": "secured.project.test.extra-service.path_prefix.toml", "expected": "secured.project.test.extra-service.path_prefix.expected.toml" }, "secured_path_prefix_with_redirect": { "generated": "secured.project.test.extra-service.path_prefix_with_redirect.toml", "expected": "secured.project.test.extra-service.path_prefix_with_redirect.expected.toml" } } for file in files: generated = os.path.join( config.data['traefik']['config_directory'], files.get(file).get('generated')) assert os.path.exists(generated) expected = Path( os.path.join(config.data['traefik']['config_directory'], files.get(file).get('expected'))).read_text() if files.get(file).get('replaces'): for replace in files.get(file).get('replaces'): expected = expected.replace( replace, files.get(file).get('replaces').get(replace)) assert expected == Path(generated).read_text()
def test_empty_project_with_core(self, project_loader): project_loader("empty") features.register(CoreFeature()) features.register(TraefikFeature()) load_registered_features() assert config.data.get('traefik.disabled') is True
def test_empty_project_with_core(self, project_loader): project_loader("empty") features.register(CoreFeature()) features.register(FixuidFeature()) load_registered_features() action = FixuidDockerComposeAction() action.execute(docker_compose_config={})
def test_empty_project_with_core(self, project_loader, mocker: MockerFixture): mocker.patch('ddb.feature.version.is_git_repository', is_git_repository) project_loader("no_repo") features.register(CoreFeature()) features.register(VersionFeature()) load_registered_features()
def test_stage_env(self, project_loader): project_loader("stage-env") gitignore_feature = GitignoreFeature() features.register(CoreFeature()) features.register(gitignore_feature) load_registered_features() assert gitignore_feature.disabled
def test_empty_project_with_core(self, project_loader): project_loader("empty") features.register(CoreFeature()) features.register(DockerFeature()) load_registered_features() action = actions.get('docker:emit-docker-compose-config' ) # type:EmitDockerComposeConfigAction action.execute()
def test_empty_skip_outside_additions(self, project_loader): project_loader("empty") features.register(CoreFeature()) features.register(GitignoreFeature()) load_registered_features() action = UpdateGitignoreAction() action.execute(target="../outside/to-ignore.yml") assert not os.path.exists('.gitignore')
def test_empty_project_with_core(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() action.execute()
def test_update_files_disabled(project_loader): project_loader("disabled") features.register(CoreFeature()) features.register(GitFeature()) load_registered_features() config.data['git.fix_file_permissions'] = False action = FixFilePermissionsAction() assert action.disabled is True
def test_empty_project_with_core(self, project_loader): project_loader("empty") features.register(CoreFeature()) features.register(FileFeature()) features.register(JsonnetFeature()) load_registered_features() register_actions_in_event_bus(True) action = FileWalkAction() action.initialize() action.execute()
def test_empty_project_with_core(self, project_loader): project_loader("empty") features.register(CoreFeature()) features.register(SmartcdFeature()) load_registered_features() action = WindowsProjectActivate() action.execute() assert not os.path.exists("ddb_activate.bat") assert not os.path.exists("ddb_deactivate.bat")
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")
def test_run_activate_deactivate_project(self, capsys: CaptureFixture, project_loader): project_loader("project") features.register(CoreFeature()) features.register(ShellFeature()) load_registered_features() action = ActivateAction(self.build_shell_integration()) action.execute() capture = capsys.readouterr() assert capture.out assert not capture.err filepath = re.match(self.filepath_regex, capture.out).group(1) with open(filepath, 'r', encoding='utf-8') as file: script = file.read() export_match = re.findall(self.export_regex, script, re.MULTILINE) env = dict(export_match) system_path = env.get('PATH', '') first = system_path.split(os.pathsep)[0] assert first == os.path.normpath(os.path.join(os.getcwd(), "./bin")) \ or first == os.path.normpath(os.path.join(os.getcwd(), "./.bin")) os.environ.update(env) deactivate_action = DeactivateAction(self.build_shell_integration()) deactivate_action.execute() capture = capsys.readouterr() assert capture.out assert not capture.err filepath = re.match(self.filepath_regex, capture.out).group(1) with open(filepath, 'r', encoding='utf-8') as file: script = file.read() export_match = re.findall(self.export_regex, script, re.MULTILINE) env = dict(export_match) system_path = env.get('PATH', '').split(os.pathsep) first = system_path[0] assert first != os.path.normpath(os.path.join(os.getcwd(), "./bin")) or \ first != os.path.normpath(os.path.join(os.getcwd(), "./.bin"))
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'
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')
def test_extra_services_redirect(self, project_loader): project_loader("extra-services-redirect") features.register(CoreFeature()) features.register(TraefikFeature()) features.register(DockerFeature()) features.register(JsonnetFeature()) load_registered_features() install_action = TraefikExtraServicesAction() install_action.initialize() install_action.execute() api_toml = os.path.join(config.data['traefik']['config_directory'], "sub.project.test.extra-service.api.toml") assert os.path.exists(api_toml) api_toml_expected = Path( os.path.join(config.data['traefik']['config_directory'], "sub.project.test.extra-service.api.expected.toml")) assert api_toml_expected.read_text() == Path(api_toml).read_text() web_toml = os.path.join(config.data['traefik']['config_directory'], "rule.project.test.extra-service.web.toml") assert os.path.exists(web_toml) web_toml_expected = Path( os.path.join(config.data['traefik']['config_directory'], "rule.project.test.extra-service.web.expected.toml")) assert web_toml_expected.read_text().replace( '{{ip}}', config.data.get('docker.debug.host')) == Path( web_toml).read_text() secured_toml = os.path.join( config.data['traefik']['config_directory'], "secured.project.test.extra-service.no-redirect.toml") assert os.path.exists(secured_toml) secured_toml_expected = Path( os.path.join( config.data['traefik']['config_directory'], "secured.project.test.extra-service.no-redirect.expected.toml") ) assert secured_toml_expected.read_text() == Path( secured_toml).read_text()
def test_empty_project_with_core(self, project_loader): project_loader("empty") features.register(CoreFeature()) features.register(GitignoreFeature()) load_registered_features() action = UpdateGitignoreAction() action.execute(target="./to-ignore.yml") assert os.path.exists('.gitignore') assert expect_gitignore('.gitignore', '/to-ignore.yml') action.remove("./to-ignore.yml") assert not os.path.exists('.gitignore')
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)
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)
def test_empty_project_with_core(self, project_loader, mocker: MockerFixture): mocker.patch('ddb.feature.smartcd.actions.is_smartcd_installed', lambda: True) project_loader("empty") features.register(CoreFeature()) features.register(SmartcdFeature()) load_registered_features() action = SmartcdAction() action.execute() assert not os.path.exists(".bash_enter") assert not os.path.exists(".bash_leave")
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')
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