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_unlock_persistent_graph_no_such_key( self, mocker: MockerFixture) -> None: """Test unlock_persistent_graph empty graph NoSuchKey.""" mocker.patch.object( CfnginContext, "persistent_graph_location", { "Bucket": "test-bucket", "Key": "something.json" }, ) mocker.patch.object(CfnginContext, "persistent_graph_locked", True) mocker.patch.object(CfnginContext, "persistent_graph_lock_code", "123") obj = CfnginContext() obj.persistent_graph = Graph.from_dict({}, context=obj) stubber = Stubber(obj.s3_client) stubber.add_response( "get_object", {"Body": "{}".encode()}, { "ResponseContentType": "application/json", **obj.persistent_graph_location, }, ) stubber.add_client_error("delete_object_tagging", "NoSuchKey") with stubber: assert obj.unlock_persistent_graph("123")
def test_unlock_persistent_graph(self, graph_dict: Dict[str, List[str]], mocker: MockerFixture) -> None: """Test unlock_persistent_graph.""" mocker.patch.object( CfnginContext, "persistent_graph_location", { "Bucket": "test-bucket", "Key": "something.json" }, ) mocker.patch.object(CfnginContext, "persistent_graph_locked", True) mocker.patch.object(CfnginContext, "persistent_graph_lock_code", "123") obj = CfnginContext() obj.persistent_graph = Graph.from_dict(graph_dict, context=obj) stubber = Stubber(obj.s3_client) if not graph_dict: stubber.add_response( "get_object", {"Body": "{}".encode()}, { "ResponseContentType": "application/json", **obj.persistent_graph_location, }, ) stubber.add_response("delete_object_tagging", {}, obj.persistent_graph_location) with stubber: assert obj.unlock_persistent_graph("123")
def test_from_dict(self) -> None: """Test from dict.""" graph = Graph.from_dict(self.graph_dict, self.context) self.assertIsInstance(graph, Graph) self.assertEqual([step.name for step in self.steps], list(graph.steps.keys())) self.assertEqual(self.graph_dict_expected, graph.to_dict())
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_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_put_persistent_graph_unlocked(self): """Error raised when trying to update an unlocked object.""" context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True context._persistent_graph = Graph.from_dict({"stack1": []}, context) stubber = Stubber(context.s3_conn) stubber.add_response("get_object_tagging", {"TagSet": []}, context.persistent_graph_location) with stubber: with self.assertRaises(PersistentGraphUnlocked): context.put_persistent_graph("") stubber.assert_no_pending_responses()
def test_unlock_persistent_graph_not_locked(self): """Error raised when object is not locked.""" code = "0000" context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True context._persistent_graph = Graph.from_dict({"stack1": []}, context) stubber = Stubber(context.s3_conn) stubber.add_response("get_object_tagging", {"TagSet": []}, context.persistent_graph_location) with stubber: with self.assertRaises(PersistentGraphCannotUnlock): context.unlock_persistent_graph(code) stubber.assert_no_pending_responses()
def test_unlock_persistent_graph_empty_no_such_key( self, mocker: MockerFixture) -> None: """Test unlock_persistent_graph empty graph NoSuchKey.""" mocker.patch.object( CfnginContext, "persistent_graph_location", { "Bucket": "test-bucket", "Key": "something.json" }, ) obj = CfnginContext() obj.persistent_graph = Graph.from_dict({}, context=obj) stubber = Stubber(obj.s3_client) stubber.add_client_error("get_object", "NoSuchKey") with stubber: assert obj.unlock_persistent_graph("123")
def test_put_persistent_graph_not_locked(self, mocker: MockerFixture) -> None: """Test put_persistent_graph not locked.""" mocker.patch.object( CfnginContext, "persistent_graph_location", { "Bucket": "test-bucket", "Key": "something.json" }, ) mocker.patch.object(CfnginContext, "persistent_graph_locked", False) obj = CfnginContext() obj.persistent_graph = Graph.from_dict(self.persist_graph_raw, context=obj) with pytest.raises(PersistentGraphUnlocked): obj.put_persistent_graph("123")
def test_put_persistent_graph_empty(self, mocker: MockerFixture) -> None: """Test put_persistent_graph empty.""" mocker.patch.object( CfnginContext, "persistent_graph_location", { "Bucket": "test-bucket", "Key": "something.json" }, ) obj = CfnginContext() obj.persistent_graph = Graph.from_dict({}, context=obj) stubber = Stubber(obj.s3_client) stubber.add_response("delete_object", {}, obj.persistent_graph_location) with stubber: assert not obj.put_persistent_graph("123")
def test_unlock_persistent_graph_code_missmatch(self): """Error raised when local code does not match object.""" code = '0000' context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True context._persistent_graph = Graph.from_dict({'stack1': []}, context) stubber = Stubber(context.s3_conn) stubber.add_response('get_object_tagging', { 'TagSet': gen_tagset({context._persistent_graph_lock_tag: '1111'}) }, context.persistent_graph_location) with stubber: with self.assertRaises(PersistentGraphCannotUnlock): context.unlock_persistent_graph(code) stubber.assert_no_pending_responses()
def test_unlock_persistent_graph_lock_code_mismatch( self, mocker: MockerFixture) -> None: """Test unlock_persistent_graph lock code mismatch.""" mocker.patch.object( CfnginContext, "persistent_graph_location", { "Bucket": "test-bucket", "Key": "something.json" }, ) mocker.patch.object(CfnginContext, "persistent_graph_locked", True) mocker.patch.object(CfnginContext, "persistent_graph_lock_code", "0") obj = CfnginContext() obj.persistent_graph = Graph.from_dict(self.persist_graph_raw, context=obj) with pytest.raises(PersistentGraphCannotUnlock): assert obj.unlock_persistent_graph("123")
def test_unlock_persistent_graph(self): """Return 'True' when delete tag is successful.""" code = '0000' context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True context._persistent_graph = Graph.from_dict({'stack1': []}, context) stubber = Stubber(context.s3_conn) stubber.add_response( 'get_object_tagging', {'TagSet': gen_tagset({context._persistent_graph_lock_tag: code})}, context.persistent_graph_location) stubber.add_response('delete_object_tagging', {}, context.persistent_graph_location) with stubber: self.assertTrue(context.unlock_persistent_graph(code)) stubber.assert_no_pending_responses()
def test_put_persistent_graph_code_missmatch(self): """Error raised when provided lock code does not match object.""" code = "0000" context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True context._persistent_graph = Graph.from_dict({"stack1": []}, context) stubber = Stubber(context.s3_conn) stubber.add_response( "get_object_tagging", { "TagSet": gen_tagset( {context._persistent_graph_lock_tag: "1111"}) }, context.persistent_graph_location, ) with stubber: with self.assertRaises(PersistentGraphLockCodeMissmatch): context.put_persistent_graph(code) stubber.assert_no_pending_responses()
def test_put_persistent_graph(self, mocker: MockerFixture) -> None: """Test put_persistent_graph.""" mocker.patch.object( CfnginContext, "persistent_graph_location", { "Bucket": "test-bucket", "Key": "something.json" }, ) mocker.patch.object(CfnginContext, "persistent_graph_locked", True) mocker.patch.object(CfnginContext, "persistent_graph_lock_code", "123") obj = CfnginContext() obj.persistent_graph = Graph.from_dict(self.persist_graph_raw, context=obj) stubber = Stubber(obj.s3_client) stubber.add_response( "put_object", {}, { "Body": json.dumps(self.persist_graph_raw, default=json_serial, indent=4).encode(), "ServerSideEncryption": "AES256", "ACL": "bucket-owner-full-control", "ContentType": "application/json", "Tagging": "cfngin_lock_code=123", **obj.persistent_graph_location, }, ) with stubber: assert not obj.put_persistent_graph("123")