Skip to content

Commit eec3b78

Browse files
authored
tests(cmd): add tests for 'healthchecks:list', 'healthchecks:set', and 'healthchecks:unset' (#239)
* fix(cmd): make printHealthCheck a method on DeisCmd * tests(cmd): add tests for 'healthchecks:list', 'healthchecks:set', and 'healthchecks:unset'
1 parent f33e320 commit eec3b78

2 files changed

Lines changed: 189 additions & 15 deletions

File tree

cmd/healthchecks.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
"io"
6-
"os"
7-
84
"github.com/deis/controller-sdk-go/api"
95
"github.com/deis/controller-sdk-go/config"
106
)
117

12-
func printHealthCheck(out io.Writer, healthcheck api.Healthchecks) {
13-
fmt.Fprintln(out, "--- Liveness")
8+
func (d *DeisCmd) printHealthCheck(healthcheck api.Healthchecks) {
9+
d.Println("--- Liveness")
1410
if livenessProbe, found := healthcheck["livenessProbe"]; found {
15-
fmt.Fprintln(out, livenessProbe)
11+
d.Println(livenessProbe)
1612
} else {
17-
fmt.Fprintln(out, "No liveness probe configured.")
13+
d.Println("No liveness probe configured.")
1814
}
1915

20-
fmt.Fprintln(out, "\n--- Readiness")
16+
d.Println("\n--- Readiness")
2117
if readinessProbe, found := healthcheck["readinessProbe"]; found {
22-
fmt.Fprintln(out, readinessProbe)
18+
d.Println(readinessProbe)
2319
} else {
24-
fmt.Fprintln(out, "No readiness probe configured.")
20+
d.Println("No readiness probe configured.")
2521
}
2622
}
2723

@@ -41,9 +37,9 @@ func (d *DeisCmd) HealthchecksList(appID, procType string) error {
4137
d.Printf("=== %s Healthchecks\n\n", appID)
4238
d.Println(procType + ":")
4339
if healthcheck, found := config.Healthcheck[procType]; found {
44-
printHealthCheck(os.Stdout, *healthcheck)
40+
d.printHealthCheck(*healthcheck)
4541
} else {
46-
printHealthCheck(os.Stdout, api.Healthchecks{})
42+
d.printHealthCheck(api.Healthchecks{})
4743
}
4844

4945
return nil

cmd/healthchecks_test.go

Lines changed: 180 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,198 @@ package cmd
22

33
import (
44
"bytes"
5+
"fmt"
6+
"net/http"
57
"testing"
68

79
"github.com/arschles/assert"
810
"github.com/deis/controller-sdk-go/api"
11+
"github.com/deis/workflow-cli/pkg/testutil"
912
)
1013

1114
func TestPrintHealthCheck(t *testing.T) {
15+
t.Parallel()
16+
cf, server, err := testutil.NewTestServerAndClient()
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
defer server.Close()
1221
var b bytes.Buffer
22+
cmdr := DeisCmd{WOut: &b, ConfigFile: cf}
23+
1324
testHealthCheck := api.Healthchecks{}
14-
printHealthCheck(&b, testHealthCheck)
25+
cmdr.printHealthCheck(testHealthCheck)
1526
assert.Equal(t, b.String(), "--- Liveness\nNo liveness probe configured.\n\n--- Readiness\nNo readiness probe configured.\n", "healthcheck")
1627
b.Reset()
1728
testHealthCheck["livenessProbe"] = &api.Healthcheck{}
1829
testHealthCheck["readinessProbe"] = &api.Healthcheck{}
19-
printHealthCheck(&b, testHealthCheck)
30+
cmdr.printHealthCheck(testHealthCheck)
2031
assert.Equal(t, b.String(), "--- Liveness\nInitial Delay (seconds): 0\nTimeout (seconds): 0\nPeriod (seconds): 0\nSuccess Threshold: 0\nFailure Threshold: 0\nExec Probe: N/A\nHTTP GET Probe: N/A\nTCP Socket Probe: N/A\n\n--- Readiness\nInitial Delay (seconds): 0\nTimeout (seconds): 0\nPeriod (seconds): 0\nSuccess Threshold: 0\nFailure Threshold: 0\nExec Probe: N/A\nHTTP GET Probe: N/A\nTCP Socket Probe: N/A\n", "healthcheck")
2132
}
33+
34+
func TestHealthchecksList(t *testing.T) {
35+
t.Parallel()
36+
cf, server, err := testutil.NewTestServerAndClient()
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
defer server.Close()
41+
var b bytes.Buffer
42+
cmdr := DeisCmd{WOut: &b, ConfigFile: cf}
43+
44+
server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, r *http.Request) {
45+
testutil.SetHeaders(w)
46+
fmt.Fprintf(w, `{
47+
"uuid": "c039a380-6068-4511-b35a-535a73b86ef5",
48+
"app": "foo",
49+
"owner": "bar",
50+
"values": {},
51+
"memory": {},
52+
"cpu": {},
53+
"tags": {},
54+
"registry": {},
55+
"healthcheck": {
56+
"web/cmd": {
57+
"livenessProbe": {
58+
"initialDelaySeconds": 50,
59+
"timeoutSeconds": 50,
60+
"periodSeconds": 10,
61+
"failureThreshold": 3,
62+
"httpGet": {
63+
"port": 80,
64+
"path": "/"
65+
},
66+
"successThreshold": 1
67+
}
68+
}
69+
},
70+
"created": "2016-09-12T22:20:14Z",
71+
"updated": "2016-09-12T22:20:14Z"
72+
}`)
73+
})
74+
75+
err = cmdr.HealthchecksList("foo", "web/cmd")
76+
assert.NoErr(t, err)
77+
78+
assert.Equal(t, b.String(), `=== foo Healthchecks
79+
80+
web/cmd:
81+
--- Liveness
82+
Initial Delay (seconds): 50
83+
Timeout (seconds): 50
84+
Period (seconds): 10
85+
Success Threshold: 1
86+
Failure Threshold: 3
87+
Exec Probe: N/A
88+
HTTP GET Probe: Path="/" Port=80 HTTPHeaders=[]
89+
TCP Socket Probe: N/A
90+
91+
--- Readiness
92+
No readiness probe configured.
93+
`, "output")
94+
}
95+
96+
func TestHealthchecksSet(t *testing.T) {
97+
t.Parallel()
98+
cf, server, err := testutil.NewTestServerAndClient()
99+
if err != nil {
100+
t.Fatal(err)
101+
}
102+
defer server.Close()
103+
var b bytes.Buffer
104+
cmdr := DeisCmd{WOut: &b, ConfigFile: cf}
105+
106+
server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, r *http.Request) {
107+
testutil.SetHeaders(w)
108+
fmt.Fprintf(w, `{
109+
"uuid": "c039a380-6068-4511-b35a-535a73b86ef5",
110+
"app": "foo",
111+
"owner": "bar",
112+
"values": {},
113+
"memory": {},
114+
"cpu": {},
115+
"tags": {},
116+
"registry": {},
117+
"healthcheck": {
118+
"web/cmd": {
119+
"livenessProbe": {
120+
"initialDelaySeconds": 50,
121+
"timeoutSeconds": 50,
122+
"periodSeconds": 10,
123+
"failureThreshold": 3,
124+
"httpGet": {
125+
"port": 80,
126+
"path": "/"
127+
},
128+
"successThreshold": 1
129+
}
130+
}
131+
},
132+
"created": "2016-09-12T22:20:14Z",
133+
"updated": "2016-09-12T22:20:14Z"
134+
}`)
135+
})
136+
137+
err = cmdr.HealthchecksSet("foo", "liveness", "web/cmd", &api.Healthcheck{})
138+
assert.NoErr(t, err)
139+
assert.Equal(t, testutil.StripProgress(b.String()), `Applying liveness healthcheck... done
140+
141+
=== foo Healthchecks
142+
143+
web/cmd:
144+
--- Liveness
145+
Initial Delay (seconds): 50
146+
Timeout (seconds): 50
147+
Period (seconds): 10
148+
Success Threshold: 1
149+
Failure Threshold: 3
150+
Exec Probe: N/A
151+
HTTP GET Probe: Path="/" Port=80 HTTPHeaders=[]
152+
TCP Socket Probe: N/A
153+
154+
--- Readiness
155+
No readiness probe configured.
156+
`, "output")
157+
}
158+
159+
func TestHealthchecksUnset(t *testing.T) {
160+
t.Parallel()
161+
cf, server, err := testutil.NewTestServerAndClient()
162+
if err != nil {
163+
t.Fatal(err)
164+
}
165+
defer server.Close()
166+
var b bytes.Buffer
167+
cmdr := DeisCmd{WOut: &b, ConfigFile: cf}
168+
169+
server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, r *http.Request) {
170+
testutil.SetHeaders(w)
171+
fmt.Fprintf(w, `{
172+
"uuid": "c039a380-6068-4511-b35a-535a73b86ef5",
173+
"app": "foo",
174+
"owner": "bar",
175+
"values": {},
176+
"memory": {},
177+
"cpu": {},
178+
"tags": {},
179+
"registry": {},
180+
"healthcheck": {},
181+
"created": "2016-09-12T22:20:14Z",
182+
"updated": "2016-09-12T22:20:14Z"
183+
}`)
184+
})
185+
186+
err = cmdr.HealthchecksUnset("foo", "web/cmd", []string{"web/cmd"})
187+
assert.NoErr(t, err)
188+
assert.Equal(t, testutil.StripProgress(b.String()), `Removing healthchecks... done
189+
190+
=== foo Healthchecks
191+
192+
web/cmd:
193+
--- Liveness
194+
No liveness probe configured.
195+
196+
--- Readiness
197+
No readiness probe configured.
198+
`, "output")
199+
}

0 commit comments

Comments
 (0)