Professional Cloud Developer · Domain 2: Building and testing applications · 23% of the exam
Task 2.3: Testing.
Writing unit tests with an AI coding assistant and running automated integration tests in Cloud Build.
Study it
Unit tests with an AI coding assistant and integration tests in Cloud Build
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the Google Cloud documentation page that proves it.
Question 1 · choose 1
An integration test suite is packaged as an image and started from a build
step. The tests call Firestore in a test project through the client library
with Application Default Credentials:
- id: integration
name: gcr.io/cloud-builders/docker
args: ['run', '--rm', '-e', 'GOOGLE_CLOUD_PROJECT=acme-it',
'${_TEST_IMAGE}']
The tests fail because the client library finds no credentials, although the
build's service account has the Firestore role in acme-it. What should the
team change?
AAdd --network=cloudbuild to the docker run arguments
BGrant the build's service account the Firestore role again on the build project
CPass -e GOOGLE_APPLICATION_CREDENTIALS=/workspace/key.json to docker run
DRun the step with entrypoint: bash and call docker run from a script
Show the answer and why
AAdd --network=cloudbuild to the docker run arguments
Correct
The cloudbuild network hosts ADC; a nested container must join it to find the credentials.
BGrant the build's service account the Firestore role again on the build project
Incorrect
The role exists; the nested container simply can't reach the credentials.
CPass -e GOOGLE_APPLICATION_CREDENTIALS=/workspace/key.json to docker run
Incorrect
No key file exists in /workspace, and the nested container doesn't mount it anyway.
DRun the step with entrypoint: bash and call docker run from a script
Incorrect
The shell changes nothing about the nested container's network.
Cloud Build attaches each step's container to a local Docker network named cloudbuild, which hosts Application Default Credentials for Google Cloud services. When a step runs nested Docker containers that need ADC, those containers must join the cloudbuild network with the --network flag.
A team moved its smoke test into the script field of a step:
- id: smoke
name: python:3.12
script: |
#!/usr/bin/env bash
python -m pytest tests/smoke --base-url "$_TEST_URL"
substitutions:
_TEST_URL: https://staging.example.com
The tests now run against an empty base URL. What should the team add?
A$$_TEST_URL in place of $_TEST_URL in the script
Boptions: substitutionOption: 'ALLOW_LOOSE'
Coptions: automapSubstitutions: true for the build
DThe substitution renamed to TEST_URL, without the leading underscore
Show the answer and why
A$$_TEST_URL in place of $_TEST_URL in the script
Incorrect
$$ only makes a literal $; the script still has no variable with that name in its environment.
Boptions: substitutionOption: 'ALLOW_LOOSE'
Incorrect
ALLOW_LOOSE only skips the check for missing substitutions.
Coptions: automapSubstitutions: true for the build
Correct
Scripts don't support substitutions directly; automapSubstitutions maps them to environment variables.
DThe substitution renamed to TEST_URL, without the leading underscore
Incorrect
User-defined substitutions must begin with an underscore, and scripts still wouldn't receive it.
The script field runs a shell script in a step. Scripts don't directly support substitutions, but they support environment variables: setting automapSubstitutions to true at the build level maps all substitutions to environment variables for the whole build, or each variable can be mapped by hand.
A test step uses the script field:
- name: ubuntu
script: |
if [[ -f tests/run.sh ]]; then bash tests/run.sh; fi
The step fails with a syntax error on [[, although the same line works in a
developer's Bash terminal. What should the team change?
AMove the line into args and keep the script field as well
BStart the script with #!/usr/bin/env bash
CRun the step in the python image without changing the script
DRun the step in the bash image without changing the script
Show the answer and why
AMove the line into args and keep the script field as well
Incorrect
A step that specifies script can't also specify args or entrypoint.
BStart the script with #!/usr/bin/env bash
Correct
Without a shebang, Cloud Build runs the script with #!/bin/sh, the basic sh shell, not Bash.
CRun the step in the python image without changing the script
Incorrect
The image doesn't pick the shell; without a shebang the script runs with /bin/sh.
DRun the step in the bash image without changing the script
Incorrect
Without a shebang the script is still run with /bin/sh.
The script field takes a single string, which can start with a shebang to choose the shell, such as #!/usr/bin/env bash. If there is no shebang, Cloud Build uses #!/bin/sh, the basic sh shell, not Bash. A step that specifies script can't also specify args or entrypoint.
A tester can see in Build History that the integration test build failed, but can't open its logs. The build writes its logs to a user-specified Cloud Storage bucket, ci-logs. Which role lets the tester read those logs?
AProject Viewer on the project that runs the builds
BCloud Build Approver on the project
CLogs Writer on the project
DStorage Object Viewer on the ci-logs bucket
Show the answer and why
AProject Viewer on the project that runs the builds
Incorrect
That role is what's needed when the logs are in the default bucket, not a user-specified one.
BCloud Build Approver on the project
Incorrect
Approval rights don't grant access to log objects in a bucket.
CLogs Writer on the project
Incorrect
Writing logs doesn't give read access to the bucket's log files.
DStorage Object Viewer on the ci-logs bucket
Correct
For logs in a user-specified bucket, principals need Storage Object Viewer in addition to the Cloud Build permissions.
To view build logs, principals need a role in addition to the Cloud Build permissions: Project Viewer if the logs are in the default Cloud Storage bucket, or Storage Object Viewer if the logs are in a user-specified bucket.
A monorepo keeps a service's code and tests under services/api. A test step must run pytest from that folder rather than from the repository root. Which step field does that?
Adir: 'services/api'
Bentrypoint: 'services/api'
Cvolumes with a volume named services/api
Did: 'services/api'
Show the answer and why
Adir: 'services/api'
Correct
dir sets the step's working directory, here /workspace/services/api.
Bentrypoint: 'services/api'
Incorrect
entrypoint sets the command the step runs, not its working directory.
Cvolumes with a volume named services/api
Incorrect
A volume adds a shared path; it doesn't change where the step runs.
Did: 'services/api'
Incorrect
id only names the step.
The dir field of a build step sets the working directory used when running the step's container. A relative value is relative to the build's working directory, so dir: 'services/api' runs the step in /workspace/services/api.