ingress-nginx-helm/internal/k8s/main_test.go

243 lines
5.6 KiB
Go
Raw Normal View History

/*
Copyright 2015 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 k8s
import (
2017-04-01 14:39:42 +00:00
"os"
"testing"
2017-09-17 18:42:31 +00:00
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2017-04-01 14:39:42 +00:00
testclient "k8s.io/client-go/kubernetes/fake"
)
func TestParseNameNS(t *testing.T) {
tests := []struct {
title string
input string
ns string
name string
expErr bool
}{
{"empty string", "", "", "", true},
{"demo", "demo", "", "", true},
{"kube-system", "kube-system", "", "", true},
{"default/kube-system", "default/kube-system", "default", "kube-system", false},
}
for _, test := range tests {
ns, name, err := ParseNameNS(test.input)
if test.expErr {
if err == nil {
2017-01-11 13:44:06 +00:00
t.Errorf("%v: expected error but returned nil", test.title)
}
continue
}
if test.ns != ns {
2017-01-11 13:44:06 +00:00
t.Errorf("%v: expected %v but returned %v", test.title, test.ns, ns)
}
if test.name != name {
2017-01-11 13:44:06 +00:00
t.Errorf("%v: expected %v but returned %v", test.title, test.name, name)
}
}
}
2017-01-11 13:44:06 +00:00
func TestGetNodeIP(t *testing.T) {
fKNodes := []struct {
cs *testclient.Clientset
n string
ea string
i bool
2017-01-11 13:44:06 +00:00
}{
// empty node list
{testclient.NewSimpleClientset(), "demo", "", true},
2017-01-11 13:44:06 +00:00
// node not exist
2017-09-17 18:42:31 +00:00
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
ObjectMeta: metav1.ObjectMeta{
2017-01-11 13:44:06 +00:00
Name: "demo",
},
2017-09-17 18:42:31 +00:00
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
2017-01-11 13:44:06 +00:00
{
2017-09-17 18:42:31 +00:00
Type: apiv1.NodeInternalIP,
2017-01-11 13:44:06 +00:00
Address: "10.0.0.1",
},
},
},
}}}), "notexistnode", "", true},
2017-01-11 13:44:06 +00:00
// node exist
2017-09-17 18:42:31 +00:00
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
ObjectMeta: metav1.ObjectMeta{
2017-01-11 13:44:06 +00:00
Name: "demo",
},
2017-09-17 18:42:31 +00:00
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
2017-01-11 13:44:06 +00:00
{
2017-09-17 18:42:31 +00:00
Type: apiv1.NodeInternalIP,
2017-01-11 13:44:06 +00:00
Address: "10.0.0.1",
},
},
},
}}}), "demo", "10.0.0.1", true},
2017-01-11 13:44:06 +00:00
// search the correct node
2017-09-17 18:42:31 +00:00
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{
2017-01-11 13:44:06 +00:00
{
2017-09-17 18:42:31 +00:00
ObjectMeta: metav1.ObjectMeta{
2017-01-11 13:44:06 +00:00
Name: "demo1",
},
2017-09-17 18:42:31 +00:00
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
2017-01-11 13:44:06 +00:00
{
2017-09-17 18:42:31 +00:00
Type: apiv1.NodeInternalIP,
2017-01-11 13:44:06 +00:00
Address: "10.0.0.1",
},
},
},
},
{
2017-09-17 18:42:31 +00:00
ObjectMeta: metav1.ObjectMeta{
2017-01-11 13:44:06 +00:00
Name: "demo2",
},
2017-09-17 18:42:31 +00:00
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
2017-01-11 13:44:06 +00:00
{
2017-09-17 18:42:31 +00:00
Type: apiv1.NodeInternalIP,
2017-01-11 13:44:06 +00:00
Address: "10.0.0.2",
},
},
},
},
}}), "demo2", "10.0.0.2", true},
2017-01-11 13:44:06 +00:00
// get NodeExternalIP
2017-09-17 18:42:31 +00:00
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
ObjectMeta: metav1.ObjectMeta{
2017-01-11 13:44:06 +00:00
Name: "demo",
},
2017-09-17 18:42:31 +00:00
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
2017-01-11 13:44:06 +00:00
{
2017-09-17 18:42:31 +00:00
Type: apiv1.NodeInternalIP,
2017-01-11 13:44:06 +00:00
Address: "10.0.0.1",
}, {
2017-09-17 18:42:31 +00:00
Type: apiv1.NodeExternalIP,
2017-01-11 13:44:06 +00:00
Address: "10.0.0.2",
},
},
},
}}}), "demo", "10.0.0.2", false},
2017-01-11 13:44:06 +00:00
// get NodeInternalIP
2017-09-17 18:42:31 +00:00
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
ObjectMeta: metav1.ObjectMeta{
2017-01-11 13:44:06 +00:00
Name: "demo",
},
2017-09-17 18:42:31 +00:00
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
2017-01-11 13:44:06 +00:00
{
2017-09-17 18:42:31 +00:00
Type: apiv1.NodeExternalIP,
2017-01-11 13:44:06 +00:00
Address: "",
}, {
2017-09-17 18:42:31 +00:00
Type: apiv1.NodeInternalIP,
2017-01-11 13:44:06 +00:00
Address: "10.0.0.2",
},
},
},
}}}), "demo", "10.0.0.2", true},
2017-01-11 13:44:06 +00:00
}
for _, fk := range fKNodes {
2017-11-06 01:22:49 +00:00
address := GetNodeIPOrName(fk.cs, fk.n, fk.i)
2017-01-11 13:44:06 +00:00
if address != fk.ea {
t.Errorf("expected %s, but returned %s", fk.ea, address)
}
}
}
func TestGetPodDetails(t *testing.T) {
// POD_NAME & POD_NAMESPACE not exist
os.Setenv("POD_NAME", "")
os.Setenv("POD_NAMESPACE", "")
_, err1 := GetPodDetails(testclient.NewSimpleClientset())
if err1 == nil {
t.Errorf("expected an error but returned nil")
}
// POD_NAME not exist
os.Setenv("POD_NAME", "")
2017-09-17 18:42:31 +00:00
os.Setenv("POD_NAMESPACE", apiv1.NamespaceDefault)
2017-01-11 13:44:06 +00:00
_, err2 := GetPodDetails(testclient.NewSimpleClientset())
if err2 == nil {
t.Errorf("expected an error but returned nil")
}
// POD_NAMESPACE not exist
os.Setenv("POD_NAME", "testpod")
os.Setenv("POD_NAMESPACE", "")
_, err3 := GetPodDetails(testclient.NewSimpleClientset())
if err3 == nil {
t.Errorf("expected an error but returned nil")
}
// POD not exist
os.Setenv("POD_NAME", "testpod")
2017-09-17 18:42:31 +00:00
os.Setenv("POD_NAMESPACE", apiv1.NamespaceDefault)
_, err4 := GetPodDetails(testclient.NewSimpleClientset())
if err4 == nil {
t.Errorf("expected an error but returned nil")
}
2017-01-11 13:44:06 +00:00
// success to get PodInfo
fkClient := testclient.NewSimpleClientset(
2017-09-17 18:42:31 +00:00
&apiv1.PodList{Items: []apiv1.Pod{{
ObjectMeta: metav1.ObjectMeta{
2017-01-11 13:44:06 +00:00
Name: "testpod",
2017-09-17 18:42:31 +00:00
Namespace: apiv1.NamespaceDefault,
2017-01-11 13:44:06 +00:00
Labels: map[string]string{
"first": "first_label",
"second": "second_label",
},
},
}}},
2017-09-17 18:42:31 +00:00
&apiv1.NodeList{Items: []apiv1.Node{{
ObjectMeta: metav1.ObjectMeta{
2017-01-11 13:44:06 +00:00
Name: "demo",
},
2017-09-17 18:42:31 +00:00
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
2017-01-11 13:44:06 +00:00
{
2017-09-17 18:42:31 +00:00
Type: apiv1.NodeInternalIP,
2017-01-11 13:44:06 +00:00
Address: "10.0.0.1",
},
},
},
}}})
epi, err5 := GetPodDetails(fkClient)
if err5 != nil {
2017-01-11 13:44:06 +00:00
t.Errorf("expected a PodInfo but returned error")
return
}
if epi == nil {
t.Errorf("expected a PodInfo but returned nil")
}
}