Skip to content

Commit

Permalink
Custom Task E2E Test with A Controller Installed
Browse files Browse the repository at this point in the history
This commit introduces a `wait-task` controller into test/ folder to
better support Custom Task e2e testing.

Previously we update the Run object to mimic the behavior of a custom
task controller. This commit introduces a real controller to reconcile
the custom task run.
  • Loading branch information
XinruZhang committed Aug 19, 2022
1 parent 8413f85 commit e76577d
Show file tree
Hide file tree
Showing 9 changed files with 2,710 additions and 3 deletions.
133 changes: 131 additions & 2 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 @@ -46,7 +48,7 @@ import (

const (
apiVersion = "example.dev/v0"
kind = "Example"
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)
}
}

func TestCustomTask_NoCRD(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")
)
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("example.dev/v0", "Wait"),
Handler: controller.HandleAll(impl.Enqueue),
})

return impl
}
Loading

0 comments on commit e76577d

Please sign in to comment.