Commit 92031722 authored by Adam Reese's avatar Adam Reese

test(cli): refactor cli test setup

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