Merge pull request #13 from adamreese/dm-tests

test(dm): add unit tests for dm client
pull/291/head
Adam Reese 9 years ago
commit fccfcc8e38

@ -47,7 +47,7 @@ quicktest:
$(PATH_WITH_HELM) go test -short $(addprefix ./,$(GO_PACKAGES))
test: test-style
$(PATH_WITH_HELM) go test -v $(addprefix ./,$(GO_PACKAGES))
$(PATH_WITH_HELM) go test -v -cover $(addprefix ./,$(GO_PACKAGES))
test-style:
@if [ $(shell gofmt -e -l -s *.go $(GO_PACKAGES)) ]; then \

@ -8,6 +8,7 @@ import (
)
var version = "0.0.1"
var isDebugging bool
func main() {
app := cli.NewApp()
@ -24,6 +25,15 @@ func main() {
EnvVar: "HELM_HOST",
Value: "https://localhost:8181/FIXME_NOT_RIGHT",
},
cli.BoolFlag{
Name: "debug",
Usage: "Enable verbose debugging output",
},
}
app.Before = func(ctx *cli.Context) error {
isDebugging = ctx.Bool("debug")
return nil
}
app.Run(os.Args)

@ -1,6 +1,7 @@
package main
import (
"fmt"
"os"
"github.com/codegangsta/cli"
@ -22,6 +23,11 @@ func listCmd() cli.Command {
}
func list(host string) error {
client := dm.NewClient(host).SetDebug(true)
return client.ListDeployments()
client := dm.NewClient(host).SetDebug(isDebugging)
list, err := client.ListDeployments()
if err != nil {
return err
}
fmt.Println(list)
return nil
}

@ -11,8 +11,6 @@ import (
"path/filepath"
"strings"
"time"
"github.com/ghodss/yaml"
)
// The default HTTP timeout
@ -94,15 +92,6 @@ func (c *Client) CallService(path, method, action string, dest interface{}, read
if err := json.Unmarshal([]byte(resp), dest); err != nil {
return fmt.Errorf("Failed to parse JSON response from service: %s", resp)
}
// From here down is just printing the data.
y, err := yaml.Marshal(dest)
if err != nil {
return fmt.Errorf("Failed to serialize JSON response from service: %s", resp)
}
fmt.Println(string(y))
return nil
}
@ -137,14 +126,13 @@ func (c *Client) callHTTP(path, method, action string, reader io.ReadCloser) (st
}
// ListDeployments lists the deployments in DM.
func (c *Client) ListDeployments() error {
var d interface{}
if err := c.CallService("deployments", "GET", "foo", &d, nil); err != nil {
return err
func (c *Client) ListDeployments() ([]string, error) {
var l []string
if err := c.CallService("deployments", "GET", "foo", &l, nil); err != nil {
return nil, err
}
fmt.Printf("%#v\n", d)
return nil
return l, nil
}
// DeployChart sends a chart to DM for deploying.

@ -1,6 +1,8 @@
package dm
import (
"net/http"
"net/http/httptest"
"testing"
)
@ -58,3 +60,39 @@ func TestURL(t *testing.T) {
}
}
}
type fakeClient struct {
*Client
server *httptest.Server
handler http.HandlerFunc
response []byte
}
func (c *fakeClient) setup() *fakeClient {
c.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(c.response)
})
c.server = httptest.NewServer(c.handler)
c.Client = NewClient(c.server.URL)
return c
}
func (c *fakeClient) teardown() {
c.server.Close()
}
func TestListDeployments(t *testing.T) {
fc := &fakeClient{
response: []byte(`["guestbook.yaml"]`),
}
defer fc.teardown()
l, err := fc.setup().ListDeployments()
if err != nil {
t.Fatal(err)
}
if len(l) != 1 {
t.Fatal("expected a single deployment")
}
}

Loading…
Cancel
Save