Skip to content

Commit

Permalink
Fix test comparison stability. (projectcontour#6)
Browse files Browse the repository at this point in the history
Internally, the check response heaers use maps, so the header slice
ordering is not stable. Add a test helper to sort this for stable test
comparisons.

Signed-off-by: James Peach <[email protected]>
Signed-off-by: robinfoe <[email protected]>
  • Loading branch information
jpeach authored and robinfoe committed Feb 11, 2021
1 parent 22a94e2 commit 9f78c28
Showing 1 changed file with 50 additions and 7 deletions.
57 changes: 50 additions & 7 deletions pkg/auth/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package auth
import (
"net/http"
"net/url"
"sort"
"testing"

envoy_api_v2_core "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
Expand Down Expand Up @@ -55,6 +56,40 @@ func testConvertedRequest(t *testing.T, actual *Request) {
assert.True(t, ok, "expected.BasicAuth() should return true")
}

// stableV2 sorts the headers slice so that test equality is deterministic.
func stableV2(c *CheckResponseV2) *CheckResponseV2 {
if d := c.GetDeniedResponse(); d != nil {
sort.Slice(d.Headers, func(i, j int) bool {
return d.Headers[i].GetHeader().GetKey() < d.Headers[j].GetHeader().GetKey()
})
}

if o := c.GetOkResponse(); o != nil {
sort.Slice(o.Headers, func(i, j int) bool {
return o.Headers[i].GetHeader().GetKey() < o.Headers[j].GetHeader().GetKey()
})
}

return c
}

// stableV3 sorts the headers slice so that test equality is deterministic.
func stableV3(c *CheckResponseV3) *CheckResponseV3 {
if d := c.GetDeniedResponse(); d != nil {
sort.Slice(d.Headers, func(i, j int) bool {
return d.Headers[i].GetHeader().GetKey() < d.Headers[j].GetHeader().GetKey()
})
}

if o := c.GetOkResponse(); o != nil {
sort.Slice(o.Headers, func(i, j int) bool {
return o.Headers[i].GetHeader().GetKey() < o.Headers[j].GetHeader().GetKey()
})
}

return c
}

func TestConvertRequestV2(t *testing.T) {
in := CheckRequestV2{
Attributes: &envoy_service_auth_v2.AttributeContext{
Expand Down Expand Up @@ -129,7 +164,7 @@ func TestConvertDenied(t *testing.T) {
},
}

assert.Equal(t, response.AsV2(),
assert.Equal(t, stableV2(response.AsV2()),
&CheckResponseV2{
Status: &status.Status{
Code: int32(codes.PermissionDenied),
Expand All @@ -156,7 +191,7 @@ func TestConvertDenied(t *testing.T) {
},
)

assert.Equal(t, response.AsV3(),
assert.Equal(t, stableV3(response.AsV3()),
&CheckResponseV3{
Status: &status.Status{
Code: int32(codes.PermissionDenied),
Expand Down Expand Up @@ -190,15 +225,13 @@ func TestConvertAllowed(t *testing.T) {
Response: http.Response{
StatusCode: 415,
Header: http.Header{
// We only have 1 header here, so that we don't run into spurious
// test failures by emitting the Envoy headers slice in an undefined
// order.
"k1": {"v1"},
"k2": {"v2"},
},
},
}

assert.Equal(t, response.AsV2(),
assert.Equal(t, stableV2(response.AsV2()),
&CheckResponseV2{
Status: &status.Status{
Code: int32(codes.OK),
Expand All @@ -211,13 +244,18 @@ func TestConvertAllowed(t *testing.T) {
Key: "k1", Value: "v1",
},
},
{
Header: &envoy_api_v2_core.HeaderValue{
Key: "k2", Value: "v2",
},
},
},
},
},
},
)

assert.Equal(t, response.AsV3(),
assert.Equal(t, stableV3(response.AsV3()),
&CheckResponseV3{
Status: &status.Status{
Code: int32(codes.OK),
Expand All @@ -230,6 +268,11 @@ func TestConvertAllowed(t *testing.T) {
Key: "k1", Value: "v1",
},
},
{
Header: &envoy_config_core_v3.HeaderValue{
Key: "k2", Value: "v2",
},
},
},
},
},
Expand Down

0 comments on commit 9f78c28

Please sign in to comment.