Skip to content

Commit

Permalink
Add missing tests for pipelinerun_defaults and taskrun_defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuhei Kitagawa authored and tekton-robot committed Aug 5, 2019
1 parent 0eaa862 commit 7a11326
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkg/apis/pipeline/v1alpha1/pipelinerun_defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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 v1alpha1_test

import (
"context"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestPipelineRunSpec_SetDefaults(t *testing.T) {
cases := []struct {
desc string
prs *v1alpha1.PipelineRunSpec
want *v1alpha1.PipelineRunSpec
}{
{
desc: "timeout is nil",
prs: &v1alpha1.PipelineRunSpec{},
want: &v1alpha1.PipelineRunSpec{
Timeout: &metav1.Duration{Duration: config.DefaultTimeoutMinutes * time.Minute},
},
},
{
desc: "timeout is not nil",
prs: &v1alpha1.PipelineRunSpec{
Timeout: &metav1.Duration{Duration: 500 * time.Millisecond},
},
want: &v1alpha1.PipelineRunSpec{
Timeout: &metav1.Duration{Duration: 500 * time.Millisecond},
},
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
ctx := context.Background()
tc.prs.SetDefaults(ctx)

if diff := cmp.Diff(tc.want, tc.prs); diff != "" {
t.Errorf("Mismatch of PipelineRunSpec: %s", diff)
}
})
}
}
79 changes: 79 additions & 0 deletions pkg/apis/pipeline/v1alpha1/taskrun_defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
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 v1alpha1_test

import (
"context"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestTaskRunSpec_SetDefaults(t *testing.T) {
cases := []struct {
desc string
trs *v1alpha1.TaskRunSpec
want *v1alpha1.TaskRunSpec
}{
{
desc: "taskref is nil",
trs: &v1alpha1.TaskRunSpec{
TaskRef: nil,
Timeout: &metav1.Duration{Duration: 500 * time.Millisecond},
},
want: &v1alpha1.TaskRunSpec{
TaskRef: nil,
Timeout: &metav1.Duration{Duration: 500 * time.Millisecond},
},
},
{
desc: "taskref kind is empty",
trs: &v1alpha1.TaskRunSpec{
TaskRef: &v1alpha1.TaskRef{},
Timeout: &metav1.Duration{Duration: 500 * time.Millisecond},
},
want: &v1alpha1.TaskRunSpec{
TaskRef: &v1alpha1.TaskRef{Kind: v1alpha1.NamespacedTaskKind},
Timeout: &metav1.Duration{Duration: 500 * time.Millisecond},
},
},
{
desc: "timeout is nil",
trs: &v1alpha1.TaskRunSpec{
TaskRef: &v1alpha1.TaskRef{Kind: v1alpha1.ClusterTaskKind},
},
want: &v1alpha1.TaskRunSpec{
TaskRef: &v1alpha1.TaskRef{Kind: v1alpha1.ClusterTaskKind},
Timeout: &metav1.Duration{Duration: config.DefaultTimeoutMinutes * time.Minute},
},
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
ctx := context.Background()
tc.trs.SetDefaults(ctx)

if diff := cmp.Diff(tc.want, tc.trs); diff != "" {
t.Errorf("Mismatch of TaskRunSpec: %s", diff)
}
})
}
}

0 comments on commit 7a11326

Please sign in to comment.