Skip to content

Commit e6d96b7

Browse files
test(tags): add tests for tags (#197)
1 parent 272b1cd commit e6d96b7

2 files changed

Lines changed: 208 additions & 1 deletion

File tree

cmd/tags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func parseTags(tags []string) (map[string]interface{}, error) {
120120
func parseTag(tag string) (string, string, error) {
121121
parts := strings.Split(tag, "=")
122122

123-
if len(parts) != 2 {
123+
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
124124
return "", "", fmt.Errorf(`%s is invalid, Must be in format key=value
125125
Examples: rack=1 evironment=production`, tag)
126126
}

cmd/tags_test.go

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/arschles/assert"
10+
"github.com/deis/controller-sdk-go/api"
11+
"github.com/deis/workflow-cli/pkg/testutil"
12+
)
13+
14+
type parseTagCase struct {
15+
Input string
16+
Key string
17+
Value string
18+
ExpectedError bool
19+
ExpectedMsg string
20+
}
21+
22+
func TestParseTag(t *testing.T) {
23+
t.Parallel()
24+
25+
cases := []parseTagCase{
26+
{"foo=bar", "foo", "bar", false, ""},
27+
{"test=1", "test", "1", false, ""},
28+
{"test", "", "", true, `test is invalid, Must be in format key=value
29+
Examples: rack=1 evironment=production`},
30+
{"test=1=2", "", "", true, `test=1=2 is invalid, Must be in format key=value
31+
Examples: rack=1 evironment=production`},
32+
{"test=", "", "", true, `test= is invalid, Must be in format key=value
33+
Examples: rack=1 evironment=production`},
34+
{"=test", "", "", true, `=test is invalid, Must be in format key=value
35+
Examples: rack=1 evironment=production`},
36+
}
37+
38+
for _, check := range cases {
39+
key, value, err := parseTag(check.Input)
40+
if check.ExpectedError {
41+
assert.Equal(t, err.Error(), check.ExpectedMsg, "error")
42+
} else {
43+
assert.NoErr(t, err)
44+
assert.Equal(t, key, check.Key, "key")
45+
assert.Equal(t, value, check.Value, "value")
46+
}
47+
}
48+
}
49+
50+
type parseTagsCase struct {
51+
Input []string
52+
ExpectedMap map[string]interface{}
53+
ExpectedError bool
54+
ExpectedMsg string
55+
}
56+
57+
func TestParseTags(t *testing.T) {
58+
t.Parallel()
59+
60+
cases := []parseTagsCase{
61+
{[]string{"foo=bar", "true=false"}, map[string]interface{}{"foo": "bar", "true": "false"}, false, ""},
62+
{[]string{"foo=", "true=false"}, nil, true, `foo= is invalid, Must be in format key=value
63+
Examples: rack=1 evironment=production`},
64+
}
65+
66+
for _, check := range cases {
67+
actual, err := parseTags(check.Input)
68+
if check.ExpectedError {
69+
assert.Equal(t, err.Error(), check.ExpectedMsg, "error")
70+
} else {
71+
assert.NoErr(t, err)
72+
assert.Equal(t, actual, check.ExpectedMap, "map")
73+
}
74+
}
75+
}
76+
77+
func TestTagsList(t *testing.T) {
78+
t.Parallel()
79+
cf, server, err := testutil.NewTestServerAndClient()
80+
if err != nil {
81+
t.Fatal(err)
82+
}
83+
defer server.Close()
84+
85+
server.Mux.HandleFunc("/v2/apps/enterprise/config/", func(w http.ResponseWriter, r *http.Request) {
86+
testutil.SetHeaders(w)
87+
fmt.Fprintf(w, `{
88+
"owner": "jkirk",
89+
"app": "enterprise",
90+
"values": {},
91+
"memory": {},
92+
"cpu": {},
93+
"tags": {
94+
"warp": "8",
95+
"ncc": "1701"
96+
},
97+
"registry": {},
98+
"created": "2014-01-01T00:00:00UTC",
99+
"updated": "2014-01-01T00:00:00UTC",
100+
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
101+
}`)
102+
})
103+
104+
var b bytes.Buffer
105+
cmdr := DeisCmd{WOut: &b, ConfigFile: cf}
106+
107+
err = cmdr.TagsList("enterprise")
108+
assert.NoErr(t, err)
109+
assert.Equal(t, b.String(), `=== enterprise Tags
110+
ncc 1701
111+
warp 8
112+
`, "output")
113+
}
114+
115+
func TestTagsSet(t *testing.T) {
116+
t.Parallel()
117+
cf, server, err := testutil.NewTestServerAndClient()
118+
if err != nil {
119+
t.Fatal(err)
120+
}
121+
defer server.Close()
122+
123+
server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, r *http.Request) {
124+
testutil.SetHeaders(w)
125+
if r.Method == "POST" {
126+
testutil.AssertBody(t, api.Config{
127+
Tags: map[string]interface{}{
128+
"true": "false",
129+
},
130+
}, r)
131+
}
132+
133+
fmt.Fprintf(w, `{
134+
"owner": "jkirk",
135+
"app": "foo",
136+
"values": {},
137+
"memory": {},
138+
"cpu": {},
139+
"tags": {
140+
"true": "false"
141+
},
142+
"registry": {},
143+
"created": "2014-01-01T00:00:00UTC",
144+
"updated": "2014-01-01T00:00:00UTC",
145+
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
146+
}`)
147+
})
148+
149+
var b bytes.Buffer
150+
cmdr := DeisCmd{WOut: &b, ConfigFile: cf}
151+
152+
err = cmdr.TagsSet("foo", []string{"true=false"})
153+
assert.NoErr(t, err)
154+
155+
assert.Equal(t, testutil.StripProgress(b.String()), `Applying tags... done
156+
157+
=== foo Tags
158+
true false
159+
`, "output")
160+
}
161+
162+
func TestTagsUnset(t *testing.T) {
163+
t.Parallel()
164+
cf, server, err := testutil.NewTestServerAndClient()
165+
if err != nil {
166+
t.Fatal(err)
167+
}
168+
defer server.Close()
169+
170+
server.Mux.HandleFunc("/v2/apps/foo/config/", func(w http.ResponseWriter, r *http.Request) {
171+
testutil.SetHeaders(w)
172+
if r.Method == "POST" {
173+
testutil.AssertBody(t, api.Config{
174+
Tags: map[string]interface{}{
175+
"ncc": nil,
176+
},
177+
}, r)
178+
}
179+
180+
fmt.Fprintf(w, `{
181+
"owner": "jkirk",
182+
"app": "foo",
183+
"values": {},
184+
"memory": {},
185+
"cpu": {},
186+
"tags": {
187+
"warp": 8
188+
},
189+
"registry": {},
190+
"created": "2014-01-01T00:00:00UTC",
191+
"updated": "2014-01-01T00:00:00UTC",
192+
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
193+
}`)
194+
})
195+
196+
var b bytes.Buffer
197+
cmdr := DeisCmd{WOut: &b, ConfigFile: cf}
198+
199+
err = cmdr.TagsUnset("foo", []string{"ncc"})
200+
assert.NoErr(t, err)
201+
202+
assert.Equal(t, testutil.StripProgress(b.String()), `Applying tags... done
203+
204+
=== foo Tags
205+
warp 8
206+
`, "output")
207+
}

0 commit comments

Comments
 (0)