mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-06 07:57:35 +00:00
Load input object for increased safety
This commit is contained in:
@@ -17,8 +17,10 @@ limitations under the License.
|
||||
package util
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
|
||||
clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
|
||||
)
|
||||
@@ -39,3 +41,62 @@ func TestNewFactoryNoFlagBindings(t *testing.T) {
|
||||
t.Errorf("Expected zero flags, but got %v", factory.flags)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPodSelectorForObject(t *testing.T) {
|
||||
f := NewFactory(nil)
|
||||
|
||||
svc := &api.Service{
|
||||
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test"},
|
||||
Spec: api.ServiceSpec{
|
||||
Selector: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
expected := "foo=bar"
|
||||
got, err := f.PodSelectorForObject(svc)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
if expected != got {
|
||||
t.Fatalf("Selector mismatch! Expected %s, got %s", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPortsForObject(t *testing.T) {
|
||||
f := NewFactory(nil)
|
||||
|
||||
pod := &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Ports: []api.ContainerPort{
|
||||
{
|
||||
ContainerPort: 101,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
expected := []string{"101"}
|
||||
got, err := f.PortsForObject(pod)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
if len(expected) != len(got) {
|
||||
t.Fatalf("Ports size mismatch! Expected %d, got %d", len(expected), len(got))
|
||||
}
|
||||
|
||||
sort.Strings(expected)
|
||||
sort.Strings(got)
|
||||
|
||||
for i, port := range got {
|
||||
if port != expected[i] {
|
||||
t.Fatalf("Port mismatch! Expected %s, got %s", expected[i], port)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user