Skip to content

Commit

Permalink
Merge pull request #1372 from gemmahou/vcr-matcher
Browse files Browse the repository at this point in the history
vcr: Apply custom matcher
  • Loading branch information
google-oss-prow[bot] authored Mar 25, 2024
2 parents 4545799 + d150012 commit 523d79a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ interactions:
- application/json; charset=UTF-8
status: 404 Not Found
code: 404
duration: 214.994989ms
duration: 217.52251ms
- id: 1
request:
proto: HTTP/1.1
Expand Down Expand Up @@ -92,25 +92,25 @@ interactions:
body: |
{
"kind": "compute#operation",
"id": "6628313260286154337",
"name": "operation-1710467214059-613a92eb1a9a2-e761c6e4-c35dd456",
"id": "1302384153447785103",
"name": "operation-1710742623772-613e94e645e42-0e46b5eb-36d6a6e7",
"operationType": "compute.nodeTemplates.insert",
"targetLink": "https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/nodeTemplates/computenodetemplate-uniqueid111111",
"targetId": "1840433634538821217",
"targetId": "1885163588622639759",
"status": "RUNNING",
"user": "[email protected]",
"progress": 0,
"insertTime": "2024-03-14T18:46:54.656-07:00",
"startTime": "2024-03-14T18:46:54.697-07:00",
"selfLink": "https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710467214059-613a92eb1a9a2-e761c6e4-c35dd456",
"insertTime": "2024-03-17T23:17:04.077-07:00",
"startTime": "2024-03-17T23:17:04.115-07:00",
"selfLink": "https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710742623772-613e94e645e42-0e46b5eb-36d6a6e7",
"region": "https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1"
}
headers:
Content-Type:
- application/json; charset=UTF-8
status: 200 OK
code: 200
duration: 765.124261ms
duration: 474.632907ms
- id: 2
request:
proto: HTTP/1.1
Expand All @@ -127,7 +127,7 @@ interactions:
headers:
X-Goog-Api-Client:
- gl-go/1.21.5 gdcl/0.160.0
url: https://compute.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710467214059-613a92eb1a9a2-e761c6e4-c35dd456?alt=json&prettyPrint=false
url: https://compute.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710742623772-613e94e645e42-0e46b5eb-36d6a6e7?alt=json&prettyPrint=false
method: GET
response:
proto: HTTP/2.0
Expand All @@ -137,13 +137,13 @@ interactions:
trailer: {}
content_length: -1
uncompressed: true
body: '{"kind":"compute#operation","id":"6628313260286154337","name":"operation-1710467214059-613a92eb1a9a2-e761c6e4-c35dd456","operationType":"compute.nodeTemplates.insert","targetLink":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/nodeTemplates/computenodetemplate-uniqueid111111","targetId":"1840433634538821217","status":"DONE","user":"[email protected]","progress":100,"insertTime":"2024-03-14T18:46:54.656-07:00","startTime":"2024-03-14T18:46:54.697-07:00","endTime":"2024-03-14T18:46:54.994-07:00","selfLink":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710467214059-613a92eb1a9a2-e761c6e4-c35dd456","region":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1"}'
body: '{"kind":"compute#operation","id":"1302384153447785103","name":"operation-1710742623772-613e94e645e42-0e46b5eb-36d6a6e7","operationType":"compute.nodeTemplates.insert","targetLink":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/nodeTemplates/computenodetemplate-uniqueid111111","targetId":"1885163588622639759","status":"DONE","user":"[email protected]","progress":100,"insertTime":"2024-03-17T23:17:04.077-07:00","startTime":"2024-03-17T23:17:04.115-07:00","endTime":"2024-03-17T23:17:04.425-07:00","selfLink":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710742623772-613e94e645e42-0e46b5eb-36d6a6e7","region":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1"}'
headers:
Content-Type:
- application/json; charset=UTF-8
status: 200 OK
code: 200
duration: 346.050124ms
duration: 154.077924ms
- id: 3
request:
proto: HTTP/1.1
Expand Down Expand Up @@ -173,13 +173,13 @@ interactions:
body: |
{
"kind": "compute#nodeTemplate",
"id": "1840433634538821217",
"creationTimestamp": "2024-03-14T18:46:54.668-07:00",
"id": "1885163588622639759",
"creationTimestamp": "2024-03-17T23:17:04.087-07:00",
"name": "computenodetemplate-uniqueid111111",
"description": "Node template for sole tenant nodes running in us-central1, with 96vCPUs and any amount of memory on any machine type.",
"nodeAffinityLabels": {
"managed-by-cnrm": "true",
"memory_guarantee": "false",
"managed-by-cnrm": "true",
"cnrm-test": "true"
},
"status": "READY",
Expand All @@ -199,7 +199,7 @@ interactions:
- application/json; charset=UTF-8
status: 200 OK
code: 200
duration: 170.297617ms
duration: 250.057884ms
- id: 4
request:
proto: HTTP/1.1
Expand Down Expand Up @@ -229,8 +229,8 @@ interactions:
body: |
{
"kind": "compute#nodeTemplate",
"id": "1840433634538821217",
"creationTimestamp": "2024-03-14T18:46:54.668-07:00",
"id": "1885163588622639759",
"creationTimestamp": "2024-03-17T23:17:04.087-07:00",
"name": "computenodetemplate-uniqueid111111",
"description": "Node template for sole tenant nodes running in us-central1, with 96vCPUs and any amount of memory on any machine type.",
"nodeAffinityLabels": {
Expand All @@ -255,7 +255,7 @@ interactions:
- application/json; charset=UTF-8
status: 200 OK
code: 200
duration: 172.725397ms
duration: 108.804931ms
- id: 5
request:
proto: HTTP/1.1
Expand Down Expand Up @@ -285,14 +285,14 @@ interactions:
body: |
{
"kind": "compute#nodeTemplate",
"id": "1840433634538821217",
"creationTimestamp": "2024-03-14T18:46:54.668-07:00",
"id": "1885163588622639759",
"creationTimestamp": "2024-03-17T23:17:04.087-07:00",
"name": "computenodetemplate-uniqueid111111",
"description": "Node template for sole tenant nodes running in us-central1, with 96vCPUs and any amount of memory on any machine type.",
"nodeAffinityLabels": {
"cnrm-test": "true",
"managed-by-cnrm": "true",
"memory_guarantee": "false",
"cnrm-test": "true"
"memory_guarantee": "false"
},
"status": "READY",
"region": "https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1",
Expand All @@ -311,7 +311,7 @@ interactions:
- application/json; charset=UTF-8
status: 200 OK
code: 200
duration: 139.970247ms
duration: 172.9582ms
- id: 6
request:
proto: HTTP/1.1
Expand Down Expand Up @@ -341,25 +341,25 @@ interactions:
body: |
{
"kind": "compute#operation",
"id": "7418327646336849534",
"name": "operation-1710467217500-613a92ee627a5-f1e1baa3-4ae62678",
"id": "1935185951732687501",
"name": "operation-1710742626712-613e94e9139de-33ed3c19-1121ea67",
"operationType": "compute.nodeTemplates.delete",
"targetLink": "https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/nodeTemplates/computenodetemplate-uniqueid111111",
"targetId": "1840433634538821217",
"targetId": "1885163588622639759",
"status": "RUNNING",
"user": "[email protected]",
"progress": 0,
"insertTime": "2024-03-14T18:46:57.768-07:00",
"startTime": "2024-03-14T18:46:57.795-07:00",
"selfLink": "https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710467217500-613a92ee627a5-f1e1baa3-4ae62678",
"insertTime": "2024-03-17T23:17:06.938-07:00",
"startTime": "2024-03-17T23:17:06.965-07:00",
"selfLink": "https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710742626712-613e94e9139de-33ed3c19-1121ea67",
"region": "https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1"
}
headers:
Content-Type:
- application/json; charset=UTF-8
status: 200 OK
code: 200
duration: 405.508656ms
duration: 363.349932ms
- id: 7
request:
proto: HTTP/1.1
Expand All @@ -376,7 +376,7 @@ interactions:
headers:
X-Goog-Api-Client:
- gl-go/1.21.5 gdcl/0.160.0
url: https://compute.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710467217500-613a92ee627a5-f1e1baa3-4ae62678?alt=json&prettyPrint=false
url: https://compute.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710742626712-613e94e9139de-33ed3c19-1121ea67?alt=json&prettyPrint=false
method: GET
response:
proto: HTTP/2.0
Expand All @@ -386,10 +386,10 @@ interactions:
trailer: {}
content_length: -1
uncompressed: true
body: '{"kind":"compute#operation","id":"7418327646336849534","name":"operation-1710467217500-613a92ee627a5-f1e1baa3-4ae62678","operationType":"compute.nodeTemplates.delete","targetLink":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/nodeTemplates/computenodetemplate-uniqueid111111","targetId":"1840433634538821217","status":"DONE","user":"[email protected]","progress":100,"insertTime":"2024-03-14T18:46:57.768-07:00","startTime":"2024-03-14T18:46:57.795-07:00","endTime":"2024-03-14T18:46:58.153-07:00","selfLink":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710467217500-613a92ee627a5-f1e1baa3-4ae62678","region":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1"}'
body: '{"kind":"compute#operation","id":"1935185951732687501","name":"operation-1710742626712-613e94e9139de-33ed3c19-1121ea67","operationType":"compute.nodeTemplates.delete","targetLink":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/nodeTemplates/computenodetemplate-uniqueid111111","targetId":"1885163588622639759","status":"DONE","user":"[email protected]","progress":100,"insertTime":"2024-03-17T23:17:06.938-07:00","startTime":"2024-03-17T23:17:06.965-07:00","endTime":"2024-03-17T23:17:07.308-07:00","selfLink":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1/operations/operation-1710742626712-613e94e9139de-33ed3c19-1121ea67","region":"https://www.googleapis.com/compute/beta/projects/cnrm-user/regions/us-central1"}'
headers:
Content-Type:
- application/json; charset=UTF-8
status: 200 OK
code: 200
duration: 152.277906ms
duration: 171.223683ms
45 changes: 39 additions & 6 deletions tests/e2e/unified_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
package e2e

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"k8s.io/klog/v2"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -166,6 +170,12 @@ func testFixturesInSeries(ctx context.Context, t *testing.T, testPause bool, can
}
})

replaceFunc := func(s string) string {
result := strings.Replace(s, uniqueID, "uniqueid111111", -1)
result = strings.Replace(result, project.ProjectID, "cnrm-user", -1)
return result
}

hook := func(i *cassette.Interaction) error {
var requestHeadersToRemove = []string{
"Authorization",
Expand All @@ -189,12 +199,6 @@ func testFixturesInSeries(ctx context.Context, t *testing.T, testPause bool, can
delete(i.Response.Headers, header)
}

replaceFunc := func(s string) string {
result := strings.Replace(s, uniqueID, "uniqueid111111", -1)
result = strings.Replace(result, project.ProjectID, "cnrm-user", -1)
return result
}

i.Request.Body = replaceFunc(i.Request.Body)
i.Response.Body = replaceFunc(i.Response.Body)
i.Request.URL = replaceFunc(i.Request.URL)
Expand Down Expand Up @@ -228,6 +232,35 @@ func testFixturesInSeries(ctx context.Context, t *testing.T, testPause bool, can
return nil
}
h.VCRRecorder.AddHook(hook, recorder.BeforeSaveHook)

h.VCRRecorder.SetMatcher(func(r *http.Request, i cassette.Request) bool {
// We applied BeforeSaveHook, need to modify the incoming request,
// so that incoming request matches the saved request.
modifiedURL := replaceFunc(r.URL.String())

if r.Method != i.Method || modifiedURL != i.URL {
return false
}

// Default matcher only checks the request URL and Method. If request body exists, check the body as well.
// This guarantees that the replayed response matches what the real service would return for that particular request.
if r.Body != nil && r.Body != http.NoBody {
var reqBody []byte
var err error
reqBody, err = io.ReadAll(r.Body)
if err != nil {
t.Fatal("[VCR] Failed to read request body")
}
r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))

modifiedBody := replaceFunc(string(reqBody))
if modifiedBody != i.Body {
return false
}
}
return true
})
}

primaryResource, opt := loadFixture(project)
Expand Down

0 comments on commit 523d79a

Please sign in to comment.