Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtuna committed Dec 19, 2017
1 parent 5b53b81 commit 989be0f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ List of components that kubeapps up installs:
}

if c.DryRun {
return dump(out, objs)
return dump(cmd.OutOrStdout(), out, objs)
}

err = c.Run(objs)
Expand Down Expand Up @@ -186,7 +186,7 @@ func init() {
upCmd.Flags().StringP("out", "o", "yaml", "Provides manifest format: yaml | json")
}

func dump(out string, objs []*unstructured.Unstructured) error {
func dump(w io.Writer, out string, objs []*unstructured.Unstructured) error {
bObjs := [][]byte{}

switch out {
Expand All @@ -208,7 +208,7 @@ func dump(out string, objs []*unstructured.Unstructured) error {
// append ']'
b = append(b, ']')

fmt.Println(string(b[:]))
fmt.Fprintln(w, string(b[:]))

case "yaml":
for _, obj := range objs {
Expand All @@ -224,7 +224,7 @@ func dump(out string, objs []*unstructured.Unstructured) error {
}

b := bytes.Join(bObjs, []byte(fmt.Sprintf("---\n")))
fmt.Println(string(b[:]))
fmt.Fprintln(w, string(b[:]))
}

return nil
Expand Down
26 changes: 26 additions & 0 deletions cmd/up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes/fake"
Expand Down Expand Up @@ -194,3 +195,28 @@ func TestBuildSecret(t *testing.T) {
t.Errorf("wrong data")
}
}

func TestDump(t *testing.T) {
objs := []*unstructured.Unstructured{}
obj := &unstructured.Unstructured{
Object: map[string]interface{}{
"foo": "bar",
"baz": map[string]string{
"1": "2",
},
},
}
objs = append(objs, obj)
var buf bytes.Buffer

err := dump(&buf, "yaml", objs)
if err != nil {
t.Error(err)
}
output := buf.String()
t.Log("output is", output)
if !strings.Contains(output, "foo") || !strings.Contains(output, "baz") {
t.Errorf("manifest output didn't mention both keys")
}

}

0 comments on commit 989be0f

Please sign in to comment.