Skip to content

Commit

Permalink
Adds e2e tests for Approve CLI command
Browse files Browse the repository at this point in the history
Signed-off-by: PuneetPunamiya <[email protected]>
  • Loading branch information
PuneetPunamiya authored and openshift-merge-bot[bot] committed May 14, 2024
1 parent 140587e commit e9cd2c9
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 4 deletions.
2 changes: 0 additions & 2 deletions pkg/cli/cmd/approve/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,5 @@ func Command(p cli.Params) *cobra.Command {

flags.AddOptions(c)

c.Flags().BoolVarP(&opts.AllNamespaces, "all-namespaces", "A", opts.AllNamespaces, "list Tasks from all namespaces")

return c
}
3 changes: 1 addition & 2 deletions pkg/cli/cmd/approve/approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ func TestApproveApprovalTask(t *testing.T) {
command: command(t, approvaltasks, ns, dc, "tekton"),
args: []string{"at-3", "-n", "test"},
expectedOutput: fmt.Sprintf("Error: failed to approve approvalTask from namespace %s: approvaltasks.openshift-pipelines.org \"%s\" not found\n", "test", "at-3"),

wantError: true,
wantError: true,
},
}

Expand Down
48 changes: 48 additions & 0 deletions test/cli/approve/approve_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//go:build e2e
// +build e2e

package approve

import (
"context"
"testing"

"github.com/openshift-pipelines/manual-approval-gate/test/cli"
"github.com/openshift-pipelines/manual-approval-gate/test/client"
"github.com/openshift-pipelines/manual-approval-gate/test/resources"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestApprovalTaskApproveCommand(t *testing.T) {
tknApprovaltask, err := cli.NewTknApprovalTaskRunner()
assert.Nil(t, err)

clients := client.Setup(t, "default")

t.Run("approve-approvaltask", func(t *testing.T) {
cr := resources.Create(t, clients, "./testdata/cr-1.yaml")

_, err := resources.WaitForApprovalTaskCreation(clients.ApprovalTaskClient, cr.GetName(), cr.GetNamespace())
if err != nil {
t.Fatal("Failed to get the approval task")
}

res := tknApprovaltask.MustSucceed(t, "approve", cr.GetName(), "-n", "test-3")

_, err = resources.WaitForApproverResponseUpdate(clients.ApprovalTaskClient, cr, "kubernetes-admin", "approved")
if err != nil {
t.Fatal("Failed to get the approval task")
}

approvalTask, err := clients.ApprovalTaskClient.ApprovalTasks(cr.GetNamespace()).Get(context.TODO(), cr.GetName(), metav1.GetOptions{})
if err != nil {
t.Fatal("Failed to get the approval task")
}

assert.Equal(t, 1, len(approvalTask.Status.ApproversResponse))
assert.Equal(t, "ApprovalTask at-1 is approved in test-3 namespace\n", res.Stdout())
assert.Equal(t, "kubernetes-admin", approvalTask.Status.ApproversResponse[0].Name)
assert.Equal(t, "approved", approvalTask.Status.ApproversResponse[0].Response)
})
}
19 changes: 19 additions & 0 deletions test/cli/approve/testdata/cr-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: tekton.dev/v1beta1
kind: CustomRun
metadata:
name: at-1
namespace: test-3
spec:
retries: 2
customRef:
apiVersion: openshift-pipelines.org/v1alpha1
kind: ApprovalTask
params:
- name: approvers
value:
- foo
- bar
- tekton
- kubernetes-admin
- name: numberOfApprovalsRequired
value: 2
5 changes: 5 additions & 0 deletions test/e2e-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,16 @@ main() {
echo "Running CLI e2e tests"
kubectl create ns test-1
kubectl create ns test-2
kubectl create ns test-3

go build -o tkn-approvaltask github.com/openshift-pipelines/manual-approval-gate/cmd/tkn-approvaltask
export TEST_CLIENT_BINARY="${PWD}/tkn-approvaltask"

go test -v -count=1 -tags=e2e -timeout=20m ./test/cli/... ${KUBECONFIG_PARAM} || fail_test "E2E test failed....."

kubectl delete ns test-1
kubectl delete ns test-2
kubectl delete ns test-3

success
}
Expand Down
1 change: 1 addition & 0 deletions test/kind.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ networking:
apiServerPort: 8443
nodes:
- role: control-plane
image: kindest/node:v1.28.0
kubeadmConfigPatches:
- |
kind: InitConfiguration
Expand Down
32 changes: 32 additions & 0 deletions test/resources/customrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,35 @@ func WaitForApprovalTaskStatusUpdate(client typedopenshiftpipelinesv1alpha1.Open

return approvalTask, nil
}

func WaitForApproverResponseUpdate(client typedopenshiftpipelinesv1alpha1.OpenshiftpipelinesV1alpha1Interface, cr *v1beta1.CustomRun, name, response string) (*v1alpha1.ApprovalTask, error) {
var approvalTask *v1alpha1.ApprovalTask

waitErr := wait.PollImmediate(Interval, Timeout, func() (done bool, err error) {
approvalTask, err = client.ApprovalTasks(cr.GetNamespace()).Get(context.TODO(), cr.GetName(), metav1.GetOptions{})
if err != nil {
return false, err
}

if containsApprover(approvalTask.Status.ApproversResponse, name, response) {
return true, nil
}

return false, nil
})

if waitErr != nil {
return nil, fmt.Errorf("error waiting for ApprovalTask %s to reach status %s: %w", cr.GetName(), name, waitErr)
}

return approvalTask, nil
}

func containsApprover(approvers []v1alpha1.ApproverState, name string, response string) bool {
for _, approver := range approvers {
if approver.Name == name && approver.Response == response {
return true
}
}
return false
}

0 comments on commit e9cd2c9

Please sign in to comment.