Skip to content

Commit c86bdc6

Browse files
committed
ref(_tests): implementing cmd, replacing execute implementation with cmd
also spreading usage of cmd elsewhere in the test suite
1 parent 8b39413 commit c86bdc6

4 files changed

Lines changed: 77 additions & 68 deletions

File tree

_tests/apps_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,30 @@ var _ = Describe("Apps", func() {
1414
})
1515

1616
It("can't get app info", func() {
17-
Expect(execute("deis info -a %s", app1Name)).To(SucceedWithOutput(ContainSubstring("NOT FOUND")))
17+
Expect(execute("deis info -a %s", app1Name)).To(BeASuccessfulCmdWithOutput(ContainSubstring("NOT FOUND")))
1818
})
1919

2020
It("can't get app logs", func() {
21-
out := execute("deis logs -a %s", app1Name)
22-
Expect(out.err).To(HaveOccurred())
23-
Expect(out.str).To(ContainSubstring("NOT FOUND"))
21+
out, err := execute("deis logs -a %s", app1Name)
22+
Expect(err).To(HaveOccurred())
23+
Expect(out).To(ContainSubstring("NOT FOUND"))
2424
})
2525

2626
// TODO: this currently returns "Error: json: cannot unmarshal object into Go value of type []interface {}"
2727
XIt("can't run a command in the app environment", func() {
28-
out := execute("deis apps:run echo Hello, 世界")
29-
Expect(out.err).To(HaveOccurred())
30-
Expect(out.str).To(ContainSubstring("NOT FOUND"))
28+
out, err := execute("deis apps:run echo Hello, 世界")
29+
Expect(err).To(HaveOccurred())
30+
Expect(out).To(ContainSubstring("NOT FOUND"))
3131
})
3232

