Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cassettes): add check for cassettes interactions #1145

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1,155 changes: 561 additions & 594 deletions scaleway/testdata/data-source-rdb-privilege-basic.cassette.yaml

Large diffs are not rendered by default.

283 changes: 124 additions & 159 deletions scaleway/testdata/data-source-vpc-private-network-basic.cassette.yaml

Large diffs are not rendered by default.

326 changes: 162 additions & 164 deletions scaleway/testdata/data-source-vpc-public-gateway-basic.cassette.yaml

Large diffs are not rendered by default.

6,259 changes: 2,726 additions & 3,533 deletions scaleway/testdata/instance-server-with-placement-group.cassette.yaml

Large diffs are not rendered by default.

3,521 changes: 1,498 additions & 2,023 deletions scaleway/testdata/instance-server-with-reserved-ip.cassette.yaml

Large diffs are not rendered by default.

684 changes: 0 additions & 684 deletions scaleway/testdata/object-bucket-cors-empty-origin.cassette.yaml

This file was deleted.

367 changes: 148 additions & 219 deletions scaleway/testdata/object-bucket-destroy-force.cassette.yaml

Large diffs are not rendered by default.

842 changes: 420 additions & 422 deletions scaleway/testdata/rdb-instance-private-network-dhcp.cassette.yaml

Large diffs are not rendered by default.

1,567 changes: 800 additions & 767 deletions scaleway/testdata/rdb-privilege-basic.cassette.yaml

Large diffs are not rendered by default.

349 changes: 156 additions & 193 deletions scaleway/testdata/vpc-gateway-network-without-dhcp.cassette.yaml

Large diffs are not rendered by default.

275 changes: 85 additions & 190 deletions scaleway/testdata/vpc-private-network-basic.cassette.yaml

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions scaleway/validate_cassettes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package scaleway

import (
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"testing"

"github.com/dnaeon/go-vcr/cassette"
"github.com/stretchr/testify/assert"
)

const testDirectory = "testdata/"

// getTestFiles returns a map of cassettes files
func getTestFiles() (map[string]struct{}, error) {
filesMap := make(map[string]struct{})
files, err := ioutil.ReadDir(testDirectory)
if err != nil {
return nil, err
}

for _, file := range files {
filesMap[fileNameWithoutExtSuffix(file.Name())] = struct{}{}
}

return filesMap, nil
}

func TestAccScalewayCassettes_Validator(t *testing.T) {
files, err := getTestFiles()
assert.NoError(t, err)

for name := range files {
c, err := cassette.Load(fmt.Sprintf("%s%s", testDirectory, name))
assert.NoError(t, err)
assert.NoError(t, checkErrorCode(c))
}
}

func checkErrorCode(c *cassette.Cassette) error {
for _, i := range c.Interactions {
if !checkErrCode(i, c, http.StatusConflict, http.StatusInternalServerError) {
return fmt.Errorf("status: %v founded on %s. method: %s, url %s", i.Code, c.Name, i.Request.Method, i.Request.URL)
}
}

return nil
}

func exceptionsCassettesCases() map[string]struct{} {
return map[string]struct{}{
"testdata/object-bucket-destroy-force.cassette.yaml": {},
"testdata/rdb-privilege-basic.cassette.yaml": {},
"testdata/data-source-rdb-privilege-basic.cassette.yaml": {}}
}

func checkErrCode(i *cassette.Interaction, c *cassette.Cassette, codes ...int) bool {
exceptions := exceptionsCassettesCases()
_, isException := exceptions[c.File]
if isException {
return isException
}

for _, httpCode := range codes {
if i.Code == httpCode {
return true
}
}

return true
}

func fileNameWithoutExtSuffix(fileName string) string {
return strings.TrimSuffix(fileName, filepath.Ext(fileName))
}