Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TEP-0114: Custom Task E2E Test with A Controller Installed #5332

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 132 additions & 3 deletions test/custom_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ package test
import (
"context"
"fmt"
"os/exec"
"strings"
"sync"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/config"

"github.com/tektoncd/pipeline/test/parse"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/test/diff"
Expand All @@ -45,8 +47,8 @@ import (
)

const (
apiVersion = "example.dev/v0"
kind = "Example"
apiVersion = "wait.testing.tekton.dev/v1alpha1"
kind = "Wait"
)

var supportedFeatureGates = map[string]string{
Expand Down Expand Up @@ -401,3 +403,130 @@ spec:
t.Fatalf("Failed to get PipelineRun `%s`: %s", pipelineRun.Name, err)
}
}

func applyController(t *testing.T) {
t.Log("Creating Wait Custom Task Controller...")
cmd := exec.Command("ko", "apply", "-f", "./config/controller.yaml")
cmd.Dir = "./wait-task"
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to create Wait Custom Task Controller: %s, Output: %s", err, out)
}
}

func cleanUpController(t *testing.T) {
t.Log("Tearing down Wait Custom Task Controller...")
cmd := exec.Command("ko", "delete", "-f", "./config/controller.yaml")
cmd.Dir = "./wait-task"
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to tear down Wait Custom Task Controller: %s, Output: %s", err, out)
}
}
XinruZhang marked this conversation as resolved.
Show resolved Hide resolved

func TestWaitCustomTask(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c, namespace := setup(ctx, t, requireAnyGate(supportedFeatureGates))
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)

// Create a custom task controller
applyController(t)
// Cleanup the controller after finishing the test
defer cleanUpController(t)

for _, tc := range []struct {
name string
duration string
conditionAccessorFn func(string) ConditionAccessorFn
wantConditionType apis.ConditionType
wantConditionStatus corev1.ConditionStatus
wantConditionReason string
}{{
name: "Wait Task Has Passed",
duration: "1s",
conditionAccessorFn: Succeed,
wantConditionType: apis.ConditionSucceeded,
wantConditionStatus: corev1.ConditionTrue,
wantConditionReason: "DurationElapsed",
}, {
name: "Wait Task Is Running",
duration: "2s",
conditionAccessorFn: Running,
wantConditionType: apis.ConditionSucceeded,
wantConditionStatus: corev1.ConditionUnknown,
wantConditionReason: "Running",
}} {
t.Run(tc.name, func(t *testing.T) {
runName := helpers.ObjectNameForTest(t)
run := parse.MustParseRun(t, fmt.Sprintf(`
metadata:
name: %s
spec:
ref:
apiVersion: %s
kind: %s
params:
- name: duration
value: %s
`, runName, apiVersion, kind, tc.duration))
if _, err := c.RunClient.Create(ctx, run, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create TaskRun %q: %v", runName, err)
}

// Wait for the Run.
if err := WaitForRunState(ctx, c, runName, time.Minute, tc.conditionAccessorFn(runName), tc.wantConditionReason); err != nil {
t.Fatalf("Waiting for Run to finish running: %v", err)
}

// Get the actual Run
gotRun, err := c.RunClient.Get(ctx, runName, metav1.GetOptions{})
if err != nil {
t.Fatalf("%v", err)
}

gotCondition := gotRun.GetStatus().GetCondition(apis.ConditionSucceeded)
if gotCondition == nil {
t.Fatal("The Run failed to succeed")
}

// Compose the expected Run
runYAML := fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
ref:
apiVersion: %s
kind: %s
params:
- name: duration
value: %s
serviceAccountName: default
status:
conditions:
- reason: %s
status: %q
type: %s
observedGeneration: 1
`, runName, namespace, apiVersion, kind, tc.duration, tc.wantConditionReason, tc.wantConditionStatus, tc.wantConditionType)
wantRun := parse.MustParseRun(t, runYAML)
var (
filterTypeMeta = cmpopts.IgnoreFields(v1alpha1.Run{}, "TypeMeta")
filterObjectMeta = cmpopts.IgnoreFields(metav1.ObjectMeta{}, "ResourceVersion", "UID", "CreationTimestamp", "Generation", "ManagedFields")
filterCondition = cmpopts.IgnoreFields(apis.Condition{}, "LastTransitionTime.Inner.Time", "Message")
filterRunStatus = cmpopts.IgnoreFields(v1alpha1.RunStatusFields{}, "StartTime", "CompletionTime")
jerop marked this conversation as resolved.
Show resolved Hide resolved
)
if d := cmp.Diff(wantRun, gotRun,
filterTypeMeta,
filterObjectMeta,
filterCondition,
filterRunStatus,
); d != "" {
t.Errorf("-got +want: %v", d)
}
})
}
}
10 changes: 10 additions & 0 deletions test/wait-task/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Wait Custom Task for Tekton

This folder is largely copied from [experimental/wait-task](https://github.com/tektoncd/experimental/tree/main/wait-task)
for the testing purpose, with resources used for building and releasing the
wait custom task removed.

It provides a [Tekton Custom
Task](https://tekton.dev/docs/pipelines/runs/) that, when run, simply waits a
given amount of time before succeeding, specified by an input parameter named
`duration`.
52 changes: 52 additions & 0 deletions test/wait-task/cmd/controller/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2021 The Tekton Authors

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 main // import "github.com/tektoncd/pipeline/test/wait-task/cmd/controller"

import (
"context"

runinformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1alpha1/run"
runreconciler "github.com/tektoncd/pipeline/pkg/client/injection/reconciler/pipeline/v1alpha1/run"
tkncontroller "github.com/tektoncd/pipeline/pkg/controller"
"github.com/tektoncd/pipeline/test/wait-task/pkg/reconciler"
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection/sharedmain"
)

const controllerName = "wait-task-controller"

func main() {
sharedmain.Main(controllerName, newController)
}

func newController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
c := &reconciler.Reconciler{}
impl := runreconciler.NewImpl(ctx, c, func(impl *controller.Impl) controller.Options {
return controller.Options{
AgentName: controllerName,
}
})

runinformer.Get(ctx).Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: tkncontroller.FilterRunRef("wait.testing.tekton.dev/v1alpha1", "Wait"),
Handler: controller.HandleAll(impl.Enqueue),
})

return impl
}
Loading