Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task, ClusterTask and Pipeline auto-conversion from v1alpha1 to v1alpha2 🎋 #2002

Merged
merged 4 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions pkg/apis/pipeline/v1alpha1/cluster_task_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2020 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.
*/

// nolint: golint
package v1alpha1

import (
"context"
"fmt"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2"
"knative.dev/pkg/apis"
)

var _ apis.Convertible = (*ClusterTask)(nil)

// ConvertUp implements api.Convertible
func (source *ClusterTask) ConvertUp(ctx context.Context, obj apis.Convertible) error {
switch sink := obj.(type) {
case *v1alpha2.ClusterTask:
sink.ObjectMeta = source.ObjectMeta
return source.Spec.ConvertUp(ctx, &sink.Spec)
default:
return fmt.Errorf("unknown version, got: %T", sink)
}
}

// ConvertDown implements api.Convertible
func (sink *ClusterTask) ConvertDown(ctx context.Context, obj apis.Convertible) error {
switch source := obj.(type) {
case *v1alpha2.ClusterTask:
sink.ObjectMeta = source.ObjectMeta
return sink.Spec.ConvertDown(ctx, &source.Spec)
default:
return fmt.Errorf("unknown version, got: %T", sink)
}
}
321 changes: 321 additions & 0 deletions pkg/apis/pipeline/v1alpha1/cluster_task_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
/*
Copyright 2020 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

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2"
resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)

func TestClusterTaskConversionBadType(t *testing.T) {
good, bad := &ClusterTask{}, &Pipeline{}

if err := good.ConvertUp(context.Background(), bad); err == nil {
t.Errorf("ConvertUp() = %#v, wanted error", bad)
}

if err := good.ConvertDown(context.Background(), bad); err == nil {
t.Errorf("ConvertUp() = %#v, wanted error", bad)
}
}

func TestClusterTaskConversion(t *testing.T) {
versions := []apis.Convertible{&v1alpha2.ClusterTask{}}

tests := []struct {
name string
in *ClusterTask
wantErr bool
}{{
name: "simple conversion",
in: &ClusterTask{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Generation: 1,
},
Spec: TaskSpec{
TaskSpec: v1alpha2.TaskSpec{
Steps: []v1alpha2.Step{{Container: corev1.Container{
Image: "foo",
}}},
Volumes: []corev1.Volume{{}},
Params: []v1alpha2.ParamSpec{{
Name: "param-1",
Type: v1alpha2.ParamTypeString,
Description: "My first param",
}},
Resources: &v1alpha2.TaskResources{
Inputs: []v1alpha2.TaskResource{{ResourceDeclaration: v1alpha2.ResourceDeclaration{
Name: "input-1",
Type: resource.PipelineResourceTypeGit,
}}},
Outputs: []v1alpha2.TaskResource{{ResourceDeclaration: v1alpha2.ResourceDeclaration{
Name: "output-1",
Type: resource.PipelineResourceTypeGit,
}}},
},
},
},
},
}, {
name: "deprecated and non deprecated inputs",
in: &ClusterTask{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Generation: 1,
},
Spec: TaskSpec{
TaskSpec: v1alpha2.TaskSpec{
Resources: &v1alpha2.TaskResources{
Inputs: []v1alpha2.TaskResource{{ResourceDeclaration: v1alpha2.ResourceDeclaration{
Name: "input-1",
Type: resource.PipelineResourceTypeGit,
}}},
},
},
Inputs: &Inputs{
Resources: []TaskResource{{ResourceDeclaration: ResourceDeclaration{
Name: "input-1",
Type: resource.PipelineResourceTypeGit,
}}},
},
},
},
wantErr: true,
}, {
name: "deprecated and non deprecated inputs",
in: &ClusterTask{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Generation: 1,
},
Spec: TaskSpec{
TaskSpec: v1alpha2.TaskSpec{
Params: []v1alpha2.ParamSpec{{
Name: "param-1",
Type: v1alpha2.ParamTypeString,
Description: "My first param",
}},
},
Inputs: &Inputs{
Params: []ParamSpec{{
Name: "param-1",
Type: v1alpha2.ParamTypeString,
Description: "My first param",
}},
},
},
},
wantErr: true,
}, {
name: "deprecated and non deprecated outputs",
in: &ClusterTask{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Generation: 1,
},
Spec: TaskSpec{
TaskSpec: v1alpha2.TaskSpec{
Resources: &v1alpha2.TaskResources{
Outputs: []v1alpha2.TaskResource{{ResourceDeclaration: v1alpha2.ResourceDeclaration{
Name: "output-1",
Type: resource.PipelineResourceTypeGit,
}}},
},
},
Outputs: &Outputs{
Resources: []TaskResource{{ResourceDeclaration: ResourceDeclaration{
Name: "output-1",
Type: resource.PipelineResourceTypeGit,
}}},
},
},
},
wantErr: true,
}}

for _, test := range tests {
for _, version := range versions {
t.Run(test.name, func(t *testing.T) {
ver := version
if err := test.in.ConvertUp(context.Background(), ver); err != nil {
if !test.wantErr {
t.Errorf("ConvertUp() = %v", err)
}
return
}
t.Logf("ConvertUp() = %#v", ver)
got := &ClusterTask{}
if err := got.ConvertDown(context.Background(), ver); err != nil {
t.Errorf("ConvertDown() = %v", err)
}
t.Logf("ConvertDown() = %#v", got)
if diff := cmp.Diff(test.in, got); diff != "" {
t.Errorf("roundtrip (-want, +got) = %v", diff)
}
})
}
}
}

func TestClusterTaskConversionFromDeprecated(t *testing.T) {
versions := []apis.Convertible{&v1alpha2.ClusterTask{}}
tests := []struct {
name string
in *ClusterTask
want *ClusterTask
badField string
}{{
name: "inputs params",
in: &ClusterTask{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Generation: 1,
},
Spec: TaskSpec{
Inputs: &Inputs{
Params: []ParamSpec{{
Name: "param-1",
Type: v1alpha2.ParamTypeString,
Description: "My first param",
}},
},
},
},
want: &ClusterTask{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Generation: 1,
},
Spec: TaskSpec{
TaskSpec: v1alpha2.TaskSpec{
Params: []v1alpha2.ParamSpec{{
Name: "param-1",
Type: v1alpha2.ParamTypeString,
Description: "My first param",
}},
},
},
},
}, {
name: "inputs resource",
in: &ClusterTask{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Generation: 1,
},
Spec: TaskSpec{
Inputs: &Inputs{
Resources: []TaskResource{{ResourceDeclaration: ResourceDeclaration{
Name: "input-1",
Type: resource.PipelineResourceTypeGit,
}}},
},
},
},
want: &ClusterTask{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Generation: 1,
},
Spec: TaskSpec{
TaskSpec: v1alpha2.TaskSpec{
Resources: &v1alpha2.TaskResources{
Inputs: []v1alpha2.TaskResource{{ResourceDeclaration: v1alpha2.ResourceDeclaration{
Name: "input-1",
Type: resource.PipelineResourceTypeGit,
}}},
},
},
},
},
}, {
name: "outputs resource",
in: &ClusterTask{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Generation: 1,
},
Spec: TaskSpec{
Outputs: &Outputs{
Resources: []TaskResource{{ResourceDeclaration: ResourceDeclaration{
Name: "output-1",
Type: resource.PipelineResourceTypeGit,
}}},
},
},
},
want: &ClusterTask{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Generation: 1,
},
Spec: TaskSpec{
TaskSpec: v1alpha2.TaskSpec{
Resources: &v1alpha2.TaskResources{
Outputs: []v1alpha2.TaskResource{{ResourceDeclaration: v1alpha2.ResourceDeclaration{
Name: "output-1",
Type: resource.PipelineResourceTypeGit,
}}},
},
},
},
},
}}
for _, test := range tests {
for _, version := range versions {
t.Run(test.name, func(t *testing.T) {
ver := version
if err := test.in.ConvertUp(context.Background(), ver); err != nil {
if test.badField != "" {
cce, ok := err.(*CannotConvertError)
if ok && cce.Field == test.badField {
return
}
}
t.Errorf("ConvertUp() = %v", err)
}
t.Logf("ConvertUp() = %#v", ver)
got := &ClusterTask{}
if err := got.ConvertDown(context.Background(), ver); err != nil {
t.Errorf("ConvertDown() = %v", err)
}
t.Logf("ConvertDown() = %#v", got)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("roundtrip (-want, +got) = %v", diff)
}
})
}
}
}
Loading