Skip to content

Commit

Permalink
tests: add test for AttributeFromType
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeykhalil committed Apr 28, 2019
1 parent 93e68d0 commit 1b10032
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/apis/pipeline/v1alpha1/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions pkg/apis/pipeline/v1alpha1/resource_types_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
59 changes: 59 additions & 0 deletions pkg/apis/pipeline/v1alpha1/task_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 1b10032

Please sign in to comment.