mirror of https://github.com/k3s-io/k3s
123 lines
2.7 KiB
Go
123 lines
2.7 KiB
Go
/*
|
|
Copyright 2014 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 util
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
|
)
|
|
|
|
func TestProtocolsForObject(t *testing.T) {
|
|
f := NewFactory(genericclioptions.NewTestConfigFlags())
|
|
|
|
pod := &api.Pod{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
|
Spec: api.PodSpec{
|
|
Containers: []api.Container{
|
|
{
|
|
Ports: []api.ContainerPort{
|
|
{
|
|
ContainerPort: 101,
|
|
Protocol: api.ProtocolTCP,
|
|
},
|
|
{
|
|
ContainerPort: 102,
|
|
Protocol: api.ProtocolUDP,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
expected := sets.NewString("101/TCP", "102/UDP")
|
|
protocolsMap, err := f.ProtocolsForObject(pod)
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
}
|
|
|
|
protocolsString := kubectl.MakeProtocols(protocolsMap)
|
|
protocolsStrings := strings.Split(protocolsString, ",")
|
|
got := sets.NewString(protocolsStrings...)
|
|
|
|
if !expected.Equal(got) {
|
|
t.Fatalf("Protocols mismatch! Expected %v, got %v", expected, got)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|