|
| 1 | +package scaleway |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/dnaeon/go-vcr/cassette" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +const testDirectory = "testdata/" |
| 16 | + |
| 17 | +// getTestFiles returns a map of cassettes files |
| 18 | +func getTestFiles() (map[string]struct{}, error) { |
| 19 | + filesMap := make(map[string]struct{}) |
| 20 | + files, err := ioutil.ReadDir(testDirectory) |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + |
| 25 | + for _, file := range files { |
| 26 | + filesMap[fileNameWithoutExtSuffix(file.Name())] = struct{}{} |
| 27 | + } |
| 28 | + |
| 29 | + return filesMap, nil |
| 30 | +} |
| 31 | + |
| 32 | +func TestAccScalewayCassettes_Validator(t *testing.T) { |
| 33 | + files, err := getTestFiles() |
| 34 | + assert.NoError(t, err) |
| 35 | + |
| 36 | + for name := range files { |
| 37 | + c, err := cassette.Load(fmt.Sprintf("%s%s", testDirectory, name)) |
| 38 | + assert.NoError(t, err) |
| 39 | + assert.NoError(t, checkErrorCode(c)) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func checkErrorCode(c *cassette.Cassette) error { |
| 44 | + for _, i := range c.Interactions { |
| 45 | + if !checkErrCode(i, c, http.StatusConflict, http.StatusInternalServerError) { |
| 46 | + return fmt.Errorf("status: %v founded on %s. method: %s, url %s", i.Code, c.Name, i.Request.Method, i.Request.URL) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +func exceptionsCassettesCases() map[string]struct{} { |
| 54 | + return map[string]struct{}{ |
| 55 | + "testdata/object-bucket-destroy-force.cassette.yaml": {}, |
| 56 | + "testdata/rdb-privilege-basic.cassette.yaml": {}, |
| 57 | + "testdata/data-source-rdb-privilege-basic.cassette.yaml": {}} |
| 58 | +} |
| 59 | + |
| 60 | +func checkErrCode(i *cassette.Interaction, c *cassette.Cassette, codes ...int) bool { |
| 61 | + exceptions := exceptionsCassettesCases() |
| 62 | + _, isException := exceptions[c.File] |
| 63 | + if isException { |
| 64 | + return isException |
| 65 | + } |
| 66 | + |
| 67 | + for _, httpCode := range codes { |
| 68 | + if i.Code == httpCode { |
| 69 | + return true |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return true |
| 74 | +} |
| 75 | + |
| 76 | +func fileNameWithoutExtSuffix(fileName string) string { |
| 77 | + return strings.TrimSuffix(fileName, filepath.Ext(fileName)) |
| 78 | +} |
0 commit comments