Skip to content

Commit 002da75

Browse files
authored
tests(parser): adding tests for parser package. (#243)
* fix(parser): remove superfluous error catch * tests(parser): adding tests for parser package. * tests(parser): add additional parser tests for additional command args. * fix(parser): use cmdr print functions for printHelp * tests(parser): add parser tests for bare commands. * tests(parser): better detect which function is called by returning specific error. * tests(parser): remove FakeCommander. * tests(parser): minor move to keep tests similar. * tests(parser): minor addition of ps->ps:list test * Revert "fix(parser): use cmdr print functions for printHelp" This reverts commit 572014fe7bba29efe76fc896e1adb72519936197.
1 parent c98ecdd commit 002da75

25 files changed

Lines changed: 1809 additions & 3 deletions

parser/apps_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package parser
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"testing"
7+
8+
"github.com/arschles/assert"
9+
"github.com/deis/workflow-cli/pkg/testutil"
10+
)
11+
12+
// Create fake implementations of each method that return the argument
13+
// we expect to have called the function (as an error to satisfy the interface).
14+
15+
func (d FakeDeisCmd) AppCreate(string, string, string, bool) error {
16+
return errors.New("apps:create")
17+
}
18+
19+
func (d FakeDeisCmd) AppsList(int) error {
20+
return errors.New("apps:list")
21+
}
22+
23+
func (d FakeDeisCmd) AppInfo(string) error {
24+
return errors.New("apps:info")
25+
}
26+
27+
func (d FakeDeisCmd) AppOpen(string) error {
28+
return errors.New("apps:open")
29+
}
30+
31+
func (d FakeDeisCmd) AppLogs(string, int) error {
32+
return errors.New("apps:logs")
33+
}
34+
35+
func (d FakeDeisCmd) AppRun(string, string) error {
36+
return errors.New("apps:run")
37+
}
38+
39+
func (d FakeDeisCmd) AppDestroy(string, string) error {
40+
return errors.New("apps:destroy")
41+
}
42+
43+
func (d FakeDeisCmd) AppTransfer(string, string) error {
44+
return errors.New("apps:transfer")
45+
}
46+
47+
func TestApps(t *testing.T) {
48+
t.Parallel()
49+
50+
cf, server, err := testutil.NewTestServerAndClient()
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
defer server.Close()
55+
var b bytes.Buffer
56+
cmdr := FakeDeisCmd{WOut: &b, ConfigFile: cf}
57+
58+
// cases defines the arguments and expected return of the call.
59+
// if expected is "", it defaults to args[0].
60+
cases := []struct {
61+
args []string
62+
expected string
63+
}{
64+
{
65+
args: []string{"apps:create"},
66+
expected: "",
67+
},
68+
{
69+
args: []string{"apps:list"},
70+
expected: "",
71+
},
72+
{
73+
args: []string{"apps:info"},
74+
expected: "",
75+
},
76+
{
77+
args: []string{"apps:open"},
78+
expected: "",
79+
},
80+
{
81+
args: []string{"apps:logs"},
82+
expected: "",
83+
},
84+
{
85+
args: []string{"apps:logs", "--lines=1"},
86+
expected: "",
87+
},
88+
{
89+
args: []string{"apps:run", "ls"},
90+
expected: "",
91+
},
92+
{
93+
args: []string{"apps:destroy"},
94+
expected: "",
95+
},
96+
{
97+
args: []string{"apps:transfer", "test-user"},
98+
expected: "",
99+
},
100+
{
101+
args: []string{"apps"},
102+
expected: "apps:list",
103+
},
104+
}
105+
106+
// For each case, check that calling the route with the arguments
107+
// returns the expected error, which is args[0] if not provided.
108+
for _, c := range cases {
109+
var expected string
110+
if c.expected == "" {
111+
expected = c.args[0]
112+
} else {
113+
expected = c.expected
114+
}
115+
err = Apps(c.args, cmdr)
116+
assert.Err(t, errors.New(expected), err)
117+
}
118+
}

