def test_yaml_diff(root): from batou import output from batou._output import TestBackend output.backend = TestBackend() p = YAMLContent("target.yaml", data={"asdf": 1, "bsdf": 2}) root.component += p with open(p.path, "w") as f: assert f.write(json.dumps({"bsdf": 2}, sort_keys=True, indent=4)) p.deploy() # fmt: off assert output.backend.output == Ellipsis("""\ host > MyComponent > YAMLContent(\'work/mycomponent/target.yaml\') target.yaml --- target.yaml +++ target.yaml @@ -1,3 +1,2 @@ target.yaml -{ target.yaml - "bsdf": 2 target.yaml -} target.yaml +asdf: 1 target.yaml +bsdf: 2 """)
def test_yaml_content_source_with_override(root): with open(root.defdir + "/source.yaml", "w", encoding="utf-8") as f: f.write( yaml.safe_dump({ "database": { "address": "localhost", "password": "******"}})) p = YAMLContent( "target.yaml", source="source.yaml", override={"database": { "password": "******"}}, ) root.component += p root.component.deploy() # fmt: off assert (p.content == b"""\ database: address: localhost password: realpassword """) # fmt: on with open(p.path, "rb") as f: assert f.read() == p.content
def test_yaml_diff_not_for_sensitive(output, root): p = YAMLContent("target.yaml", data={ "asdf": 1, "bsdf": 2 }, sensitive_data=True) root.component += p with open(p.path, "w") as f: assert f.write(json.dumps({"bsdf": 2}, sort_keys=True, indent=4)) p.deploy() assert output.backend.output == Ellipsis("""\ host > MyComponent > YAMLContent('work/mycomponent/target.yaml') Not showing diff as it contains sensitive data. """)
def test_yaml_content_data_given(root): p = YAMLContent("target.yaml", data={"asdf": 1}) root.component += p root.component.deploy() assert (p.content == b"""\ asdf: 1 """) with open(p.path, "rb") as f: assert f.read() == p.content
def test_yaml_content_source_given(root): with open(root.defdir + "/source.json", "w", encoding="utf-8") as f: f.write(json.dumps([1, 2, 3, 4])) p = YAMLContent("target.json", source="source.json") root.component += p root.component.deploy() # fmt: off assert p.content == b"""\ - 1 - 2 - 3 - 4 """ # fmt: on with open(p.path, "rb") as f: assert f.read() == p.content
def test_yaml_content_delayed_source_given(root): p = YAMLContent("target.yaml", source="source.yaml") root.component += p with open(root.defdir + "/source.yaml", "w", encoding="utf-8") as f: f.write(yaml.safe_dump([1, 2, 3, 4])) root.component.deploy() # fmt: off assert p.content == b"""\ - 1 - 2 - 3 - 4 """ # fmt: on with open(p.path, "rb") as f: assert f.read() == p.content
def test_yaml_content_explicit_absolute_source(root): p = YAMLContent("target.json", source="/tmp/thesource.json") root.component += p assert p.source == "/tmp/thesource.json"
def test_yaml_content_explicit_source(root): p = YAMLContent("target.json", source="thesource.json") root.component += p assert p.source.endswith("/thesource.json")
def test_yaml_content_either_source_or_data(root): p = YAMLContent("target.json", data={"asdf": 1}, source="asdf") with pytest.raises(ValueError): root.component += p
def test_yaml_content_source_missing(root): p = YAMLContent("target.json", source="source.json") root.component += p with pytest.raises(FileNotFoundError): root.component.deploy()
def test_yaml_content_delayed_source_causes_predicting_verify_to_raise(root): p = YAMLContent("target.json", source="source.json") root.component += p with pytest.raises(AssertionError): p.verify(predicting=True)