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

Remove default from resource name #4270

Merged
merged 2 commits into from
May 28, 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
5 changes: 4 additions & 1 deletion pkg/diag/validator/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (r Resource) Namespace() string { return r.namespace }
func (r Resource) Status() Status { return r.status }
func (r Resource) Error() error { return r.err }
func (r Resource) String() string {
return fmt.Sprintf("{%s:%s/%s}", r.kind, r.namespace, r.name)
if r.namespace == "default" {
return fmt.Sprintf("%s/%s", r.kind, r.name)
}
return fmt.Sprintf("%s:%s/%s", r.namespace, r.kind, r.name)
}

// NewResource creates new Resource of kind
Expand Down
68 changes: 68 additions & 0 deletions pkg/diag/validator/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2020 The Skaffold 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 validator

import (
"testing"

"github.com/google/go-cmp/cmp"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestNewResource(t *testing.T) {
tests := []struct {
description string
resource objectWithMetadata
expected Resource
expectedName string
}{
{
description: "pod in default namespace",
resource: &v1.Pod{
TypeMeta: metav1.TypeMeta{Kind: "pod"},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "foo",
},
},
expected: Resource{"default", "pod", "foo", Status(""), nil, 0},
expectedName: "pod/foo",
},
{
description: "pod in another namespace",
resource: &v1.Pod{
TypeMeta: metav1.TypeMeta{Kind: "pod"},
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "bar",
},
},
expected: Resource{"test", "pod", "bar", Status(""), nil, 0},
expectedName: "test:pod/bar",
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
actual := NewResourceFromObject(test.resource, Status(""), nil, 0)
t.CheckDeepEqual(test.expected, actual, cmp.AllowUnexported(Resource{}))
t.CheckDeepEqual(test.expectedName, actual.String(), cmp.AllowUnexported(Resource{}))
})
}
}