|
| 1 | +/* |
| 2 | +Copyright 2019 The Tekton Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha1_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/google/go-cmp/cmp" |
| 25 | + "github.com/tektoncd/pipeline/pkg/apis/config" |
| 26 | + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | +) |
| 29 | + |
| 30 | +func TestTaskRunSpec_SetDefaults(t *testing.T) { |
| 31 | + cases := []struct { |
| 32 | + desc string |
| 33 | + trs *v1alpha1.TaskRunSpec |
| 34 | + want *v1alpha1.TaskRunSpec |
| 35 | + }{ |
| 36 | + { |
| 37 | + desc: "taskref is nil", |
| 38 | + trs: &v1alpha1.TaskRunSpec{ |
| 39 | + TaskRef: nil, |
| 40 | + Timeout: &metav1.Duration{Duration: 500 * time.Millisecond}, |
| 41 | + }, |
| 42 | + want: &v1alpha1.TaskRunSpec{ |
| 43 | + TaskRef: nil, |
| 44 | + Timeout: &metav1.Duration{Duration: 500 * time.Millisecond}, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + desc: "taskref kind is empty", |
| 49 | + trs: &v1alpha1.TaskRunSpec{ |
| 50 | + TaskRef: &v1alpha1.TaskRef{}, |
| 51 | + Timeout: &metav1.Duration{Duration: 500 * time.Millisecond}, |
| 52 | + }, |
| 53 | + want: &v1alpha1.TaskRunSpec{ |
| 54 | + TaskRef: &v1alpha1.TaskRef{Kind: v1alpha1.NamespacedTaskKind}, |
| 55 | + Timeout: &metav1.Duration{Duration: 500 * time.Millisecond}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + desc: "timeout is nil", |
| 60 | + trs: &v1alpha1.TaskRunSpec{ |
| 61 | + TaskRef: &v1alpha1.TaskRef{Kind: v1alpha1.ClusterTaskKind}, |
| 62 | + }, |
| 63 | + want: &v1alpha1.TaskRunSpec{ |
| 64 | + TaskRef: &v1alpha1.TaskRef{Kind: v1alpha1.ClusterTaskKind}, |
| 65 | + Timeout: &metav1.Duration{Duration: config.DefaultTimeoutMinutes * time.Minute}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | + for _, tc := range cases { |
| 70 | + t.Run(tc.desc, func(t *testing.T) { |
| 71 | + ctx := context.Background() |
| 72 | + tc.trs.SetDefaults(ctx) |
| 73 | + |
| 74 | + if diff := cmp.Diff(tc.want, tc.trs); diff != "" { |
| 75 | + t.Errorf("Mismatch of TaskRunSpec: %s", diff) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
0 commit comments