2015-01-20 22:07:36 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-01-20 22:07:36 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-04-07 18:21:25 +00:00
|
|
|
package util
|
2015-01-20 22:07:36 +00:00
|
|
|
|
|
|
|
import (
|
2016-03-29 17:01:54 +00:00
|
|
|
"fmt"
|
2015-05-07 15:30:28 +00:00
|
|
|
"sort"
|
2015-09-10 21:58:09 +00:00
|
|
|
"strings"
|
2015-01-20 22:07:36 +00:00
|
|
|
"testing"
|
2016-03-29 17:01:54 +00:00
|
|
|
"time"
|
2015-01-20 22:07:36 +00:00
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-03-21 03:07:57 +00:00
|
|
|
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2017-08-22 02:57:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
2017-01-23 14:46:05 +00:00
|
|
|
"k8s.io/apiserver/pkg/util/flag"
|
2017-01-24 15:00:24 +00:00
|
|
|
manualfake "k8s.io/client-go/rest/fake"
|
2017-01-25 20:07:10 +00:00
|
|
|
testcore "k8s.io/client-go/testing"
|
2017-01-20 18:06:17 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2017-01-18 14:57:11 +00:00
|
|
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
2017-10-16 11:41:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
2015-09-10 21:58:09 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
2017-11-08 22:34:54 +00:00
|
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
2016-09-08 15:50:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
2016-03-29 17:01:54 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
2017-11-09 02:11:54 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/categories"
|
2016-07-14 11:48:32 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
2015-01-20 22:07:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewFactoryDefaultFlagBindings(t *testing.T) {
|
|
|
|
factory := NewFactory(nil)
|
|
|
|
|
2016-10-13 00:09:26 +00:00
|
|
|
if !factory.FlagSet().HasFlags() {
|
2015-01-20 22:07:36 +00:00
|
|
|
t.Errorf("Expected flags, but didn't get any")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFactoryNoFlagBindings(t *testing.T) {
|
|
|
|
clientConfig := clientcmd.NewDefaultClientConfig(*clientcmdapi.NewConfig(), &clientcmd.ConfigOverrides{})
|
|
|
|
factory := NewFactory(clientConfig)
|
|
|
|
|
2016-10-13 00:09:26 +00:00
|
|
|
if factory.FlagSet().HasFlags() {
|
|
|
|
t.Errorf("Expected zero flags, but got %v", factory.FlagSet())
|
2015-01-20 22:07:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-07 15:30:28 +00:00
|
|
|
|
|
|
|
func TestPortsForObject(t *testing.T) {
|
|
|
|
f := NewFactory(nil)
|
|
|
|
|
|
|
|
pod := &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
2015-05-07 15:30:28 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Ports: []api.ContainerPort{
|
|
|
|
{
|
|
|
|
ContainerPort: 101,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-08-22 02:57:48 +00:00
|
|
|
expected := sets.NewString("101")
|
|
|
|
ports, err := f.PortsForObject(pod)
|
2015-05-07 15:30:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-22 02:57:48 +00:00
|
|
|
got := sets.NewString(ports...)
|
2015-05-07 15:30:28 +00:00
|
|
|
|
2017-08-22 02:57:48 +00:00
|
|
|
if !expected.Equal(got) {
|
|
|
|
t.Fatalf("Ports mismatch! Expected %v, got %v", expected, got)
|
2015-05-07 15:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-13 08:52:25 +00:00
|
|
|
|
2016-05-12 04:07:07 +00:00
|
|
|
func TestProtocolsForObject(t *testing.T) {
|
|
|
|
f := NewFactory(nil)
|
|
|
|
|
|
|
|
pod := &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
2016-05-12 04:07:07 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Ports: []api.ContainerPort{
|
|
|
|
{
|
|
|
|
ContainerPort: 101,
|
|
|
|
Protocol: api.ProtocolTCP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ContainerPort: 102,
|
|
|
|
Protocol: api.ProtocolUDP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-08-22 02:57:48 +00:00
|
|
|
expected := sets.NewString("101/TCP", "102/UDP")
|
2016-05-12 04:07:07 +00:00
|
|
|
protocolsMap, err := f.ProtocolsForObject(pod)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-22 02:57:48 +00:00
|
|
|
protocolsString := kubectl.MakeProtocols(protocolsMap)
|
|
|
|
protocolsStrings := strings.Split(protocolsString, ",")
|
|
|
|
got := sets.NewString(protocolsStrings...)
|
2016-05-12 04:07:07 +00:00
|
|
|
|
2017-08-22 02:57:48 +00:00
|
|
|
if !expected.Equal(got) {
|
|
|
|
t.Fatalf("Protocols mismatch! Expected %v, got %v", expected, got)
|
2016-05-12 04:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 08:52:25 +00:00
|
|
|
func TestLabelsForObject(t *testing.T) {
|
|
|
|
f := NewFactory(nil)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
object runtime.Object
|
|
|
|
expected string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "successful re-use of labels",
|
|
|
|
object: &api.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", Labels: map[string]string{"svc": "test"}},
|
2016-12-03 18:57:26 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: "v1"},
|
2015-05-13 08:52:25 +00:00
|
|
|
},
|
|
|
|
expected: "svc=test",
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty labels",
|
|
|
|
object: &api.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test", Labels: map[string]string{}},
|
2016-12-03 18:57:26 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: "v1"},
|
2015-05-13 08:52:25 +00:00
|
|
|
},
|
|
|
|
expected: "",
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nil labels",
|
|
|
|
object: &api.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "zen", Namespace: "test", Labels: nil},
|
2016-12-03 18:57:26 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: "v1"},
|
2015-05-13 08:52:25 +00:00
|
|
|
},
|
|
|
|
expected: "",
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
gotLabels, err := f.LabelsForObject(test.object)
|
|
|
|
if err != test.err {
|
|
|
|
t.Fatalf("%s: Error mismatch: Expected %v, got %v", test.name, test.err, err)
|
|
|
|
}
|
|
|
|
got := kubectl.MakeLabels(gotLabels)
|
|
|
|
if test.expected != got {
|
|
|
|
t.Fatalf("%s: Labels mismatch! Expected %s, got %s", test.name, test.expected, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-05-16 16:44:42 +00:00
|
|
|
|
2015-08-26 08:06:40 +00:00
|
|
|
func TestCanBeExposed(t *testing.T) {
|
|
|
|
factory := NewFactory(nil)
|
|
|
|
tests := []struct {
|
2016-11-21 02:55:31 +00:00
|
|
|
kind schema.GroupKind
|
2015-08-26 08:06:40 +00:00
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
2015-12-01 16:52:11 +00:00
|
|
|
kind: api.Kind("ReplicationController"),
|
2015-08-26 08:06:40 +00:00
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
2015-12-01 16:52:11 +00:00
|
|
|
kind: api.Kind("Node"),
|
2015-08-26 08:06:40 +00:00
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
err := factory.CanBeExposed(test.kind)
|
|
|
|
if test.expectErr && err == nil {
|
|
|
|
t.Error("unexpected non-error")
|
|
|
|
}
|
|
|
|
if !test.expectErr && err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 16:44:42 +00:00
|
|
|
func TestFlagUnderscoreRenaming(t *testing.T) {
|
|
|
|
factory := NewFactory(nil)
|
|
|
|
|
2016-10-13 00:09:26 +00:00
|
|
|
factory.FlagSet().SetNormalizeFunc(flag.WordSepNormalizeFunc)
|
|
|
|
factory.FlagSet().Bool("valid_flag", false, "bool value")
|
2015-05-16 16:44:42 +00:00
|
|
|
|
|
|
|
// In case of failure of this test check this PR: spf13/pflag#23
|
2016-10-13 00:09:26 +00:00
|
|
|
if factory.FlagSet().Lookup("valid_flag").Name != "valid-flag" {
|
|
|
|
t.Fatalf("Expected flag name to be valid-flag, got %s", factory.FlagSet().Lookup("valid_flag").Name)
|
2015-05-16 16:44:42 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 21:58:09 +00:00
|
|
|
|
2016-03-29 17:01:54 +00:00
|
|
|
func newPodList(count, isUnready, isUnhealthy int, labels map[string]string) *api.PodList {
|
|
|
|
pods := []api.Pod{}
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
newPod := api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-03-29 17:01:54 +00:00
|
|
|
Name: fmt.Sprintf("pod-%d", i+1),
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, i, 0, time.UTC),
|
2016-03-29 17:01:54 +00:00
|
|
|
Labels: labels,
|
|
|
|
},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Conditions: []api.PodCondition{
|
|
|
|
{
|
|
|
|
Status: api.ConditionTrue,
|
|
|
|
Type: api.PodReady,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
pods = append(pods, newPod)
|
|
|
|
}
|
|
|
|
if isUnready > -1 && isUnready < count {
|
|
|
|
pods[isUnready].Status.Conditions[0].Status = api.ConditionFalse
|
|
|
|
}
|
|
|
|
if isUnhealthy > -1 && isUnhealthy < count {
|
|
|
|
pods[isUnhealthy].Status.ContainerStatuses = []api.ContainerStatus{{RestartCount: 5}}
|
|
|
|
}
|
|
|
|
return &api.PodList{
|
|
|
|
Items: pods,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFirstPod(t *testing.T) {
|
|
|
|
labelSet := map[string]string{"test": "selector"}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
podList *api.PodList
|
|
|
|
watching []watch.Event
|
2016-11-18 20:58:22 +00:00
|
|
|
sortBy func([]*v1.Pod) sort.Interface
|
2016-03-29 17:01:54 +00:00
|
|
|
|
|
|
|
expected *api.Pod
|
|
|
|
expectedNum int
|
|
|
|
expectedErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "kubectl logs - two ready pods",
|
|
|
|
podList: newPodList(2, -1, -1, labelSet),
|
2016-11-18 20:58:22 +00:00
|
|
|
sortBy: func(pods []*v1.Pod) sort.Interface { return controller.ByLogging(pods) },
|
2016-03-29 17:01:54 +00:00
|
|
|
expected: &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-06-03 10:12:26 +00:00
|
|
|
Name: "pod-1",
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, 0, 0, time.UTC),
|
2016-03-29 17:01:54 +00:00
|
|
|
Labels: map[string]string{"test": "selector"},
|
|
|
|
},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Conditions: []api.PodCondition{
|
|
|
|
{
|
|
|
|
Status: api.ConditionTrue,
|
|
|
|
Type: api.PodReady,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedNum: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "kubectl logs - one unhealthy, one healthy",
|
|
|
|
podList: newPodList(2, -1, 1, labelSet),
|
2016-11-18 20:58:22 +00:00
|
|
|
sortBy: func(pods []*v1.Pod) sort.Interface { return controller.ByLogging(pods) },
|
2016-03-29 17:01:54 +00:00
|
|
|
expected: &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-03-29 17:01:54 +00:00
|
|
|
Name: "pod-2",
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, 1, 0, time.UTC),
|
2016-03-29 17:01:54 +00:00
|
|
|
Labels: map[string]string{"test": "selector"},
|
|
|
|
},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Conditions: []api.PodCondition{
|
|
|
|
{
|
|
|
|
Status: api.ConditionTrue,
|
|
|
|
Type: api.PodReady,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ContainerStatuses: []api.ContainerStatus{{RestartCount: 5}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedNum: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "kubectl attach - two ready pods",
|
|
|
|
podList: newPodList(2, -1, -1, labelSet),
|
2016-11-18 20:58:22 +00:00
|
|
|
sortBy: func(pods []*v1.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) },
|
2016-03-29 17:01:54 +00:00
|
|
|
expected: &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-03-29 17:01:54 +00:00
|
|
|
Name: "pod-1",
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, 0, 0, time.UTC),
|
2016-03-29 17:01:54 +00:00
|
|
|
Labels: map[string]string{"test": "selector"},
|
|
|
|
},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Conditions: []api.PodCondition{
|
|
|
|
{
|
|
|
|
Status: api.ConditionTrue,
|
|
|
|
Type: api.PodReady,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedNum: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "kubectl attach - wait for ready pod",
|
|
|
|
podList: newPodList(1, 1, -1, labelSet),
|
|
|
|
watching: []watch.Event{
|
|
|
|
{
|
|
|
|
Type: watch.Modified,
|
|
|
|
Object: &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-03-29 17:01:54 +00:00
|
|
|
Name: "pod-1",
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, 0, 0, time.UTC),
|
2016-03-29 17:01:54 +00:00
|
|
|
Labels: map[string]string{"test": "selector"},
|
|
|
|
},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Conditions: []api.PodCondition{
|
|
|
|
{
|
|
|
|
Status: api.ConditionTrue,
|
|
|
|
Type: api.PodReady,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-11-18 20:58:22 +00:00
|
|
|
sortBy: func(pods []*v1.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) },
|
2016-03-29 17:01:54 +00:00
|
|
|
expected: &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-03-29 17:01:54 +00:00
|
|
|
Name: "pod-1",
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Date(2016, time.April, 1, 1, 0, 0, 0, time.UTC),
|
2016-03-29 17:01:54 +00:00
|
|
|
Labels: map[string]string{"test": "selector"},
|
|
|
|
},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Conditions: []api.PodCondition{
|
|
|
|
{
|
|
|
|
Status: api.ConditionTrue,
|
|
|
|
Type: api.PodReady,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedNum: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tests {
|
|
|
|
test := tests[i]
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := fake.NewSimpleClientset(test.podList)
|
2016-03-29 17:01:54 +00:00
|
|
|
if len(test.watching) > 0 {
|
|
|
|
watcher := watch.NewFake()
|
|
|
|
for _, event := range test.watching {
|
|
|
|
switch event.Type {
|
|
|
|
case watch.Added:
|
|
|
|
go watcher.Add(event.Object)
|
|
|
|
case watch.Modified:
|
|
|
|
go watcher.Modify(event.Object)
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 15:50:53 +00:00
|
|
|
fake.PrependWatchReactor("pods", testcore.DefaultWatchReactor(watcher, nil))
|
2016-03-29 17:01:54 +00:00
|
|
|
}
|
|
|
|
selector := labels.Set(labelSet).AsSelector()
|
|
|
|
|
2017-09-25 06:32:52 +00:00
|
|
|
pod, numPods, err := GetFirstPod(fake.Core(), metav1.NamespaceDefault, selector.String(), 1*time.Minute, test.sortBy)
|
2016-11-18 20:58:22 +00:00
|
|
|
pod.Spec.SecurityContext = nil
|
2016-03-29 17:01:54 +00:00
|
|
|
if !test.expectedErr && err != nil {
|
|
|
|
t.Errorf("%s: unexpected error: %v", test.name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if test.expectedErr && err == nil {
|
|
|
|
t.Errorf("%s: expected an error", test.name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if test.expectedNum != numPods {
|
|
|
|
t.Errorf("%s: expected %d pods, got %d", test.name, test.expectedNum, numPods)
|
|
|
|
continue
|
|
|
|
}
|
2017-03-21 03:07:57 +00:00
|
|
|
if !apiequality.Semantic.DeepEqual(test.expected, pod) {
|
2016-03-29 17:01:54 +00:00
|
|
|
t.Errorf("%s:\nexpected pod:\n%#v\ngot:\n%#v\n\n", test.name, test.expected, pod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 12:56:12 +00:00
|
|
|
|
|
|
|
func TestMakePortsString(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
ports []api.ServicePort
|
|
|
|
useNodePort bool
|
|
|
|
expectedOutput string
|
|
|
|
}{
|
|
|
|
{ports: nil, expectedOutput: ""},
|
|
|
|
{ports: []api.ServicePort{}, expectedOutput: ""},
|
|
|
|
{ports: []api.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedOutput: "tcp:80",
|
|
|
|
},
|
|
|
|
{ports: []api.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port: 8080,
|
|
|
|
Protocol: "UDP",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port: 9000,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedOutput: "tcp:80,udp:8080,tcp:9000",
|
|
|
|
},
|
|
|
|
{ports: []api.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
NodePort: 9090,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port: 8080,
|
|
|
|
NodePort: 80,
|
|
|
|
Protocol: "UDP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
useNodePort: true,
|
|
|
|
expectedOutput: "tcp:9090,udp:80",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
output := makePortsString(test.ports, test.useNodePort)
|
|
|
|
if output != test.expectedOutput {
|
|
|
|
t.Errorf("expected: %s, saw: %s.", test.expectedOutput, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-14 11:48:32 +00:00
|
|
|
|
|
|
|
func fakeClient() resource.ClientMapper {
|
|
|
|
return resource.ClientMapperFunc(func(*meta.RESTMapping) (resource.RESTClient, error) {
|
|
|
|
return &manualfake.RESTClient{}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiscoveryReplaceAliases(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
arg string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no-replacement",
|
|
|
|
arg: "service",
|
|
|
|
expected: "service",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all-replacement",
|
|
|
|
arg: "all",
|
2017-07-26 07:41:47 +00:00
|
|
|
expected: "pods,replicationcontrollers,services,statefulsets.apps,horizontalpodautoscalers.autoscaling,jobs.batch,cronjobs.batch,daemonsets.extensions,deployments.extensions,replicasets.extensions",
|
2016-07-14 11:48:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alias-in-comma-separated-arg",
|
|
|
|
arg: "all,secrets",
|
2017-07-26 07:41:47 +00:00
|
|
|
expected: "pods,replicationcontrollers,services,statefulsets.apps,horizontalpodautoscalers.autoscaling,jobs.batch,cronjobs.batch,daemonsets.extensions,deployments.extensions,replicasets.extensions,secrets",
|
2016-07-14 11:48:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-13 17:36:12 +00:00
|
|
|
ds := &fakeDiscoveryClient{}
|
2017-11-15 03:16:10 +00:00
|
|
|
mapper := NewShortcutExpander(testapi.Default.RESTMapper(), ds)
|
2017-11-14 03:56:59 +00:00
|
|
|
b := resource.NewBuilder(
|
|
|
|
&resource.Mapper{
|
|
|
|
RESTMapper: mapper,
|
|
|
|
ObjectTyper: legacyscheme.Scheme,
|
|
|
|
ClientMapper: fakeClient(),
|
|
|
|
Decoder: testapi.Default.Codec(),
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
categories.LegacyCategoryExpander,
|
|
|
|
)
|
2016-07-14 11:48:32 +00:00
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
replaced := b.ReplaceAliases(test.arg)
|
|
|
|
if replaced != test.expected {
|
|
|
|
t.Errorf("%s: unexpected argument: expected %s, got %s", test.name, test.expected, replaced)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|