From 1b1003234cbb4f2be0d76ff9c973126ca8a87110 Mon Sep 17 00:00:00 2001 From: mikeykhalil Date: Tue, 23 Apr 2019 20:02:58 -0700 Subject: [PATCH] tests: add test for AttributeFromType --- pkg/apis/pipeline/v1alpha1/resource_types.go | 13 ++++ .../pipeline/v1alpha1/resource_types_test.go | 60 +++++++++++++++++++ .../pipeline/v1alpha1/task_validation_test.go | 59 ++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 pkg/apis/pipeline/v1alpha1/resource_types_test.go diff --git a/pkg/apis/pipeline/v1alpha1/resource_types.go b/pkg/apis/pipeline/v1alpha1/resource_types.go index 359177de7d2..26b5ffa6d5e 100644 --- a/pkg/apis/pipeline/v1alpha1/resource_types.go +++ b/pkg/apis/pipeline/v1alpha1/resource_types.go @@ -170,6 +170,19 @@ func ResourceFromType(r *PipelineResource) (PipelineResourceInterface, error) { func AttributesFromType(prt PipelineResourceType) ([]string, error) { r := &PipelineResource{} r.Spec.Type = prt + if prt == PipelineResourceTypeStorage { + r.Spec.Params = []Param{ + + { + Name: "type", + Value: string(PipelineResourceTypeGCS), + }, + { + Name: "Location", + Value: "/", + }, + } + } resource, err := ResourceFromType(r) if err != nil { return nil, err diff --git a/pkg/apis/pipeline/v1alpha1/resource_types_test.go b/pkg/apis/pipeline/v1alpha1/resource_types_test.go new file mode 100644 index 00000000000..a6c746d5546 --- /dev/null +++ b/pkg/apis/pipeline/v1alpha1/resource_types_test.go @@ -0,0 +1,60 @@ +/* +Copyright 2018 The Knative 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 ( + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestAttributesFromType(t *testing.T) { + tests := []struct { + name string + resourceType PipelineResourceType + expectedErr error + }{ + { + name: "git resource type", + resourceType: PipelineResourceTypeGit, + expectedErr: nil, + }, + { + name: "storage resource type", + resourceType: PipelineResourceTypeStorage, + expectedErr: nil, + }, + { + name: "image resource type", + resourceType: PipelineResourceTypeImage, + expectedErr: nil, + }, + { + name: "cluster resource type", + resourceType: PipelineResourceTypeCluster, + expectedErr: nil, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + _, err := AttributesFromType(tc.resourceType) + if d := cmp.Diff(err, tc.expectedErr); d != "" { + t.Errorf("AttributesFromType error did not match expected error %s", d) + } + }) + } +} diff --git a/pkg/apis/pipeline/v1alpha1/task_validation_test.go b/pkg/apis/pipeline/v1alpha1/task_validation_test.go index 9b70c212c2a..5f1b97403cd 100644 --- a/pkg/apis/pipeline/v1alpha1/task_validation_test.go +++ b/pkg/apis/pipeline/v1alpha1/task_validation_test.go @@ -378,3 +378,62 @@ func TestTaskSpec_ValidateError(t *testing.T) { }) } } + +func TestGetResourceVariables(t *testing.T) { + tests := []struct { + name string + pathPrefix string + resources []TaskResource + expectedVars map[string]struct{} + }{ + { + name: "empty resources", + pathPrefix: "taskspec.outputs.resources.", + resources: []TaskResource{}, + expectedVars: map[string]struct{}{}, + }, + { + name: "single resource", + pathPrefix: "taskspec.outputs.resources.", + resources: []TaskResource{validResource}, + expectedVars: map[string]struct{}{ + "source.name": struct{}{}, + "source.type": struct{}{}, + "source.url": struct{}{}, + "source.revision": struct{}{}, + }, + }, + { + name: "multiple resources", + pathPrefix: "taskspec.outputs.resources.", + resources: []TaskResource{ + validResource, + TaskResource{ + Name: "foo", + Type: PipelineResourceTypeImage, + }, + }, + expectedVars: map[string]struct{}{ + "source.name": struct{}{}, + "source.type": struct{}{}, + "source.url": struct{}{}, + "source.revision": struct{}{}, + "foo.name": struct{}{}, + "foo.type": struct{}{}, + "foo.url": struct{}{}, + "foo.digest": struct{}{}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + vars, err := getResourceVariables(tt.resources, tt.pathPrefix) + if err != nil { + t.Errorf("getResourceVariables() = %v", err) + } + if d := cmp.Diff(tt.expectedVars, vars); d != "" { + t.Errorf("getResourceVariables results diff -want, +got: %v", d) + } + }) + } +}