Skip to content

Commit

Permalink
Move context*.go to contexts and add tests 🎐
Browse files Browse the repository at this point in the history
The package it is on doesn't really matter, so adding it to the
latests api version. Also adding quick tests for this file.

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester authored and tekton-robot committed Dec 11, 2019
1 parent e741743 commit db33fd7
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 8 deletions.
3 changes: 2 additions & 1 deletion cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

apiconfig "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/contexts"
tklogging "github.com/tektoncd/pipeline/pkg/logging"
"github.com/tektoncd/pipeline/pkg/system"
"go.uber.org/zap"
Expand Down Expand Up @@ -110,7 +111,7 @@ func main() {

// Decorate contexts with the current state of the config.
ctxFunc := func(ctx context.Context) context.Context {
return v1alpha1.WithDefaultConfigurationName(store.ToContext(ctx))
return contexts.WithDefaultConfigurationName(store.ToContext(ctx))
}

controller, err := webhook.New(kubeClient, options, admissionControllers, logger, ctxFunc)
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/pipeline/v1alpha1/pipelinerun_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/contexts"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)
Expand All @@ -35,7 +36,7 @@ func (prs *PipelineRunSpec) SetDefaults(ctx context.Context) {
cfg := config.FromContextOrDefaults(ctx)
if prs.Timeout == nil {
var timeout *metav1.Duration
if IsUpgradeViaDefaulting(ctx) {
if contexts.IsUpgradeViaDefaulting(ctx) {
// This case is for preexisting `TaskRun` before 0.5.0, so let's
// add the old default timeout.
// Most likely those TaskRun passing here are already done and/or already running
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/pipeline/v1alpha1/pipelinerun_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/contexts"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
logtesting "knative.dev/pkg/logging/testing"
Expand Down Expand Up @@ -97,7 +98,7 @@ func TestPipelineRunDefaulting(t *testing.T) {
Timeout: &metav1.Duration{Duration: config.DefaultTimeoutMinutes * time.Minute},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
}, {
name: "PipelineRef default config context",
in: &v1alpha1.PipelineRun{
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/pipeline/v1alpha1/taskrun_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/contexts"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)
Expand All @@ -39,7 +40,7 @@ func (trs *TaskRunSpec) SetDefaults(ctx context.Context) {

if trs.Timeout == nil {
var timeout *metav1.Duration
if IsUpgradeViaDefaulting(ctx) {
if contexts.IsUpgradeViaDefaulting(ctx) {
// This case is for preexisting `TaskRun` before 0.5.0, so let's
// add the old default timeout.
// Most likely those TaskRun passing here are already done and/or already running
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/pipeline/v1alpha1/taskrun_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/contexts"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
logtesting "knative.dev/pkg/logging/testing"
Expand Down Expand Up @@ -138,7 +139,7 @@ func TestTaskRunDefaulting(t *testing.T) {
Timeout: &metav1.Duration{Duration: config.DefaultTimeoutMinutes * time.Minute},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
}, {
name: "TaskRef default config context",
in: &v1alpha1.TaskRun{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1
package contexts

import "context"

Expand Down
60 changes: 60 additions & 0 deletions pkg/contexts/contexts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2019 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 contexts

import (
"context"
"testing"
)

func TestContexts(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
ctx context.Context
check func(context.Context) bool
want bool
}{{
name: "has default config name",
ctx: WithDefaultConfigurationName(ctx),
check: HasDefaultConfigurationName,
want: true,
}, {
name: "doesn't have default config name",
ctx: ctx,
check: HasDefaultConfigurationName,
want: false,
}, {
name: "are upgrading via defaulting",
ctx: WithUpgradeViaDefaulting(ctx),
check: IsUpgradeViaDefaulting,
want: true,
}, {
name: "aren't upgrading via defaulting",
ctx: ctx,
check: IsUpgradeViaDefaulting,
want: false,
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := tc.check(tc.ctx)
if tc.want != got {
t.Errorf("check() = %v, wanted %v", tc.want, got)
}
})
}
}
3 changes: 2 additions & 1 deletion pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/artifacts"
listers "github.com/tektoncd/pipeline/pkg/client/listers/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/contexts"
"github.com/tektoncd/pipeline/pkg/reconciler"
"github.com/tektoncd/pipeline/pkg/reconciler/pipeline/dag"
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources"
Expand Down Expand Up @@ -230,7 +231,7 @@ func (c *Reconciler) getPipelineFunc(tr *v1alpha1.PipelineRun) resources.GetPipe
func (c *Reconciler) reconcile(ctx context.Context, pr *v1alpha1.PipelineRun) error {
// We may be reading a version of the object that was stored at an older version
// and may not have had all of the assumed default specified.
pr.SetDefaults(v1alpha1.WithUpgradeViaDefaulting(ctx))
pr.SetDefaults(contexts.WithUpgradeViaDefaulting(ctx))

getPipelineFunc := c.getPipelineFunc(pr)
pipelineMeta, pipelineSpec, err := resources.GetPipelineData(pr, getPipelineFunc)
Expand Down
3 changes: 2 additions & 1 deletion pkg/reconciler/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
listers "github.com/tektoncd/pipeline/pkg/client/listers/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/contexts"
podconvert "github.com/tektoncd/pipeline/pkg/pod"
"github.com/tektoncd/pipeline/pkg/reconciler"
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun/resources"
Expand Down Expand Up @@ -223,7 +224,7 @@ func (c *Reconciler) getTaskFunc(tr *v1alpha1.TaskRun) (resources.GetTask, v1alp
func (c *Reconciler) reconcile(ctx context.Context, tr *v1alpha1.TaskRun) error {
// We may be reading a version of the object that was stored at an older version
// and may not have had all of the assumed default specified.
tr.SetDefaults(v1alpha1.WithUpgradeViaDefaulting(ctx))
tr.SetDefaults(contexts.WithUpgradeViaDefaulting(ctx))

// If the taskrun is cancelled, kill resources and update status
if tr.IsCancelled() {
Expand Down

0 comments on commit db33fd7

Please sign in to comment.