Skip to content

Commit b071e08

Browse files
committed
fix(tests): allow env vars to override important test settings
1 parent d21fc37 commit b071e08

4 files changed

Lines changed: 117 additions & 94 deletions

File tree

tests/builds_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func buildsListTest(t *testing.T, params *itutils.DeisTestConfig) {
5858
if stdout, _, err := utils.RunCommandWithStdoutStderr(cmdl); err != nil {
5959
t.Fatalf("Failed:\n%v", err)
6060
} else {
61-
ImageId := strings.Split(stdout.String(), "\n")[2]
62-
params.ImageId = strings.Fields(ImageId)[0]
61+
ImageID := strings.Split(stdout.String(), "\n")[2]
62+
params.ImageID = strings.Fields(ImageID)[0]
6363
}
6464

6565
}

tests/integration-utils/itutils.go

Lines changed: 87 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,98 @@ package itutils
33
import (
44
"bytes"
55
"fmt"
6-
"github.com/ThomasRooney/gexpect"
7-
gson "github.com/bitly/go-simplejson"
8-
"github.com/deis/deis/tests/utils"
96
"io/ioutil"
107
"math/rand"
118
"net/http"
9+
"os"
1210
"os/exec"
1311
"strings"
1412
"testing"
1513
"text/template"
1614
"time"
15+
16+
"github.com/ThomasRooney/gexpect"
17+
gson "github.com/bitly/go-simplejson"
18+
"github.com/deis/deis/tests/utils"
1719
)
1820

21+
// Deis points to the CLI used to run tests.
1922
var Deis = "deis "
2023

24+
// DeisTestConfig holds paramters needed to run tests.
2125
type DeisTestConfig struct {
22-
AuthKey string
23-
Hosts string
24-
HostName string
25-
SshKey string
26-
ClusterName string
27-
UserName string
28-
Password string
29-
Email string
30-
UpdatedHosts string
31-
ExampleApp string
32-
AppName string
33-
ProcessNum string
34-
ImageId string
35-
Version string
36-
AppUser string
26+
AuthKey string
27+
Hosts string
28+
Domain string
29+
SSHKey string
30+
ClusterName string
31+
UserName string
32+
Password string
33+
Email string
34+
ExampleApp string
35+
AppName string
36+
ProcessNum string
37+
ImageID string
38+
Version string
39+
AppUser string
3740
}
3841

42+
// GetGlobalConfig returns a test configuration object.
3943
func GetGlobalConfig() *DeisTestConfig {
44+
authKey := os.Getenv("AUTH_KEY")
45+
if authKey == "" {
46+
authKey = "deis"
47+
}
48+
hosts := os.Getenv("DEIS_TEST_HOSTS")
49+
if hosts == "" {
50+
hosts = "172.17.8.100"
51+
}
52+
domain := os.Getenv("DEIS_TEST_DOMAIN")
53+
if domain == "" {
54+
domain = "local.deisapp.com"
55+
}
56+
sshKey := os.Getenv("DEIS_TEST_SSH_KEY")
57+
if sshKey == "" {
58+
sshKey = "~/.vagrant.d/insecure_private_key"
59+
}
60+
exampleApp := os.Getenv("DEIS_TEST_APP")
61+
if exampleApp == "" {
62+
exampleApp = "example-go"
63+
}
4064
var envCfg = DeisTestConfig{
41-
"deis",
42-
"172.17.8.100",
43-
"local.deisapp.com",
44-
"~/.vagrant.d/insecure_private_key",
45-
"dev",
46-
"test",
47-
"asdf1234",
48-
"test@test.co.nz",
49-
"172.17.8.100",
50-
"example-go",
51-
"sample",
52-
"2",
53-
"buildtest",
54-
"2",
55-
"test1",
65+
AuthKey: authKey,
66+
Hosts: hosts,
67+
Domain: domain,
68+
SSHKey: sshKey,
69+
ClusterName: "dev",
70+
UserName: "test",
71+
Password: "asdf1234",
72+
Email: "test@test.co.nz",
73+
ExampleApp: exampleApp,
74+
AppName: "sample",
75+
ProcessNum: "2",
76+
ImageID: "buildtest",
77+
Version: "2",
78+
AppUser: "test1",
5679
}
5780
return &envCfg
5881
}
5982

