Skip to content

add unit test cases for core.pkg.k8s #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 178 additions & 10 deletions core/pkg/k8s/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (

"k8s.io/kubernetes/pkg/api"
testclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
"os"
)

func TestParseNameNS(t *testing.T) {

tests := []struct {
title string
input string
Expand All @@ -42,15 +42,15 @@ func TestParseNameNS(t *testing.T) {
ns, name, err := ParseNameNS(test.input)
if test.expErr {
if err == nil {
t.Errorf("%v: expected error but retuned nil", test.title)
t.Errorf("%v: expected error but returned nil", test.title)
}
continue
}
if test.ns != ns {
t.Errorf("%v: expected %v but retuned %v", test.title, test.ns, ns)
t.Errorf("%v: expected %v but returned %v", test.title, test.ns, ns)
}
if test.name != name {
t.Errorf("%v: expected %v but retuned %v", test.title, test.name, name)
t.Errorf("%v: expected %v but returned %v", test.title, test.name, name)
}
}
}
Expand All @@ -65,20 +65,20 @@ func TestIsValidService(t *testing.T) {

_, err := IsValidService(fk, "")
if err == nil {
t.Errorf("expected error but retuned nil")
t.Errorf("expected error but returned nil")
}
s, err := IsValidService(fk, "default/demo")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if s == nil {
t.Errorf("expected a Service but retuned nil")
t.Errorf("expected a Service but returned nil")
}

fk = testclient.NewSimpleClientset()
s, err = IsValidService(fk, "default/demo")
if err == nil {
t.Errorf("expected an error but retuned nil")
t.Errorf("expected an error but returned nil")
}
if s != nil {
t.Errorf("unexpected Service returned: %v", s)
Expand All @@ -95,22 +95,190 @@ func TestIsValidSecret(t *testing.T) {

_, err := IsValidSecret(fk, "")
if err == nil {
t.Errorf("expected error but retuned nil")
t.Errorf("expected error but returned nil")
}
s, err := IsValidSecret(fk, "default/demo")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if s == nil {
t.Errorf("expected a Secret but retuned nil")
t.Errorf("expected a Secret but returned nil")
}

fk = testclient.NewSimpleClientset()
s, err = IsValidSecret(fk, "default/demo")
if err == nil {
t.Errorf("expected an error but retuned nil")
t.Errorf("expected an error but returned nil")
}
if s != nil {
t.Errorf("unexpected Secret returned: %v", s)
}
}

func TestGetNodeIP(t *testing.T) {
fKNodes := []struct {
cs *testclient.Clientset
n string
ea string
}{
// empty node list
{testclient.NewSimpleClientset(), "demo", ""},

// node not exist
{testclient.NewSimpleClientset(&api.NodeList{Items: []api.Node{{
ObjectMeta: api.ObjectMeta{
Name: "demo",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
{
Type: api.NodeLegacyHostIP,
Address: "10.0.0.1",
},
},
},
}}}), "notexistnode", ""},

// node exist
{testclient.NewSimpleClientset(&api.NodeList{Items: []api.Node{{
ObjectMeta: api.ObjectMeta{
Name: "demo",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
{
Type: api.NodeLegacyHostIP,
Address: "10.0.0.1",
},
},
},
}}}), "demo", "10.0.0.1"},

// search the correct node
{testclient.NewSimpleClientset(&api.NodeList{Items: []api.Node{
{
ObjectMeta: api.ObjectMeta{
Name: "demo1",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
{
Type: api.NodeLegacyHostIP,
Address: "10.0.0.1",
},
},
},
},
{
ObjectMeta: api.ObjectMeta{
Name: "demo2",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
{
Type: api.NodeLegacyHostIP,
Address: "10.0.0.2",
},
},
},
},
}}), "demo2", "10.0.0.2"},

// get NodeExternalIP
{testclient.NewSimpleClientset(&api.NodeList{Items: []api.Node{{
ObjectMeta: api.ObjectMeta{
Name: "demo",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
{
Type: api.NodeLegacyHostIP,
Address: "10.0.0.1",
}, {
Type: api.NodeExternalIP,
Address: "10.0.0.2",
},
},
},
}}}), "demo", "10.0.0.2"},

// get NodeLegacyHostIP
{testclient.NewSimpleClientset(&api.NodeList{Items: []api.Node{{
ObjectMeta: api.ObjectMeta{
Name: "demo",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
{
Type: api.NodeExternalIP,
Address: "",
}, {
Type: api.NodeLegacyHostIP,
Address: "10.0.0.2",
},
},
},
}}}), "demo", "10.0.0.2"},
}

for _, fk := range fKNodes {
address := GetNodeIP(fk.cs, fk.n)
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 not exist
os.Setenv("POD_NAME", "testpod")
os.Setenv("POD_NAMESPACE", api.NamespaceDefault)
_, err2 := GetPodDetails(testclient.NewSimpleClientset())
if err2 == nil {
t.Errorf("expected an error but returned nil")
}

// success to get PodInfo
fkClient := testclient.NewSimpleClientset(
&api.PodList{Items: []api.Pod{{
ObjectMeta: api.ObjectMeta{
Name: "testpod",
Namespace: api.NamespaceDefault,
Labels: map[string]string{
"first": "first_label",
"second": "second_label",
},
},
}}},
&api.NodeList{Items: []api.Node{{
ObjectMeta: api.ObjectMeta{
Name: "demo",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
{
Type: api.NodeLegacyHostIP,
Address: "10.0.0.1",
},
},
},
}}})

epi, err3 := GetPodDetails(fkClient)
if err3 != nil {
t.Errorf("expected a PodInfo but returned error")
return
}

if epi == nil {
t.Errorf("expected a PodInfo but returned nil")
}
}