From 324ba5df176ccd08b33d909825da3b0542e8ede7 Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" Date: Tue, 12 Feb 2019 16:09:26 -0600 Subject: [PATCH] update TestStateStore_ACLBootstrap to not rely upon request mutation (#5335) --- agent/consul/state/acl_test.go | 31 ++++++++++++++++++++++--------- agent/structs/acl.go | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/agent/consul/state/acl_test.go b/agent/consul/state/acl_test.go index bc8cf61309..6328ef32e7 100644 --- a/agent/consul/state/acl_test.go +++ b/agent/consul/state/acl_test.go @@ -1,15 +1,11 @@ package state import ( - // "reflect" "testing" "time" "github.com/hashicorp/consul/acl" "github.com/hashicorp/consul/agent/structs" - // "github.com/hashicorp/go-memdb" - // "github.com/pascaldekloe/goe/verify" - "github.com/stretchr/testify/require" ) @@ -98,6 +94,23 @@ func TestStateStore_ACLBootstrap(t *testing.T) { Type: structs.ACLTokenTypeManagement, } + stripIrrelevantFields := func(token *structs.ACLToken) *structs.ACLToken { + tokenCopy := token.Clone() + // When comparing the tokens disregard the policy link names. This + // data is not cleanly updated in a variety of scenarios and should not + // be relied upon. + for i, _ := range tokenCopy.Policies { + tokenCopy.Policies[i].Name = "" + } + // The raft indexes won't match either because the requester will not + // have access to that. + tokenCopy.RaftIndex = structs.RaftIndex{} + return tokenCopy + } + compareTokens := func(expected, actual *structs.ACLToken) { + require.Equal(t, stripIrrelevantFields(expected), stripIrrelevantFields(actual)) + } + s := testStateStore(t) setupGlobalManagement(t, s) @@ -107,7 +120,7 @@ func TestStateStore_ACLBootstrap(t *testing.T) { require.Equal(t, uint64(0), index) // Perform a regular bootstrap. - require.NoError(t, s.ACLBootstrap(3, 0, token1, false)) + require.NoError(t, s.ACLBootstrap(3, 0, token1.Clone(), false)) // Make sure we can't bootstrap again canBootstrap, index, err = s.CanBootstrapACLToken() @@ -116,7 +129,7 @@ func TestStateStore_ACLBootstrap(t *testing.T) { require.Equal(t, uint64(3), index) // Make sure another attempt fails. - err = s.ACLBootstrap(4, 0, token2, false) + err = s.ACLBootstrap(4, 0, token2.Clone(), false) require.Error(t, err) require.Equal(t, structs.ACLBootstrapNotAllowedErr, err) @@ -130,15 +143,15 @@ func TestStateStore_ACLBootstrap(t *testing.T) { _, tokens, err := s.ACLTokenList(nil, true, true, "") require.NoError(t, err) require.Len(t, tokens, 1) - require.Equal(t, token1, tokens[0]) + compareTokens(token1, tokens[0]) // bootstrap reset - err = s.ACLBootstrap(32, index-1, token2, false) + err = s.ACLBootstrap(32, index-1, token2.Clone(), false) require.Error(t, err) require.Equal(t, structs.ACLBootstrapInvalidResetIndexErr, err) // bootstrap reset - err = s.ACLBootstrap(32, index, token2, false) + err = s.ACLBootstrap(32, index, token2.Clone(), false) require.NoError(t, err) _, tokens, err = s.ACLTokenList(nil, true, true, "") diff --git a/agent/structs/acl.go b/agent/structs/acl.go index 05d536eac5..d1a6fc250f 100644 --- a/agent/structs/acl.go +++ b/agent/structs/acl.go @@ -164,6 +164,17 @@ type ACLToken struct { RaftIndex } +func (t *ACLToken) Clone() *ACLToken { + t2 := *t + t2.Policies = nil + + if len(t.Policies) > 0 { + t2.Policies = make([]ACLTokenPolicyLink, len(t.Policies)) + copy(t2.Policies, t.Policies) + } + return &t2 +} + func (t *ACLToken) ID() string { return t.AccessorID } @@ -330,6 +341,16 @@ type ACLPolicy struct { RaftIndex `hash:"ignore"` } +func (p *ACLPolicy) Clone() *ACLPolicy { + p2 := *p + p2.Datacenters = nil + if len(p.Datacenters) > 0 { + p2.Datacenters = make([]string, len(p.Datacenters)) + copy(p2.Datacenters, p.Datacenters) + } + return &p2 +} + type ACLPolicyListStub struct { ID string Name string