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 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_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_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_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_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_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_put_persistent_graph_empty(self): """Object deleted when persistent graph is empty.""" code = "0000" context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True context._persistent_graph = Graph() stubber = Stubber(context.s3_conn) stubber.add_response("delete_object", {}, context.persistent_graph_location) with stubber: self.assertFalse(context.persistent_graph.to_dict()) self.assertIsNone(context.put_persistent_graph(code)) stubber.assert_no_pending_responses()
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(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_persistent_graph(self): """Return Graph from S3 object.""" context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True stubber = Stubber(context.s3_conn) expected_params = {'ResponseContentType': 'application/json'} expected_params.update(context.persistent_graph_location) expected_content = {'stack1': set(), 'stack2': set(['stack1'])} stubber.add_response('get_object', {'Body': gen_s3_object_content(expected_content)}, expected_params) with stubber: self.assertIsNone(context._persistent_graph) self.assertIsInstance(context.persistent_graph, Graph) self.assertIsInstance(context._persistent_graph, Graph) self.assertEqual(expected_content, context.persistent_graph.to_dict()) stubber.assert_no_pending_responses()
def test_unlock_persistent_graph_no_object(self): """Return 'None' when object does not exist. This can occur if the object is deleted by 'put_persistent_graph'. """ 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 = context.persistent_graph_location.copy() expected_params.update({"ResponseContentType": "application/json"}) stubber.add_client_error("get_object", "NoSuchKey", expected_params=expected_params) with stubber: assert 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_lock_persistent_graph(self): """Return 'None' when lock is successful.""" 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": []}, context.persistent_graph_location) stubber.add_response("put_object_tagging", {}, expected_params) with stubber: self.assertIsNone(context.lock_persistent_graph(code)) stubber.assert_no_pending_responses()
def test_persistent_graph(self): """Return Graph from S3 object.""" context = Context(config=self.persist_graph_config) context._s3_bucket_verified = True stubber = Stubber(context.s3_conn) expected_params = {"ResponseContentType": "application/json"} expected_params.update(context.persistent_graph_location) expected_content = {"stack1": set(), "stack2": set(["stack1"])} stubber.add_response( "get_object", {"Body": gen_s3_object_content(expected_content)}, expected_params, ) with stubber: self.assertIsNone(context._persistent_graph) self.assertIsInstance(context.persistent_graph, Graph) self.assertIsInstance(context._persistent_graph, Graph) self.assertEqual(expected_content, context.persistent_graph.to_dict()) stubber.assert_no_pending_responses()