Skip to content

[FIX] Handle HttpError from GCP VM creation in start_test - #1279

Open
kaihere14 wants to merge 2 commits into
CCExtractor:masterfrom
kaihere14:fix/create-instance-httperror
Open

kaihere14 wants to merge 2 commits into
CCExtractor:masterfrom
kaihere14:fix/create-instance-httperror

Conversation

@kaihere14

@kaihere14 kaihere14 commented Nov 16, 2026 •

Copy link
Copy Markdown

In raising this pull request, I confirm the following (please check boxes):

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • I have considered, and confirmed that this submission will be valuable to others.
  • I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  • I give this submission freely, and claim no ownership to its content.

My familiarity with the project is as follows (check one):

  • I have never used the project.
  • I have used the project briefly.
  • I have used the project extensively, but have not contributed previously.
  • I am an active contributor to the project.

What happens today

When the platform starts a test, start_test() calls create_instance(), which asks Google Cloud to create the VM with compute.instances().insert(...).execute().

The code after that call expects any failure to come back as an error field in the returned result. But when Google Cloud rejects the request outright, the client library does not return anything. It raises an HttpError. That happens for quota and rate limits, server errors, a wrong machine type or config, or a missing image family (the getFromFamily(...).execute() calls in create_instance() can raise the same way).

Nothing in start_test() catches that exception. The artifact download a few lines above it is wrapped in try/except, but the VM creation is not. The exception goes up to the catch-all added to gcp_instance() in #1244, which logs "could not be started, will retry on next cron run" and leaves the test pending.

For a temporary problem that is fine. For a permanent one it is not: the test is retried every 10 minutes, forever. It never gets marked as failed, the PR author never sees a status on GitHub, and every retry writes the test XML files and downloads the build artifact again.

The tests did not catch this because every test that mocks create_instance only sets a return_value. None of them makes it raise.

What should happen

An error that Google Cloud raises should be treated the same way as one it returns:

  • Temporary problems (quota, rate limit, 429, 5xx) should leave the test pending, so the next cron run tries again.
  • Permanent problems (bad config, missing image, VM already exists, and so on) should mark the test as failed straight away, with the same user-friendly message the platform already uses.

How this PR fixes it

  • start_test() now wraps the create_instance() call in try/except HttpError. It logs the error and passes it to the existing if 'error' in operation: check, so there is no second copy of the retry-or-fail logic.
  • A new helper, gcp_error_from_http_error(), turns the HttpError into the same {'error': {'errors': [{'code': ..., 'message': ...}]}} shape that a failed operation has. The error body uses camelCase reasons, so the known ones are mapped to the codes the platform already uses:
    • quotaExceeded -> QUOTA_EXCEEDED (retried)
    • rateLimitExceeded, userRateLimitExceeded -> RATE_LIMIT_EXCEEDED (new, retried)
    • backendError, internalError -> BACKEND_ERROR (new, retried)
    • notFound -> RESOURCE_NOT_FOUND (fails the test)
    • alreadyExists -> RESOURCE_ALREADY_EXISTS (fails the test)
  • Any 429 or 5xx response is retried whatever reason it gives. Any other unknown reason is kept as the code and fails the test with the existing generic "contact the administrator" message. If the body isn't JSON, the code becomes HTTP_<status>.
  • RATE_LIMIT_EXCEEDED and BACKEND_ERROR are added to GCP_RETRYABLE_ERRORS and GCP_ERROR_MESSAGES.

Only HttpError is caught. Other exceptions, such as network errors, still reach the catch-all in gcp_instance() and are retried on the next cron run, as before.

Tests

  • TestGcpErrorFromHttpError checks the mapping as a table of subtests: quota, rate limit, not found, already exists, an unknown reason, a 503, a 429 with an HTML body, and a 400 with a plain-text body. A second test checks that the raw GCP message is kept for the log but not shown to the user.
  • Two new tests in TestVMCreationVerification make compute.instances().insert().execute() raise a real HttpError, with create_instance itself left unmocked so the real call path is covered:
    • a 403 quota error, a 403 rate limit and a 503 leave the test pending: no failure recorded, no GcpInstance row added, no wait on an operation;
    • a 404 and a 400 mark the test failed with the right user-facing message.

All 5 start_test cases fail on current master (the HttpError escapes start_test) and pass with this change. The full suite passes (819 tests), and isort, pycodestyle, pydocstyle, mypy and dodgy are clean.

The Compute API raises HttpError when it rejects an instance insert
(bad config, missing image, quota, rate limit, server error) instead of
returning an error inside the operation. start_test() only checked the
returned operation, so the exception escaped to gcp_instance(), which
logs it and leaves the test pending. A permanent error was retried on
every cron run and the test never failed or reported back to GitHub.

Catch HttpError around create_instance() and convert it to the same
error shape a failed operation has, so the existing retry rules and
messages apply. 429 and 5xx responses, quota and rate limit errors are
retried; anything else marks the test as failed.
The four start_test HttpError tests repeated the same decorator stack and setup,
which SonarCloud counted as duplicated new code. They now share one helper that
patches with an ExitStack and raises from compute.instances().insert().execute(),
and the cases run as subtests. The gcp_error_from_http_error unit tests are a
single table as well.
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant