Skip to content

Commit

Permalink
Merge pull request #20 from omc/qa/allow-recording-requests-fixtures-…
Browse files Browse the repository at this point in the history
…safely

QA/Allow recording requests fixtures safely
  • Loading branch information
momer authored May 4, 2024
2 parents aa0928e + 199c60c commit 2063d57
Show file tree
Hide file tree
Showing 29 changed files with 1,059 additions and 61 deletions.
4 changes: 1 addition & 3 deletions bonsai/bonsai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"log/slog"
"os"
"path/filepath"

"github.com/omc/bonsai-api-go/v1/bonsai"
)

func init() {
Expand Down Expand Up @@ -39,7 +37,7 @@ func initLogger() {

func assertGolden(s *ClientVCRTestSuite, expected any) {
s.T().Helper()
bonsai.AssertGolden(
AssertGolden(
s.T(),
filepath.Join("fixtures/golden/", s.normalize(s.T().Name())),
s.update(s.T().Name()),
Expand Down
46 changes: 28 additions & 18 deletions bonsai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type ClientVCRTestSuite struct {
}

func (s *ClientVCRTestSuite) ReadOnlyRun() bool {
return s.recordMode == recorder.ModePassthrough
return s.recordMode == recorder.ModeReplayOnly
}
func (s *ClientVCRTestSuite) WillRecord() bool {
return !s.ReadOnlyRun()
Expand All @@ -72,10 +72,14 @@ func (s *ClientVCRTestSuite) SetRecorderMode(mode string) {
s.recordMode = recorder.ModeRecordOnce
case "REC_ONLY":
s.recordMode = recorder.ModeRecordOnly
case "REPLAY_ONLY":
s.recordMode = recorder.ModeReplayOnly
case "REPLAY_WITH_NEW":
s.recordMode = recorder.ModeReplayWithNewEpisodes
default:
case "PASS_THROUGH":
s.recordMode = recorder.ModePassthrough
default:
s.recordMode = recorder.ModeReplayOnly
}
}

Expand Down Expand Up @@ -136,26 +140,34 @@ func (s *ClientVCRTestSuite) SetupSuite() {
func (s *ClientVCRTestSuite) BeforeTest(_, testName string) {
var err error

if s.WillRecord() {
s.recorder, err = recorder.NewWithOptions(
&recorder.Options{
// filepath is os agnostic
CassetteName: filepath.Join("fixtures/golden/", s.normalize(testName)),
Mode: s.recordMode,
RealTransport: s.client.Transport(),
SkipRequestLatency: true,
},
)
if err != nil {
log.Fatalf("failed to create new recorder: %+v\n", err)
}
s.recorder, err = recorder.NewWithOptions(
&recorder.Options{
// filepath is os agnostic
CassetteName: filepath.Join("fixtures/vcr/", s.normalize(testName)),
Mode: s.recordMode,
SkipRequestLatency: true,
},
)

if err != nil {
log.Fatalf("failed to create new recorder: %+v\n", err)
}

if s.WillRecord() {
// Add a hook which removes Authorization headers from all requests
hook := func(i *cassette.Interaction) error {
delete(i.Request.Headers, "Authorization")
return nil
}
s.recorder.AddHook(hook, recorder.AfterCaptureHook)

// Remove headers that might include secrets.
s.recorder.AddHook(riskyHeaderFilter, recorder.AfterCaptureHook)

s.client.SetTransport(s.recorder)
} else {
s.recorder.SetReplayableInteractions(true)
s.client.SetTransport(s.recorder)
}
}

Expand Down Expand Up @@ -188,9 +200,7 @@ func (s *ClientVCRTestSuite) normalize(path string) string {
}

func TestClientVCRTestSuite(t *testing.T) {
if _, ok := os.LookupEnv("BONSAI_RUN_INTEGRATION_TESTS"); ok {
suite.Run(t, new(ClientVCRTestSuite))
}
suite.Run(t, new(ClientVCRTestSuite))
}

// ClientMockTestSuite is used for all mocked web requests.
Expand Down
2 changes: 1 addition & 1 deletion bonsai/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ func (s *ClientVCRTestSuite) TestClusterClient_Create() {
func (s *ClientVCRTestSuite) TestClusterClient_Update() {
ctx := context.Background()

plan, err := s.client.Cluster.Update(ctx, "bonsai-api-go-9994392953", bonsai.ClusterUpdateOpts{
plan, err := s.client.Cluster.Update(ctx, "bonsai-api-go-2471463249", bonsai.ClusterUpdateOpts{
Name: "bonsai-api-go-test-cluster-updated",
Plan: "standard-nano-comped",
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"message": "Your cluster is being provisioned.",
"monitor": "https://api.bonsai.io/clusters/bonsai-api-go-9994392953",
"monitor": "https://api.bonsai.io/clusters/bonsai-api-go-2471463249",
"access": {
"host": "bonsai-api-go-9994392953",
"host": "bonsai-api-go-2471463249",
"port": 443,
"scheme": "https",
"user": "REDACTED",
"pass": "REDACTED",
"url": "://REDACTED:REDACTED@"
"url": "://REDACTED:REDACTED"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"message": "Your cluster is being updated.",
"monitor": "https://api.bonsai.io/clusters/bonsai-api-go-9994392953"
"monitor": "https://api.bonsai.io/clusters/bonsai-api-go-2471463249"
}
87 changes: 87 additions & 0 deletions bonsai/fixtures/golden/TestClientVCRTestSuite-TestPlanClient-All
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,93 @@
}
]
},
{
"slug": "sandbox-aws-us-east-1",
"name": "Sandbox",
"billing_interval_months": 1,
"available_releases": [
{
"slug": "elasticsearch-7.10.2"
},
{
"slug": "opensearch-2.6.0-mt"
}
],
"available_spaces": [
{
"path": "omc/bonsai-gcp/us-east4/common",
"private_network": false,
"cloud": {
"provider": "",
"region": ""
}
},
{
"path": "omc/bonsai-gcp/us-west1/common",
"private_network": false,
"cloud": {
"provider": "",
"region": ""
}
},
{
"path": "omc/bonsai/ap-northeast-1/common",
"private_network": false,
"cloud": {
"provider": "",
"region": ""
}
},
{
"path": "omc/bonsai/ap-southeast-2/common",
"private_network": false,
"cloud": {
"provider": "",
"region": ""
}
},
{
"path": "omc/bonsai/eu-central-1/common",
"private_network": false,
"cloud": {
"provider": "",
"region": ""
}
},
{
"path": "omc/bonsai/eu-west-1/common",
"private_network": false,
"cloud": {
"provider": "",
"region": ""
}
},
{
"path": "omc/bonsai/us-east-1/common",
"private_network": false,
"cloud": {
"provider": "",
"region": ""
}
},
{
"path": "omc/bonsai/us-west-2/common",
"private_network": false,
"cloud": {
"provider": "",
"region": ""
}
},
{
"path": "omc/websolr/us-east-1/common",
"private_network": false,
"cloud": {
"provider": "",
"region": ""
}
}
]
},
{
"slug": "standard-micro-aws-us-east-1",
"name": "Standard Micro",
Expand Down
3 changes: 0 additions & 3 deletions bonsai/fixtures/golden/TestClusterClient-All.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions bonsai/fixtures/golden/TestClusterClient-Create.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions bonsai/fixtures/golden/TestClusterClient-Delete.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions bonsai/fixtures/golden/TestClusterClient-GetBySlug.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions bonsai/fixtures/golden/TestClusterClient-Update.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions bonsai/fixtures/golden/TestPlanClient-All.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions bonsai/fixtures/golden/TestPlanClient-GetByPath.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions bonsai/fixtures/golden/TestReleaseClient-All.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions bonsai/fixtures/golden/TestReleaseClient-GetByPath.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions bonsai/fixtures/golden/TestSpaceClient-All.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions bonsai/fixtures/golden/TestSpaceClient-GetByPath.yaml

This file was deleted.

75 changes: 75 additions & 0 deletions bonsai/fixtures/vcr/TestClusterClient-All.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
version: 2
interactions:
- id: 0
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.bonsai.io
remote_addr: ""
request_uri: ""
body: ""
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- bonsai-api-go/v1 bonsai-api-go/1.0.0
url: https://api.bonsai.io/clusters
method: GET
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding:
- chunked
trailer: {}
content_length: -1
uncompressed: true
body: '{"pagination":{"page_number":1,"page_size":1000,"total_records":1},"clusters":[{"slug":"dcek-group-llc-5240651189","name":"DCEK Group, LLC search","uri":"https://api.bonsai.io/clusters/dcek-group-llc-5240651189","plan":{"slug":"sandbox-aws-us-east-1","uri":"https://api.bonsai.io/plans/sandbox-aws-us-east-1"},"release":{"service_type":"elasticsearch","package_name":"7.10.2","version":"7.10.2","slug":"elasticsearch-7.10.2","uri":"https://api.bonsai.io/releases/elasticsearch-7.10.2"},"space":{"path":"omc/bonsai/us-east-1/common","region":"aws-us-east-1","uri":"https://api.bonsai.io/spaces/omc/bonsai/us-east-1/common"},"stats":{"docs":2,"shards_used":2,"data_bytes_used":17984},"access":{"host":"dcek-group-llc-5240651189.us-east-1.bonsaisearch.net","port":443,"scheme":"https"},"state":"PROVISIONED"}]}'
headers:
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- keep-alive
Content-Security-Policy:
- 'default-src ''self'' https:; font-src ''self'' https: data:; img-src ''self'' https: data:; object-src ''none''; script-src ''self'' https: ''unsafe-inline''; style-src ''self'' https: ''unsafe-inline''; connect-src ''self'' http: wss://*.bonsaisearch.net'
Content-Type:
- application/json; charset=utf-8
Date:
- Sat, 04 May 2024 11:42:50 GMT
Etag:
- W/"067d073e09ae505a2c52df89d915b0af"
Referrer-Policy:
- strict-origin-when-cross-origin
Server:
- Cowboy
Strict-Transport-Security:
- max-age=63072000; includeSubDomains
Vary:
- Accept,Accept-Encoding
Via:
- 1.1 vegur
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- SAMEORIGIN
X-Permitted-Cross-Domain-Policies:
- none
X-Request-Id:
- 625ee3ed-ab85-4fde-8d5d-97d7d564680d
X-Runtime:
- "0.046763"
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: 292.8763ms
Loading

0 comments on commit 2063d57

Please sign in to comment.