60-
//Tests example apps are running or not
61-
83+
// Curl connects to a Deis endpoint to see if the example app is running.
6284
func Curl(t *testing.T, params *DeisTestConfig) {
63-
url := "http://" + params.AppName + "." + params.HostName
85+
url := "http://" + params.AppName + "." + params.Domain
6486
response, err := http.Get(url)
6587
if err != nil {
6688
t.Fatalf("not reachable:\n%v", err)
6789
}
6890
body, err := ioutil.ReadAll(response.Body)
6991
fmt.Println(string(body))
70-
if params.AppName == "example-python-django" {
71-
if !strings.Contains(string(body), "Powered by django") {
72-
t.Fatalf("App not started")
73-
}
74-
} else if !strings.Contains(string(body), "Powered by Deis") {
92+
if !strings.Contains(string(body), "Powered by Deis") {
7593
t.Fatalf("App not started")
7694
}
7795
}
7896

79-
//gexpect implementation of auth cancel
80-
97+
// AuthCancel tests whether `deis auth:cancel` destroys a user's account.
8198
func AuthCancel(t *testing.T, params *DeisTestConfig) {
8299
fmt.Println("deis auth:cancel")
83100
child, err := gexpect.Spawn(Deis + " auth:cancel")
@@ -109,9 +126,8 @@ func AuthCancel(t *testing.T, params *DeisTestConfig) {
109126

110127
}
111128

112-
/*CheckList takes config , command to execute and contain string and notflag .
113-
* Executes the command and checks if the contain string should be present or not according to notflag */
114-
129+
// CheckList executes a command and optionally tests whether its output contains
130+
// a given string.
115131
func CheckList(t *testing.T, params interface{}, cmd, contain string, notflag bool) {
116132
var cmdBuf bytes.Buffer
117133
tmpl := template.Must(template.New("cmd").Parse(cmd))
@@ -137,14 +153,12 @@ func CheckList(t *testing.T, params interface{}, cmd, contain string, notflag bo
137153
}
138154
}
139155

140-
/***Execute function takes command string and parameters required to execute the command
141-
A failflag to check whether the command is expected to fail
142-
An expect string to check whether the command has failed according to failflag
143-
144-
If a failflag is true and command failed we check the stdout and stderr for expect string
145-
146-
***/
147-
156+
// Execute takes command string and parameters required to execute the command,
157+
// a failflag to check whether the command is expected to fail, and an expect
158+
// string to check whether the command has failed according to failflag.
159+
//
160+
// If failflag is true and the command failed, check the stdout and stderr for
161+
// the expect string.
148162
func Execute(t *testing.T, cmd string, params interface{}, failFlag bool, expect string) {
149163
var cmdBuf bytes.Buffer
150164
tmpl := template.Must(template.New("cmd").Parse(cmd))
@@ -184,8 +198,7 @@ func Execute(t *testing.T, cmd string, params interface{}, failFlag bool, expect
184198
}
185199
}
186200

187-
//Destroys an app after execution of each integration test
188-
201+
// AppsDestroyTest destroys a Deis app and checks that it was successful.
189202
func AppsDestroyTest(t *testing.T, params *DeisTestConfig) {
190203
cmd := GetCommand("apps", "destroy")
191204
if err := utils.Chdir(params.ExampleApp); err != nil {
@@ -200,29 +213,29 @@ func AppsDestroyTest(t *testing.T, params *DeisTestConfig) {
200213
}
201214
}
202215

203-
//Fetch commands from testconfig.json
204-
216+
// GetCommand fetches the given command by type and name from a JSON resource.
205217
func GetCommand(cmdtype, cmd string) string {
206218
js, _ := gson.NewJson(utils.GetFileBytes("testconfig.json"))
207219
command, _ := js.Get("commands").Get(cmdtype).Get(cmd).String()
208220
return command
209221
}
210222

211-
//Selects a random app
212-
223+
// GetRandomApp returns a known working example app at random for testing.
213224
func GetRandomApp() string {
214-
s1 := rand.NewSource(int64(time.Now().Unix()))
215-
r1 := rand.New(s1)
216-
appmap := make(map[int]string)
217-
appmap[0] = "example-go"
218-
appmap[1] = "example-ruby-sinatra"
219-
appmap[2] = "example-java-jetty"
220-
appmap[3] = "example-nodejs-express"
221-
appmap[4] = "example-python-flask"
222-
appmap[5] = "example-dockerfile-python"
223-
appmap[6] = "example-scala"
224-
appmap[7] = "example-clojure-ring"
225-
appmap[8] = "example-python-django"
226-
app := appmap[r1.Intn(8)]
227-
return app
225+
rand.Seed(int64(time.Now().Unix()))
226+
apps := []string{
227+
"example-clojure-ring",
228+
// "example-dart",
229+
"example-dockerfile-python",
230+
"example-go",
231+
"example-java-jetty",
232+
"example-nodejs-express",
233+
// "example-php",
234+
"example-play",
235+
"example-python-django",
236+
"example-python-flask",
237+
"example-ruby-sinatra",
238+
"example-scala",
239+
}
240+
return apps[rand.Intn(len(apps))]
228241
}

tests/testconfig.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"commands": {
33
"auth": {
4-
"register":"register http://deis.{{.HostName}} --username={{.UserName}} --password={{.Password}} --email={{.Email}}",
5-
"login": "auth:login http://deis.{{.HostName}} --username={{.UserName}} --password={{.Password}}",
4+
"register":"register http://deis.{{.Domain}} --username={{.UserName}} --password={{.Password}} --email={{.Email}}",
5+
"login": "auth:login http://deis.{{.Domain}} --username={{.UserName}} --password={{.Password}}",
66
"logout": "auth:logout",
77
"cancel":"auth:cancel"
88
},
@@ -12,9 +12,9 @@
1212
"remove": "keys:remove {{.AuthKey}} ||true"
1313
},
1414
"clusters": {
15-
"create": "clusters:create {{.ClusterName}} {{.HostName}} --hosts={{.Hosts}} --auth={{.SshKey}}",
15+
"create": "clusters:create {{.ClusterName}} {{.Domain}} --hosts={{.Hosts}} --auth={{.SSHKey}}",
1616
"list": "clusters:list",
17-
"update": "clusters:update {{.ClusterName}} --domain={{.HostName}} --hosts={{.UpdatedHosts}} --auth=~/.ssh/{{.AuthKey}}",
17+
"update": "clusters:update {{.ClusterName}} --domain={{.Domain}} --hosts={{.Hosts}} --auth=~/.ssh/{{.AuthKey}}",
1818
"info": "clusters:info {{.ClusterName}}",
1919
"destroy" : "clusters:destroy {{.ClusterName}} --confirm={{.ClusterName}}"
2020
},
@@ -33,7 +33,7 @@
3333
},
3434
"builds":{
3535
"list" : "builds:list --app={{.AppName}}",
36-
"create" : "builds:create {{.ImageId}} --app={{.AppName}}"
36+
"create" : "builds:create {{.ImageID}} --app={{.AppName}}"
3737
},
3838
"releases":{
3939
"list" : "releases:list --app={{.AppName}}",

tests/utils/utils.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import (
44
_ "bufio"
55
"bytes"
66
"fmt"
7-
"github.com/satori/go.uuid"
87
"io"
98
"net"
109
"os"
11-
"path/filepath"
1210
"os/exec"
11+
"path/filepath"
1312
"strings"
1413
"syscall"
1514
"testing"
1615
"time"
16+
17+
"github.com/satori/go.uuid"
1718
)
1819

1920
// NewUuid returns a new V4-style unique identifier.
@@ -23,6 +24,7 @@ func NewUuid() string {
2324
return strings.Split(s1, "-")[0]
2425
}
2526

27+
// GetFileBytes returns a byte array of the contents of a file.
2628
func GetFileBytes(filename string) []byte {
2729
file, _ := os.Open(filename)
2830
defer file.Close()
@@ -32,31 +34,35 @@ func GetFileBytes(filename string) []byte {
3234
return bs
3335
}
3436

35-
func CreateFile(name string) error {
36-
fo, err := os.Create(name)
37-
if err != nil {
38-
return err
39-
}
40-
defer fo.Close()
41-
return nil
37+
// CreateFile creates an empty file at the specified path.
38+
func CreateFile(path string) error {
39+
fo, err := os.Create(path)
40+
if err != nil {
41+
return err
42+
}
43+
defer fo.Close()
44+
return nil
4245
}
4346

44-
func Chdir( app string ) error {
47+
// Chdir sets the current working directory to the relative path specified.
48+
func Chdir(app string) error {
4549
var wd, _ = os.Getwd()
46-
dir, _ := filepath.Abs(filepath.Join(wd,app))
50+
dir, _ := filepath.Abs(filepath.Join(wd, app))
4751
err := os.Chdir(dir)
4852
fmt.Println(dir)
4953
return err
5054
}
5155

52-
func Rmdir( app string ) error {
56+
// Rmdir removes a directory and its contents.
57+
func Rmdir(app string) error {
5358
var wd, _ = os.Getwd()
54-
dir, _ := filepath.Abs(filepath.Join(wd,app))
59+
dir, _ := filepath.Abs(filepath.Join(wd, app))
5560
err := os.RemoveAll(dir)
5661
fmt.Println(dir)
5762
return err
5863
}
5964

65+
// GetUserDetails returns sections of a UUID.
6066
func GetUserDetails() (string, string) {
6167
u1 := uuid.NewV4()
6268
s1 := fmt.Sprintf("%s", u1)
@@ -73,6 +79,7 @@ func GetHostOs() string {
7379
return "ubuntu"
7480
}
7581

82+
// GetHostIPAddress returns the host IP for accessing etcd and Deis services.
7683
func GetHostIPAddress() string {
7784
IP := os.Getenv("HOST_IPADDR")
7885
if IP == "" {
@@ -81,6 +88,7 @@ func GetHostIPAddress() string {
8188
return IP
8289
}
8390

91+
// Append grows a string array by appending a new element.
8492
func Append(slice []string, data string) []string {
8593
m := len(slice)
8694
n := m + 1
@@ -95,6 +103,7 @@ func Append(slice []string, data string) []string {
95103
return slice
96104
}
97105

106+
// GetRandomPort returns an unused TCP listen port on the host.
98107
func GetRandomPort() string {
99108
l, _ := net.Listen("tcp", "127.0.0.1:0") // listen on localhost
100109
defer l.Close()
@@ -112,6 +121,7 @@ func getExitCode(err error) (int, error) {
112121
return exitCode, fmt.Errorf("failed to get exit code")
113122
}
114123

124+
// RunCommandWithStdoutStderr execs a command and returns its output.
115125
func RunCommandWithStdoutStderr(cmd *exec.Cmd) (bytes.Buffer, bytes.Buffer, error) {
116126
var stdout, stderr bytes.Buffer
117127
stderrPipe, err := cmd.StderrPipe()

0 commit comments

Comments
 (0)