Skip to content

Commit

Permalink
Bootstrap and adds e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: PuneetPunamiya <ppunamiy@redhat.com>
  • Loading branch information
PuneetPunamiya committed Apr 25, 2024
1 parent 5a365ab commit aba7dfc
Show file tree
Hide file tree
Showing 12 changed files with 679 additions and 5 deletions.
42 changes: 42 additions & 0 deletions test/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cli

import (
"fmt"
"os"
"testing"

"gotest.tools/v3/icmd"
)

type TknApprovalTaskRunner struct {
path string
namespace string
}

func NewTknApprovalTaskRunner() (TknApprovalTaskRunner, error) {
if os.Getenv("TEST_CLIENT_BINARY") != "" {
return TknApprovalTaskRunner{
path: os.Getenv("TEST_CLIENT_BINARY"),
}, nil
}
return TknApprovalTaskRunner{
path: os.Getenv("TEST_CLIENT_BINARY"),
}, fmt.Errorf("Error: couldn't Create tknApprovalTaskRunner, please do check tkn binary path: (%+v)", os.Getenv("TEST_CLIENT_BINARY"))
}

func (tknApprovalTaskRunner TknApprovalTaskRunner) Run(args ...string) *icmd.Result {
cmd := append([]string{tknApprovalTaskRunner.path}, args...)
return icmd.RunCmd(icmd.Cmd{Command: cmd})
}

// MustSucceed asserts that the command ran with 0 exit code
func (tknApprovalTaskRunner TknApprovalTaskRunner) MustSucceed(t *testing.T, args ...string) *icmd.Result {
return tknApprovalTaskRunner.Assert(t, icmd.Success, args...)
}

// Assert runs a command and verifies exit code (0)
func (tknApprovalTaskRunner TknApprovalTaskRunner) Assert(t *testing.T, exp icmd.Expected, args ...string) *icmd.Result {
res := tknApprovalTaskRunner.Run(args...)
res.Assert(t, exp)
return res
}
74 changes: 74 additions & 0 deletions test/cli_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package test

import (
"testing"

"github.com/openshift-pipelines/manual-approval-gate/test/cli"
"github.com/openshift-pipelines/manual-approval-gate/test/resources"
"github.com/stretchr/testify/assert"
)

var expectedAt = `NAME NumberOfApprovalsRequired PendingApprovals Rejected STATUS
cr-1 2 0 1 Rejected
cr-2 2 2 0 Pending
cr-3 2 0 0 Approved
`

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

t.Run("No approvaltask found", func(t *testing.T) {
expected := "No ApprovalTasks found\n"

res := tknApprovaltask.MustSucceed(t, "list", "-n", "foo")
assert.Equal(t, expected, res.Stdout())
})

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

approvers := []resources.Approver{
{
Name: "foo",
Input: "approve",
},
{
Name: "tekton",
Input: "reject",
},
}
resources.Update(t, clients, cr, approvers)

_, _ = resources.Create(t, "./testdata/cr-2.yaml")

clients, cr3 := resources.Create(t, "./testdata/cr-3.yaml")

approvers3 := []resources.Approver{
{
Name: "foo",
Input: "approve",
},
{
Name: "bar",
Input: "approve",
},
}
resources.Update(t, clients, cr3, approvers3)

// approvers := []resources.Approver{
// {
// Name: "foo",
// Input: "approve",
// },
// {
// Name: "tekton",
// Input: "reject",
// },
// }
// resources.Update(t, clients, cr2, []resources.Approver{})

res := tknApprovaltask.MustSucceed(t, "list", "-n", "test-1")
assert.Equal(t, expectedAt, res.Stdout())
})
}
1 change: 0 additions & 1 deletion test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func TestApproveManualApprovalTask(t *testing.T) {
}
})

