Skip to content

Commit cef38a2

Browse files
committed
fix(tests_suite_test.go): move to gexec
https://godoc.org/github.com/onsi/gomega/gexec
1 parent cce623d commit cef38a2

1 file changed

Lines changed: 47 additions & 25 deletions

File tree

_tests/tests_suite_test.go

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package _tests_test
22

33
import (
4+
"bytes"
45
"fmt"
56
"math/rand"
67
"os"
78
"os/exec"
89
"os/user"
910
"path"
11+
"testing"
1012

1113
. "github.com/onsi/ginkgo"
12-
14+
. "github.com/onsi/ginkgo/config"
1315
. "github.com/onsi/gomega"
14-
15-
"testing"
16-
"time"
16+
"github.com/onsi/gomega/gbytes"
17+
"github.com/onsi/gomega/gexec"
1718
)
1819

1920
const (
@@ -52,14 +53,17 @@ var _ = BeforeSuite(func() {
5253
// register the test-admin user
5354
register(url, testAdminUser, testAdminPassword, testAdminEmail)
5455
// verify this user is an admin by running a privileged command
55-
Expect(cmd("deis users:list")).To(BeASuccessfulCmd())
56+
sess, err := start("deis users:list")
57+
Expect(err).To(BeNil())
58+
Eventually(sess).Should(gexec.Exit(0))
5659

5760
// register the test user and add a key
5861
register(url, testUser, testPassword, testEmail)
5962
createKey("deis-test")
60-
Expect(cmd("deis keys:add ~/.ssh/deis-test.pub")).To(BeASuccessfulCmdWithOutput(
61-
ContainSubstring("Uploading deis-test.pub to deis... done"),
62-
))
63+
sess, err = start("deis keys:add ~/.ssh/deis-test.pub")
64+
Expect(err).To(BeNil())
65+
Eventually(sess).Should(gexec.Exit(0))
66+
Eventually(sess).Should(gbytes.Say("Uploading deis-test.pub to deis... done"))
6367
})
6468

6569
var _ = AfterSuite(func() {
@@ -71,39 +75,53 @@ var _ = AfterSuite(func() {
7175
})
7276

7377
func register(url, username, password, email string) {
74-
Expect(cmd("deis register %s --username=%s --password=%s --email=%s", url, username, password, email)).To(BeASuccessfulCmdWithOutput(
75-
ContainSubstring("Registered %s", username),
76-
ContainSubstring("Logged in as %s", username),
77-
))
78+
sess, err := start("deis register %s --username=%s --password=%s --email=%s", url, username, password, email)
79+
Expect(err).To(BeNil())
80+
Eventually(sess).Should(gbytes.Say("Registered %s", username))
81+
Eventually(sess).Should(gbytes.Say("Logged in as %s", username))
7882
}
7983

8084
func cancel(url, username, password string) {
8185
// log in to the account
8286
login(url, username, password)
8387

8488
// cancel the account
85-
Expect(cmd("deis auth:cancel --username=%s --password=%s --yes", username, password)).To(BeASuccessfulCmdWithOutput(
86-
ContainSubstring("Account cancelled"),
87-
))
89+
sess, err := start("deis auth:cancel --username=%s --password=%s --yes", username, password)
90+
Expect(err).To(BeNil())
91+
Eventually(sess).Should(gexec.Exit(0))
92+
Eventually(sess).Should(gbytes.Say("Account cancelled"))
8893
}
8994

9095
func login(url, user, password string) {
91-
Expect(cmd("deis login %s --username=%s --password=%s", url, user, password)).To(BeASuccessfulCmdWithOutput(
92-
ContainSubstring("Logged in as %s", user),
93-
))
96+
sess, err := start("deis login %s --username=%s --password=%s", url, user, password)
97+
Expect(err).To(BeNil())
98+
Eventually(sess).Should(gexec.Exit(0))
99+
Eventually(sess).Should(gbytes.Say("Logged in as %s", user))
94100
}
95101

96102
func logout() {
97-
Expect(cmd("deis auth:logout")).To(BeASuccessfulCmdWithOutput(
98-
Equal("Logged out\n"),
99-
))
103+
sess, err := start("deis auth:logout")
104+
Expect(err).To(BeNil())
105+
Eventually(sess).Should(gexec.Exit(0))
106+
Eventually(sess).Should(gbytes.Say("Logged out\n"))
100107
}
101108

102109
// execute executes the command generated by fmt.Sprintf(cmdLine, args...) and returns its output as a cmdOut structure.
103110
// this structure can then be matched upon using the SucceedWithOutput matcher below
104111
func execute(cmdLine string, args ...interface{}) (string, error) {
105-
c := cmd(cmdLine, args...)
106-
return c.stdout, c.err
112+
var stdout, stderr bytes.Buffer
113+
var cmd *exec.Cmd
114+
cmd = exec.Command("/bin/sh", "-c", fmt.Sprintf(cmdLine, args...))
115+
cmd.Stdout, cmd.Stderr = &stdout, &stderr
116+
if err := cmd.Run(); err != nil {
117+
return stderr.String(), err
118+
}
119+
return stdout.String(), nil
120+
}
121+
122+
func start(cmdLine string, args ...interface{}) (*gexec.Session, error) {
123+
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf(cmdLine, args...))
124+
return gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
107125
}
108126

109127
func createKey(name string) {
@@ -116,10 +134,14 @@ func createKey(name string) {
116134
path := path.Join(home, ".ssh", name)
117135
// create the key under ~/.ssh/<name> if it doesn't already exist
118136
if _, err := os.Stat(path); os.IsNotExist(err) {
119-
Expect(cmd("ssh-keygen -q -t rsa -b 4096 -C %s -f %s -N ''", name, path)).To(BeASuccessfulCmd())
137+
sess, err := start("ssh-keygen -q -t rsa -b 4096 -C %s -f %s -N ''", name, path)
138+
Expect(err).To(BeNil())
139+
Eventually(sess).Should(gexec.Exit(0))
120140
}
121141
// add the key to ssh-agent
122-
Expect(cmd("eval $(ssh-agent) && ssh-add %s", path)).To(BeASuccessfulCmd())
142+
sess, err := start("eval $(ssh-agent) && ssh-add %s", path)
143+
Expect(err).To(BeNil())
144+
Eventually(sess).Should(gexec.Exit(0))
123145
}
124146

125147
func getController() string {

0 commit comments

Comments
 (0)