def test_nodeTraversal2(self): root = NodeInstance("root", {"a": [{"ref": "::child"}, {"b": 2}]}) child = NodeInstance("child", {"b": 1}, root) self.assertEqual(Ref(".ancestors").resolve(RefContext(child)), [[child, root]]) # self.assertEqual(Ref('a::b').resolve(RefContext(root)), [1]) self.assertEqual(Ref("a").resolve(RefContext(child)), [[child, {"b": 2}]]) # a resolves to [child, dict] so a::b resolves to [child[b], [b]2] self.assertEqual(Ref("a::b").resolve(RefContext(child)), [1, 2])
def test_interface(self): r = NodeInstance("test") r.addInterface(TestInterface) className = __name__ + ".TestInterface" self.assertEqual(r.attributes[".interfaces"], {className: className}) i = r.getInterface(className) assert i, "interface not found" self.assertIs(r, i.resource) self.assertEqual(i.name, className) self.assertIs(r._interfaces[className], i)
def map_value(self, val: Any) -> Any: """ Evaluate using project home as a base dir. """ from .runtime import NodeInstance from .eval import map_value instance = NodeInstance() instance._baseDir = self.config.config.get_base_dir() return map_value(val, instance)
def test_nodeTraversal1(self): root = NodeInstance( "r2", { "a": [dict(ref="::r1::a"), dict(ref="b")], "b": "r2" } #'r1' #'r2' ) child = NodeInstance("r1", {"a": dict(ref="b"), "b": "r1"}, root) ctx = RefContext(root) x = [{"a": [{"c": 1}, {"c": 2}]}] assert x == ResultsList(x, ctx) self.assertEqual(Ref("b").resolve(RefContext(child)), ["r1"]) self.assertEqual(Ref("a").resolve(RefContext(child)), ["r1"]) self.assertEqual(Ref("a").resolve(RefContext(root)), [["r1", "r2"]])
def test_innerReferences(self): resourceDef = { "a": dict(b={"ref": "a::c"}, c={"e": 1}, d=["2", {"ref": "a::d::0"}]) } resource = NodeInstance("test", attributes=resourceDef) assert not not resource.attributes self.assertEqual(len(resource.attributes), 1) expectedA = {"c": {"e": 1}, "b": {"e": 1}, "d": ["2", "2"]} self.assertEqual(resource.attributes["a"]["b"], expectedA["b"]) self.assertEqual(resource.attributes["a"], expectedA) self.assertEqual(Ref("a").resolve(RefContext(resource)), [expectedA]) self.assertEqual(Ref("a").resolve_one(RefContext(resource)), expectedA) expected = ["2"] self.assertEqual(Ref("a::d::0").resolve(RefContext(resource)), expected) self.assertEqual(Ref("a::d::1").resolve(RefContext(resource)), expected) # print('test_references', resource.attributes, # 'AAAA', resource.attributes['a'], # 'BBB', resource.attributes['a']['b'], # ) self.assertEqual(resource.attributes["a"], expectedA) self.assertEqual(resource.attributes["a"]["d"][0], "2") self.assertEqual(resource.attributes["a"]["d"][1], "2") self.assertEqual(resource.attributes["a"]["b"]["e"], 1) self.assertEqual(Ref("a::b::e").resolve(RefContext(resource)), [1]) # test again to make sure it still resolves correctly self.assertEqual(Ref("a::d::0").resolve(RefContext(resource)), expected) self.assertEqual(Ref("a::d::1").resolve(RefContext(resource)), expected)
def _getTestResource(self, more=None): resourceDef = { "name": "test", "a": {"ref": "name"}, "b": [1, 2, 3], "d": {"a": "va", "b": "vb"}, "n": {"n": {"n": "n"}}, "s": {"ref": "."}, "x": [ { "a": [ {"c": 1}, {"c": 2}, {"b": "exists"}, {"l": ["l1"]}, {"l": ["l2"]}, ] }, [{"c": 5}], {"a": [{"c": 3}, {"c": 4}, {"l": ["l3"]}, {"l": ["l4"]}]}, [{"c": 6}], ], "e": {"a1": {"b1": "v1"}, "a2": {"b2": "v2"}}, "f": {"a": 1, "b": {"ref": ".::f::a"}}, } if more: resourceDef.update(more) resource = NodeInstance("test", attributes=resourceDef) assert resource.attributes["x"] == resourceDef["x"] assert resource.attributes["a"] == "test" assert resource.attributes["s"] is resource return resource
def test_jinjaTemplate(self): resource = NodeInstance("test", attributes=dict(a1="hello")) ctx = RefContext(resource, {"foo": "hello"}) self.assertEqual(applyTemplate(" {{ foo }} ", ctx), "hello") # test jinja2 native types self.assertEqual(applyTemplate(" {{[foo]}} ", ctx), ["hello"]) self.assertEqual(applyTemplate(' {{ "::test::a1" | ref }} ', ctx), u"hello") self.assertEqual( applyTemplate(' {{ lookup("unfurl", "::test::a1") }} ', ctx), u"hello") # ansible query() always returns a list self.assertEqual( applyTemplate('{{ query("unfurl", "::test::a1") }}', ctx), [u"hello"]) os.environ["TEST_ENV"] = ( "testEnv" ) # note: tox doesn't pass on environment variables so we need to set one now self.assertEqual(mapValue("{{ lookup('env', 'TEST_ENV') }}", resource), "testEnv") # test that ref vars as can be used as template string vars exp = {"a": "{{ aVar }} world"} vars = {"aVar": "hello"} self.assertEqual(mapValue(exp, RefContext(resource, vars)), {"a": "hello world"}) vars = {"foo": {"bar": sensitive_str("sensitive")}} val = applyTemplate("{{ foo.bar }}", RefContext(resource, vars, trace=0)) assert isinstance(val, sensitive_str), type(val)
def create_local_instance(self, localName, attributes): # local or secret from .runtime import NodeInstance if "default" in attributes: if not "default" in attributes.get(".interfaces", {}): attributes.setdefault(".interfaces", {})[ "default" ] = "unfurl.support.DelegateAttributes" if "inheritFrom" in attributes: if not "inherit" in attributes.get(".interfaces", {}): attributes.setdefault(".interfaces", {})[ "inherit" ] = "unfurl.support.DelegateAttributes" instance = NodeInstance(localName, attributes) instance._baseDir = self.config.get_base_dir() return instance
def test_nodeTraversal1(self): root = NodeInstance( "r2", {"a": [dict(ref="::r1::a"), dict(ref="b")], "b": "r2"} #'r1' #'r2' ) child = NodeInstance("r1", {"a": dict(ref="b"), "b": "r1"}, root) ctx = RefContext(root) x = [{"a": [{"c": 1}, {"c": 2}]}] r1 = ResultsList(x, ctx) assert x == r1 self.assertEqual(Ref("b").resolve(RefContext(child)), ["r1"]) self.assertEqual(Ref("a").resolve(RefContext(child)), ["r1"]) self.assertEqual(Ref("a").resolve(RefContext(root)), [["r1", "r2"]]) assert not r1._haskey(1) r1.append("added") assert r1._haskey(1) r1[0]["a"][1] = "not c" assert r1[0]["a"][1] == "not c"