// Test if TektonConfig can reach the READY status
t.Run("ensure-approval-task-creation", func(t *testing.T) {
_, err := resources.WaitForApprovalTaskCreation(clients.ApprovalTaskClient, cr.GetName())
if err != nil {
Expand Down
135 changes: 135 additions & 0 deletions test/resources/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package resources

import (
"context"
"io/ioutil"
"path/filepath"
"testing"

"github.com/openshift-pipelines/manual-approval-gate/pkg/apis/approvaltask/v1alpha1"
"github.com/openshift-pipelines/manual-approval-gate/test/client"
"github.com/openshift-pipelines/manual-approval-gate/test/utils"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
)

func Create(t *testing.T, path string) (*utils.Clients, *v1beta1.CustomRun) {
clients := client.Setup(t, "default")

taskRunPath, err := filepath.Abs(path)
if err != nil {
t.Fatal(err)
}

taskRunYAML, err := ioutil.ReadFile(taskRunPath)
if err != nil {
t.Fatal(err)
}

customRun := MustParseCustomRun(t, string(taskRunYAML))

var cr *v1beta1.CustomRun
t.Run("ensure-custom-run-creation", func(t *testing.T) {
cr, err = EnsureCustomTaskRunExists(clients.TektonClient, customRun)
if err != nil {
t.Fatalf("Failed to create the custom run: %v", err)
}
})

t.Run("ensure-approval-task-creation", func(t *testing.T) {
_, err := WaitForApprovalTaskCreation(clients.ApprovalTaskClient, cr.GetName(), cr.GetNamespace())
if err != nil {
t.Fatal("Failed to get the approval task")
}
})

// t.Run("update the approval task", func(t *testing.T) {
// at, err := clients.ApprovalTaskClient.ApprovalTasks(cr.GetNamespace()).Get(context.TODO(), cr.GetName(), metav1.GetOptions{})
// if err != nil {
// t.Fatal("Failed to get the approvaltask")
// }

// att := updateApprovalTask(at, "foo", "reject")

// _, err = clients.ApprovalTaskClient.ApprovalTasks(att.Namespace).Update(context.TODO(), att, metav1.UpdateOptions{})
// if err != nil {
// t.Fatal("Failed to update the approvalTask..")
// }

// _, err = WaitForApprovalTaskStatusUpdate(clients.ApprovalTaskClient, cr.GetName(), cr.GetNamespace(), "rejected")
// 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, "rejected", approvalTask.Status.State)
// })

return clients, cr
}

type Approver struct {
Name string
Input string
}

func Update(t *testing.T, clients *utils.Clients, cr *v1beta1.CustomRun, approvers []Approver) {
t.Run("update the approval task", func(t *testing.T) {
at, err := clients.ApprovalTaskClient.ApprovalTasks(cr.GetNamespace()).Get(context.TODO(), cr.GetName(), metav1.GetOptions{})
if err != nil {
t.Fatal("Failed to get the approvaltask")
}

att := updateApprovalTask(at, approvers)

_, err = clients.ApprovalTaskClient.ApprovalTasks(att.Namespace).Update(context.TODO(), att, metav1.UpdateOptions{})
if err != nil {
t.Fatal("Failed to update the approvalTask..")
}

// _, err = WaitForApprovalTaskStatusUpdate(clients.ApprovalTaskClient, cr.GetName(), cr.GetNamespace(), "rejected")
// 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, "rejected", approvalTask.Status.State)
})
}

func updateApprovalTask(at *v1alpha1.ApprovalTask, approvers []Approver) *v1alpha1.ApprovalTask {
for _, approver := range approvers {
for j, a := range at.Spec.Approvers {
if a.Name == approver.Name {
at.Spec.Approvers[j].Input = approver.Input
}
}
}

return at
}

func MustParseCustomRun(t *testing.T, yaml string) *v1beta1.CustomRun {
t.Helper()
var r v1beta1.CustomRun
yaml = `apiVersion: tekton.dev/v1beta1
kind: CustomRun
` + yaml
mustParseYAML(t, yaml, &r)
return &r
}

func mustParseYAML(t *testing.T, yaml string, i runtime.Object) {
t.Helper()
if _, _, err := scheme.Codecs.UniversalDeserializer().Decode([]byte(yaml), nil, i); err != nil {
t.Fatalf("mustParseYAML (%s): %v", yaml, err)
}
}
8 changes: 4 additions & 4 deletions test/resources/customrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func EnsureCustomTaskRunExists(client pipelinev1beta1.TektonV1beta1Interface, cu
return cr, err
}

func WaitForApprovalTaskCreation(client typedopenshiftpipelinesv1alpha1.OpenshiftpipelinesV1alpha1Interface, name string) (*v1alpha1.ApprovalTask, error) {
func WaitForApprovalTaskCreation(client typedopenshiftpipelinesv1alpha1.OpenshiftpipelinesV1alpha1Interface, name, namespace string) (*v1alpha1.ApprovalTask, error) {
var lastState *v1alpha1.ApprovalTask
waitErr := wait.PollImmediate(Interval, Timeout, func() (done bool, err error) {
_, err = client.ApprovalTasks("default").Get(context.TODO(), name, metav1.GetOptions{})
_, err = client.ApprovalTasks("test-1").Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return false, nil
}
Expand All @@ -49,11 +49,11 @@ func WaitForApprovalTaskCreation(client typedopenshiftpipelinesv1alpha1.Openshif
return lastState, nil
}

func WaitForApprovalTaskStatusUpdate(client typedopenshiftpipelinesv1alpha1.OpenshiftpipelinesV1alpha1Interface, name, desiredStatus string) (*v1alpha1.ApprovalTask, error) {
func WaitForApprovalTaskStatusUpdate(client typedopenshiftpipelinesv1alpha1.OpenshiftpipelinesV1alpha1Interface, name, namespace, desiredStatus string) (*v1alpha1.ApprovalTask, error) {
var approvalTask *v1alpha1.ApprovalTask

waitErr := wait.PollImmediate(Interval, Timeout, func() (done bool, err error) {
approvalTask, err = client.ApprovalTasks("default").Get(context.TODO(), name, metav1.GetOptions{})
approvalTask, err = client.ApprovalTasks("test-1").Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return false, err
}
Expand Down
18 changes: 18 additions & 0 deletions test/testdata/cr-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: tekton.dev/v1beta1
kind: CustomRun
metadata:
name: cr-1
namespace: test-1
spec:
retries: 2
customRef:
apiVersion: openshift-pipelines.org/v1alpha1
kind: ApprovalTask
params:
- name: approvers
value:
- foo
- bar
- tekton
- name: numberOfApprovalsRequired
value: 2
18 changes: 18 additions & 0 deletions test/testdata/cr-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: tekton.dev/v1beta1
kind: CustomRun
metadata:
name: cr-2
namespace: test-1
spec:
retries: 2
customRef:
apiVersion: openshift-pipelines.org/v1alpha1
kind: ApprovalTask
params:
- name: approvers
value:
- foo
- bar
- tekton
- name: numberOfApprovalsRequired
value: 2
18 changes: 18 additions & 0 deletions test/testdata/cr-3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: tekton.dev/v1beta1
kind: CustomRun
metadata:
name: cr-3
namespace: test-1
spec:
retries: 2
customRef:
apiVersion: openshift-pipelines.org/v1alpha1
kind: ApprovalTask
params:
- name: approvers
value:
- foo
- bar
- tekton
- name: numberOfApprovalsRequired
value: 2
Loading

0 comments on commit aba7dfc

Please sign in to comment.