github.com/GoogleContainerTools/skaffold/v2@v2.13.2/pkg/diag/validator/resource_test.go (about) 1 /* 2 Copyright 2020 The Skaffold 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 validator 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 "google.golang.org/protobuf/testing/protocmp" 24 v1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 27 "github.com/GoogleContainerTools/skaffold/v2/proto/v1" 28 "github.com/GoogleContainerTools/skaffold/v2/testutil" 29 ) 30 31 func TestNewResource(t *testing.T) { 32 tests := []struct { 33 description string 34 resource objectWithMetadata 35 expected Resource 36 expectedName string 37 }{ 38 { 39 description: "pod in default namespace", 40 resource: &v1.Pod{ 41 TypeMeta: metav1.TypeMeta{Kind: "pod"}, 42 ObjectMeta: metav1.ObjectMeta{ 43 Namespace: "default", 44 Name: "foo", 45 }, 46 }, 47 expected: Resource{"default", "pod", "foo", nil, Status(""), &proto.ActionableErr{}}, 48 expectedName: "pod/foo", 49 }, 50 { 51 description: "pod in another namespace", 52 resource: &v1.Pod{ 53 TypeMeta: metav1.TypeMeta{Kind: "pod"}, 54 ObjectMeta: metav1.ObjectMeta{ 55 Namespace: "test", 56 Name: "bar", 57 }, 58 }, 59 expected: Resource{"test", "pod", "bar", nil, Status(""), &proto.ActionableErr{}}, 60 expectedName: "test:pod/bar", 61 }, 62 } 63 for _, test := range tests { 64 testutil.Run(t, test.description, func(t *testutil.T) { 65 actual := NewResourceFromObject(test.resource, Status(""), &proto.ActionableErr{}, nil) 66 t.CheckDeepEqual(test.expected, actual, cmp.AllowUnexported(Resource{}), protocmp.Transform()) 67 t.CheckDeepEqual(test.expectedName, actual.String(), cmp.AllowUnexported(Resource{}), protocmp.Transform()) 68 }) 69 } 70 }