Skip to content

Commit

Permalink
Modify unit tests
Browse files Browse the repository at this point in the history
modify some unit test files
  • Loading branch information
anxinyf committed Nov 11, 2019
1 parent d0e1bdb commit cd77e3d
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 182 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1alpha1/pipelinerun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (ps *PipelineRunSpec) Validate(ctx context.Context) *apis.FieldError {
}

if ps.ExpirationSecondsTTL != nil {
// DelayDeleteTTL should be a valid duration of at least 0.
// ExpirationSecondsTTL should be a valid duration of at least 0.
if ps.ExpirationSecondsTTL.Duration < 0 {
return apis.ErrInvalidValue(fmt.Sprintf("%s should be >= 0", ps.ExpirationSecondsTTL.Duration.String()), "spec.expirationSecondsTTL")
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/pipeline/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions pkg/reconciler/pipelinerun/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
taskruninformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1alpha1/taskrun"
"github.com/tektoncd/pipeline/pkg/reconciler"
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/config"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
kubeclient "knative.dev/pkg/client/injection/kube/client"
Expand Down Expand Up @@ -97,35 +98,37 @@ func NewController(images pipeline.Images) func(context.Context, configmap.Watch
})

AddPipelineRun := func(obj interface{}) {
pr := obj.(*v1alpha1.PipelineRun)
c.Logger.Infof("Adding PipelineRun %s/%s", pr.Namespace, pr.Name)
pr := obj.(*v1alpha1.PipelineRun)
c.Logger.Infof("Adding PipelineRun %s/%s", pr.Namespace, pr.Name)

if pr.DeletionTimestamp == nil && pipelineRunCleanup(pr) {
impl.Enqueue(pr)
}
if pr.DeletionTimestamp == nil && pipelineRunCleanup(pr) {
impl.Enqueue(pr)
}
}

UpdatePipelineRun := func(old, cur interface{}) {
pr := cur.(*v1alpha1.PipelineRun)
c.Logger.Infof("Updating PipelineRun %s/%s", pr.Namespace, pr.Name)
pr := cur.(*v1alpha1.PipelineRun)
c.Logger.Infof("Updating PipelineRun %s/%s", pr.Namespace, pr.Name)

if pr.DeletionTimestamp == nil && pipelineRunCleanup(pr) {
impl.Enqueue(pr)
}
if pr.DeletionTimestamp == nil && pipelineRunCleanup(pr) {
impl.Enqueue(pr)
}
}

pipelineRunInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: AddPipelineRun,
UpdateFunc: UpdatePipelineRun,
})

c.pipelineRunLister = pipelineRunInformer.Lister()
c.ListerSynced = pipelineRunInformer.Informer().HasSynced

c.tracker = tracker.New(impl.EnqueueKey, 30*time.Minute)
taskRunInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: controller.PassNew(impl.EnqueueControllerOf),
})

c.clock = clock.RealClock{}
c.Logger.Info("Setting up ConfigMap receivers")
c.configStore = config.NewStore(images, c.Logger.Named("config-store"))
c.configStore.WatchConfigs(opt.ConfigMapWatcher)
Expand Down
2 changes: 0 additions & 2 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/artifacts"
clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
listers "github.com/tektoncd/pipeline/pkg/client/listers/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/reconciler"
"github.com/tektoncd/pipeline/pkg/reconciler/pipeline/dag"
Expand All @@ -42,7 +41,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"knative.dev/pkg/apis"
"knative.dev/pkg/configmap"
Expand Down
56 changes: 20 additions & 36 deletions pkg/reconciler/pipelinerun/pipelinerun_expired_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pipelinerun

