Skip to content

Commit

Permalink
Merge pull request #48831 from enisoc/resource-filter-test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kubernetes Submit Queue authored Jul 12, 2017
2 parents 4da48f1 + bbe3ac9 commit b31d1db
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/kubectl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
76 changes: 76 additions & 0 deletions pkg/kubectl/resource_filter_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit b31d1db

Please sign in to comment.