parser/auth_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package parser
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"testing"
7+
8+
"github.com/arschles/assert"
9+
"github.com/deis/workflow-cli/pkg/testutil"
10+
)
11+
12+
// Create fake implementations of each method that return the argument
13+
// we expect to have called the function (as an error to satisfy the interface).
14+
15+
func (d FakeDeisCmd) Register(string, string, string, string, bool) error {
16+
return errors.New("auth:register")
17+
}
18+
19+
func (d FakeDeisCmd) Login(string, string, string, bool) error {
20+
return errors.New("auth:login")
21+
}
22+
23+
func (d FakeDeisCmd) Logout() error {
24+
return errors.New("auth:logout")
25+
}
26+
27+
func (d FakeDeisCmd) Passwd(string, string, string) error {
28+
return errors.New("auth:passwd")
29+
}
30+
31+
func (d FakeDeisCmd) Cancel(string, string, bool) error {
32+
return errors.New("auth:cancel")
33+
}
34+
35+
func (d FakeDeisCmd) Whoami(bool) error {
36+
return errors.New("auth:whoami")
37+
}
38+
39+
func (d FakeDeisCmd) Regenerate(string, bool) error {
40+
return errors.New("auth:regenerate")
41+
}
42+
43+
func TestAuth(t *testing.T) {
44+
t.Parallel()
45+
46+
cf, server, err := testutil.NewTestServerAndClient()
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
defer server.Close()
51+
var b bytes.Buffer
52+
cmdr := FakeDeisCmd{WOut: &b, ConfigFile: cf}
53+
54+
// cases defines the arguments and expected return of the call.
55+
// if expected is "", it defaults to args[0].
56+
cases := []struct {
57+
args []string
58+
expected string
59+
}{
60+
{
61+
args: []string{"auth:register", server.Server.URL},
62+
expected: "",
63+
},
64+
{
65+
args: []string{"auth:login", server.Server.URL},
66+
expected: "",
67+
},
68+
{
69+
args: []string{"auth:login", server.Server.URL, "--ssl-verify=true"},
70+
expected: "",
71+
},
72+
{
73+
args: []string{"auth:logout"},
74+
expected: "",
75+
},
76+
{
77+
args: []string{"auth:passwd"},
78+
expected: "",
79+
},
80+
{
81+
args: []string{"auth:whoami"},
82+
expected: "",
83+
},
84+
{
85+
args: []string{"auth:cancel"},
86+
expected: "",
87+
},
88+
{
89+
args: []string{"auth:regenerate"},
90+
expected: "",
91+
},
92+
}
93+
94+
// For each case, check that calling the route with the arguments
95+
// returns the expected error, which is args[0] if not provided.
96+
for _, c := range cases {
97+
var expected string
98+
if c.expected == "" {
99+
expected = c.args[0]
100+
} else {
101+
expected = c.expected
102+
}
103+
err = Auth(c.args, cmdr)
104+
assert.Err(t, errors.New(expected), err)
105+
}
106+
}

parser/autoscale_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package parser
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"testing"
7+
8+
"github.com/arschles/assert"
9+
"github.com/deis/workflow-cli/pkg/testutil"
10+
)
11+
12+
// Create fake implementations of each method that return the argument
13+
// we expect to have called the function (as an error to satisfy the interface).
14+
15+
func (d FakeDeisCmd) AutoscaleList(string) error {
16+
return errors.New("autoscale:list")
17+
}
18+
19+
func (d FakeDeisCmd) AutoscaleSet(string, string, int, int, int) error {
20+
return errors.New("autoscale:set")
21+
}
22+
23+
func (d FakeDeisCmd) AutoscaleUnset(string, string) error {
24+
return errors.New("autoscale:unset")
25+
}
26+
27+
func TestAutoscale(t *testing.T) {
28+
t.Parallel()
29+
30+
cf, server, err := testutil.NewTestServerAndClient()
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
defer server.Close()
35+
var b bytes.Buffer
36+
cmdr := FakeDeisCmd{WOut: &b, ConfigFile: cf}
37+
38+
// cases defines the arguments and expected return of the call.
39+
// if expected is "", it defaults to args[0].
40+
cases := []struct {
41+
args []string
42+
expected string
43+
}{
44+
{
45+
args: []string{"autoscale:list"},
46+
expected: "",
47+
},
48+
{
49+
args: []string{"autoscale:set", "web/cmd", "--min=1", "--max=3", "--cpu-percent=50"},
50+
expected: "",
51+
},
52+
{
53+
args: []string{"autoscale:unset", "web/cmd"},
54+
expected: "",
55+
},
56+
{
57+
args: []string{"autoscale"},
58+
expected: "autoscale:list",
59+
},
60+
}
61+
62+
// For each case, check that calling the route with the arguments
63+
// returns the expected error, which is args[0] if not provided.
64+
for _, c := range cases {
65+
var expected string
66+
if c.expected == "" {
67+
expected = c.args[0]
68+
} else {
69+
expected = c.expected
70+
}
71+
err = Autoscale(c.args, cmdr)
72+
assert.Err(t, errors.New(expected), err)
73+
}
74+
}

parser/builds_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package parser
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"testing"
7+
8+
"github.com/arschles/assert"
9+
"github.com/deis/workflow-cli/pkg/testutil"
10+
)
11+
12+
func (d FakeDeisCmd) BuildsList(string, int) error {
13+
return errors.New("builds:list")
14+
}
15+
16+
func (d FakeDeisCmd) BuildsCreate(string, string, string) error {
17+
return errors.New("builds:create")
18+
}
19+
20+
func TestBuilds(t *testing.T) {
21+
t.Parallel()
22+
23+
cf, server, err := testutil.NewTestServerAndClient()
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
defer server.Close()
28+
var b bytes.Buffer
29+
cmdr := FakeDeisCmd{WOut: &b, ConfigFile: cf}
30+
31+
// cases defines the arguments and expected return of the call.
32+
// if expected is "", it defaults to args[0].
33+
cases := []struct {
34+
args []string
35+
expected string
36+
}{
37+
{
38+
args: []string{"builds:list"},
39+
expected: "",
40+
},
41+
{
42+
args: []string{"builds:create", "deis/example-go:latest"},
43+
expected: "",
44+
},
45+
{
46+
args: []string{"builds"},
47+
expected: "builds:list",
48+
},
49+
}
50+
51+
// For each case, check that calling the route with the arguments
52+
// returns the expected error, which is args[0] if not provided.
53+
for _, c := range cases {
54+
var expected string
55+
if c.expected == "" {
56+
expected = c.args[0]
57+
} else {
58+
expected = c.expected
59+
}
60+
err = Builds(c.args, cmdr)
61+
assert.Err(t, errors.New(expected), err)
62+
}
63+
}

0 commit comments

Comments
 (0)