Commit 92031722 authored by Adam Reese's avatar Adam Reese

test(cli): refactor cli test setup

parent caa14b19
...@@ -24,10 +24,10 @@ import ( ...@@ -24,10 +24,10 @@ import (
"github.com/kubernetes/helm/pkg/common" "github.com/kubernetes/helm/pkg/common"
) )
func TestShowDeployment(t *testing.T) { func TestDeployment(t *testing.T) {
var deploymentTestCases = []struct { var deploymentTestCases = []struct {
args []string args []string
resp *common.Deployment resp interface{}
expected string expected string
}{ }{
{ {
...@@ -48,43 +48,26 @@ func TestShowDeployment(t *testing.T) { ...@@ -48,43 +48,26 @@ func TestShowDeployment(t *testing.T) {
}, },
"Name: guestbook.yaml\nStatus: Failed\nErrors:\n error message\n", "Name: guestbook.yaml\nStatus: Failed\nErrors:\n error message\n",
}, },
{
[]string{"deployment", "list"},
[]string{"guestbook.yaml"},
"guestbook.yaml\n",
},
} }
for _, tc := range deploymentTestCases { for _, tc := range deploymentTestCases {
th := setup() th := testHelm(t)
th.mux.HandleFunc("/deployments/", func(w http.ResponseWriter, r *http.Request) { th.mux.HandleFunc("/deployments/", func(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(tc.resp) data, err := json.Marshal(tc.resp)
if err != nil { th.must(err)
t.Fatal(err)
}
w.Write(data) w.Write(data)
}) })
actual := CaptureOutput(func() { th.run(tc.args...)
th.Run(tc.args...)
})
if tc.expected != actual {
t.Errorf("Expected %v got %v", tc.expected, actual)
}
th.teardown()
}
}
func TestListDeployment(t *testing.T) { if tc.expected != th.output {
th := setup() t.Errorf("Expected %v got %v", tc.expected, th.output)
defer th.teardown() }
th.cleanup()
th.mux.HandleFunc("/deployments", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`["guestbook.yaml"]`))
})
expected := "guestbook.yaml\n"
actual := CaptureOutput(func() {
th.Run("deployment", "list")
})
if expected != actual {
t.Errorf("Expected %v got %v", expected, actual)
} }
} }
...@@ -27,15 +27,16 @@ import ( ...@@ -27,15 +27,16 @@ import (
"github.com/kubernetes/helm/pkg/format" "github.com/kubernetes/helm/pkg/format"
) )
type testHelm struct { type testHelmData struct {
t *testing.T t *testing.T
mux *http.ServeMux mux *http.ServeMux
server *httptest.Server server *httptest.Server
app *cli.App app *cli.App
output string
} }
func setup() *testHelm { func testHelm(t *testing.T) *testHelmData {
th := &testHelm{} th := &testHelmData{t: t}
th.app = cli.NewApp() th.app = cli.NewApp()
th.app.Commands = commands th.app.Commands = commands
...@@ -64,39 +65,49 @@ func setup() *testHelm { ...@@ -64,39 +65,49 @@ func setup() *testHelm {
return th return th
} }
func (th *testHelm) teardown() { func (th *testHelmData) cleanup() {
th.server.Close() th.server.Close()
} }
func (th *testHelm) URL() string { func (th *testHelmData) URL() string {
return th.server.URL return th.server.URL
} }
func (th *testHelm) Run(args ...string) { // must gives a fatal error if err is not nil.
args = append([]string{"helm", "--host", th.URL()}, args...) func (th *testHelmData) must(err error) {
th.app.Run(args) if err != nil {
th.t.Fatal(err)
}
} }
// CaptureOutput redirect all log/std streams, capture and replace // check gives a test non-fatal error if err is not nil.
func CaptureOutput(fn func()) string { func (th *testHelmData) check(err error) {
logStderr := format.Stderr if err != nil {
logStdout := format.Stdout th.t.Error(err)
osStdout := os.Stdout }
osStderr := os.Stderr }
func (th *testHelmData) run(args ...string) {
th.output = ""
args = append([]string{"helm", "--host", th.URL()}, args...)
th.output = captureOutput(func() {
th.app.Run(args)
})
}
// captureOutput redirect all log/std streams, capture and replace
func captureOutput(fn func()) string {
osStdout, osStderr := os.Stdout, os.Stderr
logStdout, logStderr := format.Stdout, format.Stderr
defer func() { defer func() {
format.Stderr = logStderr os.Stdout, os.Stderr = osStdout, osStderr
format.Stdout = logStdout format.Stdout, format.Stderr = logStdout, logStderr
os.Stdout = osStdout
os.Stderr = osStderr
}() }()
r, w, _ := os.Pipe() r, w, _ := os.Pipe()
format.Stderr = w os.Stdout, os.Stderr = w, w
format.Stdout = w format.Stdout, format.Stderr = w, w
os.Stdout = w
os.Stderr = w
fn() fn()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment