Skip to content

Commit cd2941b

Browse files
authored
fix: ignore transient state errors in cassette validation (#1704)
1 parent d340e60 commit cd2941b

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

scaleway/validate_cassettes_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scaleway
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io/ioutil"
67
"net/http"
@@ -42,7 +43,8 @@ func TestAccScalewayCassettes_Validator(t *testing.T) {
4243

4344
func checkErrorCode(c *cassette.Cassette) error {
4445
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) {
4648
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)
4749
}
4850
}
@@ -81,3 +83,26 @@ func checkErrCodeExcept(i *cassette.Interaction, c *cassette.Cassette, codes ...
8183
func fileNameWithoutExtSuffix(fileName string) string {
8284
return strings.TrimSuffix(fileName, filepath.Ext(fileName))
8385
}
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

Comments
 (0)