github.com/blystad/deis@v0.11.0/tests/smoke_test.go (about) 1 // +build integration 2 3 package tests 4 5 import ( 6 "bytes" 7 "fmt" 8 "os" 9 "os/exec" 10 "path/filepath" 11 "strings" 12 "testing" 13 "text/template" 14 15 "github.com/deis/deis/tests/utils" 16 ) 17 18 // A test case is a relative directory plus a command that is expected to 19 // return 0 for success. 20 // The cmd field is run as an argument to "sh -c", so it can be arbitrarily 21 // complex. 22 type deisTest struct { 23 dir string 24 cmd string 25 } 26 27 // Tests to exercise a basic Deis workflow. 28 var smokeTests = []deisTest{ 29 // Generate and activate a new SSH key named "deis". 30 {"", ` 31 if [ ! -f {{.AuthKey}} ]; then 32 ssh-keygen -q -t rsa -f {{.AuthKey}} -N '' -C deis 33 fi 34 ssh-add {{.AuthKey}} 35 `}, 36 // Register a "test" Deis user with the CLI, or skip if already registered. 37 {"", ` 38 deis register http://deis.{{.Domain}} \ 39 --username=test \ 40 --password=asdf1234 \ 41 --email=test@test.co.nz || true 42 `}, 43 // Log in as the "test" user. 44 {"", ` 45 deis login http://deis.{{.Domain}} \ 46 --username=test \ 47 --password=asdf1234 48 `}, 49 // Add the "deis" SSH key, or skip if it's been added already. 50 {"", ` 51 deis keys:add {{.AuthKey}}.pub || true 52 `}, 53 // Destroy the "dev" cluster if it exists. 54 {"", ` 55 deis clusters:destroy dev --confirm=dev || true 56 `}, 57 // Create a cluster named "dev". 58 {"", ` 59 deis init dev {{.Domain}} --hosts={{.Hosts}} --auth={{.SSHKey}} 60 `}, 61 // Clone the example app git repository locally. 62 {"", ` 63 if [ ! -d ./{{.ExampleApp}} ]; then 64 git clone https://github.com/deis/{{.ExampleApp}}.git 65 fi 66 `}, 67 // Remove the stale "deis" git remote if it exists. 68 {"{{.ExampleApp}}", ` 69 git remote remove deis || true 70 `}, 71 // TODO: GH issue about this sleep hack 72 // Create an app named "testing". 73 {"{{.ExampleApp}}", ` 74 sleep 6 && deis apps:create testing 75 `}, 76 // git push the app to Deis 77 {"{{.ExampleApp}}", ` 78 git push deis master 79 `}, 80 // TODO: GH issue about this sleep hack 81 // Test that the app's URL responds with "Powered by Deis". 82 {"{{.ExampleApp}}", ` 83 sleep 6 && curl -s http://testing.{{.Domain}} | grep -q 'Powered by Deis' || \ 84 (curl -v http://testing.{{.Domain}} ; exit 1) 85 `}, 86 // Scale the app's web containers up to 3. 87 {"{{.ExampleApp}}", ` 88 deis scale web=3 || deis scale cmd=3 89 `}, 90 // Test that the app's URL responds with "Powered by Deis". 91 {"{{.ExampleApp}}", ` 92 sleep 7 && curl -s http://testing.{{.Domain}} | grep -q 'Powered by Deis' || \ 93 (curl -v http://testing.{{.Domain}} ; exit 1) 94 `}, 95 } 96 97 // TestSmokeExampleApp updates a Vagrant instance to run Deis with docker 98 // containers using the current codebase, then registers a user, pushes an 99 // example app, and looks for "Powered by Deis" in the HTTP response. 100 func TestSmokeExampleApp(t *testing.T) { 101 cfg := utils.GetGlobalConfig() 102 103 for _, tt := range smokeTests { 104 runTest(t, &tt, cfg) 105 } 106 } 107 108 var wd, _ = os.Getwd() 109 110 // Runs a test case and logs the results. 111 func runTest(t *testing.T, tt *deisTest, cfg *utils.DeisTestConfig) { 112 // Fill in the command string template from our test configuration. 113 var cmdBuf bytes.Buffer 114 tmpl := template.Must(template.New("cmd").Parse(tt.cmd)) 115 if err := tmpl.Execute(&cmdBuf, cfg); err != nil { 116 t.Fatal(err) 117 } 118 cmdString := cmdBuf.String() 119 // Change to the target directory if needed. 120 if tt.dir != "" { 121 // Fill in the directory template from our test configuration. 122 var dirBuf bytes.Buffer 123 tmpl := template.Must(template.New("dir").Parse(tt.dir)) 124 if err := tmpl.Execute(&dirBuf, cfg); err != nil { 125 t.Fatal(err) 126 } 127 dir, _ := filepath.Abs(filepath.Join(wd, dirBuf.String())) 128 if err := os.Chdir(dir); err != nil { 129 t.Fatal(err) 130 } 131 } 132 // Execute the command and log the input and output on error. 133 fmt.Printf("%v ... ", strings.TrimSpace(cmdString)) 134 cmd := exec.Command("sh", "-c", cmdString) 135 if out, err := cmd.Output(); err != nil { 136 t.Fatalf("%v\nOutput:\n%v", err, string(out)) 137 } else { 138 fmt.Println("ok") 139 } 140 }