3333
It("can create an app", func() {
34-
Expect(execute("deis apps:create %s", app1Name)).To(SucceedWithOutput(
34+
Expect(execute("deis apps:create %s", app1Name)).To(BeASuccessfulCmdWithOutput(
3535
ContainSubstring("Creating Application... done, created %s", app1Name),
3636
ContainSubstring("Git remote deis added"),
3737
ContainSubstring("remote available at "),
3838
))
3939

40-
Expect(execute("deis apps:destroy --confirm=%s", app1Name)).To(SucceedWithOutput(
40+
Expect(execute("deis apps:destroy --confirm=%s", app1Name)).To(BeASuccessfulCmdWithOutput(
4141
ContainSubstring("Destroying %s...", app1Name),
4242
ContainSubstring("done in "),
4343
ContainSubstring("Git remote deis removed"),

_tests/auth_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var _ = Describe("Auth", func() {
1212
})
1313

1414
It("won't print the current user", func() {
15-
Expect(execute("deis auth:whoami")).To(BeASuccessfulCommandWithOutputMatching(
15+
Expect(execute("deis auth:whoami")).To(BeASuccessfulCmdWithOutput(
1616
ContainSubstring("Not logged in."),
1717
ContainSubstring(testUser),
1818
))
@@ -30,19 +30,19 @@ var _ = Describe("Auth", func() {
3030

3131
It("won't register twice", func() {
3232
cmd := "deis register %s --username=%s --password=%s --email=%s"
33-
out := execute(cmd, url, testUser, testPassword, testEmail)
34-
Expect(out.err).To(HaveOccurred())
35-
Expect(out.str).To(ContainSubstring("Registration failed"))
33+
out, err := execute(cmd, url, testUser, testPassword, testEmail)
34+
Expect(err).To(HaveOccurred())
35+
Expect(out).To(ContainSubstring("Registration failed"))
3636
})
3737

3838
It("prints the current user", func() {
39-
Expect(execute("deis auth:whoami")).To(SucceedWithOutput(
39+
Expect(execute("deis auth:whoami")).To(BeASuccessfulCmdWithOutput(
4040
ContainSubstring("You are %s", testUser),
4141
))
4242
})
4343

4444
It("regenerates the token for the current user", func() {
45-
Expect(execute("deis auth:regenerate")).To(SucceedWithOutput(
45+
Expect(execute("deis auth:regenerate")).To(BeASuccessfulCmdWithOutput(
4646
ContainSubstring("Token Regenerated"),
4747
))
4848
})

_tests/cmd_test.go

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,54 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"github.com/onsi/gomega"
78
"github.com/onsi/gomega/types"
9+
// "io"
10+
"os"
811
"os/exec"
912
"strings"
1013
)
1114

1215
// cmdOut is the output of a command. it's used by cmdMatcher to match on
1316
type cmdOut struct {
14-
args []string
15-
str string
16-
err error
17+
args []string
18+
stdout string
19+
stderr string
20+
err error
1721
}
1822

19-
// execute executes the command generated by fmt.Sprintf(cmdLine, args...) and returns its output as a cmdOut structure.
20-
// this structure can then be matched upon using the SucceedWithOutput matcher below
21-
func execute(cmdLine string, args ...interface{}) *cmdOut {
23+
func (c *cmdOut) String() string {
24+
return strings.Join(c.args, " ")
25+
}
26+
27+
func cmd(cmdLine string, args ...interface{}) *cmdOut {
2228
var stdout, stderr bytes.Buffer
2329
var cmd *exec.Cmd
2430
cmd = exec.Command("/bin/sh", "-c", fmt.Sprintf(cmdLine, args...))
25-
cmd.Stdout, cmd.Stderr = &stdout, &stderr
31+
cmd.Stdout = &stdout
32+
cmd.Stderr = &stderr
33+
cmd.Env = os.Environ()
2634
ret := &cmdOut{}
2735
ret.args = cmd.Args
28-
ret.str = stdout.String()
29-
if err := cmd.Run(); err != nil {
30-
ret.err = err
31-
return ret
32-
}
36+
ret.err = cmd.Run()
37+
ret.stdout = stdout.String()
38+
ret.stderr = stderr.String()
3339
return ret
3440
}
3541

3642
var (
3743
errExpectedCmdOut = errors.New("cmdMatcher expects a *cmdOut")
38-
errFailedCmd = errors.New("command failed")
3944
)
4045

46+
func cmdErr(co *cmdOut) error {
47+
return fmt.Errorf("COMMAND ERROR\n[%s]\nError: %s\nSTDOUT: %s\nSTDERR: %s",
48+
strings.Join(co.args, " "),
49+
co.err,
50+
co.stdout,
51+
co.stderr,
52+
)
53+
}
54+
4155
// Command
4256
type successfulCmdMatcher struct {
4357
// will be filled in when Match is called
@@ -47,10 +61,14 @@ type successfulCmdMatcher struct {
4761

4862
// SucceedWithOutput returns a matcher that will match on a *cmdOut, ensuring that the command returned no error
4963
// and its output matches all the given matchers
50-
func SucceedWithOutput(matchers ...types.GomegaMatcher) types.GomegaMatcher {
64+
func BeASuccessfulCmdWithOutput(matchers ...types.GomegaMatcher) types.GomegaMatcher {
5165
return &successfulCmdMatcher{matchers: matchers}
5266
}
5367

68+
func BeASuccessfulCmd() types.GomegaMatcher {
69+
return &successfulCmdMatcher{matchers: nil}
70+
}
71+
5472
// Match is the interface implementation of github.com/onsi/gomega/types.GomegaMatcher
5573
func (c *successfulCmdMatcher) Match(actual interface{}) (bool, error) {
5674
cmdo, ok := actual.(*cmdOut)
@@ -59,21 +77,17 @@ func (c *successfulCmdMatcher) Match(actual interface{}) (bool, error) {
5977
}
6078
c.cmdo = cmdo
6179
if cmdo.err != nil {
62-
return false, errFailedCmd
80+
return false, cmdErr(cmdo)
6381
}
64-
return SatisfyAll(c.matchers...)
82+
return gomega.SatisfyAll(c.matchers...).Match(actual)
6583
}
6684

6785
// FailureMessage is the interface implementation of github.com/onsi/gomega/types.GomegaMatcher
6886
func (c *successfulCmdMatcher) FailureMessage(actual interface{}) string {
6987
if c.cmdo == nil {
7088
return "command failed, but wasn't recorded"
7189
}
72-
return fmt.Sprintf("command %s errored.\noutput:\n%s\nerror:%s\n",
73-
strings.Join(c.cmdo.args, " "),
74-
c.cmdo.str,
75-
c.cmdo.err,
76-
)
90+
return cmdErr(c.cmdo).Error()
7791
}
7892

7993
// NegatedFailureMessage is the interface implementation of github.com/onsi/gomega/types.GomegaMatcher
@@ -82,9 +96,5 @@ func (c *successfulCmdMatcher) NegatedFailureMessage(actual interface{}) string
8296
return "command failed, but wasn't recorded"
8397
}
8498

85-
return fmt.Sprintf("command %s errored.\noutput:\n%s\nerror:%s\n",
86-
strings.Join(c.cmdo.args, " "),
87-
c.cmdo.str,
88-
c.cmdo.err,
89-
)
99+
return cmdErr(c.cmdo).Error()
90100
}

_tests/tests_suite_test.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"path"
1010

1111
. "github.com/onsi/ginkgo"
12-
. "github.com/onsi/ginkgo/config"
1312

1413
. "github.com/onsi/gomega"
1514

1615
"testing"
16+
"time"
1717
)
1818

1919
const (
@@ -52,15 +52,14 @@ var _ = BeforeSuite(func() {
5252
// register the test-admin user
5353
register(url, testAdminUser, testAdminPassword, testAdminEmail)
5454
// verify this user is an admin by running a privileged command
55-
_, err = execute("deis users:list")
56-
Expect(err).NotTo(HaveOccurred())
55+
Expect(cmd("deis users:list")).To(BeASuccessfulCmd())
5756

5857
// register the test user and add a key
5958
register(url, testUser, testPassword, testEmail)
6059
createKey("deis-test")
61-
output, err := execute("deis keys:add ~/.ssh/deis-test.pub")
62-
Expect(err).NotTo(HaveOccurred())
63-
Expect(output).To(ContainSubstring("Uploading deis-test.pub to deis... done"))
60+
Expect(cmd("deis keys:add ~/.ssh/deis-test.pub")).To(BeASuccessfulCmdWithOutput(
61+
ContainSubstring("Uploading deis-test.pub to deis... done"),
62+
))
6463
})
6564

6665
var _ = AfterSuite(func() {
@@ -72,36 +71,39 @@ var _ = AfterSuite(func() {
7271
})
7372

7473
func register(url, username, password, email string) {
75-
cmd := "deis register %s --username=%s --password=%s --email=%s"
76-
output, err := execute(cmd, url, username, password, email)
77-
Expect(err).NotTo(HaveOccurred())
78-
Expect(output).To(SatisfyAll(
74+
Expect(cmd("deis register %s --username=%s --password=%s --email=%s", url, username, password, email)).To(BeASuccessfulCmdWithOutput(
7975
ContainSubstring("Registered %s", username),
80-
ContainSubstring("Logged in as %s", username)))
76+
ContainSubstring("Logged in as %s", username),
77+
))
8178
}
8279

8380
func cancel(url, username, password string) {
8481
// log in to the account
8582
login(url, username, password)
8683

8784
// cancel the account
88-
cmd := "deis auth:cancel --username=%s --password=%s --yes"
89-
output, err := execute(cmd, username, password)
90-
Expect(err).NotTo(HaveOccurred())
91-
Expect(output).To(ContainSubstring("Account cancelled"))
85+
Expect(cmd("deis auth:cancel --username=%s --password=%s --yes", username, password)).To(BeASuccessfulCmdWithOutput(
86+
ContainSubstring("Account cancelled"),
87+
))
9288
}
9389

9490
func login(url, user, password string) {
95-
cmd := "deis login %s --username=%s --password=%s"
96-
output, err := execute(cmd, url, user, password)
97-
Expect(err).NotTo(HaveOccurred())
98-
Expect(output).To(ContainSubstring("Logged in as %s", user))
91+
Expect(cmd("deis login %s --username=%s --password=%s", url, user, password)).To(BeASuccessfulCmdWithOutput(
92+
ContainSubstring("Logged in as %s", user),
93+
))
9994
}
10095

10196
func logout() {
102-
output, err := execute("deis auth:logout")
103-
Expect(err).NotTo(HaveOccurred())
104-
Expect(output).To(Equal("Logged out\n"))
97+
Expect(cmd("deis auth:logout")).To(BeASuccessfulCmdWithOutput(
98+
Equal("Logged out\n"),
99+
))
100+
}
101+
102+
// execute executes the command generated by fmt.Sprintf(cmdLine, args...) and returns its output as a cmdOut structure.
103+
// this structure can then be matched upon using the SucceedWithOutput matcher below
104+
func execute(cmdLine string, args ...interface{}) (string, error) {
105+
c := cmd(cmdLine, args...)
106+
return c.stdout, c.err
105107
}
106108

107109
func createKey(name string) {
@@ -114,13 +116,10 @@ func createKey(name string) {
114116
path := path.Join(home, ".ssh", name)
115117
// create the key under ~/.ssh/<name> if it doesn't already exist
116118
if _, err := os.Stat(path); os.IsNotExist(err) {
117-
cmd := "ssh-keygen -q -t rsa -b 4096 -C %s -f %s -N ''"
118-
_, err := execute(cmd, name, path)
119-
Expect(err).NotTo(HaveOccurred())
119+
Expect(cmd("ssh-keygen -q -t rsa -b 4096 -C %s -f %s -N ''", name, path)).To(BeASuccessfulCmd())
120120
}
121121
// add the key to ssh-agent
122-
_, err := execute("eval $(ssh-agent) && ssh-add %s", path)
123-
Expect(err).NotTo(HaveOccurred())
122+
Expect(cmd("eval $(ssh-agent) && ssh-add %s", path)).To(BeASuccessfulCmd())
124123
}
125124

126125
func getController() string {

0 commit comments

Comments
 (0)