|
1 | 1 | package scaleway |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "io/ioutil" |
6 | 7 | "net/http" |
@@ -42,7 +43,8 @@ func TestAccScalewayCassettes_Validator(t *testing.T) { |
42 | 43 |
|
43 | 44 | func checkErrorCode(c *cassette.Cassette) error { |
44 | 45 | for _, i := range c.Interactions { |
45 | | - if !checkErrCodeExcept(i, c, http.StatusNotFound, http.StatusTooManyRequests, http.StatusForbidden) { |
| 46 | + if !checkErrCodeExcept(i, c, http.StatusNotFound, http.StatusTooManyRequests, http.StatusForbidden) && |
| 47 | + !isTransientStateError(i) { |
46 | 48 | return fmt.Errorf("status: %v found on %s. method: %s, url %s\nrequest body = %v\nresponse body = %v", i.Code, c.Name, i.Request.Method, i.Request.URL, i.Request.Body, i.Response.Body) |
47 | 49 | } |
48 | 50 | } |
@@ -81,3 +83,26 @@ func checkErrCodeExcept(i *cassette.Interaction, c *cassette.Cassette, codes ... |
81 | 83 | func fileNameWithoutExtSuffix(fileName string) string { |
82 | 84 | return strings.TrimSuffix(fileName, filepath.Ext(fileName)) |
83 | 85 | } |
| 86 | + |
| 87 | +// isTransientStateError checks if the interaction response is a transient state error |
| 88 | +// Transient state error are expected when creating resource linked to each other |
| 89 | +// example: |
| 90 | +// creating a gateway_network will set its public gateway to a transient state |
| 91 | +// when creating 2 gateway_network, one will fail with a transient state error |
| 92 | +// but the transient state error will be caught, it will wait again for the resource to be ready |
| 93 | +func isTransientStateError(i *cassette.Interaction) bool { |
| 94 | + if i.Code != 409 { |
| 95 | + return false |
| 96 | + } |
| 97 | + |
| 98 | + scwError := struct { |
| 99 | + Type string `json:"type"` |
| 100 | + }{} |
| 101 | + |
| 102 | + err := json.Unmarshal([]byte(i.Response.Body), &scwError) |
| 103 | + if err != nil { |
| 104 | + return false |
| 105 | + } |
| 106 | + |
| 107 | + return scwError.Type == "transient_state" |
| 108 | +} |
0 commit comments