github.com/tmlbl/deis@v1.0.2/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  	// Clone the example app git repository locally.
    54  	{"", `
    55  if [ ! -d ./{{.ExampleApp}} ]; then
    56    git clone https://github.com/deis/{{.ExampleApp}}.git
    57  fi
    58  `},
    59  	// Remove the stale "deis" git remote if it exists.
    60  	{"{{.ExampleApp}}", `
    61  git remote remove deis || true
    62  `},
    63  	// TODO: GH issue about this sleep hack
    64  	// Create an app named "testing".
    65  	{"{{.ExampleApp}}", `
    66  sleep 6 && deis apps:create testing
    67  `},
    68  	// git push the app to Deis
    69  	{"{{.ExampleApp}}", `
    70  git push deis master
    71  `},
    72  	// TODO: GH issue about this sleep hack
    73  	// Test that the app's URL responds with "Powered by Deis".
    74  	{"{{.ExampleApp}}", `
    75  sleep 6 && curl -s http://testing.{{.Domain}} | grep -q 'Powered by Deis' || \
    76  	(curl -v http://testing.{{.Domain}} ; exit 1)
    77  `},
    78  	// Scale the app's web containers up to 3.
    79  	{"{{.ExampleApp}}", `
    80  deis scale web=3 || deis scale cmd=3
    81  `},
    82  	// Test that the app's URL responds with "Powered by Deis".
    83  	{"{{.ExampleApp}}", `
    84  sleep 7 && curl -s http://testing.{{.Domain}} | grep -q 'Powered by Deis' || \
    85  	(curl -v http://testing.{{.Domain}} ; exit 1)
    86  `},
    87  }
    88  
    89  // TestSmokeExampleApp updates a Vagrant instance to run Deis with docker
    90  // containers using the current codebase, then registers a user, pushes an
    91  // example app, and looks for "Powered by Deis" in the HTTP response.
    92  func TestSmokeExampleApp(t *testing.T) {
    93  	cfg := utils.GetGlobalConfig()
    94  
    95  	for _, tt := range smokeTests {
    96  		runTest(t, &tt, cfg)
    97  	}
    98  }
    99  
   100  var wd, _ = os.Getwd()
   101  
   102  // Runs a test case and logs the results.
   103  func runTest(t *testing.T, tt *deisTest, cfg *utils.DeisTestConfig) {
   104  	// Fill in the command string template from our test configuration.
   105  	var cmdBuf bytes.Buffer
   106  	tmpl := template.Must(template.New("cmd").Parse(tt.cmd))
   107  	if err := tmpl.Execute(&cmdBuf, cfg); err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	cmdString := cmdBuf.String()
   111  	// Change to the target directory if needed.
   112  	if tt.dir != "" {
   113  		// Fill in the directory template from our test configuration.
   114  		var dirBuf bytes.Buffer
   115  		tmpl := template.Must(template.New("dir").Parse(tt.dir))
   116  		if err := tmpl.Execute(&dirBuf, cfg); err != nil {
   117  			t.Fatal(err)
   118  		}
   119  		dir, _ := filepath.Abs(filepath.Join(wd, dirBuf.String()))
   120  		if err := os.Chdir(dir); err != nil {
   121  			t.Fatal(err)
   122  		}
   123  	}
   124  	// Execute the command and log the input and output on error.
   125  	fmt.Printf("%v ... ", strings.TrimSpace(cmdString))
   126  	cmd := exec.Command("sh", "-c", cmdString)
   127  	if out, err := cmd.Output(); err != nil {
   128  		t.Fatalf("%v\nOutput:\n%v", err, string(out))
   129  	} else {
   130  		fmt.Println("ok")
   131  	}
   132  }