Skip to content

Commit

Permalink
Fix flakey server-acl-init tests with retries (#1095)
Browse files Browse the repository at this point in the history
* Fix flakey server-acl-init tests with retries

* Adding retry for flakey server-acl-init enterprise test

* adding missing retry module in server-acl-init enterprise tests
  • Loading branch information
jmurret committed Mar 11, 2022
1 parent 4ddea76 commit e41855a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 61 deletions.
27 changes: 15 additions & 12 deletions control-plane/subcommand/server-acl-init/command_ent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/consul-k8s/control-plane/subcommand/common"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -1208,18 +1209,20 @@ func TestRun_NamespaceEnabled_ValidateLoginToken_SecondaryDatacenter(t *testing.
})
require.NoError(t, err)

tok, _, err := client.ACL().Login(&api.ACLLoginParams{
AuthMethod: authMethodName,
BearerToken: jwtToken,
Meta: map[string]string{},
}, &api.WriteOptions{})
require.NoError(t, err)

require.Equal(t, len(tok.Roles), len(c.Roles))
for _, role := range tok.Roles {
require.Contains(t, c.Roles, role.Name)
}
require.Equal(t, !c.GlobalToken, tok.Local)
retry.Run(t, func(r *retry.R) {
tok, _, err := client.ACL().Login(&api.ACLLoginParams{
AuthMethod: authMethodName,
BearerToken: jwtToken,
Meta: map[string]string{},
}, &api.WriteOptions{})
require.NoError(r, err)

require.Equal(r, len(tok.Roles), len(c.Roles))
for _, role := range tok.Roles {
require.Contains(r, c.Roles, role.Name)
}
require.Equal(r, !c.GlobalToken, tok.Local)
})
})
}
}
Expand Down
102 changes: 53 additions & 49 deletions control-plane/subcommand/server-acl-init/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1894,9 +1894,9 @@ func TestRun_ACLReplicationTokenValid(t *testing.T) {
// Test that replication was successful.
retry.Run(t, func(r *retry.R) {
replicationStatus, _, err := secondaryConsulClient.ACL().Replication(nil)
require.NoError(t, err)
require.True(t, replicationStatus.Enabled)
require.Greater(t, replicationStatus.ReplicatedIndex, uint64(0))
require.NoError(r, err)
require.True(r, replicationStatus.Enabled)
require.Greater(r, replicationStatus.ReplicatedIndex, uint64(0))
})

// Test that the client policy was created.
Expand Down Expand Up @@ -2280,45 +2280,47 @@ func TestRun_PoliciesAndBindingRulesACLLogin_SecondaryDatacenter(t *testing.T) {
datacenter = primaryDatacenter
}

// Check that the Role exists + has correct Policy and is associated with a BindingRule.
for i := range c.Roles {
// Check that the Policy exists.
policy, _, err := consul.ACL().PolicyReadByName(c.PolicyNames[i], &api.QueryOptions{Datacenter: primaryDatacenter})
require.NoError(t, err)
require.NotNil(t, policy)

// Check that the Role exists.
role, _, err := consul.ACL().RoleReadByName(c.Roles[i], &api.QueryOptions{Datacenter: datacenter})
require.NoError(t, err)
require.NotNil(t, role)
retry.Run(t, func(r *retry.R) {
// Check that the Role exists + has correct Policy and is associated with a BindingRule.
for i := range c.Roles {
// Check that the Policy exists.
policy, _, err := consul.ACL().PolicyReadByName(c.PolicyNames[i], &api.QueryOptions{Datacenter: primaryDatacenter})
require.NoError(r, err)
require.NotNil(r, policy)

// Check that the Role references the Policy.
found := false
for j := range role.Policies {
if role.Policies[j].Name == policy.Name {
found = true
break
// Check that the Role exists.
role, _, err := consul.ACL().RoleReadByName(c.Roles[i], &api.QueryOptions{Datacenter: datacenter})
require.NoError(r, err)
require.NotNil(r, role)

// Check that the Role references the Policy.
found := false
for j := range role.Policies {
if role.Policies[j].Name == policy.Name {
found = true
break
}
}
}
require.True(t, found)
require.True(r, found)

// Check that there exists a BindingRule that references this Role.
authMethodName := fmt.Sprintf("%s-%s", resourcePrefix, componentAuthMethod)
if c.GlobalAuthMethod {
authMethodName = fmt.Sprintf("%s-%s-%s", resourcePrefix, componentAuthMethod, secondaryDatacenter)
}
rb, _, err := consul.ACL().BindingRuleList(authMethodName, &api.QueryOptions{Datacenter: datacenter})
require.NoError(t, err)
require.NotNil(t, rb)
found = false
for j := range rb {
if rb[j].BindName == c.Roles[i] {
found = true
break
// Check that there exists a BindingRule that references this Role.
authMethodName := fmt.Sprintf("%s-%s", resourcePrefix, componentAuthMethod)
if c.GlobalAuthMethod {
authMethodName = fmt.Sprintf("%s-%s-%s", resourcePrefix, componentAuthMethod, secondaryDatacenter)
}
rb, _, err := consul.ACL().BindingRuleList(authMethodName, &api.QueryOptions{Datacenter: datacenter})
require.NoError(r, err)
require.NotNil(r, rb)
found = false
for j := range rb {
if rb[j].BindName == c.Roles[i] {
found = true
break
}
}
require.True(r, found)
}
require.True(t, found)
}
})
})
}
}
Expand Down Expand Up @@ -2544,18 +2546,20 @@ func TestRun_ValidateLoginToken_SecondaryDatacenter(t *testing.T) {
})
require.NoError(t, err)

tok, _, err := client.ACL().Login(&api.ACLLoginParams{
AuthMethod: authMethodName,
BearerToken: jwtToken,
Meta: map[string]string{},
}, &api.WriteOptions{})
require.NoError(t, err)

require.Equal(t, len(tok.Roles), len(c.Roles))
for _, role := range tok.Roles {
require.Contains(t, c.Roles, role.Name)
}
require.Equal(t, !c.GlobalToken, tok.Local)
retry.Run(t, func(r *retry.R) {
tok, _, err := client.ACL().Login(&api.ACLLoginParams{
AuthMethod: authMethodName,
BearerToken: jwtToken,
Meta: map[string]string{},
}, &api.WriteOptions{})
require.NoError(r, err)

require.Equal(r, len(tok.Roles), len(c.Roles))
for _, role := range tok.Roles {
require.Contains(r, c.Roles, role.Name)
}
require.Equal(r, !c.GlobalToken, tok.Local)
})
})
}
}
Expand Down

0 comments on commit e41855a

Please sign in to comment.