Package: goblet-gcp-client
Goblet GCP ClientGoblet GCP Client is a util library with support for creating GCP resource clients, GCP integration tests, and other related utils.Getting startedTo install run:pip install goblet-gcp-clientUsing a Clientfromgoblet_gcp_clientimportClientcloudfunction_client=Client("cloudfunctions","v1",calls="projects.locations.functions",parent_schema="projects/{project_id}/locations/{location_id}",)scheduler_client=Client("cloudscheduler","v1",calls="projects.locations.jobs",parent_schema="projects/{project_id}/locations/{location_id}",)# override default parent_key with "name"cloudfunction_client.execute("get",parent_key="name",parent_schema="projects/{project_id}/locations/{location_id}/functions/{name}")# pass in body paramssample_body={}resp=cloudfunction_client.execute("patch",parent_key="name",parent_schema="projects/{project_id}/locations/{location_id}/functions/{name}",params={"body":sample_body},)# Wait for previous operation to completecloudfunction_client.wait_for_operation(resp["name"],calls="operations")Writing Integration Tests:Write your test and set env variableG_HTTP_TESTtoRECORDBy default responses will be written to the/tests/datafolder. You can customize this by settingG_TEST_DATA_DIRenv variable.Setting theG_MOCK_CREDENTIALSenvironment variable will use AnonymousCredentials.defTestDeploy(self):monkeypatch.setenv("G_HTTP_TEST","RECORD")monkeypatch.setenv("G_TEST_NAME","TEST_NAME")cloudfunction_client=Client("cloudfunctions","v1",calls="projects.locations.functions",parent_schema="projects/{project_id}/locations/{location_id}",)cloudfunction_client.execute("get",parent_key="name",parent_schema="projects/{project_id}/locations/{location_id}/functions/{name}")Running your test will record all responses that your Client makesNow you can run your tests withG_HTTP_TESTwithREPLAY. You can access the responses withget_responsesorget_responsefromgoblet_gcp_clientimportget_responses,get_response,get_replay_count,reset_replay_countdefTestDeploy(self):monkeypatch.setenv("G_HTTP_TEST","REPLAY")monkeypatch.setenv("G_TEST_NAME","TEST_NAME")reset_replay_count()cloudfunction_client=Client("cloudfunctions","v1",calls="projects.locations.functions",parent_schema="projects/{project_id}/locations/{location_id}",)cloudfunction_client.execute("get",parent_key="name",parent_schema="projects/{project_id}/locations/{location_id}/functions/{name}")responses=get_responses("TEST_NAME")assertlen(responses)==2assert"test_value"inresponses[0]["body"]assertget_replay_count()==1You can assert that your tests are making all required api called by usingget_replay_count. If you are running multiple tests you can runreset_replay_countbefore each test to ensure the counts are accurate.Point to EmulatorYou can point to an emulator by setting theG_EMULATOR_HOSTenvironment variable, or specifying in the Client constructor.spanner_instances_client=Client(resource="spanner",version="v1",calls="projects.instances"emulator_host="localhost:9020",)FeaturesGCP resource clientsGCP HTTP Test Recording and ReplayingExamplesClient and Testing ExamplesIssuesPlease file any issues, bugs or feature requests as an issue on ourGitHubpage.Want to ContributeIf you would like to contribute to the library (e.g. by improving the documentation, solving a bug or adding a cool new feature) submit apull request.