-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: PuneetPunamiya <ppunamiy@redhat.com>
- Loading branch information
1 parent
5a365ab
commit aba7dfc
Showing
12 changed files
with
679 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.