Skip to content

Commit f3e6ec5

Browse files
committed
Merge pull request #191 from arschles/better-errs
ref(_tests): use gexec for executing commands
2 parents eaec7c8 + e0bc466 commit f3e6ec5

5 files changed

Lines changed: 101 additions & 77 deletions

File tree

_tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test-setup:
44
go get github.com/onsi/gomega
55

66
test-integration:
7-
go test -v ./...
7+
go test -v -ginkgo.v ./...
88

99
build:
1010
# Precompile the test suite into a binary "_tests.test"

_tests/apps_test.go

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package _tests_test
33
import (
44
. "github.com/onsi/ginkgo"
55
. "github.com/onsi/gomega"
6+
"github.com/onsi/gomega/gbytes"
7+
"github.com/onsi/gomega/gexec"
68
)
79

810
var _ = Describe("Apps", func() {
@@ -14,52 +16,57 @@ var _ = Describe("Apps", func() {
1416
})
1517

1618
It("can't get app info", func() {
17-
output, err := execute("deis info -a %s", app1Name)
18-
Expect(err).To(HaveOccurred())
19-
Expect(output).To(ContainSubstring("NOT FOUND"))
19+
sess, err := start("deis info -a %s", app1Name)
20+
Expect(err).ToNot(BeNil())
21+
Eventually(sess).ShouldNot(gexec.Exit(0))
22+
Eventually(sess).Should(gbytes.Say("NOT FOUND"))
2023
})
2124

2225
It("can't get app logs", func() {
23-
output, err := execute("deis logs -a %s", app1Name)
24-
Expect(err).To(HaveOccurred())
25-
Expect(output).To(ContainSubstring("NOT FOUND"))
26+
sess, err := start("deis logs -a %s", app1Name)
27+
Expect(err).To(BeNil())
28+
Eventually(sess).ShouldNot(gexec.Exit(0))
29+
Eventually(sess).Should(gbytes.Say("NOT FOUND"))
2630
})
2731

2832
// TODO: this currently returns "Error: json: cannot unmarshal object into Go value of type []interface {}"
2933
XIt("can't run a command in the app environment", func() {
30-
output, err := execute("deis apps:run echo Hello, 世界")
31-
Expect(err).To(HaveOccurred())
32-
Expect(output).To(ContainSubstring("NOT FOUND"))
34+
sess, err := start("deis apps:run echo Hello, 世界")
35+
Expect(err).To(BeNil())
36+
Eventually(sess).ShouldNot(gexec.Exit(0))
37+
Eventually(sess).Should(gbytes.Say("NOT FOUND"))
3338
})
3439

3540
It("can create an app", func() {
36-
output, err := execute("deis apps:create %s", app1Name)
37-
Expect(err).NotTo(HaveOccurred())
38-
Expect(output).To(SatisfyAll(
39-
ContainSubstring("Creating Application... done, created %s", app1Name),
40-
ContainSubstring("Git remote deis added"),
41-
ContainSubstring("remote available at ")))
42-
output, err = execute("deis apps:destroy --confirm=%s", app1Name)
43-
Expect(err).NotTo(HaveOccurred())
44-
Expect(output).To(SatisfyAll(
45-
ContainSubstring("Destroying %s...", app1Name),
46-
ContainSubstring("done in "),
47-
ContainSubstring("Git remote deis removed")))
41+
sess, err := start("deis apps:create %s", app1Name)
42+
Expect(err).To(BeNil())
43+
Eventually(sess).Should(gexec.Exit(0))
44+
Eventually(sess).Should(gbytes.Say("Creating Application... done, created %s", app1Name))
45+
Eventually(sess).Should(gbytes.Say("Git remote deis added"))
46+
Eventually(sess).Should(gbytes.Say("remote available at "))
47+
48+
sess, err = start("deis apps:destroy --confirm=%s", app1Name)
49+
Expect(err).To(BeNil())
50+
Eventually(sess).Should(gexec.Exit(0))
51+
Eventually(sess).Should(gbytes.Say("Destroying %s...", app1Name))
52+
Eventually(sess).Should(gbytes.Say("done in "))
53+
Eventually(sess).Should(gbytes.Say("Git remote deis removed"))
4854
})
4955

5056
It("can create an app with no git remote", func() {
51-
output, err := execute("deis apps:create %s --no-remote", app1Name)
52-
Expect(err).NotTo(HaveOccurred())
53-
Expect(output).To(SatisfyAll(
54-
ContainSubstring("Creating Application... done, created %s", app1Name),
55-
ContainSubstring("remote available at ")))
56-
Expect(output).NotTo(ContainSubstring("Git remote deis added"))
57-
output, err = execute("deis apps:destroy --app=%s --confirm=%s", app1Name, app1Name)
58-
Expect(err).NotTo(HaveOccurred())
59-
Expect(output).To(SatisfyAll(
60-
ContainSubstring("Destroying %s...", app1Name),
61-
ContainSubstring("done in ")))
62-
Expect(output).NotTo(ContainSubstring("Git remote deis removed"))
57+
sess, err := start("deis apps:create %s --no-remote", app1Name)
58+
Expect(err).To(BeNil())
59+
Eventually(sess).Should(gexec.Exit(0))
60+
Eventually(sess).Should(gbytes.Say("Creating Application... done, created %s", app1Name))
61+
Eventually(sess).Should(gbytes.Say("remove available at "))
62+
Eventually(sess).ShouldNot(gbytes.Say("git remote deis added"))
63+
64+
sess, err = start("deis apps:destroy --app=%s --confirm=%s", app1Name, app1Name)
65+
Expect(err).To(BeNil())
66+
Eventually(sess).Should(gexec.Exit(0))
67+
Eventually(sess).Should(gbytes.Say("Destroying %s...", app1Name))
68+
Eventually(sess).Should(gbytes.Say("done in "))
69+
Eventually(sess).ShouldNot(gbytes.Say("Git remote deis removed"))
6370
})
6471

6572
It("can create an app with a custom buildpack", func() {

_tests/auth_test.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package _tests_test
33
import (
44
. "github.com/onsi/ginkgo"
55
. "github.com/onsi/gomega"
6+
"github.com/onsi/gomega/gbytes"
7+
"github.com/onsi/gomega/gexec"
68
)
79

810
var _ = Describe("Auth", func() {
@@ -12,10 +14,11 @@ var _ = Describe("Auth", func() {
1214
})
1315

1416
It("won't print the current user", func() {
15-
output, err := execute("deis auth:whoami")
16-
Expect(err).To(HaveOccurred())
17-
Expect(output).To(ContainSubstring("Not logged in."))
18-
Expect(output).NotTo(ContainSubstring(testUser))
17+
sess, err := start("deis auth:whoami")
18+
Expect(err).To(BeNil())
19+
Eventually(sess).Should(gexec.Exit(0))
20+
Eventually(sess).Should(gbytes.Say("Not logged in"))
21+
Eventually(sess).Should(gbytes.Say(testUser))
1922
})
2023
})
2124

@@ -30,21 +33,23 @@ var _ = Describe("Auth", func() {
3033

3134
It("won't register twice", func() {
3235
cmd := "deis register %s --username=%s --password=%s --email=%s"
33-
output, err := execute(cmd, url, testUser, testPassword, testEmail)
36+
out, err := execute(cmd, url, testUser, testPassword, testEmail)
3437
Expect(err).To(HaveOccurred())
35-
Expect(output).To(ContainSubstring("Registration failed"))
38+
Expect(out).To(ContainSubstring("Registration failed"))
3639
})
3740

3841
It("prints the current user", func() {
39-
output, err := execute("deis auth:whoami")
40-
Expect(err).NotTo(HaveOccurred())
41-
Expect(output).To(ContainSubstring("You are %s", testUser))
42+
sess, err := start("deis auth:whoami")
43+
Expect(err).To(BeNil())
44+
Eventually(sess).Should(gexec.Exit(0))
45+
Eventually(sess).Should(gbytes.Say("You are %s", testUser))
4246
})
4347

4448
It("regenerates the token for the current user", func() {
45-
output, err := execute("deis auth:regenerate")
46-
Expect(err).NotTo(HaveOccurred())
47-
Expect(output).To(ContainSubstring("Token Regenerated"))
49+
sess, err := start("deis auth:regenerate")
50+
Expect(err).To(BeNil())
51+
Eventually(sess).Should(gexec.Exit(0))
52+
Eventually(sess).Should(gbytes.Say("Token Regenerated"))
4853
})
4954
})
5055

_tests/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var _ = Describe("Config", func() {
1414

1515
It("can create a new app", func() {
1616
output, err := execute("deis apps:create %s", appName)
17-
Expect(err).NotTo(HaveOccurred())
17+
Expect(err).To(BeNil())
1818
Expect(output).To(SatisfyAll(
1919
ContainSubstring("Creating Application... done, created %s", appName),
2020
ContainSubstring("Git remote deis added"),

_tests/tests_suite_test.go

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ package _tests_test
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"math/rand"
78
"os"
89
"os/exec"
910
"os/user"
1011
"path"
12+
"testing"
1113

1214
. "github.com/onsi/ginkgo"
1315
. "github.com/onsi/ginkgo/config"
14-
1516
. "github.com/onsi/gomega"
16-
17-
"testing"
17+
"github.com/onsi/gomega/gbytes"
18+
"github.com/onsi/gomega/gexec"
1819
)
1920

2021
const (
@@ -53,15 +54,17 @@ var _ = BeforeSuite(func() {
5354
// register the test-admin user
5455
register(url, testAdminUser, testAdminPassword, testAdminEmail)
5556
// verify this user is an admin by running a privileged command
56-
_, err = execute("deis users:list")
57-
Expect(err).NotTo(HaveOccurred())
57+
sess, err := start("deis users:list")
58+
Expect(err).To(BeNil())
59+
Eventually(sess).Should(gexec.Exit(0))
5860

5961
// register the test user and add a key
6062
register(url, testUser, testPassword, testEmail)
6163
createKey("deis-test")
62-
output, err := execute("deis keys:add ~/.ssh/deis-test.pub")
63-
Expect(err).NotTo(HaveOccurred())
64-
Expect(output).To(ContainSubstring("Uploading deis-test.pub to deis... done"))
64+
sess, err = start("deis keys:add ~/.ssh/deis-test.pub")
65+
Expect(err).To(BeNil())
66+
Eventually(sess).Should(gexec.Exit(0))
67+
Eventually(sess).Should(gbytes.Say("Uploading deis-test.pub to deis... done"))
6568
})
6669

6770
var _ = AfterSuite(func() {
@@ -73,38 +76,39 @@ var _ = AfterSuite(func() {
7376
})
7477

7578
func register(url, username, password, email string) {
76-
cmd := "deis register %s --username=%s --password=%s --email=%s"
77-
output, err := execute(cmd, url, username, password, email)
78-
Expect(err).NotTo(HaveOccurred())
79-
Expect(output).To(SatisfyAll(
80-
ContainSubstring("Registered %s", username),
81-
ContainSubstring("Logged in as %s", username)))
79+
sess, err := start("deis register %s --username=%s --password=%s --email=%s", url, username, password, email)
80+
Expect(err).To(BeNil())
81+
Eventually(sess).Should(gbytes.Say("Registered %s", username))
82+
Eventually(sess).Should(gbytes.Say("Logged in as %s", username))
8283
}
8384

8485
func cancel(url, username, password string) {
8586
// log in to the account
8687
login(url, username, password)
8788

8889
// cancel the account
89-
cmd := "deis auth:cancel --username=%s --password=%s --yes"
90-
output, err := execute(cmd, username, password)
91-
Expect(err).NotTo(HaveOccurred())
92-
Expect(output).To(ContainSubstring("Account cancelled"))
90+
sess, err := start("deis auth:cancel --username=%s --password=%s --yes", username, password)
91+
Expect(err).To(BeNil())
92+
Eventually(sess).Should(gexec.Exit(0))
93+
Eventually(sess).Should(gbytes.Say("Account cancelled"))
9394
}
9495

9596
func login(url, user, password string) {
96-
cmd := "deis login %s --username=%s --password=%s"
97-
output, err := execute(cmd, url, user, password)
98-
Expect(err).NotTo(HaveOccurred())
99-
Expect(output).To(ContainSubstring("Logged in as %s", user))
97+
sess, err := start("deis login %s --username=%s --password=%s", url, user, password)
98+
Expect(err).To(BeNil())
99+
Eventually(sess).Should(gexec.Exit(0))
100+
Eventually(sess).Should(gbytes.Say("Logged in as %s", user))
100101
}
101102

102103
func logout() {
103-
output, err := execute("deis auth:logout")
104-
Expect(err).NotTo(HaveOccurred())
105-
Expect(output).To(Equal("Logged out\n"))
104+
sess, err := start("deis auth:logout")
105+
Expect(err).To(BeNil())
106+
Eventually(sess).Should(gexec.Exit(0))
107+
Eventually(sess).Should(gbytes.Say("Logged out\n"))
106108
}
107109

110+
// execute executes the command generated by fmt.Sprintf(cmdLine, args...) and returns its output as a cmdOut structure.
111+
// this structure can then be matched upon using the SucceedWithOutput matcher below
108112
func execute(cmdLine string, args ...interface{}) (string, error) {
109113
var stdout, stderr bytes.Buffer
110114
var cmd *exec.Cmd
@@ -116,6 +120,13 @@ func execute(cmdLine string, args ...interface{}) (string, error) {
116120
return stdout.String(), nil
117121
}
118122

123+
func start(cmdLine string, args ...interface{}) (*gexec.Session, error) {
124+
cmdStr := fmt.Sprintf(cmdLine, args...)
125+
fmt.Println(cmdStr)
126+
cmd := exec.Command("/bin/sh", "-c", cmdStr)
127+
return gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
128+
}
129+
119130
func createKey(name string) {
120131
var home string
121132
if user, err := user.Current(); err != nil {
@@ -126,13 +137,14 @@ func createKey(name string) {
126137
path := path.Join(home, ".ssh", name)
127138
// create the key under ~/.ssh/<name> if it doesn't already exist
128139
if _, err := os.Stat(path); os.IsNotExist(err) {
129-
cmd := "ssh-keygen -q -t rsa -b 4096 -C %s -f %s -N ''"
130-
_, err := execute(cmd, name, path)
131-
Expect(err).NotTo(HaveOccurred())
140+
sess, err := start("ssh-keygen -q -t rsa -b 4096 -C %s -f %s -N ''", name, path)
141+
Expect(err).To(BeNil())
142+
Eventually(sess).Should(gexec.Exit(0))
132143
}
133144
// add the key to ssh-agent
134-
_, err := execute("eval $(ssh-agent) && ssh-add %s", path)
135-
Expect(err).NotTo(HaveOccurred())
145+
sess, err := start("eval $(ssh-agent) && ssh-add %s", path)
146+
Expect(err).To(BeNil())
147+
Eventually(sess).Should(gexec.Exit(0))
136148
}
137149

138150
func getController() string {

0 commit comments

Comments
 (0)