def test_deploy_pubsub_cloudrun(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "pubsub-deploy-cloudrun") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") pubsub = PubSub("goblet", backend="cloudrun") pubsub.register_topic("test", None, kwargs={ "topic": "test", "kwargs": {} }) cloudrun_url = "https://goblet-12345.a.run.app" service_account = "*****@*****.**" pubsub._deploy( config={"pubsub": { "serviceAccountEmail": service_account }}) responses = get_responses("pubsub-deploy-cloudrun") assert len(responses) == 2 assert responses[0]["body"]["status"]["url"] == cloudrun_url assert (responses[1]["body"]["pushConfig"]["oidcToken"] ["serviceAccountEmail"] == service_account) assert (responses[1]["body"]["pushConfig"]["oidcToken"]["audience"] == cloudrun_url) assert responses[1]["body"]["pushConfig"][ "pushEndpoint"] == cloudrun_url assert responses[1]["body"]["topic"] == "projects/goblet/topics/test"
def test_deploy_pubsub_subscription_with_config(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "pubsub-deploy-subscription-config") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") service_account = "*****@*****.**" app = Goblet(function_name="goblet-topic-subscription-config") setattr(app, "entrypoint", "app") app.topic("test", use_subscription=True, config={"enableExactlyOnceDelivery": True})(dummy_function) app.deploy( force=True, skip_function=True, config={"pubsub": { "serviceAccountEmail": service_account }}, ) put_subscription = get_response( "pubsub-deploy-subscription-config", "put-v1-projects-goblet-subscriptions-goblet-topic-subscription-config-test_1.json", ) responses = get_responses("pubsub-deploy-subscription-config") assert put_subscription["body"]["enableExactlyOnceDelivery"] assert len(responses) == 2
def test_deploy_pubsub(self, monkeypatch, requests_mock): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "pubsub-deploy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") requests_mock.register_uri("PUT", "https://storage.googleapis.com/mock") app = Goblet(function_name="goblet_topic") setattr(app, "entrypoint", "app") app.topic("test-topic")(dummy_function) app.deploy(force=True) responses = get_responses("pubsub-deploy") assert len(responses) == 3 assert responses[2]["body"]["metadata"]["target"].endswith( "goblet_topic-topic-test-topic") assert (responses[2]["body"]["metadata"]["request"]["eventTrigger"] ["resource"] == "projects/goblet/topics/test-topic") assert ( responses[2]["body"]["metadata"]["request"]["eventTrigger"] ["eventType"] == "providers/cloud.pubsub/eventTypes/topic.publish")
def test_deploy_cloudrun(self, monkeypatch, requests_mock): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "deployer-cloudrun-deploy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") requests_mock.register_uri("PUT", "https://storage.googleapis.com/mock") app = Goblet(function_name="goblet", backend="cloudrun") setattr(app, "entrypoint", "app") app.handlers["http"] = HTTP(dummy_function) app.deploy( only_function=True, force=True, config={ "cloudrun_revision": { "serviceAccount": "*****@*****.**" } }, ) responses = get_responses("deployer-cloudrun-deploy") assert len(responses) == 9
def test_deploy_storage(self, monkeypatch, requests_mock): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "storage-deploy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") requests_mock.register_uri("PUT", "https://storage.googleapis.com/mock") app = Goblet(function_name="goblet_storage") setattr(app, "entrypoint", "app") app.storage("test", "finalize")(dummy_function) app.deploy(force=True) responses = get_responses("storage-deploy") assert len(responses) == 3 assert responses[2]["body"]["metadata"]["target"].endswith( "goblet_storage-storage-test-finalize") assert (responses[2]["body"]["metadata"]["request"]["eventTrigger"] ["resource"] == "projects/goblet/buckets/test") assert (responses[2]["body"]["metadata"]["request"]["eventTrigger"] ["eventType"] == "google.storage.object.finalize")
def test_deploy_pubsub_cross_project(self, monkeypatch, requests_mock): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "pubsub-deploy-cross-project") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") service_account = "*****@*****.**" requests_mock.register_uri("PUT", "https://storage.googleapis.com/mock") app = Goblet(function_name="goblet-topic-cross-project") setattr(app, "entrypoint", "app") app.topic("test", project="goblet-cross-project")(dummy_function) app.deploy(force=True, config={"pubsub": { "serviceAccountEmail": service_account }}) put_subscription = get_response( "pubsub-deploy-cross-project", "put-v1-projects-goblet-subscriptions-goblet-topic-cross-project-test_1.json", ) responses = get_responses("pubsub-deploy-cross-project") assert "goblet-cross-project" in put_subscription["body"]["topic"] assert len(responses) == 5
def test_destroy_pubsub(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "pubsub-destroy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") pubsub = PubSub( "goblet_topic", resources={ "test-topic": { "trigger": { "test-topic": {} }, "subscription": {} } }, ) pubsub.destroy() responses = get_responses("pubsub-destroy") assert len(responses) == 1 assert responses[0]["body"]["metadata"]["type"] == "DELETE_FUNCTION" assert responses[0]["body"]["metadata"]["target"].endswith( "goblet_topic-topic-test-topic")
def test_update_pubsub_subscription(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "pubsub-update-subscription") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") pubsub = PubSub("test-cross-project") pubsub.register_topic( "test", None, kwargs={ "topic": "test", "kwargs": { "project": "goblet-cross-project" } }, ) new_service_account = "*****@*****.**" pubsub._deploy( config={"pubsub": { "serviceAccountEmail": new_service_account }}) responses = get_responses("pubsub-update-subscription") assert len(responses) == 3 assert (responses[1]["body"]["pushConfig"]["oidcToken"] ["serviceAccountEmail"] == new_service_account) assert responses[2]["body"]["error"]["code"] == 409
def test_deploy_schedule_cloudrun(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "schedule-deploy-cloudrun") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") scheduler = Scheduler("goblet", backend="cloudrun") cloudrun_url = "https://goblet-12345.a.run.app" service_account = "*****@*****.**" scheduler.register_job( "test-job", None, kwargs={ "schedule": "* * * * *", "timezone": "UTC", "kwargs": {} }, ) scheduler._deploy( config={"scheduler": { "serviceAccount": service_account }}) responses = get_responses("schedule-deploy-cloudrun") assert responses[0]["body"]["status"]["url"] == cloudrun_url assert (responses[1]["body"]["httpTarget"]["oidcToken"] ["serviceAccountEmail"] == service_account) assert (responses[1]["body"]["httpTarget"]["oidcToken"]["audience"] == cloudrun_url) assert responses[1]["body"]["schedule"] == "* * * * *"
def test_deploy_schedule(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "schedule-deploy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") goblet_name = "goblet_example" scheduler = Scheduler(goblet_name) scheduler.register_job( "test-job", None, kwargs={ "schedule": "* * * * *", "timezone": "UTC", "kwargs": {} }, ) scheduler.deploy() responses = get_responses("schedule-deploy") assert goblet_name in responses[0]["body"]["name"] assert (responses[1]["body"]["httpTarget"]["headers"]["X-Goblet-Name"] == "test-job") assert (responses[1]["body"]["httpTarget"]["headers"]["X-Goblet-Type"] == "schedule") assert responses[1]["body"]["schedule"] == "* * * * *"
def test_destroy_http_function_all(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "deployer-function-destroy-all") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") app = Goblet(function_name="goblet_example") app.destroy(all=True) responses = get_responses("deployer-function-destroy-all") assert len(responses) == 4
def test_destroy_cloudrun(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "deployer-cloudrun-destroy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") app = Goblet(function_name="goblet", backend="cloudrun") app.destroy() responses = get_responses("deployer-cloudrun-destroy") assert len(responses) == 1 assert responses[0]["body"]["status"] == "Success" assert responses[0]["body"]["details"]["name"] == "goblet"
def test_deploy_http_function(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "deployer-function-deploy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") app = Goblet(function_name="goblet_example") setattr(app, "entrypoint", "app") app.handlers["http"] = HTTP(dummy_function) Deployer().deploy(app, only_function=True, force=True) responses = get_responses("deployer-function-deploy") assert len(responses) == 3
def test_sync_pubsub_cloudrun(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "pubsub-sync-cloudrun") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") pubsub = PubSub("goblet", backend="cloudrun") pubsub.sync(dryrun=True) pubsub.sync() responses = get_responses("pubsub-sync-cloudrun") assert len(responses) == 3 assert responses[1] == responses[2] assert responses[0]["body"] == {}
def test_update_eventarc(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "eventarc-update") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") eventarc = EventArc( "test-eventarc", backend="cloudrun", resources=[{ "trigger_name": "test-eventarc-bucket-get", "event_filters": [ { "attribute": "type", "value": "google.cloud.audit.log.v1.written", }, { "attribute": "methodName", "value": "storage.objects.get" }, { "attribute": "serviceName", "value": "storage.googleapis.com" }, ], "topic": None, "region": "us-central1", "name": "bucket_get", "func": None, }], ) eventarc._deploy(config={ "eventarc": { "serviceAccount": "*****@*****.**" } }) responses = get_responses("eventarc-update") assert len(responses) == 2 assert "test-eventarc-bucket-get" in responses[0]["body"]["metadata"][ "target"] assert responses[1]["body"]["error"]["status"] == "ALREADY_EXISTS"
def test_deploy_http_function(self, monkeypatch, requests_mock): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "deployer-function-deploy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") requests_mock.register_uri("PUT", "https://storage.googleapis.com/mock") app = Goblet(function_name="goblet_example") setattr(app, "entrypoint", "app") app.handlers["http"] = HTTP(dummy_function) app.deploy(only_function=True, force=True) responses = get_responses("deployer-function-deploy") assert len(responses) == 3
def test_destroy_http_function(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "deployer-function-destroy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") app = Goblet(function_name="goblet_test_app") app.destroy() responses = get_responses("deployer-function-destroy") assert len(responses) == 1 assert responses[0]["body"]["metadata"]["type"] == "DELETE_FUNCTION" assert ( responses[0]["body"]["metadata"]["target"] == "projects/goblet/locations/us-central1/functions/goblet_test_app" )
def test_deploy_http_function_v2(self, monkeypatch, requests_mock): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "deployer-function-deploy-v2") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") requests_mock.register_uri("PUT", "https://storage.googleapis.com/mock") app = Goblet(function_name="goblet-test-http-v2", backend="cloudfunctionv2") setattr(app, "entrypoint", "app") app.handlers["http"].register_http(dummy_function, {}) app.deploy(only_function=True, force=True, config={"runtime": "python38"}) responses = get_responses("deployer-function-deploy-v2") assert len(responses) == 3
def test_sync_eventarc(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "eventarc-sync") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") eventarc = EventArc( "test-eventarc", backend="cloudrun", resources=[{ "trigger_name": "test-eventarc-bucket-get", "event_filters": [ { "attribute": "type", "value": "google.cloud.audit.log.v1.written", }, { "attribute": "methodName", "value": "storage.objects.get" }, { "attribute": "serviceName", "value": "storage.googleapis.com" }, ], "topic": None, "region": "us-central1", "name": "bucket_get", "func": None, }], ) eventarc.sync(dryrun=True) eventarc.sync() responses = get_responses("eventarc-sync") assert len(responses) == 3 assert responses[1] == responses[2] assert ("test-eventarc-bucket-get" not in responses[0]["body"]["metadata"]["target"])
def test_set_iam_bindings(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "deployer-function-bindings") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") app = Goblet(function_name="goblet_bindings") setattr(app, "entrypoint", "app") app.handlers["http"] = HTTP(dummy_function) bindings = [{"role": "roles/cloudfunctions.invoker", "members": ["allUsers"]}] Deployer(config={"name": "goblet_test_app"}).deploy( app, only_function=True, config={"bindings": bindings}, force=True ) responses = get_responses("deployer-function-bindings") assert len(responses) == 4 assert responses[2]["body"]["bindings"] == bindings
def test_set_iam_bindings(self, monkeypatch, requests_mock): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "deployer-function-bindings") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") requests_mock.register_uri("PUT", "https://storage.googleapis.com/mock") app = Goblet(function_name="goblet_bindings") setattr(app, "entrypoint", "app") app.handlers["http"] = HTTP(dummy_function) bindings = [{"role": "roles/cloudfunctions.invoker", "members": ["allUsers"]}] app.deploy(only_function=True, config={"bindings": bindings}, force=True) responses = get_responses("deployer-function-bindings") assert len(responses) == 4 assert responses[2]["body"]["bindings"] == bindings
def test_destroy_routes(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "routes-destroy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") apigw = ApiGateway("goblet_routes", resources=["not_empty"]) apigw.destroy() responses = get_responses("routes-destroy") assert responses[0]["body"]["metadata"]["verb"] == "delete" assert responses[0]["body"]["metadata"]["target"].endswith( "goblet-routes") assert responses[1]["body"]["metadata"]["verb"] == "delete" assert responses[1]["body"]["metadata"]["target"].endswith( "goblet-routes") assert responses[2]["body"]["metadata"]["verb"] == "delete" assert responses[2]["body"]["metadata"]["target"].endswith( "goblet-routes")
def test_destroy_schedule(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "schedule-destroy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") goblet_name = "goblet_example" scheduler = Scheduler(goblet_name) scheduler.register_job("test-job", None, kwargs={ "schedule": "* * * * *", "kwargs": {} }) scheduler.destroy() responses = get_responses("schedule-destroy") assert len(responses) == 1 assert responses[0]["body"] == {}
def test_sync_schedule(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "schedule-sync") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") goblet_name = "goblet" scheduler = Scheduler(goblet_name) scheduler.register_job("scheduled_job", None, kwargs={ "schedule": "* * * * *", "kwargs": {} }) scheduler.sync(dryrun=True) scheduler.sync(dryrun=False) responses = get_responses("schedule-sync") assert len(responses) == 3 assert responses[1] == responses[2] assert responses[0]["body"] == {}
def test_destroy_pubsub_cloudrun(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "pubsub-destroy-cloudrun") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") pubsub = PubSub( "goblet", resources={"test": { "trigger": {}, "subscription": { "test": {} } }}, backend="cloudrun", ) pubsub.destroy() responses = get_responses("pubsub-destroy-cloudrun") assert len(responses) == 1 assert responses[0]["body"] == {}
def test_destroy_storage(self, monkeypatch): monkeypatch.setenv("GOOGLE_PROJECT", "goblet") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") monkeypatch.setenv("GOBLET_TEST_NAME", "storage-destroy") monkeypatch.setenv("GOBLET_HTTP_TEST", "REPLAY") storage = Storage( "goblet_storage", resources=[{ "bucket": "test", "event_type": "finalize", "name": "test" }], ) storage.destroy() responses = get_responses("storage-destroy") assert len(responses) == 1 assert responses[0]["body"]["metadata"]["type"] == "DELETE_FUNCTION" assert responses[0]["body"]["metadata"]["target"].endswith( "goblet_storage-storage-test-finalize")