Skip to content

Commit

Permalink
added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
aahel committed Jun 26, 2023
1 parent 8007920 commit 7ab54ce
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions control-plane/subcommand/server-acl-init/create_or_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,93 @@ func TestCreateOrUpdateACLPolicy_ErrorsIfDescriptionDoesNotMatch(t *testing.T) {
require.NoError(err)
require.Equal(policyDescription, rereadPolicy.Description)
}

func TestCreateOrUpdateACLPolicy_Update(t *testing.T) {
require := require.New(t)
ui := cli.NewMockUi()
k8s := fake.NewSimpleClientset()
cmd := Command{
UI: ui,
clientset: k8s,
log: hclog.NewNullLogger(),
}
cmd.init()
// Start Consul.
bootToken := "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
svr, err := testutil.NewTestServerConfigT(t, func(c *testutil.TestServerConfig) {
c.ACL.Enabled = true
c.ACL.Tokens.InitialManagement = bootToken
})
require.NoError(err)
defer svr.Stop()
svr.WaitForLeader(t)

// Get a Consul client.
consul, err := api.NewClient(&api.Config{
Address: svr.HTTPAddr,
Token: bootToken,
})
require.NoError(err)
connectInjectRule, err := cmd.injectRules()
require.NoError(err)
aclReplRule, err := cmd.aclReplicationRules()
require.NoError(err)
policyDescription := "policy-description"
policyName := "policy-name"
policy, _, err := consul.ACL().PolicyCreate(&api.ACLPolicy{
Name: "new-policy-name",
Description: "new-policy-desc",
}, nil)
require.NoError(err)
cases := []struct {
Name string
ID string
PolicyDescription string
PolicyName string
Rules string
Err error
ExpPolicy *api.ACLPolicy
}{
{
Name: "create",
ID: "",
PolicyDescription: policyDescription,
PolicyName: policyName,
Rules: connectInjectRule,
Err: nil,
ExpPolicy: &api.ACLPolicy{
Name: policyName,
Description: policyDescription,
Rules: connectInjectRule,
},
},
{
Name: "update",
ID: policy.ID,
PolicyDescription: policy.Description,
PolicyName: policy.Name,
Rules: aclReplRule,
Err: nil,
ExpPolicy: &api.ACLPolicy{
Name: policyName,
Description: policyDescription,
Rules: aclReplRule,
},
},
}
for _, tt := range cases {
t.Run(tt.Name, func(t *testing.T) {
err = cmd.createOrUpdateACLPolicy(api.ACLPolicy{
Name: tt.PolicyName,
Description: tt.PolicyDescription,
Rules: tt.Rules,
}, consul)
require.Equal(tt.Err, err)
if tt.ID != "" {
readPolicy, _, err := consul.ACL().PolicyRead(tt.ID, nil)
require.NoError(err)
require.Equal(tt.Rules, readPolicy.Rules)
}
})
}
}

0 comments on commit 7ab54ce

Please sign in to comment.