def test_kube_merge(): docs = list(yaml.load_all(StringIO(MERGE_TEST), yaml.Loader)) overrides = list(yaml.load_all(StringIO(MERGE_CHANGES), yaml.Loader)) base_overrides = list( yaml.load_all(StringIO(MERGE_CHANGES), yaml.BaseLoader)) expected = list(yaml.load_all(StringIO(MERGE_EXPECTED), yaml.Loader)) merged = merge_docs(docs, overrides, base_overrides) for i, merged_doc in enumerate(merged): expected_doc = expected[i] merged_json = json.dumps(merged_doc, **JSON_FORMAT) expected_json = json.dumps(expected_doc, **JSON_FORMAT) print(f"Doc {i + 1} expected: {expected_json}") print(f"Doc {i + 1} actual : {merged_json}") assert merged_json == expected_json
def _prepare_configs(self, dst: Path): dst = dst / self.path kube_dst = dst / "kube" kube_dst.mkdir(mode=0o700, parents=True) logger.info(f"Writing configs to {dst}") dockerfile = self.path / "Dockerfile" if dockerfile.exists(): logger.info("Copying Dockerfile") copy(dockerfile, dst / "Dockerfile") for config in self.kube_configs: config_file = self.path / "kube" / config src = self.kube_configs[config] # Incl. env patch logger.info(f"Patching {config_file}") with src.open("r") as f: docs = list(yaml.load_all(f, Loader)) self._patch_yaml_docs(docs) if config in self.kube_merges: # Use the Loader to get the values with the actual types. with self.kube_merges[config].open("r") as f: overrides = list(yaml.load_all(f, Loader)) # Use the BaseLoader to get literal values, such as tilde (~). with self.kube_merges[config].open("r") as f: base_overrides = list(yaml.load_all(f, BaseLoader)) docs = merge_docs(docs, overrides, base_overrides) dst_path = kube_dst / config with dst_path.open("w") as config_dst: yaml.dump_all(docs, stream=config_dst, Dumper=Dumper) self.kube_configs[config] = dst_path self.path = dst