Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtuna committed Nov 28, 2017
1 parent 9d3f702 commit 0f693c8
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var downCmd = &cobra.Command{
return fmt.Errorf("can't remove kubeapps components: %v", err)
}

fmt.Printf("\nKubeapps has been removed successfully\n\n")
fmt.Printf("\nKubeapps has been removed successfully.\n\n")
return nil
},
}
Expand Down
39 changes: 20 additions & 19 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"fmt"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -138,7 +139,7 @@ List of components that kubeapps up installs:
}
clientset, err := kubernetes.NewForConfig(config)

err = printOutput(clientset)
err = printOutput(cmd.OutOrStdout(), clientset)
if err != nil {
return err
}
Expand All @@ -164,34 +165,34 @@ func isGKE(disco discovery.DiscoveryInterface) (bool, error) {
return false, nil
}

func printOutput(c *kubernetes.Clientset) error {
func printOutput(w io.Writer, c *kubernetes.Clientset) error {
fmt.Printf("\nKubeapps has been deployed successfully. \n" +
"It may takes few minutes for all components to be ready. \n\n")
nss := []string{KubeappsNS, KubelessNS, SystemNS}
err := printSvc(c, nss)
err := printSvc(w, c, nss)
if err != nil {
return err
}
err = printDeployment(c, nss)
err = printDeployment(w, c, nss)
if err != nil {
return err
}
err = printStS(c, nss)
err = printStS(w, c, nss)
if err != nil {
return err
}

err = printPod(c, nss)
err = printPod(w, c, nss)
if err != nil {
return err
}

fmt.Printf("Checking `kubectl get all --all-namespaces -l created-by=kubeapps` for details \n\n")
fmt.Printf("Checking `kubectl get all --all-namespaces -l created-by=kubeapps` for details. \n\n")

return nil
}

func printPod(c *kubernetes.Clientset, nss []string) error {
func printPod(w io.Writer, c kubernetes.Interface, nss []string) error {
table := uitable.New()
table.MaxColWidth = 50
table.Wrap = true
Expand All @@ -209,12 +210,12 @@ func printPod(c *kubernetes.Clientset, nss []string) error {
for _, p := range pods {
table.AddRow(p.Namespace, fmt.Sprintf("pod/%s", p.Name), p.Status.Phase)
}
fmt.Println(table)
fmt.Println()
fmt.Fprintln(w, table)
fmt.Fprintln(w)
return nil
}

func printStS(c *kubernetes.Clientset, nss []string) error {
func printStS(w io.Writer, c kubernetes.Interface, nss []string) error {
table := uitable.New()
table.MaxColWidth = 50
table.Wrap = true
Expand All @@ -232,12 +233,12 @@ func printStS(c *kubernetes.Clientset, nss []string) error {
for _, s := range sts {
table.AddRow(s.Namespace, fmt.Sprintf("statefulsets/%s", s.Name), *s.Spec.Replicas, s.Status.Replicas)
}
fmt.Println(table)
fmt.Println()
fmt.Fprintln(w, table)
fmt.Fprintln(w)
return nil
}

func printDeployment(c *kubernetes.Clientset, nss []string) error {
func printDeployment(w io.Writer, c kubernetes.Interface, nss []string) error {
table := uitable.New()
table.MaxColWidth = 50
table.Wrap = true
Expand All @@ -256,12 +257,12 @@ func printDeployment(c *kubernetes.Clientset, nss []string) error {
for _, d := range deps {
table.AddRow(d.Namespace, fmt.Sprintf("deploy/%s", d.Name), *d.Spec.Replicas, d.Status.Replicas, d.Status.UpdatedReplicas, d.Status.AvailableReplicas)
}
fmt.Println(table)
fmt.Println()
fmt.Fprintln(w, table)
fmt.Fprintln(w)
return nil
}

func printSvc(c *kubernetes.Clientset, nss []string) error {
func printSvc(w io.Writer, c kubernetes.Interface, nss []string) error {
table := uitable.New()
table.MaxColWidth = 50
table.Wrap = true
Expand Down Expand Up @@ -292,7 +293,7 @@ func printSvc(c *kubernetes.Clientset, nss []string) error {
}
table.AddRow(s.Namespace, fmt.Sprintf("svc/%s", s.Name), s.Spec.ClusterIP, eIPs, ports)
}
fmt.Println(table)
fmt.Println()
fmt.Fprintln(w, table)
fmt.Fprintln(w)
return nil
}
107 changes: 107 additions & 0 deletions cmd/up_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package cmd

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/apis/apps/v1beta1"
restclient "k8s.io/client-go/rest"
)

Expand Down Expand Up @@ -61,3 +67,104 @@ func TestIsGKE(t *testing.T) {
t.Errorf("expect non-GKE but got GKE")
}
}

func TestPrintOutput(t *testing.T) {
om1 := metav1.ObjectMeta{
Name: "foo",
Namespace: "myns",
Labels: map[string]string{
"created-by": "kubeapps",
},
}
om2 := metav1.ObjectMeta{
Name: "bar",
Namespace: "myns",
}

po1 := &v1.Pod{
ObjectMeta: om1,
}
po2 := &v1.Pod{
ObjectMeta: om2,
}

svc1 := &v1.Service{
ObjectMeta: om1,
}
svc2 := &v1.Service{
ObjectMeta: om2,
}

replicas := int32(1)
sts1 := &v1beta1.StatefulSet{
ObjectMeta: om1,
Spec: v1beta1.StatefulSetSpec{
Replicas: &replicas,
},
}
sts2 := &v1beta1.StatefulSet{
ObjectMeta: om2,
}

dep1 := &v1beta1.Deployment{
ObjectMeta: om1,
Spec: v1beta1.DeploymentSpec{
Replicas: &replicas,
},
}
dep2 := &v1beta1.Deployment{
ObjectMeta: om2,
}

client := fake.NewSimpleClientset(po1, po2, svc1, svc2, sts1, sts2, dep1, dep2)
ns := []string{"myns"}
var buf bytes.Buffer

err := printPod(&buf, client, ns)
if err != nil {
t.Error(err)
}
output := buf.String()
if !strings.Contains(output, "foo") {
t.Errorf("pod %s isn't listed", po1.Name)
}
if strings.Contains(output, "bar") {
t.Errorf("pod %s shouldn't be listed", po2.Name)
}

err = printSvc(&buf, client, ns)
if err != nil {
t.Error(err)
}
output = buf.String()
if !strings.Contains(output, "foo") {
t.Errorf("service %s isn't listed", po1.Name)
}
if strings.Contains(output, "bar") {
t.Errorf("service %s shouldn't be listed", po2.Name)
}

err = printDeployment(&buf, client, ns)
if err != nil {
t.Error(err)
}
output = buf.String()
if !strings.Contains(output, "foo") {
t.Errorf("deployment %s isn't listed", po1.Name)
}
if strings.Contains(output, "bar") {
t.Errorf("deployment %s shouldn't be listed", po2.Name)
}

err = printStS(&buf, client, ns)
if err != nil {
t.Error(err)
}
output = buf.String()
if !strings.Contains(output, "foo") {
t.Errorf("statefulset %s isn't listed", po1.Name)
}
if strings.Contains(output, "bar") {
t.Errorf("statefulset %s shouldn't be listed", po2.Name)
}
}

0 comments on commit 0f693c8

Please sign in to comment.