def test_config_build(self): vpc = Stack({"name": "vpc", "class_path": "blueprints.VPC"}) config = Config({"namespace": "prod", "stacks": [vpc]}) self.assertEquals(config.namespace, "prod") self.assertEquals(config.stacks[0].name, "vpc") self.assertEquals(config["namespace"], "prod") config.validate()
def test_config_validate_missing_stack_class_path(self): config = Config({"namespace": "prod", "stacks": [{"name": "bastion"}]}) with self.assertRaises(exceptions.InvalidConfig) as ex: config.validate() error = ex.exception.errors['stacks'][0]['class_path'].errors[0] self.assertEquals(error.__str__(), "This field is required.")
def test_config_validate_missing_stack_source_when_locked(self): config = Config({ "namespace": "prod", "stacks": [ { "name": "bastion", "locked": True}]}) config.validate()
def test_context_bucket_name_is_overriden_but_is_none(self): config = Config({"namespace": "test", "stacker_bucket": ""}) context = Context(config=config) self.assertEqual(context.bucket_name, None) config = Config({"namespace": "test", "stacker_bucket": None}) context = Context(config=config) self.assertEqual(context.bucket_name, "stacker-test")
def test_config_validate_no_stacks(self): config = Config({"namespace": "prod"}) with self.assertRaises(exceptions.InvalidConfig) as ex: config.validate() error = ex.exception.errors['stacks'].errors[0] self.assertEquals(error.__str__(), "Should have more than one element.")
def test_config_validate_missing_stack_source(self): config = Config({"namespace": "prod", "stacks": [{"name": "bastion"}]}) with self.assertRaises(exceptions.InvalidConfig) as ex: config.validate() stack_errors = ex.exception.errors['stacks'][0] self.assertEquals(stack_errors['template_path'][0].__str__(), "class_path or template_path is required.") self.assertEquals(stack_errors['class_path'][0].__str__(), "class_path or template_path is required.")
def test_context_default_bucket_no_namespace(self): context = Context(config=Config({"namespace": ""})) self.assertEqual(context.bucket_name, None) context = Context(config=Config({"namespace": None})) self.assertEqual(context.bucket_name, None) context = Context( config=Config({"namespace": None, "stacker_bucket": ""})) self.assertEqual(context.bucket_name, None)
def test_config_validate_missing_name(self): config = Config({ "namespace": "prod", "stacks": [ { "class_path": "blueprints.Bastion"}]}) with self.assertRaises(exceptions.InvalidConfig) as ex: config.validate() error = ex.exception.errors['stacks'][0]['name'].errors[0] self.assertEquals( error.__str__(), "This field is required.")
def test_dump_unicode(self): config = Config() config.namespace = "test" self.assertEquals(dump(config), b"""namespace: test stacks: [] """) config = Config({"namespace": "test"}) # Ensure that we're producing standard yaml, that doesn't include # python specific objects. self.assertNotEquals( dump(config), b"namespace: !!python/unicode 'test'\n") self.assertEquals(dump(config), b"""namespace: test stacks: [] """)
def test_lookup_with_sys_path(self): config = Config({ "sys_path": "stacker/tests", "lookups": { "custom": "fixtures.mock_lookups.handler"}}) load(config) self.assertTrue(callable(LOOKUP_HANDLERS["custom"]))
def test_config_validate_missing_stack_source(self): config = Config({ "namespace": "prod", "stacks": [ { "name": "bastion"}]}) with self.assertRaises(exceptions.InvalidConfig) as ex: config.validate() stack_errors = ex.exception.errors['stacks'][0] self.assertEquals( stack_errors['template_path'][0].__str__(), "class_path or template_path is required.") self.assertEquals( stack_errors['class_path'][0].__str__(), "class_path or template_path is required.")
def setUp(self): config = Config({ "namespace": "namespace", "stacks": [ { "name": "vpc" }, { "name": "bastion", "requires": ["vpc"] }, { "name": "instance", "requires": ["vpc", "bastion"] }, { "name": "db", "requires": ["instance", "vpc", "bastion"] }, { "name": "other", "requires": ["db"] }, ], }) self.context = Context(config=config) self.action = destroy.Action(self.context, provider=mock.MagicMock())
def test_dump_complex(self): config = Config({ "namespace": "prod", "stacks": [ Stack({ "name": "vpc", "class_path": "blueprints.VPC"}), Stack({ "name": "bastion", "class_path": "blueprints.Bastion", "requires": ["vpc"]})]}) self.assertEqual(dump(config), b"""namespace: prod stacks: - class_path: blueprints.VPC enabled: true locked: false name: vpc protected: false - class_path: blueprints.Bastion enabled: true locked: false name: bastion protected: false requires: - vpc """)
def mock_context(namespace=None, **kwargs): config = Config({"namespace": namespace}) environment = kwargs.get("environment", {}) return Context( config=config, environment=environment, **kwargs)
def construct_context(subnet_id): config_dict = { "namespace": "", "namespace_delimiter": "", "stacker_bucket": "", "bucket_region": "ap-southeast-2", "region": "ap-southeast-2", "stacks": [ { "name": "ebs-pin-test", "template_path": "./e2e/cloudformation.yml", "variables": { "AvailabilityZone": "ap-southeast-2a", "AMI": ami, "VpcId": vpc_id, "Subnets": subnet_id, "KeyName": "id_rsa" }, "tags": { "Name": "ebs-pin-test" } } ] } config = Config(config_dict) context = Context(config=config) return context
def test_context_optional_keys_set(self): context = Context( config=Config({}), stack_names=["stack"], ) self.assertEqual(context.mappings, {}) self.assertEqual(context.stack_names, ["stack"])
def mock_context(namespace="default", extra_config_args=None, **kwargs): config_args = {"namespace": namespace} if extra_config_args: config_args.update(extra_config_args) config = Config(config_args) environment = kwargs.get("environment", {}) return Context(config=config, environment=environment, **kwargs)
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, )
def test_config_validate_duplicate_stack_names(self): config = Config({ "namespace": "prod", "stacks": [ { "name": "bastion", "class_path": "blueprints.Bastion"}, { "name": "bastion", "class_path": "blueprints.BastionV2"}]}) with self.assertRaises(exceptions.InvalidConfig) as ex: config.validate() error = ex.exception.errors['stacks'][0] self.assertEquals( error.__str__(), "Duplicate stack bastion found at index 0.")
def setUp(self): self.ctx = Context(config=Config({'namespace': 'test'})) self.common_variables = { "VpcId": "vpc-abc1234", "VpcDefaultSecurityGroup": "sg-01234abc", "AvailabilityZone": "us-east-1a", "CidrBlock": "10.0.0.0/24", }
def setUp(self): self.ctx = Context(config=Config({'namespace': 'test'})) self.common_variables = { "VPC": { VPC_NAME: { "CidrBlock": "10.0.0.0/16" } } }
def test_config_validate_stack_class_and_template_paths(self): config = Config({ "namespace": "prod", "stacks": [ { "name": "bastion", "class_path": "foo", "template_path": "bar"}]}) with self.assertRaises(exceptions.InvalidConfig) as ex: config.validate() stack_errors = ex.exception.errors['stacks'][0] self.assertEquals( stack_errors['template_path'][0].__str__(), "class_path cannot be present when template_path is provided.") self.assertEquals( stack_errors['class_path'][0].__str__(), "template_path cannot be present when class_path is provided.")
def setUp(self): self.config = Config({ "namespace": "namespace", "stacks": [{ "name": "stack1" }, { "name": "stack2" }] })
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 __init__(self, environment=None, stack_names=None, config=None, force_stacks=None): self.environment = environment self.stack_names = stack_names or [] self.config = config or Config() self.force_stacks = force_stacks or [] self.hook_data = {}
def _get_context(self, **kwargs): config = Config({ "namespace": "namespace", "stacks": [ {"name": "vpc"}, {"name": "bastion", "variables": {"test": "${output vpc::something}"}}, {"name": "db", "variables": {"test": "${output vpc::something}", "else": "${output bastion::something}"}}, {"name": "other", "variables": {}} ] }) return Context(config=config, **kwargs)
def test_Raises_StackDoesNotExist_from_lookup_non_included_stack(self): # This test is testing the specific scenario listed in PR 466 # Because the issue only threw a KeyError when a stack was missing # in the `--stacks` flag at runtime of a `stacker build` run # but needed for an output lookup in the stack specified mock_provider = mock.MagicMock() context = Context(config=Config({ "namespace": "namespace", "stacks": [ {"name": "bastion", "variables": {"test": "${output vpc::something}"} }] })) build_action = build.Action(context, provider=mock_provider) with self.assertRaises(StackDoesNotExist): build_action._generate_plan()
def setUp(self): self.context = Context(config=Config({ 'namespace': 'test', 'stacker_bucket': 'test' })) self.provider = mock_provider(region="us-east-1") self.mock_process = MockProcess() self.popen_mock = \ mock.patch('stacker.hooks.command.Popen', return_value=self.mock_process).start() self.devnull = mock.Mock() self.devnull_mock = \ mock.patch('stacker.hooks.command._devnull', return_value=self.devnull).start()
def test_hook_with_sys_path(self): config = Config({ "namespace": "test", "sys_path": "stacker/tests", "pre_build": [ { "data_key": "myHook", "path": "fixtures.mock_hooks.mock_hook", "required": True, "args": { "value": "mockResult"}}]}) load(config) context = Context(config=config) stage = "pre_build" handle_hooks(stage, context.config[stage], "mock-region-1", context) self.assertEqual("mockResult", context.hook_data["myHook"]["result"])
def setUp(self): self.code = Code(S3Bucket="test_bucket", S3Key="code_key") self.common_variables = { "Code": self.code, "DeadLetterArn": "arn:aws:sqs:us-east-1:12345:dlq", "Description": "Test function.", "Environment": { "Env1": "Value1" }, "Handler": "handler", "KmsKeyArn": "arn:aws:kms:us-east-1:12345:key", "MemorySize": 128, "Runtime": "python2.7", "Timeout": 3, } self.ctx = Context(config=Config({'namespace': 'test'}))
def setUp(self): self.launch_config = { "ImageId": "i-abc1234", "InstanceType": "m3.medium", "KeyName": "mock_ssh_key", "SecurityGroups": ["sg-abc1234", "sg-bcd2345"], } self.asg_config = { "MinSize": 1, "MaxSize": 3, } self.common_variables = { "LaunchConfiguration": { "LaunchConfiguration": self.launch_config, }, "AutoScalingGroup": { "AutoScalingGroup": self.asg_config }, } self.ctx = Context(config=Config({"namespace": "test"}))
def test_load_adds_sys_path(self): config = Config({"sys_path": "/foo/bar"}) load(config) self.assertIn("/foo/bar", sys.path)