def test_variable_resolve_nested_lookup(self): stack = Stack(definition=generate_definition("vpc", 1), context=self.context) stack.set_outputs({ "FakeOutput": "resolved", "FakeOutput2": "resolved2", }) def mock_handler(value, context, provider, **kwargs): return "looked up: {}".format(value) register_lookup_handler("lookup", mock_handler) self.context.get_stack.return_value = stack var = Variable( "Param1", "${lookup ${lookup ${output fakeStack::FakeOutput}}}", ) self.assertEqual( len(var.lookups), 1, "should only parse out the first complete lookup first", ) var.resolve(self.context, self.provider) self.assertTrue(var.resolved) self.assertEqual(var.value, "looked up: looked up: resolved")
def setUp(self): self.sd = {"name": "test"} self.config = Config({"namespace": "namespace"}) self.context = Context(config=self.config) self.stack = Stack( definition=generate_definition("vpc", 1), context=self.context, ) register_lookup_handler("noop", lambda **kwargs: "test")
def setUp(self): self.sd = {"name": "test"} self.config = Config({"namespace": "namespace"}) self.context = Context(config=self.config) self.stack = Stack( definition=generate_definition("vpc", 1), context=self.context, ) register_lookup_handler("noop", lambda **kwargs: "test")
def test_resolve_variables_lookup_returns_non_string_invalid_combo(self): class TestBlueprint(Blueprint): VARIABLES = { "Param1": {"type": list}, } def return_list_something(*_args, **_kwargs): return ["something"] register_lookup_handler("custom", return_list_something) variable = Variable( "Param1", "${custom non-string-return-val},${output some-stack::Output}", ) variable._value[0].resolve({}, {}) with self.assertRaises(InvalidLookupCombination): variable.value()
def test_resolve_variables_lookup_returns_non_string(self): class TestBlueprint(Blueprint): VARIABLES = { "Param1": {"type": list}, } def return_list_something(*_args, **_kwargs): return ["something"] register_lookup_handler("custom", return_list_something) blueprint = TestBlueprint(name="test", context=MagicMock()) variables = [Variable("Param1", "${custom non-string-return-val}")] for var in variables: var._value.resolve({}, {}) blueprint.resolve_variables(variables) self.assertEqual(blueprint.resolved_variables["Param1"], ["something"])
def test_variable_resolve_nested_lookup(self): def mock_handler(value, context, provider, **kwargs): return "looked up: {}".format(value) register_lookup_handler("lookup", mock_handler) self.provider.get_output.return_value = "resolved" var = Variable( "Param1", "${lookup ${lookup ${output fakeStack::FakeOutput}}}", ) self.assertEqual( len(var.lookups), 1, "should only parse out the first complete lookup first", ) var.resolve(self.context, self.provider) self.assertTrue(var.resolved) self.assertEqual(var.value, "looked up: looked up: resolved")
def test_resolve_variables_lookup_returns_troposphere_obj(self): class TestBlueprint(Blueprint): VARIABLES = { "Param1": {"type": Base64}, } def return_obj(*_args, **_kwargs): return Base64("test") register_lookup_handler("custom", return_obj) blueprint = TestBlueprint(name="test", context=MagicMock()) variables = [Variable("Param1", "${custom non-string-return-val}")] for var in variables: var._value.resolve({}, {}) blueprint.resolve_variables(variables) self.assertEqual(blueprint.resolved_variables["Param1"].data, Base64("test").data)
def test_resolve_variables_lookup_returns_non_string_invalid_combo(self): class TestBlueprint(Blueprint): VARIABLES = { "Param1": { "type": list }, } def return_list_something(*_args, **_kwargs): return ["something"] register_lookup_handler("custom", return_list_something) variable = Variable( "Param1", "${custom non-string-return-val},${output some-stack::Output}", ) variable._value[0].resolve({}, {}) with self.assertRaises(InvalidLookupCombination): variable.value()
def test_resolve_variables_lookup_returns_non_string(self): class TestBlueprint(Blueprint): VARIABLES = { "Param1": { "type": list }, } def return_list_something(*_args, **_kwargs): return ["something"] register_lookup_handler("custom", return_list_something) blueprint = TestBlueprint(name="test", context=MagicMock()) variables = [Variable("Param1", "${custom non-string-return-val}")] for var in variables: var._value.resolve({}, {}) blueprint.resolve_variables(variables) self.assertEqual(blueprint.resolved_variables["Param1"], ["something"])
def test_resolve_variables_lookup_returns_troposphere_obj(self): class TestBlueprint(Blueprint): VARIABLES = { "Param1": { "type": Base64 }, } def return_obj(*_args, **_kwargs): return Base64("test") register_lookup_handler("custom", return_obj) blueprint = TestBlueprint(name="test", context=MagicMock()) variables = [Variable("Param1", "${custom non-string-return-val}")] for var in variables: var._value.resolve({}, {}) blueprint.resolve_variables(variables) self.assertEqual(blueprint.resolved_variables["Param1"].data, Base64("test").data)
def test_variable_resolve_nested_lookup(self): stack = Stack( definition=generate_definition("vpc", 1), context=self.context) stack.set_outputs({ "FakeOutput": "resolved", "FakeOutput2": "resolved2", }) def mock_handler(value, context, provider, **kwargs): return "looked up: {}".format(value) register_lookup_handler("lookup", mock_handler) self.context.get_stack.return_value = stack var = Variable( "Param1", "${lookup ${lookup ${output fakeStack::FakeOutput}}}", ) var.resolve(self.context, self.provider) self.assertTrue(var.resolved) self.assertEqual(var.value, "looked up: looked up: resolved")
InvalidUserdataPlaceholder) from stacker.variables import Variable from stacker.lookups import register_lookup_handler from ..factories import mock_lookup def mock_lookup_handler(value, provider=None, context=None, fqn=False, **kwargs): return value register_lookup_handler("mock", mock_lookup_handler) class TestBuildParameter(unittest.TestCase): def test_base_parameter(self): p = build_parameter("BasicParam", {"type": "String"}) p.validate() self.assertEquals(p.Type, "String") class TestBlueprintRendering(unittest.TestCase): def test_to_json(self): class TestBlueprint(Blueprint): VARIABLES = { "Param1": { "default": "default",
ValidatorError, VariableTypeRequired, InvalidUserdataPlaceholder ) from stacker.variables import Variable from stacker.lookups import register_lookup_handler from ..factories import mock_context def mock_lookup_handler(value, provider=None, context=None, fqn=False, **kwargs): return value register_lookup_handler("mock", mock_lookup_handler) class TestBuildParameter(unittest.TestCase): def test_base_parameter(self): p = build_parameter("BasicParam", {"type": "String"}) p.validate() self.assertEquals(p.Type, "String") class TestBlueprintRendering(unittest.TestCase): def test_to_json(self): class TestBlueprint(Blueprint): VARIABLES = {