Merge pull request #48831 from enisoc/resource-filter-test

Automatic merge from submit-queue (batch tested with PRs 46738, 48827, 48831)

Add test for kubectl resource filter.

This should prevent regression of the bug fixed in #48786.
This commit is contained in:
Kubernetes Submit Queue 2017-07-12 16:00:12 -07:00 committed by GitHub
commit b31d1db4f4
2 changed files with 78 additions and 0 deletions

View File

@ -20,6 +20,7 @@ go_test(
"namespace_test.go",
"proxy_server_test.go",
"quota_test.go",
"resource_filter_test.go",
"rolebinding_test.go",
"rolling_updater_test.go",
"rollout_status_test.go",
@ -51,6 +52,7 @@ go_test(
"//pkg/client/clientset_generated/internalclientset/typed/core/internalversion:go_default_library",
"//pkg/client/clientset_generated/internalclientset/typed/extensions/internalversion:go_default_library",
"//pkg/kubectl/util:go_default_library",
"//pkg/printers:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/k8s.io/api/apps/v1beta1:go_default_library",
"//vendor/k8s.io/api/batch/v1:go_default_library",

View File

@ -0,0 +1,76 @@
/*
Copyright 2017 The Kubernetes 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 kubectl
import (
"testing"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/printers"
)
func TestResourceFilter(t *testing.T) {
tests := []struct {
name string
hide bool
object runtime.Object
}{
{"v1.Pod pending", false, &v1.Pod{Status: v1.PodStatus{Phase: v1.PodPending}}},
{"v1.Pod running", false, &v1.Pod{Status: v1.PodStatus{Phase: v1.PodRunning}}},
{"v1.Pod succeeded", true, &v1.Pod{Status: v1.PodStatus{Phase: v1.PodSucceeded}}},
{"v1.Pod failed", true, &v1.Pod{Status: v1.PodStatus{Phase: v1.PodFailed}}},
{"v1.Pod evicted", true, &v1.Pod{Status: v1.PodStatus{Phase: v1.PodFailed, Reason: "Evicted"}}},
{"v1.Pod unknown", false, &v1.Pod{Status: v1.PodStatus{Phase: v1.PodUnknown}}},
{"api.Pod pending", false, &api.Pod{Status: api.PodStatus{Phase: api.PodPending}}},
{"api.Pod running", false, &api.Pod{Status: api.PodStatus{Phase: api.PodRunning}}},
{"api.Pod succeeded", true, &api.Pod{Status: api.PodStatus{Phase: api.PodSucceeded}}},
{"api.Pod failed", true, &api.Pod{Status: api.PodStatus{Phase: api.PodFailed}}},
{"api.Pod evicted", true, &api.Pod{Status: api.PodStatus{Phase: api.PodFailed, Reason: "Evicted"}}},
{"api.Pod unknown", false, &api.Pod{Status: api.PodStatus{Phase: api.PodUnknown}}},
}
filters := NewResourceFilter()
options := &printers.PrintOptions{
ShowAll: false,
}
for _, test := range tests {
got, err := filters.Filter(test.object, options)
if err != nil {
t.Errorf("%v: unexpected error: %v", test.name, err)
continue
}
if want := test.hide; got != want {
t.Errorf("%v: got %v, want %v", test.name, got, want)
}
}
options.ShowAll = true
for _, test := range tests {
got, err := filters.Filter(test.object, options)
if err != nil {
t.Errorf("%v: unexpected error: %v", test.name, err)
continue
}
if want := false; got != want {
t.Errorf("%v (ShowAll): got %v, want %v", test.name, got, want)
}
}
}