Skill: Match Mock behaviour with real GCP api
This skill provides a structured workflow for matching the mock {service}{resource} behaviour with the real GCP.
When the golden tests for K8s Config Connector mock output diverge from real GCP output, we need to inspect the discrepancies and fix either the mock implementation or the normalizers. This ensures that hack/compare-mock runs cleanly and accurately represents GCP API contracts.
Critical Rules
- Real GCP Baseline Required: You must always generate the initial baseline
_http.logby runninghack/record-gcpagainst a real GCP project and commit it first. - Do NOT Generate Golden Logs from Mock: You are strictly forbidden from generating or updating
_http.logfiles against the mock in this step. You MUST first establish a baseline againstrealGCP withE2E_GCP_TARGET=real E2E_KUBE_TARGET=envtestwithWRITE_GOLDEN_OUTPUT=1.
Workflow
Step 1: Record GCP logs
- Run
hack/record-gcp "fixtures/^<testname>$"to capture real GCP behavior.- Troubleshooting Service Not Enabled: If
hack/record-gcpfails because a GCP service is not enabled (e.g., error mentions that the API is disabled or has not been used in the project before), enable the service usinggcloudand try again:
(For example:gcloud services enable <service-name>.googleapis.comgcloud services enable compute.googleapis.comorgcloud services enable run.googleapis.com)
- Troubleshooting Service Not Enabled: If
- Please commit the real gcp logs generated by running the command.
Important:
- It is important to commit the files modified by running realgcp tests in its own commit.
- This is for the human reviewer to compare the diff in the test artifacts when running real and mockgcp.
Step 2: Match mockgcp behaviour with real GCP
- Run
hack/compare-mock "fixtures/^<testname>$"to check mock behavior. - Iteratively fix discrepancies in the mock implementation or
normalize.go.
Tips for fixing the discrepancies:
- Look closely at the
compare-mockHTTP log differences (typically mock on left=>real on right). - For missing default values (e.g.,
<missing> => REGIONAL), add apopulateDefaultsFor<Resource>function to the mock service's file (e.g.mockgcp/mockcompute/networksv1.go). Make sure it is called onInsertandGet. - For generated IDs or volatile values (e.g. IPs, resource URLs) where real GCP generates dynamically but mockgcp outputs something static, you need to update the normalizer
mockgcp/mock<service>/normalize.go. - Run
hack/compare-mock "fixtures/^<testname>$"to see the diff and overwrite_http.log. - Run
git diffon the test fixtures to ensure that the golden_http.logaccurately replaces volatile data with placeholder variables (e.g.,${ipAddress}). - Certain operation metadata values (e.g.
done: <missing> => false) can be safely ignored as mock operations are generally simpler.
Source: GoogleCloudPlatform/k8s-config-connector — distributed by TomeVault.