From 2ca5c0170f78ca330e58f2b0af1670a0e49cca5b Mon Sep 17 00:00:00 2001 From: Herman Slatman Date: Mon, 18 Apr 2022 22:39:47 +0200 Subject: [PATCH] Fix flaky test behavior for protobuf messages --- authority/admin/api/policy_test.go | 67 +++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/authority/admin/api/policy_test.go b/authority/admin/api/policy_test.go index ab09c5bd..5717e73a 100644 --- a/authority/admin/api/policy_test.go +++ b/authority/admin/api/policy_test.go @@ -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 }