Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: marcolan018 <llan@redhat.com>
  • Loading branch information
marcolan018 committed Apr 26, 2024
1 parent 9534a4c commit 9022e84
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/upgrade/roles/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ func run(cmd *cobra.Command, argv []string) {
reporter.Warnf("Failed to get rolePolicyBindings after upgrade," +
" please check the attached policies on upgraded roles")
} else {
output, isPolicyMissed := rolepolicybindings.CompareRolePolicyBindings(rolePolicyBindings,
output, isPolicyMissed := rolepolicybindings.CheckMissingRolePolicyBindings(rolePolicyBindings,
newRolePolicyBindings)
if isPolicyMissed {
reporter.Infof(output)
Expand Down
8 changes: 4 additions & 4 deletions pkg/helper/rolepolicybindings/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func ValidateRolePolicyBindings(bindings *cmv1.RolePolicyBindingList) error {
return nil
}

func CompareRolePolicyBindings(desired, actual *cmv1.RolePolicyBindingList) (string, bool) {
func CheckMissingRolePolicyBindings(desired, actual *cmv1.RolePolicyBindingList) (string, bool) {
output := ""
actualBindings := map[string][]string{}
for _, binding := range actual.Slice() {
Expand All @@ -60,17 +60,17 @@ func CompareRolePolicyBindings(desired, actual *cmv1.RolePolicyBindingList) (str
missingBindings[binding.Name()] = append(missingBindings[binding.Name()],
policy.Arn())
}
output = output + "Policy %s missed in role %s\n"
output = fmt.Sprintf(output+"Policy '%s' missed in role '%s'\n", policy.Arn(), binding.Name())
}
}
}
if len(missingBindings) == 0 {
return output, true
return output, false
}
output = output + "Run the following commands to attach the missing policies:\n"
for roleName, policies := range missingBindings {
output = output + fmt.Sprintf("rosa attach policy --role-name %s --policy-arns %s --mode auto\n",
roleName, strings.Join(policies[:], ","))
}
return output, false
return output, true
}
89 changes: 89 additions & 0 deletions pkg/helper/rolepolicybindings/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright (c) 2024 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rolepolicybindings

import (
"fmt"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
)

func TestIdp(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "RolePolicyBinding")
}

var (
roleName1 = "sample-role-1"
roleName2 = "sample-role-2"
policyArn1 = "sample-policy-arn-1"
policyArn2 = "sample-policy-arn-2"
errDesc = "sample-err-description"
failedBinding = cmv1.NewRolePolicyBinding().
Name(roleName1).
Status(cmv1.NewRolePolicyBindingStatus().
Value(RolePolicyBindingFailedStatus).
Description(errDesc))
policyBuilder1 = cmv1.NewRolePolicy().Arn(policyArn1)
policyBuilder2 = cmv1.NewRolePolicy().Arn(policyArn2)
binding1 = cmv1.NewRolePolicyBinding().
Name(roleName1).
Policies(policyBuilder1)
actualBinding2 = cmv1.NewRolePolicyBinding().
Name(roleName2).
Policies(policyBuilder2)
desiredBinding2 = cmv1.NewRolePolicyBinding().
Name(roleName2).
Policies(policyBuilder1, policyBuilder2)
)

var _ = Describe("Policy Service", func() {
Context("Attach Policy", Ordered, func() {
It("Test ValidateRolePolicyBindings", func() {
bindingList, err := cmv1.NewRolePolicyBindingList().Items(failedBinding).Build()
Expect(err).ShouldNot(HaveOccurred())
err = ValidateRolePolicyBindings(bindingList)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(Equal(fmt.Sprintf("Failed to get attach policies of role %s: %s",
roleName1, errDesc)))
})
It("Test CheckMissingRolePolicyBindings -- No missing rolepolicy binding", func() {
desiredBindings, err := cmv1.NewRolePolicyBindingList().Items(binding1).Build()
Expect(err).ShouldNot(HaveOccurred())
actualBindings, err := cmv1.NewRolePolicyBindingList().Items(binding1).Build()
Expect(err).ShouldNot(HaveOccurred())
output, isBindingMissed := CheckMissingRolePolicyBindings(desiredBindings, actualBindings)
Expect(isBindingMissed).To(Equal(false))
Expect(output).To(Equal(""))
})
It("Test CheckMissingRolePolicyBindings -- Find missing rolepolicy binding", func() {
desiredBindings, err := cmv1.NewRolePolicyBindingList().Items(binding1, desiredBinding2).Build()
Expect(err).ShouldNot(HaveOccurred())
actualBindings, err := cmv1.NewRolePolicyBindingList().Items(binding1, actualBinding2).Build()
Expect(err).ShouldNot(HaveOccurred())
output, isBindingMissed := CheckMissingRolePolicyBindings(desiredBindings, actualBindings)
Expect(isBindingMissed).To(Equal(true))
Expect(output).To(Equal(fmt.Sprintf("Policy '%s' missed in role '%s'\n"+
"Run the following commands to attach the missing policies:\n"+
"rosa attach policy --role-name %s --policy-arns %s --mode auto\n",
policyArn1, roleName2, roleName2, policyArn1)))
})
})
})

0 comments on commit 9022e84

Please sign in to comment.