Merge pull request #442 from adamreese/feat/cmd-tests

test(cli): add test pattern for cli tests
pull/444/head
Adam Reese 10 years ago
commit 91a007cfee

@ -30,13 +30,9 @@ var errMissingDeploymentArg = errors.New("First argument, deployment name, is re
const defaultShowFormat = `Name: {{.Name}}
Status: {{.State.Status}}
{{- with .State.Errors}}
Errors:
{{- range .}}
{{.}}
{{- end}}
{{- end}}
`
{{with .State.Errors}}Errors:
{{range .}} {{.}}{{end}}
{{end}}`
func init() {
addCommands(deploymentCommands())

@ -0,0 +1,73 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"net/http"
"testing"
"github.com/kubernetes/helm/pkg/common"
)
func TestDeployment(t *testing.T) {
var deploymentTestCases = []struct {
args []string
resp interface{}
expected string
}{
{
[]string{"deployment", "show", "guestbook.yaml"},
&common.Deployment{
Name: "guestbook.yaml",
State: &common.DeploymentState{Status: common.CreatedStatus},
},
"Name: guestbook.yaml\nStatus: Created\n",
},
{
[]string{"deployment", "show", "guestbook.yaml"},
&common.Deployment{
Name: "guestbook.yaml",
State: &common.DeploymentState{
common.FailedStatus, []string{"error message"},
},
},
"Name: guestbook.yaml\nStatus: Failed\nErrors:\n error message\n",
},
{
[]string{"deployment", "list"},
[]string{"guestbook.yaml"},
"guestbook.yaml\n",
},
}
for _, tc := range deploymentTestCases {
th := testHelm(t)
th.mux.HandleFunc("/deployments/", func(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(tc.resp)
th.must(err)
w.Write(data)
})
th.run(tc.args...)
if tc.expected != th.output {
t.Errorf("Expected %v got %v", tc.expected, th.output)
}
th.cleanup()
}
}

@ -0,0 +1,118 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/codegangsta/cli"
"github.com/kubernetes/helm/pkg/format"
)
type testHelmData struct {
t *testing.T
mux *http.ServeMux
server *httptest.Server
app *cli.App
output string
}
func testHelm(t *testing.T) *testHelmData {
th := &testHelmData{t: t}
th.app = cli.NewApp()
th.app.Commands = commands
th.app.Flags = []cli.Flag{
cli.StringFlag{
Name: "host,u",
Value: "https://localhost:8000/",
},
cli.IntFlag{
Name: "timeout",
Value: 10,
},
cli.BoolFlag{
Name: "debug",
},
}
th.app.Before = func(c *cli.Context) error {
debug = c.GlobalBool("debug")
return nil
}
th.mux = http.NewServeMux()
th.server = httptest.NewServer(th.mux)
return th
}
func (th *testHelmData) cleanup() {
th.server.Close()
}
func (th *testHelmData) URL() string {
return th.server.URL
}
// must gives a fatal error if err is not nil.
func (th *testHelmData) must(err error) {
if err != nil {
th.t.Fatal(err)
}
}
// 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() {
os.Stdout, os.Stderr = osStdout, osStderr
format.Stdout, format.Stderr = logStdout, logStderr
}()
r, w, _ := os.Pipe()
os.Stdout, os.Stderr = w, w
format.Stdout, format.Stderr = w, w
fn()
// read test output and restore previous stdout
w.Close()
b, _ := ioutil.ReadAll(r)
return string(b)
}
Loading…
Cancel
Save