def test_resolve_variables_lookup_returns_troposphere_obj(self): """Test resolve variables lookup returns troposphere obj.""" class TestBlueprint(Blueprint): """Test blueprint.""" VARIABLES = { "Param1": { "type": Base64 }, } def return_obj(*_args, **_kwargs): """Return object.""" return Base64("test") register_lookup_handler("custom", return_obj) blueprint = TestBlueprint(name="test", context=MagicMock()) variables = [ Variable("Param1", "${custom non-string-return-val}", 'cfngin') ] 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(self): """Test resolve variables lookup returns non string.""" class TestBlueprint(Blueprint): """Test blueprint.""" VARIABLES = { "Param1": { "type": list }, } def return_list_something(*_args, **_kwargs): """Return list something.""" return ["something"] register_lookup_handler("custom", return_list_something) blueprint = TestBlueprint(name="test", context=MagicMock()) variables = [ Variable("Param1", "${custom non-string-return-val}", 'cfngin') ] for var in variables: var._value.resolve({}, {}) blueprint.resolve_variables(variables) self.assertEqual(blueprint.resolved_variables["Param1"], ["something"])
def setUp(self): """Run before tests.""" self.sd = {"name": "test"} # pylint: disable=invalid-name 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): """Test resolve variables lookup returns non string invalid combo.""" def return_list_something(*_args, **_kwargs): """Return list something.""" return ["something"] register_lookup_handler("custom", return_list_something) variable = Variable( "Param1", "${custom non-string-return-val},${output some-stack::Output}", 'cfngin') variable._value[0].resolve({}, {}) with self.assertRaises(InvalidLookupCombination): variable.value() # pylint: disable=not-callable
def test_variable_resolve_nested_lookup(self): """Test variable resolve nested lookup.""" 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")
UnresolvedVariables, ValidatorError, VariableTypeRequired, ) from runway.cfngin.lookups import register_lookup_handler from runway.variables import Variable from ..factories import mock_context def mock_lookup_handler(value, provider=None, context=None, fqn=False, **kwargs): """Mock lookup handler.""" return value register_lookup_handler("mock", mock_lookup_handler) class TestBuildParameter(unittest.TestCase): """Tests for runway.cfngin.blueprints.base.build_parameter.""" def test_base_parameter(self): """Test base parameter.""" param = build_parameter("BasicParam", {"type": "String"}) param.validate() self.assertEqual(param.Type, "String") class TestBlueprintRendering(unittest.TestCase): """Tests for runway.cfngin.blueprints.base.Blueprint rendering."""