@@ -3,81 +3,98 @@ package itutils
33import (
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.
1922var Deis = "deis "
2023
24+ // DeisTestConfig holds paramters needed to run tests.
2125type 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.
3943func 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.
6284func 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.
8198func 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.
115131func 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.
148162func 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.
189202func 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.
205217func 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.
213224func 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}
0 commit comments