Fix flaky test behavior for protobuf messages

This commit is contained in:
Herman Slatman 2022-04-18 22:39:47 +02:00
parent ff8cb19b78
commit 2ca5c0170f
No known key found for this signature in database
GPG key ID: F4D8A44EA0A75A4F

View file

@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"google.golang.org/protobuf/encoding/protojson"
@ -342,10 +343,19 @@ func TestPolicyAdminResponder_CreateAuthorityPolicy(t *testing.T) {
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
assert.Equals(t, tc.err.Type, ae.Type)
assert.Equals(t, tc.err.Message, ae.Message)
assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
assert.Equals(t, tc.err.Detail, ae.Detail)
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
// when the error message starts with "proto", we expect it to have
// a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error"))
} else {
assert.Equals(t, tc.err.Message, ae.Message)
}
return
}
@ -583,10 +593,19 @@ func TestPolicyAdminResponder_UpdateAuthorityPolicy(t *testing.T) {
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
assert.Equals(t, tc.err.Type, ae.Type)
assert.Equals(t, tc.err.Message, ae.Message)
assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
assert.Equals(t, tc.err.Detail, ae.Detail)
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
// when the error message starts with "proto", we expect it to have
// a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error"))
} else {
assert.Equals(t, tc.err.Message, ae.Message)
}
return
}
@ -994,10 +1013,19 @@ func TestPolicyAdminResponder_CreateProvisionerPolicy(t *testing.T) {
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
assert.Equals(t, tc.err.Type, ae.Type)
assert.Equals(t, tc.err.Message, ae.Message)
assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
assert.Equals(t, tc.err.Detail, ae.Detail)
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
// when the error message starts with "proto", we expect it to have
// a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error"))
} else {
assert.Equals(t, tc.err.Message, ae.Message)
}
return
}
@ -1185,10 +1213,19 @@ func TestPolicyAdminResponder_UpdateProvisionerPolicy(t *testing.T) {
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
assert.Equals(t, tc.err.Type, ae.Type)
assert.Equals(t, tc.err.Message, ae.Message)
assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
assert.Equals(t, tc.err.Detail, ae.Detail)
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
// when the error message starts with "proto", we expect it to have
// a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error"))
} else {
assert.Equals(t, tc.err.Message, ae.Message)
}
return
}
@ -1549,10 +1586,19 @@ func TestPolicyAdminResponder_CreateACMEAccountPolicy(t *testing.T) {
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
assert.Equals(t, tc.err.Type, ae.Type)
assert.Equals(t, tc.err.Message, ae.Message)
assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
assert.Equals(t, tc.err.Detail, ae.Detail)
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
// when the error message starts with "proto", we expect it to have
// a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error"))
} else {
assert.Equals(t, tc.err.Message, ae.Message)
}
return
}
@ -1715,10 +1761,19 @@ func TestPolicyAdminResponder_UpdateACMEAccountPolicy(t *testing.T) {
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
assert.Equals(t, tc.err.Type, ae.Type)
assert.Equals(t, tc.err.Message, ae.Message)
assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
assert.Equals(t, tc.err.Detail, ae.Detail)
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
// when the error message starts with "proto", we expect it to have
// a syntax error (in the tests). If the message doesn't start with "proto",
// we expect a full string match.
if strings.HasPrefix(tc.err.Message, "proto:") {
assert.True(t, strings.Contains(tc.err.Message, "syntax error"))
} else {
assert.Equals(t, tc.err.Message, ae.Message)
}
return
}