12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241 |
- import json
- import tempfile
- import uuid
- import mock
- import pytest
- from decimal import Decimal
- from tests.acceptance import query_json_file, query_parquet_file, download_and_decrypt
- pytestmark = [
- pytest.mark.acceptance_iam,
- pytest.mark.jobs,
- pytest.mark.usefixtures("empty_jobs"),
- ]
- @pytest.mark.auth
- @pytest.mark.api
- def test_auth(api_client_iam, jobs_endpoint):
- headers = {"Authorization": None}
- assert (
- 403
- == api_client_iam.get(
- "{}/{}".format(jobs_endpoint, "a"), headers=headers
- ).status_code
- )
- assert 403 == api_client_iam.get(jobs_endpoint, headers=headers).status_code
- assert (
- 403
- == api_client_iam.get(
- "{}/{}/events".format(jobs_endpoint, "a"), headers=headers
- ).status_code
- )
- @pytest.mark.api
- def test_it_gets_jobs(
- api_client_iam, jobs_endpoint, job_factory, stack, job_table, job_exists_waiter
- ):
- # Arrange
- job_id = job_factory()["Id"]
- job_exists_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Act
- response = api_client_iam.get("{}/{}".format(jobs_endpoint, job_id))
- response_body = response.json()
- # Assert
- assert response.status_code == 200
- assert {
- "Id": job_id,
- "Sk": job_id,
- "Type": "Job",
- "JobStatus": mock.ANY,
- "GSIBucket": mock.ANY,
- "CreatedAt": mock.ANY,
- "AthenaConcurrencyLimit": mock.ANY,
- "AthenaQueryMaxRetries": mock.ANY,
- "DeletionTasksMaxNumber": mock.ANY,
- "QueryExecutionWaitSeconds": mock.ANY,
- "QueryQueueWaitSeconds": mock.ANY,
- "ForgetQueueWaitSeconds": mock.ANY,
- } == response_body
- assert (
- response.headers.get("Access-Control-Allow-Origin")
- == stack["APIAccessControlAllowOriginHeader"]
- )
- @pytest.mark.api
- def test_it_handles_unknown_jobs(api_client_iam, jobs_endpoint, stack):
- # Arrange
- job_id = "invalid"
- # Act
- response = api_client_iam.get("{}/{}".format(jobs_endpoint, job_id))
- # Assert
- assert response.status_code == 404
- assert (
- response.headers.get("Access-Control-Allow-Origin")
- == stack["APIAccessControlAllowOriginHeader"]
- )
- @pytest.mark.api
- def test_it_lists_jobs_by_date(
- api_client_iam, jobs_endpoint, job_factory, stack, job_table, job_exists_waiter
- ):
- # Arrange
- job_id_1 = job_factory(job_id=str(uuid.uuid4()), created_at=1576861489)["Id"]
- job_id_2 = job_factory(job_id=str(uuid.uuid4()), created_at=1576861490)["Id"]
- job_exists_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id_1}, "Sk": {"S": job_id_1}}
- )
- job_exists_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id_2}, "Sk": {"S": job_id_2}}
- )
- # Act
- response = api_client_iam.get(jobs_endpoint)
- response_body = response.json()
- # Assert
- assert response.status_code == 200
- assert (
- response_body["Jobs"][0]["CreatedAt"] >= response_body["Jobs"][1]["CreatedAt"]
- )
- assert (
- response.headers.get("Access-Control-Allow-Origin")
- == stack["APIAccessControlAllowOriginHeader"]
- )
- @pytest.mark.api
- def test_it_returns_summary_fields_in_list(
- api_client_iam, jobs_endpoint, job_factory, job_table, job_finished_waiter
- ):
- # Arrange
- job_id = job_factory(job_id=str(uuid.uuid4()), created_at=1576861489)["Id"]
- job_finished_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Act
- response = api_client_iam.get(jobs_endpoint)
- response_body = response.json()
- # Assert
- assert response.status_code == 200
- for job in response_body["Jobs"]:
- assert all(
- [
- k in job
- for k in [
- "Id",
- "CreatedAt",
- "JobStatus",
- "JobFinishTime",
- "JobStartTime",
- "TotalObjectRollbackFailedCount",
- "TotalObjectUpdatedCount",
- "TotalObjectUpdateSkippedCount",
- "TotalObjectUpdateFailedCount",
- "TotalQueryCount",
- "TotalQueryScannedInBytes",
- "TotalQuerySucceededCount",
- "TotalQueryTimeInMillis",
- ]
- ]
- )
- @pytest.mark.api
- def test_it_lists_job_events_by_date(
- api_client_iam, jobs_endpoint, job_factory, stack, job_table, job_finished_waiter
- ):
- # Arrange
- job_id = str(uuid.uuid4())
- job_id = job_factory(job_id=job_id, created_at=1576861489)["Id"]
- job_finished_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Act
- response = api_client_iam.get("{}/{}/events".format(jobs_endpoint, job_id))
- response_body = response.json()
- # Assert
- assert response.status_code == 200
- job_events = response_body["JobEvents"]
- assert len(job_events) > 0
- assert (
- response.headers.get("Access-Control-Allow-Origin")
- == stack["APIAccessControlAllowOriginHeader"]
- )
- assert all(
- job_events[i]["CreatedAt"] <= job_events[i + 1]["CreatedAt"]
- for i in range(len(job_events) - 1)
- )
- @pytest.mark.api
- def test_it_filters_job_events_by_event_name(
- api_client_iam, jobs_endpoint, job_factory, stack, job_table, job_finished_waiter
- ):
- # Arrange
- job_id = str(uuid.uuid4())
- job_id = job_factory(job_id=job_id, created_at=1576861489)["Id"]
- job_finished_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Act
- response = api_client_iam.get(
- "{}/{}/events?filter=EventName%3DFindPhaseStarted".format(jobs_endpoint, job_id)
- )
- response_body = response.json()
- job_events = response_body["JobEvents"]
- # Assert
- assert response.status_code == 200
- assert len(job_events) == 1
- assert "FindPhaseStarted" == job_events[0]["EventName"]
- assert (
- response.headers.get("Access-Control-Allow-Origin")
- == stack["APIAccessControlAllowOriginHeader"]
- )
- def test_it_runs_for_parquet_happy_path(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- )
- item = del_queue_factory("12345")
- object_key = "test/2019/08/20/test.parquet"
- data_loader(
- "basic.parquet", object_key, Metadata={"foo": "bar"}, CacheControl="cache"
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customer_id", "12345"))
- assert 1 == len(query_parquet_file(tmp, "customer_id", "23456"))
- assert 1 == len(query_parquet_file(tmp, "customer_id", "34567"))
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_runs_for_parquet_cse_kms(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- encrypted_data_loader,
- job_complete_waiter,
- job_table,
- kms_factory,
- kms_client,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- encrypted=True,
- )
- item = del_queue_factory("12345")
- encryption_key = kms_factory
- object_key_cbc = "test/2019/08/20/test_cbc.parquet"
- object_key_gcm = "test/2019/08/20/test_gcm.parquet"
- encrypted_data_loader(
- "basic.parquet",
- object_key_cbc,
- encryption_key,
- "AES/CBC/PKCS5Padding",
- CacheControl="cache",
- )
- encrypted_data_loader(
- "basic.parquet",
- object_key_gcm,
- encryption_key,
- "AES/GCM/NoPadding",
- CacheControl="cache",
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- decrypted_cbc, metadata_cbc = download_and_decrypt(
- bucket, object_key_cbc, kms_client
- )
- decrypted_gcm, metadata_gcm = download_and_decrypt(
- bucket, object_key_gcm, kms_client
- )
- tmp_cbc = tempfile.NamedTemporaryFile()
- tmp_gcm = tempfile.NamedTemporaryFile()
- open(tmp_cbc.name, "wb").write(decrypted_cbc)
- open(tmp_gcm.name, "wb").write(decrypted_gcm)
- # Assert
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp_cbc, "customer_id", "12345"))
- assert 1 == len(query_parquet_file(tmp_cbc, "customer_id", "23456"))
- assert 1 == len(query_parquet_file(tmp_cbc, "customer_id", "34567"))
- assert metadata_cbc["x-amz-cek-alg"] == "AES/CBC/PKCS5Padding"
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key_cbc)))
- assert "cache" == bucket.Object(object_key_cbc).cache_control
- assert 0 == len(query_parquet_file(tmp_gcm, "customer_id", "12345"))
- assert 1 == len(query_parquet_file(tmp_gcm, "customer_id", "23456"))
- assert 1 == len(query_parquet_file(tmp_gcm, "customer_id", "34567"))
- assert metadata_gcm["x-amz-cek-alg"] == "AES/GCM/NoPadding"
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key_gcm)))
- assert "cache" == bucket.Object(object_key_gcm).cache_control
- def test_it_runs_for_parquet_backwards_compatible_matches(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- )
- # MatchId Type was introduced in 0.19 only and it should default to Simple
- item = del_queue_factory("12345", matchid_type=None)
- object_key = "test/2019/08/20/test.parquet"
- data_loader(
- "basic.parquet", object_key, Metadata={"foo": "bar"}, CacheControl="cache"
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customer_id", "12345"))
- assert 1 == len(query_parquet_file(tmp, "customer_id", "23456"))
- assert 1 == len(query_parquet_file(tmp, "customer_id", "34567"))
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_runs_for_parquet_composite_matches(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- )
- item = del_queue_factory(
- [
- {"Column": "user_info.personal_information.first_name", "Value": "John"},
- {"Column": "user_info.personal_information.last_name", "Value": "Doe"},
- ],
- "id123",
- matchid_type="Composite",
- data_mappers=["test"],
- )
- object_key = "test/2019/08/20/test.parquet"
- data_loader(
- "basic.parquet", object_key, Metadata={"foo": "bar"}, CacheControl="cache"
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customer_id", "12345"))
- assert 1 == len(query_parquet_file(tmp, "customer_id", "23456"))
- assert 1 == len(query_parquet_file(tmp, "customer_id", "34567"))
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_runs_for_parquet_mixed_matches(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- )
- composite_item = del_queue_factory(
- [
- {"Column": "user_info.personal_information.first_name", "Value": "John"},
- {"Column": "user_info.personal_information.last_name", "Value": "Doe"},
- ],
- "id123",
- matchid_type="Composite",
- data_mappers=["test"],
- )
- simple_item = del_queue_factory("23456", "id234")
- object_key = "test/2019/08/20/test.parquet"
- data_loader(
- "basic.parquet", object_key, Metadata={"foo": "bar"}, CacheControl="cache"
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[composite_item, simple_item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customer_id", "12345"))
- assert 0 == len(query_parquet_file(tmp, "customer_id", "23456"))
- assert 1 == len(query_parquet_file(tmp, "customer_id", "34567"))
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_runs_for_json_happy_path(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- fmt="json",
- )
- item = del_queue_factory("12345")
- object_key = "test/2019/08/20/test.json"
- data_loader("basic.json", object_key, Metadata={"foo": "bar"}, CacheControl="cache")
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_file(object_key, tmp.name)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_json_file(tmp.name, "customer_id", "12345"))
- assert 1 == len(query_json_file(tmp.name, "customer_id", "23456"))
- assert 1 == len(query_json_file(tmp.name, "customer_id", "34567"))
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_runs_for_json_cse_kms(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- encrypted_data_loader,
- job_complete_waiter,
- job_table,
- kms_factory,
- kms_client,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- fmt="json",
- encrypted=True,
- )
- item = del_queue_factory("12345")
- encryption_key = kms_factory
- object_key_cbc = "test/2019/08/20/test_cbc.json"
- object_key_gcm = "test/2019/08/20/test_gcm.json"
- encrypted_data_loader(
- "basic.json",
- object_key_cbc,
- encryption_key,
- "AES/CBC/PKCS5Padding",
- CacheControl="cache",
- )
- encrypted_data_loader(
- "basic.json",
- object_key_gcm,
- encryption_key,
- "AES/GCM/NoPadding",
- CacheControl="cache",
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- decrypted_cbc, metadata_cbc = download_and_decrypt(
- bucket, object_key_cbc, kms_client
- )
- decrypted_gcm, metadata_gcm = download_and_decrypt(
- bucket, object_key_gcm, kms_client
- )
- tmp_cbc = tempfile.NamedTemporaryFile()
- tmp_gcm = tempfile.NamedTemporaryFile()
- open(tmp_cbc.name, "wb").write(decrypted_cbc)
- open(tmp_gcm.name, "wb").write(decrypted_gcm)
- # Assert
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_json_file(tmp_cbc.name, "customer_id", "12345"))
- assert 1 == len(query_json_file(tmp_cbc.name, "customer_id", "23456"))
- assert 1 == len(query_json_file(tmp_cbc.name, "customer_id", "34567"))
- assert metadata_cbc["x-amz-cek-alg"] == "AES/CBC/PKCS5Padding"
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key_cbc)))
- assert "cache" == bucket.Object(object_key_cbc).cache_control
- assert 0 == len(query_json_file(tmp_gcm.name, "customer_id", "12345"))
- assert 1 == len(query_json_file(tmp_gcm.name, "customer_id", "23456"))
- assert 1 == len(query_json_file(tmp_gcm.name, "customer_id", "34567"))
- assert metadata_gcm["x-amz-cek-alg"] == "AES/GCM/NoPadding"
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key_gcm)))
- assert "cache" == bucket.Object(object_key_gcm).cache_control
- def test_it_runs_for_json_composite_matches(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- fmt="json",
- )
- composite_item = del_queue_factory(
- [
- {"Column": "user_info.personal_information.first_name", "Value": "John"},
- {"Column": "user_info.personal_information.last_name", "Value": "Doe"},
- ],
- "id123",
- matchid_type="Composite",
- data_mappers=["test"],
- )
- object_key = "test/2019/08/20/test.json"
- data_loader("basic.json", object_key, Metadata={"foo": "bar"}, CacheControl="cache")
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[composite_item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_file(object_key, tmp.name)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_json_file(tmp.name, "customer_id", "12345"))
- assert 1 == len(query_json_file(tmp.name, "customer_id", "23456"))
- assert 1 == len(query_json_file(tmp.name, "customer_id", "34567"))
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_runs_for_json_mixed_matches(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- fmt="json",
- )
- composite_item = del_queue_factory(
- [
- {"Column": "user_info.personal_information.first_name", "Value": "John"},
- {"Column": "user_info.personal_information.last_name", "Value": "Doe"},
- ],
- "id123",
- matchid_type="Composite",
- data_mappers=["test"],
- )
- simple_item = del_queue_factory("23456", "id234")
- object_key = "test/2019/08/20/test.json"
- data_loader("basic.json", object_key, Metadata={"foo": "bar"}, CacheControl="cache")
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[composite_item, simple_item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_file(object_key, tmp.name)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_json_file(tmp.name, "customer_id", "12345"))
- assert 0 == len(query_json_file(tmp.name, "customer_id", "23456"))
- assert 1 == len(query_json_file(tmp.name, "customer_id", "34567"))
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_runs_for_unpartitioned_data(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory("test")
- item = del_queue_factory("12345")
- object_key = "test/test.parquet"
- data_loader(
- "basic.parquet", object_key, Metadata={"foo": "bar"}, CacheControl="cache"
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customer_id", "12345"))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_runs_for_complex_types(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test", column_identifiers=["user_info.personal_information.first_name"]
- )
- item = del_queue_factory("John")
- object_key = "test/test.parquet"
- data_loader(
- "basic.parquet", object_key, Metadata={"foo": "bar"}, CacheControl="cache"
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customer_id", "12345"))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_runs_for_partitioned_data_with_non_string_partitions(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "10", "20"]],
- partition_key_types="int",
- )
- item = del_queue_factory("12345")
- object_key = "test/2019/10/20/test.parquet"
- data_loader(
- "basic.parquet", object_key, Metadata={"foo": "bar"}, CacheControl="cache"
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customer_id", "12345"))
- assert 1 == len(query_parquet_file(tmp, "customer_id", "23456"))
- assert 1 == len(query_parquet_file(tmp, "customer_id", "34567"))
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_runs_for_caselowered_identifier_parquet(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory("test", column_identifiers=["customerid"])
- item = del_queue_factory(12345)
- object_key = "test/test.parquet"
- data_loader(
- "basic.parquet", object_key, Metadata={"foo": "bar"}, CacheControl="cache"
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customerId", 12345))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_runs_for_decimal_identifier_parquet(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory("test", column_identifiers=["customer_id_decimal"])
- item = del_queue_factory("123.450")
- object_key = "test/test.parquet"
- data_loader(
- "basic.parquet", object_key, Metadata={"foo": "bar"}, CacheControl="cache"
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customer_id_decimal", Decimal("123.450")))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_does_not_permit_unversioned_buckets(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_finished_waiter,
- job_table,
- s3_resource,
- ):
- try:
- # Arrange
- s3_resource.BucketVersioning(dummy_lake["bucket_name"]).suspend()
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- )
- item = del_queue_factory("12345")
- object_key = "test/2019/08/20/test.parquet"
- data_loader("basic.parquet", object_key)
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item], delete_previous_versions=False)[
- "Id"
- ]
- # Act
- job_finished_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "FORGET_PARTIALLY_FAILED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 1 == len(query_parquet_file(tmp, "customer_id", "12345"))
- finally:
- s3_resource.BucketVersioning(dummy_lake["bucket_name"]).enable()
- def test_it_executes_successfully_for_empty_queue(
- job_factory, job_finished_waiter, job_table
- ):
- # Arrange
- job_id = job_factory()["Id"]
- # Act
- job_finished_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- def test_it_supports_data_access_roles(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- data_access_role,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- delete_old_versions=False,
- role_arn=data_access_role["Arn"],
- )
- item = del_queue_factory("12345")
- object_key = "test/2019/08/20/test.parquet"
- data_loader("basic.parquet", object_key)
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customer_id", "12345"))
- def test_it_deletes_old_versions(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- data_access_role,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- delete_old_versions=True,
- role_arn=data_access_role["Arn"],
- )
- item = del_queue_factory("12345")
- object_key = "test/2019/08/20/test.parquet"
- bucket = dummy_lake["bucket"]
- # Create the object, add a deletion marker, then recreate it
- data_loader("basic.parquet", object_key)
- bucket.Object("basic.parquet").delete()
- data_loader("basic.parquet", object_key)
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customer_id", "12345"))
- assert 1 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- def test_it_ignores_not_found_exceptions(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- data_access_role,
- find_phase_ended_waiter,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- ignore_object_not_found_exceptions=True,
- role_arn=data_access_role["Arn"],
- )
- item = del_queue_factory("12345")
- object_key = "test/2019/08/20/test.parquet"
- bucket = dummy_lake["bucket"]
- data_loader("basic.parquet", object_key)
- # Start job, wait for find phase to end, delete object
- job_id = job_factory(del_queue_items=[item])["Id"]
- find_phase_ended_waiter.wait(job_id)
- bucket.Object(key=object_key).delete()
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- job = job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]
- assert "COMPLETED" == job["JobStatus"]
- assert 0 == job["TotalObjectUpdatedCount"]
- assert 1 == job["TotalObjectUpdateSkippedCount"]
- assert 0 == job["TotalObjectUpdateFailedCount"]
- assert 0 == job["TotalObjectRollbackFailedCount"]
- def test_it_handles_find_permission_issues(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_finished_waiter,
- job_table,
- policy_changer,
- stack,
- arn_partition,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- )
- item = del_queue_factory("12345")
- object_key = "test/2019/08/20/test.parquet"
- data_loader("basic.parquet", object_key)
- bucket = dummy_lake["bucket"]
- bucket_name = dummy_lake["bucket_name"]
- policy_changer(
- {
- "Version": "2012-10-17",
- "Statement": [
- {
- "Effect": "Deny",
- "Principal": {"AWS": [stack["AthenaExecutionRoleArn"]]},
- "Action": "s3:*",
- "Resource": [
- "arn:{}:s3:::{}".format(arn_partition, bucket_name),
- "arn:{}:s3:::{}/*".format(arn_partition, bucket_name),
- ],
- }
- ],
- }
- )
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_finished_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- assert (
- "FIND_FAILED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 1 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- def test_it_handles_forget_permission_issues(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_finished_waiter,
- job_table,
- policy_changer,
- stack,
- arn_partition,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- )
- item = del_queue_factory("12345")
- object_key = "test/2019/08/20/test.parquet"
- data_loader("basic.parquet", object_key)
- bucket = dummy_lake["bucket"]
- bucket_name = dummy_lake["bucket_name"]
- policy = json.loads(dummy_lake["policy"].policy)
- policy["Statement"].append(
- {
- "Effect": "Deny",
- "Principal": {"AWS": [stack["DeleteTaskRoleArn"]]},
- "Action": "s3:*",
- "Resource": [
- "arn:{}:s3:::{}".format(arn_partition, bucket_name),
- "arn:{}:s3:::{}/*".format(arn_partition, bucket_name),
- ],
- }
- )
- policy_changer(policy)
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_finished_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- assert (
- "FORGET_PARTIALLY_FAILED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 1 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- def test_it_handles_forget_invalid_role(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_finished_waiter,
- job_table,
- arn_partition,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- role_arn="arn:{}:iam::invalid:role/DoesntExist".format(arn_partition),
- )
- item = del_queue_factory("12345")
- object_key = "test/2019/08/20/test.parquet"
- data_loader("basic.parquet", object_key)
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item])["Id"]
- # Act
- job_finished_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "FORGET_PARTIALLY_FAILED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 1 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- def test_it_handles_json_all_rows_deleted(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- fmt="json",
- )
- item = del_queue_factory("12345", "id1")
- item2 = del_queue_factory("23456", "id2")
- item3 = del_queue_factory("34567", "id3")
- object_key = "test/2019/08/20/test.json"
- data_loader("basic.json", object_key, Metadata={"foo": "bar"}, CacheControl="cache")
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item, item2, item3])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_file(object_key, tmp.name)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- content = open(tmp.name, "rb").read()
- assert content == b"" # JSON is empty
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
- def test_it_handles_parquet_all_rows_deleted(
- del_queue_factory,
- job_factory,
- dummy_lake,
- glue_data_mapper_factory,
- data_loader,
- job_complete_waiter,
- job_table,
- ):
- # Arrange
- glue_data_mapper_factory(
- "test",
- partition_keys=["year", "month", "day"],
- partitions=[["2019", "08", "20"]],
- )
- item = del_queue_factory("12345", "id1")
- item2 = del_queue_factory("23456", "id2")
- item3 = del_queue_factory("34567", "id3")
- object_key = "test/2019/08/20/test.parquet"
- data_loader(
- "basic.parquet", object_key, Metadata={"foo": "bar"}, CacheControl="cache"
- )
- bucket = dummy_lake["bucket"]
- job_id = job_factory(del_queue_items=[item, item2, item3])["Id"]
- # Act
- job_complete_waiter.wait(
- TableName=job_table.name, Key={"Id": {"S": job_id}, "Sk": {"S": job_id}}
- )
- # Assert
- tmp = tempfile.NamedTemporaryFile()
- bucket.download_fileobj(object_key, tmp)
- assert (
- "COMPLETED"
- == job_table.get_item(Key={"Id": job_id, "Sk": job_id})["Item"]["JobStatus"]
- )
- assert 0 == len(query_parquet_file(tmp, "customer_id", "12345"))
- assert 0 == len(query_parquet_file(tmp, "customer_id", "23456"))
- assert 0 == len(query_parquet_file(tmp, "customer_id", "34567"))
- assert 2 == len(list(bucket.object_versions.filter(Prefix=object_key)))
- assert {"foo": "bar"} == bucket.Object(object_key).metadata
- assert "cache" == bucket.Object(object_key).cache_control
|