Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
add unit tests for ingress create, delete, list
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtuna committed Jul 31, 2017
1 parent 66c34f6 commit 3098164
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 8 deletions.
4 changes: 3 additions & 1 deletion cmd/kubeless/ingressCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ var ingressCreateCmd = &cobra.Command{
logrus.Fatal(err)
}

err = utils.CreateIngress(ingressName, function, domain, ns)
client := utils.GetClientOutOfCluster()

err = utils.CreateIngress(client, ingressName, function, domain, ns)
if err != nil {
logrus.Fatal(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ var ingressDeleteCmd = &cobra.Command{
logrus.Fatal(err)
}

err = utils.DeleteIngress(ingName, ns)
client := utils.GetClientOutOfCluster()

err = utils.DeleteIngress(client, ingName, ns)
if err != nil {
logrus.Fatal(err)
}
Expand Down
File renamed without changes.
97 changes: 97 additions & 0 deletions cmd/kubeless/ingressList_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"bytes"
"fmt"
"strings"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
xv1beta1 "k8s.io/client-go/pkg/apis/extensions/v1beta1"
)

func listIngressOutput(t *testing.T, client kubernetes.Interface, ns, output string) string {
var buf bytes.Buffer

if err := doIngressList(&buf, client, ns, output); err != nil {
t.Fatalf("doList returned error: %v", err)
}

return buf.String()
}

func TestIngressList(t *testing.T) {
ing1 := xv1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "myns",
},
Spec: xv1beta1.IngressSpec{
Rules: []xv1beta1.IngressRule{
{
Host: fmt.Sprintf("%s.%s", "foo", "example.com"),
IngressRuleValue: xv1beta1.IngressRuleValue{
HTTP: &xv1beta1.HTTPIngressRuleValue{
Paths: []xv1beta1.HTTPIngressPath{
{
Path: "/",
Backend: xv1beta1.IngressBackend{
ServiceName: "foobar",
ServicePort: intstr.FromInt(8080),
},
},
},
},
},
},
},
},
}

ing2 := xv1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "bar",
Namespace: "myns",
},
Spec: xv1beta1.IngressSpec{
Rules: []xv1beta1.IngressRule{
{
Host: fmt.Sprintf("%s.%s", "bar", "example.com"),
IngressRuleValue: xv1beta1.IngressRuleValue{
HTTP: &xv1beta1.HTTPIngressRuleValue{
Paths: []xv1beta1.HTTPIngressPath{
{
Path: "/",
Backend: xv1beta1.IngressBackend{
ServiceName: "barfoo",
ServicePort: intstr.FromInt(8080),
},
},
},
},
},
},
},
},
}

client := fake.NewSimpleClientset(&ing1, &ing2)

output := listIngressOutput(t, client, "myns", "")
t.Log("output is", output)

if !strings.Contains(output, "foo") || !strings.Contains(output, "bar") {
t.Errorf("table output didn't mention both functions")
}

// json output
output = listIngressOutput(t, client, "myns", "json")
t.Log("output is", output)

// yaml output
output = listIngressOutput(t, client, "myns", "yaml")
t.Log("output is", output)
}
9 changes: 3 additions & 6 deletions pkg/utils/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ func addInitContainerAnnotation(dpm *v1beta1.Deployment) error {
}

// CreateIngress creates ingress rule for a specific function
func CreateIngress(ingressName, function, domain, ns string) error {
func CreateIngress(client kubernetes.Interface, ingressName, function, domain, ns string) error {
if domain == "" {
var err error
domain, err = getLocalDomain()
Expand All @@ -822,8 +822,6 @@ func CreateIngress(ingressName, function, domain, ns string) error {
}
}

client := GetClientOutOfCluster()

//TODO: skip annotation. We can add it later
//ingressAnnotations := map[string]string{
// "kubernetes.io/ingress.class": "nginx",
Expand Down Expand Up @@ -882,10 +880,9 @@ func getLocalDomain() (string, error) {
}

// DeleteIngress deletes an ingress rule
func DeleteIngress(name, ns string) error {
client := GetClientOutOfCluster()
func DeleteIngress(client kubernetes.Interface, name, ns string) error {
err := client.ExtensionsV1beta1().Ingresses(ns).Delete(name, &metav1.DeleteOptions{})
if err != nil {
if err != nil && !k8sErrors.IsNotFound(err) {
return err
}
return nil
Expand Down
36 changes: 36 additions & 0 deletions pkg/utils/k8sutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"testing"

k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/pkg/api/v1"
Expand Down Expand Up @@ -90,6 +91,7 @@ func check(runtime, ftype, fname string, values []string, t *testing.T) {
t.Fatalf("Retrieving the image returned a wrong file name. Received " + fileName + " while expecting " + values[1])
}
}

func TestGetFunctionData(t *testing.T) {

expectedValues := []string{"requirements.txt", "test.py"}
Expand Down Expand Up @@ -131,5 +133,39 @@ func TestGetFunctionData(t *testing.T) {
t.Fatalf("Expecting " + imageR + " to be set to " + expectedImageName)
}
os.Unsetenv("RUBY_PUBSUB_RUNTIME")
}

func TestCreateIngressResource(t *testing.T) {
clientset := fake.NewSimpleClientset()
if err := CreateIngress(clientset, "foo", "bar", "foo.bar", "myns"); err != nil {
t.Fatalf("Creating ingress returned err: %v", err)
}
if err := CreateIngress(clientset, "foo", "bar", "foo.bar", "myns"); err != nil {
if !k8sErrors.IsAlreadyExists(err) {
t.Fatalf("Expect object is already exists, got %v", err)
}
}
}

func TestDeleteIngressResource(t *testing.T) {
myNsFoo := metav1.ObjectMeta{
Namespace: "myns",
Name: "foo",
}

ing := xv1beta1.Ingress{
ObjectMeta: myNsFoo,
}

clientset := fake.NewSimpleClientset(&ing)
if err := DeleteIngress(clientset, "foo", "myns"); err != nil {
t.Fatalf("Deleting ingress returned err: %v", err)
}
a := clientset.Actions()
if ns := a[0].GetNamespace(); ns != "myns" {
t.Errorf("deleted ingress from wrong namespace (%s)", ns)
}
if name := a[0].(ktesting.DeleteAction).GetName(); name != "foo" {
t.Errorf("deleted ingress with wrong name (%s)", name)
}
}

0 comments on commit 3098164

Please sign in to comment.