def test_context_bucket_name_is_overridden_but_is_none(self): """Test context bucket name is overridden but is none.""" config = Config({"namespace": "test", "cfngin_bucket": ""}) context = Context(config=config) self.assertEqual(context.bucket_name, None) config = Config({"namespace": "test", "cfngin_bucket": None}) context = Context(config=config) self.assertEqual(context.bucket_name, "stacker-test")
def mock_context(namespace="default", extra_config_args=None, **kwargs): """Mock context.""" config_args = {"namespace": namespace} if extra_config_args: config_args.update(extra_config_args) config = Config(config_args) if kwargs.get("environment"): return Context(config=config, **kwargs) return Context(config=config, environment={}, **kwargs)
def test_context_default_bucket_no_namespace(self): """Test context default bucket no namespace.""" 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, "cfngin_bucket": "" })) self.assertEqual(context.bucket_name, None)
def test_persistent_graph_no_object(self): """Create object if one does not exist and return empty Graph.""" context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True stubber = Stubber(context.s3_conn) expected_get_params = {'ResponseContentType': 'application/json'} expected_get_params.update(context.persistent_graph_location) expected_put_params = { 'Body': '{}', 'ServerSideEncryption': 'AES256', 'ACL': 'bucket-owner-full-control', 'ContentType': 'application/json' } expected_put_params.update(context.persistent_graph_location) stubber.add_client_error('get_object', 'NoSuchKey', expected_params=expected_get_params) stubber.add_response('put_object', {}, expected_put_params) with stubber: self.assertIsNone(context._persistent_graph) self.assertIsInstance(context.persistent_graph, Graph) self.assertIsInstance(context._persistent_graph, Graph) self.assertEqual({}, context.persistent_graph.to_dict()) stubber.assert_no_pending_responses()
def test_put_persistent_graph(self): """Return 'None' when put is successful.""" code = '0000' context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True graph_dict = {'stack1': [], 'stack2': ['stack1']} context._persistent_graph = Graph.from_dict(graph_dict, context) stubber = Stubber(context.s3_conn) expected_params = { 'Body': json.dumps(graph_dict, indent=4), 'ServerSideEncryption': 'AES256', 'ACL': 'bucket-owner-full-control', 'ContentType': 'application/json', 'Tagging': '{}={}'.format(context._persistent_graph_lock_tag, code) } expected_params.update(context.persistent_graph_location) stubber.add_response( 'get_object_tagging', {'TagSet': gen_tagset({context._persistent_graph_lock_tag: code})}, context.persistent_graph_location) stubber.add_response('put_object', {}, expected_params) with stubber: self.assertIsNone(context.put_persistent_graph(code)) stubber.assert_no_pending_responses()
def test_persistent_graph_location_no_bucket(self): """Return an empty dict if key is set but no bucket name.""" cp_config = self.persist_graph_raw_config.copy() cp_config["cfngin_bucket"] = "" context = Context(config=Config(cp_config)) self.assertEqual({}, context.persistent_graph_location)
def setUp(self): """Run before tests.""" self.context = Context(config=Config({"namespace": "namespace"})) self.provider = MockProvider() self.build_action = build.Action(self.context, provider_builder=MockProviderBuilder( self.provider))
def test_lock_persistent_graph_no_object(self): """Error raised when when there is no object to lock.""" code = "0000" context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True context._persistent_graph = Graph() stubber = Stubber(context.s3_conn) expected_params = { "Tagging": { "TagSet": gen_tagset({context._persistent_graph_lock_tag: code}) } } expected_params.update(context.persistent_graph_location) stubber.add_client_error( "get_object_tagging", "NoSuchKey", expected_params=context.persistent_graph_location, ) stubber.add_client_error("put_object_tagging", "NoSuchKey", expected_params=expected_params) with stubber: with self.assertRaises(PersistentGraphCannotLock): context.lock_persistent_graph(code) stubber.assert_no_pending_responses()
def _get_context(self, extra_config_args=None, **kwargs): """Get context.""" 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"] }, ] } if extra_config_args: config.update(extra_config_args) return Context(config=Config(config), **kwargs)
def setUp(self): """Run before tests.""" self.context = Context(config=Config({ 'namespace': 'test', 'stacker_bucket': 'test' })) self.provider = mock_provider(region="us-east-1")
def setUp(self): """Run before tests.""" self.context = Context(config=Config({ "namespace": "test", "stacker_bucket": "test" })) self.provider = mock_provider(region="us-east-1")
def test_s3_bucket_does_not_exist_us_west(self): """Create S3 bucket with loc constraints when it does not exist.""" region = "us-west-1" context = Context(config=self.config, region=region) stubber = Stubber(context.s3_conn) stubber.add_client_error( "head_bucket", service_error_code="NoSuchBucket", service_message="Not Found", http_status_code=404, ) stubber.add_response( "create_bucket", service_response={}, expected_params={ "Bucket": ANY, "CreateBucketConfiguration": { "LocationConstraint": region }, }, ) with stubber: self.assertIsNone(context._s3_bucket_verified) self.assertTrue(context.s3_bucket_verified) self.assertTrue(context._s3_bucket_verified) stubber.assert_no_pending_responses()
def test_context_get_stacks_dict_use_fqn(self): """Test context get stacks dict use fqn.""" context = Context(config=self.config) stacks_dict = context.get_stacks_dict() stack_names = sorted(stacks_dict.keys()) self.assertEqual(stack_names[0], "namespace-stack1") self.assertEqual(stack_names[1], "namespace-stack2")
def test_persistent_graph_no_object(self): """Create object if one does not exist and return empty Graph.""" context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True stubber = Stubber(context.s3_conn) expected_get_params = {"ResponseContentType": "application/json"} expected_get_params.update(context.persistent_graph_location) expected_put_params = { "Body": "{}", "ServerSideEncryption": "AES256", "ACL": "bucket-owner-full-control", "ContentType": "application/json", } expected_put_params.update(context.persistent_graph_location) stubber.add_client_error("get_object", "NoSuchKey", expected_params=expected_get_params) stubber.add_response("put_object", {}, expected_put_params) with stubber: self.assertIsNone(context._persistent_graph) self.assertIsInstance(context.persistent_graph, Graph) self.assertIsInstance(context._persistent_graph, Graph) self.assertEqual({}, context.persistent_graph.to_dict()) stubber.assert_no_pending_responses()
def test_lock_persistent_graph_locked(self): """Error raised when when object is locked.""" code = "0000" context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True context._persistent_graph = Graph() stubber = Stubber(context.s3_conn) expected_params = { "Tagging": { "TagSet": gen_tagset({context._persistent_graph_lock_tag: code}) } } expected_params.update(context.persistent_graph_location) stubber.add_response( "get_object_tagging", { "TagSet": gen_tagset( {context._persistent_graph_lock_tag: "1111"}) }, context.persistent_graph_location, ) with stubber: with self.assertRaises(PersistentGraphLocked): context.lock_persistent_graph(code) stubber.assert_no_pending_responses()
def test_execute_plan_no_persist(self): """Test execute plan with no persistent graph.""" context = Context(config=self.config) context.put_persistent_graph = mock.MagicMock() vpc = Stack(definition=generate_definition("vpc", 1), context=context) bastion = Stack( definition=generate_definition("bastion", 1, requires=[vpc.name]), context=context, ) calls = [] def _launch_stack(stack, status=None): calls.append(stack.fqn) return COMPLETE graph = Graph.from_steps( [Step(vpc, _launch_stack), Step(bastion, _launch_stack)]) plan = Plan(description="Test", graph=graph, context=context) plan.execute(walk) self.assertEqual(calls, ["namespace-vpc.1", "namespace-bastion.1"]) context.put_persistent_graph.assert_not_called()
def _get_context(self, extra_config_args=None, **kwargs): """Get context.""" 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": {} }], } if extra_config_args: config.update(extra_config_args) return Context(config=Config(config), **kwargs)
def setUp(self): """Run before tests.""" self.provider = MagicMock() self.context = Context(environment={ "namespace": "test", "env_var": "val_in_env" })
def test_put_persistent_graph(self): """Return 'None' when put is successful.""" code = "0000" context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True graph_dict = {"stack1": [], "stack2": ["stack1"]} context._persistent_graph = Graph.from_dict(graph_dict, context) stubber = Stubber(context.s3_conn) expected_params = { "Body": json.dumps(graph_dict, indent=4), "ServerSideEncryption": "AES256", "ACL": "bucket-owner-full-control", "ContentType": "application/json", "Tagging": "{}={}".format(context._persistent_graph_lock_tag, code), } expected_params.update(context.persistent_graph_location) stubber.add_response( "get_object_tagging", {"TagSet": gen_tagset({context._persistent_graph_lock_tag: code})}, context.persistent_graph_location, ) stubber.add_response("put_object", {}, expected_params) with stubber: self.assertIsNone(context.put_persistent_graph(code)) stubber.assert_no_pending_responses()
def test_create_template_passes(self): """Test create template passes.""" ctx = Context({'namespace': 'test'}) blueprint = Repositories('test_repo', ctx) blueprint.resolve_variables( [Variable('Repositories', ["repo1", "repo2"], 'cfngin')]) blueprint.create_template() self.assertRenderedBlueprint(blueprint)
def setUp(self): """Run before tests.""" self.provider = MagicMock() self.context = Context( environment={ 'namespace': 'test', 'env_var': 'val_in_env'} )
def test_persistent_graph_location(self): """Test persistent graph location.""" context = Context(config=self.persist_graph_config) expected = { "Bucket": "cfngin-test", "Key": "persistent_graphs/test/test.json" } self.assertEqual(expected, context.persistent_graph_location)
def test_context_optional_keys_set(self): """Test context optional keys set.""" context = Context( config=Config({}), stack_names=["stack"], ) self.assertEqual(context.mappings, {}) self.assertEqual(context.stack_names, ["stack"])
def test_persistent_graph_location(self): """Test persistent graph location.""" context = Context(config=self.persist_graph_config) expected = { 'Bucket': 'cfngin-test', 'Key': 'persistent_graphs/test/test.json' } self.assertEqual(expected, context.persistent_graph_location)
def test_create_template_fails(self): """Test create template fails.""" ctx = Context({'namespace': 'test'}) blueprint = Repositories('test_repo', ctx) blueprint.resolve_variables( [Variable('Repositories', ["repo1", "repo2", "repo3"], 'cfngin')]) blueprint.create_template() with self.assertRaises(AssertionError): self.assertRenderedBlueprint(blueprint)
def test_create_template_passes(self): """Test create template passes.""" ctx = Context({"namespace": "test"}) blueprint = Repositories("test_repo", ctx) blueprint.resolve_variables( [Variable("Repositories", ["repo1", "repo2"], "cfngin")] ) blueprint.create_template() self.assertRenderedBlueprint(blueprint)
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_create_template_fails(self): """Test create template fails.""" ctx = Context({"namespace": "test"}) blueprint = Repositories("test_repo", ctx) blueprint.resolve_variables( [Variable("Repositories", ["repo1", "repo2", "repo3"], "cfngin")] ) blueprint.create_template() with self.assertRaises(AssertionError): self.assertRenderedBlueprint(blueprint)
def test_execute_plan_graph_locked(self): """Test execute plan with locked persistent graph.""" context = Context(config=self.config) context._persistent_graph = Graph.from_dict({"stack1": []}, context) context._persistent_graph_lock_code = "1111" plan = Plan(description="Test", graph=Graph(), context=context) print(plan.locked) with self.assertRaises(PersistentGraphLocked): plan.execute()
def test_persistent_graph_location_no_json(self): """'.json' appended to the key if it does not exist.""" cp_config = self.persist_graph_raw_config.copy() cp_config["persistent_graph_key"] = "test" context = Context(config=Config(cp_config)) expected = { "Bucket": "cfngin-test", "Key": "persistent_graphs/test/test.json" } self.assertEqual(expected, context.persistent_graph_location)