import (
"github.com/tektoncd/pipeline/test"
"strings"
"time"

Expand Down Expand Up @@ -36,7 +37,7 @@ func newPipelineRun(completionTime, failedTime apis.VolatileTime, ttl *metav1.Du
}

if ttl != nil {
pr.Spec.ExpirationSecondsTTL= ttl
pr.Spec.ExpirationSecondsTTL = ttl
}

return pr
Expand All @@ -62,7 +63,7 @@ func TestTimeLeft(t *testing.T) {
}{
{
name: "Error case: PipelineRun unfinished",
ttl: &metav1.Duration{Duration:10 * time.Second},
ttl: &metav1.Duration{Duration: 10 * time.Second},
since: &now.Inner.Time,
expectErr: true,
expectErrStr: "should not be cleaned up",
Expand All @@ -77,57 +78,41 @@ func TestTimeLeft(t *testing.T) {
{
name: "PipelineRun completed now, 0s TTL",
completionTime: now,
ttl: &metav1.Duration{Duration:0 * time.Second},
ttl: &metav1.Duration{Duration: 0 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(0),
},
{
name: "PipelineRun completed now, 10s TTL",
completionTime: now,
ttl: &metav1.Duration{Duration:10 * time.Second},
ttl: &metav1.Duration{Duration: 10 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(10),
},
{
name: "PipelineRun completed 10s ago, 15s TTL",
completionTime: apis.VolatileTime{Inner: metav1.NewTime(now.Inner.Add(-10 * time.Second))},
ttl: &metav1.Duration{Duration:15 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(5),
},
{
name: "Error case: PipelineRun failed now, no TTL",
failedTime: now,
since: &now.Inner.Time,
expectErr: true,
expectErrStr: "should not be cleaned up",
},
{
name: "PipelineRun failed now, 0s TTL",
//failedTime: now,
ttl: &metav1.Duration{Duration:0 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(0),
},
{
name: "PipelineRun failed now, 10s TTL",
failedTime: now,
ttl: &metav1.Duration{Duration:10 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(10),
},
{
name: "PipelineRun failed 10s ago, 15s TTL",
failedTime: apis.VolatileTime{Inner: metav1.NewTime(now.Inner.Add(-10 * time.Second))},
ttl: &metav1.Duration{Duration:15 * time.Second},
ttl: &metav1.Duration{Duration: 15 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(5),
},
}
for _, tc := range PrTestCases {
pr := newPipelineRun(tc.completionTime, tc.failedTime, tc.ttl)
reconcile := Reconciler{}
gotPrTimeLeft, gotPrErr := reconcile.prTimeLeft(pr, tc.since)
d := test.Data{
PipelineRuns: []*apispipeline.PipelineRun{pr},
}
testAssets, cancel := getPipelineRunController(t, d)
defer cancel()
p, ok := testAssets.Controller.Reconciler.(*Reconciler)
if !ok {
t.Errorf("failed to construct instance of taskrun reconciler")
return
}

// Prevent backoff timer from starting
p.timeoutHandler.SetPipelineRunCallbackFunc(nil)
gotPrTimeLeft, gotPrErr := p.prTimeLeft(pr, tc.since)

if tc.expectErr != (gotPrErr != nil) {
t.Errorf("%s: expected error is %t, got %t, error: %v", tc.name, tc.expectErr, gotPrErr != nil, gotPrErr)
Expand All @@ -145,4 +130,3 @@ func TestTimeLeft(t *testing.T) {
}
}
}

8 changes: 6 additions & 2 deletions pkg/reconciler/taskrun/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import (
"github.com/tektoncd/pipeline/pkg/reconciler"
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun/entrypoint"
cloudeventclient "github.com/tektoncd/pipeline/pkg/reconciler/taskrun/resources/cloudevent"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/client-go/util/workqueue"
kubeclient "knative.dev/pkg/client/injection/kube/client"
podinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/pod"
"knative.dev/pkg/configmap"
Expand Down Expand Up @@ -77,7 +78,8 @@ func NewController(images pipeline.Images) func(context.Context, configmap.Watch
timeoutHandler: timeoutHandler,
cloudEventClient: cloudeventclient.Get(ctx),
metrics: metrics,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ttl_taskruns_to_delete"),
//recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "ttl-after-finished-controller"}),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ttl_taskruns_to_delete"),
}
impl := controller.NewImpl(c, c.Logger, taskRunControllerName)

Expand Down Expand Up @@ -113,6 +115,7 @@ func NewController(images pipeline.Images) func(context.Context, configmap.Watch
UpdateFunc: UpdateTaskRun,
})

c.taskRunLister = taskRunInformer.Lister()
c.ListerSynced = taskRunInformer.Informer().HasSynced

c.tracker = tracker.New(impl.EnqueueKey, controller.GetTrackerLease(ctx))
Expand All @@ -122,6 +125,7 @@ func NewController(images pipeline.Images) func(context.Context, configmap.Watch
Handler: controller.HandleAll(impl.EnqueueControllerOf),
})

c.clock = clock.RealClock{}
// FIXME(vdemeester) it was never set
//entrypoint cache will be initialized by controller if not provided
c.Logger.Info("Setting up Entrypoint cache")
Expand Down
67 changes: 24 additions & 43 deletions pkg/reconciler/taskrun/taskrun_expired_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (

"github.com/tektoncd/pipeline/pkg/apis/pipeline"
apispipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/test"
tb "github.com/tektoncd/pipeline/test/builder"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
"testing"
)

func newTaskRun(completionTime, failedTime apis.VolatileTime, ttl *metav1.Duration) *apispipeline.TaskRun {
func newTaskRun(completionTime apis.VolatileTime, ttl *metav1.Duration) *apispipeline.TaskRun {
tr := tb.TaskRun("test-pipeline-run-with-annotations-hello-world-1-9l9zj", "foo",
//tb.TaskRunOwnerReference("PipelineRun", "test-pipeline-run-with-annotations",
// tb.OwnerReferenceAPIVersion("tekton.dev/v1alpha1"),
Expand All @@ -34,11 +35,6 @@ func newTaskRun(completionTime, failedTime apis.VolatileTime, ttl *metav1.Durati
tr.Status.Conditions = append(tr.Status.Conditions, c)
}

if !failedTime.Inner.IsZero() {
c := apis.Condition{Type: apis.ConditionSucceeded, Status: v1.ConditionFalse, LastTransitionTime: failedTime}
tr.Status.Conditions = append(tr.Status.Conditions, c)
}

if ttl != nil {
tr.Spec.ExpirationSecondsTTL = ttl
}
Expand All @@ -56,8 +52,7 @@ func TestTimeLeft(t *testing.T) {

testCases := []struct {
name string
completionTime apis.VolatileTime
failedTime apis.VolatileTime
completionTime apis.VolatileTime
ttl *metav1.Duration
since *time.Time
expectErr bool
Expand All @@ -66,7 +61,7 @@ func TestTimeLeft(t *testing.T) {
}{
{
name: "Error case: TaskRun unfinished",
ttl: &metav1.Duration{Duration:10 * time.Second},
ttl: &metav1.Duration{Duration: 10 * time.Second},
since: &now.Inner.Time,
expectErr: true,
expectErrStr: "should not be cleaned up",
Expand All @@ -81,57 +76,43 @@ func TestTimeLeft(t *testing.T) {
{
name: "TaskRun completed now, 0s TTL",
completionTime: now,
ttl: &metav1.Duration{Duration:0 * time.Second},
ttl: &metav1.Duration{Duration: 0 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(0),
},
{
name: "TaskRun completed now, 10s TTL",
completionTime: now,
ttl: &metav1.Duration{Duration:10 * time.Second},
ttl: &metav1.Duration{Duration: 10 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(10),
},
{
name: "TaskRun completed 10s ago, 15s TTL",
completionTime: apis.VolatileTime{Inner: metav1.NewTime(now.Inner.Add(-10 * time.Second))},
ttl: &metav1.Duration{Duration:15 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(5),
},
{
name: "Error case: TaskRun failed now, no TTL",
failedTime: now,
since: &now.Inner.Time,
expectErr: true,
expectErrStr: "should not be cleaned up",
},
{
name: "TaskRun failed now, 0s TTL",
failedTime: now,
ttl: &metav1.Duration{Duration:0 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(0),
},
{
name: "TaskRun failed now, 10s TTL",
failedTime: now,
ttl: &metav1.Duration{Duration:10 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(10),
},
{
name: "TaskRun failed 10s ago, 15s TTL",
failedTime: apis.VolatileTime{Inner: metav1.NewTime(now.Inner.Add(-10 * time.Second))},
ttl: &metav1.Duration{Duration:15 * time.Second},
ttl: &metav1.Duration{Duration: 15 * time.Second},
since: &now.Inner.Time,
expectedTimeLeft: durationPointer(5),
},
}
for _, tc := range testCases {
tr := newTaskRun(tc.completionTime, tc.failedTime, tc.ttl)
reconcile := Reconciler{}
gotTrTimeLeft, gotTrErr := reconcile.trTimeLeft(tr, tc.since)
tr := newTaskRun(tc.completionTime, tc.ttl)

d := test.Data{
TaskRuns: []*apispipeline.TaskRun{tr},
}
testAssets, cancel := getTaskRunController(t, d)
defer cancel()
c, ok := testAssets.Controller.Reconciler.(*Reconciler)
if !ok {
t.Errorf("failed to construct instance of taskrun reconciler")
return
}

// Prevent backoff timer from starting
c.timeoutHandler.SetTaskRunCallbackFunc(nil)

gotTrTimeLeft, gotTrErr := c.trTimeLeft(tr, tc.since)

if tc.expectErr != (gotTrErr != nil) {
t.Errorf("%s: expected error is %t, got %t, error: %v", tc.name, tc.expectErr, gotTrErr != nil, gotTrErr)
Expand Down
Loading

0 comments on commit cd77e3d

Please sign in to comment.