github.com/blystad/deis@v0.11.0/tests/utils/itutils.go (about)

     1  package utils
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"math/rand"
     8  	"net/http"
     9  	"os"
    10  	"os/exec"
    11  	"strings"
    12  	"testing"
    13  	"text/template"
    14  	"time"
    15  
    16  	"github.com/ThomasRooney/gexpect"
    17  )
    18  
    19  // Deis points to the CLI used to run tests.
    20  var Deis = "deis "
    21  
    22  // DeisTestConfig allows tests to be repeated against different
    23  // targets, with different example apps, using specific credentials, and so on.
    24  type DeisTestConfig struct {
    25  	AuthKey     string
    26  	Hosts       string
    27  	Domain      string
    28  	SSHKey      string
    29  	ClusterName string
    30  	UserName    string
    31  	Password    string
    32  	Email       string
    33  	ExampleApp  string
    34  	AppName     string
    35  	ProcessNum  string
    36  	ImageID     string
    37  	Version     string
    38  	AppUser     string
    39  }
    40  
    41  // randomApp is used for the test run if DEIS_TEST_APP isn't set
    42  var randomApp = GetRandomApp()
    43  
    44  // GetGlobalConfig returns a test configuration object.
    45  func GetGlobalConfig() *DeisTestConfig {
    46  	authKey := os.Getenv("DEIS_TEST_AUTH_KEY")
    47  	if authKey == "" {
    48  		authKey = "deis"
    49  	}
    50  	hosts := os.Getenv("DEIS_TEST_HOSTS")
    51  	if hosts == "" {
    52  		hosts = "172.17.8.100"
    53  	}
    54  	domain := os.Getenv("DEIS_TEST_DOMAIN")
    55  	if domain == "" {
    56  		domain = "local.deisapp.com"
    57  	}
    58  	sshKey := os.Getenv("DEIS_TEST_SSH_KEY")
    59  	if sshKey == "" {
    60  		sshKey = "~/.vagrant.d/insecure_private_key"
    61  	}
    62  	exampleApp := os.Getenv("DEIS_TEST_APP")
    63  	if exampleApp == "" {
    64  		exampleApp = randomApp
    65  	}
    66  	var envCfg = DeisTestConfig{
    67  		AuthKey:     authKey,
    68  		Hosts:       hosts,
    69  		Domain:      domain,
    70  		SSHKey:      sshKey,
    71  		ClusterName: "dev",
    72  		UserName:    "test",
    73  		Password:    "asdf1234",
    74  		Email:       "test@test.co.nz",
    75  		ExampleApp:  exampleApp,
    76  		AppName:     "sample",
    77  		ProcessNum:  "2",
    78  		ImageID:     "buildtest",
    79  		Version:     "2",
    80  		AppUser:     "test1",
    81  	}
    82  	return &envCfg
    83  }
    84  
    85  // Curl connects to a Deis endpoint to see if the example app is running.
    86  func Curl(t *testing.T, params *DeisTestConfig) {
    87  	url := "http://" + params.AppName + "." + params.Domain
    88  	response, err := http.Get(url)
    89  	if err != nil {
    90  		t.Fatalf("not reachable:\n%v", err)
    91  	}
    92  	body, err := ioutil.ReadAll(response.Body)
    93  	fmt.Println(string(body))
    94  	if !strings.Contains(string(body), "Powered by Deis") {
    95  		t.Fatalf("App not started")
    96  	}
    97  }
    98  
    99  // AuthCancel tests whether `deis auth:cancel` destroys a user's account.
   100  func AuthCancel(t *testing.T, params *DeisTestConfig) {
   101  	fmt.Println("deis auth:cancel")
   102  	child, err := gexpect.Spawn(Deis + " auth:cancel")
   103  	if err != nil {
   104  		t.Fatalf("command not started\n%v", err)
   105  	}
   106  	fmt.Println("username:")
   107  	err = child.Expect("username:")
   108  	if err != nil {
   109  		t.Fatalf("expect username failed\n%v", err)
   110  	}
   111  	child.SendLine(params.UserName)
   112  	fmt.Print("password:")
   113  	err = child.Expect("password:")
   114  	if err != nil {
   115  		t.Fatalf("expect password failed\n%v", err)
   116  	}
   117  	child.SendLine(params.Password)
   118  	err = child.ExpectRegex("(y/n)")
   119  	if err != nil {
   120  		t.Fatalf("expect cancel \n%v", err)
   121  	}
   122  	child.SendLine("y")
   123  	err = child.Expect("Account cancelled")
   124  	if err != nil {
   125  		t.Fatalf("command executiuon failed\n%v", err)
   126  	}
   127  	child.Close()
   128  
   129  }
   130  
   131  // CheckList executes a command and optionally tests whether its output does
   132  // or does not contain a given string.
   133  func CheckList(
   134  	t *testing.T, cmd string, params interface{}, contain string, notflag bool) {
   135  	var cmdBuf bytes.Buffer
   136  	tmpl := template.Must(template.New("cmd").Parse(cmd))
   137  	if err := tmpl.Execute(&cmdBuf, params); err != nil {
   138  		t.Fatal(err)
   139  	}
   140  	cmdString := cmdBuf.String()
   141  	fmt.Println(cmdString)
   142  	var cmdl *exec.Cmd
   143  	if strings.Contains(cmd, "cat") {
   144  		cmdl = exec.Command("sh", "-c", cmdString)
   145  	} else {
   146  		cmdl = exec.Command("sh", "-c", Deis+cmdString)
   147  	}
   148  	stdout, _, err := RunCommandWithStdoutStderr(cmdl)
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  	if strings.Contains(stdout.String(), contain) == notflag {
   153  		if notflag {
   154  			t.Fatalf(
   155  				"Didn't expect '%s' in command output:\n%v", contain, stdout)
   156  		}
   157  		t.Fatalf("Expected '%s' in command output:\n%v", contain, stdout)
   158  	}
   159  }
   160  
   161  // Execute takes command string and parameters required to execute the command,
   162  // a failflag to check whether the command is expected to fail, and an expect
   163  // string to check whether the command has failed according to failflag.
   164  //
   165  // If failflag is true and the command failed, check the stdout and stderr for
   166  // the expect string.
   167  func Execute(t *testing.T, cmd string, params interface{}, failFlag bool, expect string) {
   168  	var cmdBuf bytes.Buffer
   169  	tmpl := template.Must(template.New("cmd").Parse(cmd))
   170  	if err := tmpl.Execute(&cmdBuf, params); err != nil {
   171  		t.Fatal(err)
   172  	}
   173  	cmdString := cmdBuf.String()
   174  	fmt.Println(cmdString)
   175  	var cmdl *exec.Cmd
   176  	if strings.Contains(cmd, "git") {
   177  		cmdl = exec.Command("sh", "-c", cmdString)
   178  	} else {
   179  		cmdl = exec.Command("sh", "-c", Deis+cmdString)
   180  	}
   181  
   182  	switch failFlag {
   183  	case true:
   184  		if stdout, stderr, err := RunCommandWithStdoutStderr(cmdl); err != nil {
   185  			if strings.Contains(stdout.String(), expect) || strings.Contains(stderr.String(), expect) {
   186  				fmt.Println("(Error expected...ok)")
   187  			} else {
   188  				t.Fatal(err)
   189  			}
   190  		} else {
   191  			if strings.Contains(stdout.String(), expect) || strings.Contains(stderr.String(), expect) {
   192  				fmt.Println("(Error expected...ok)" + expect)
   193  			} else {
   194  				t.Fatal(err)
   195  			}
   196  		}
   197  	case false:
   198  		if _, _, err := RunCommandWithStdoutStderr(cmdl); err != nil {
   199  			t.Fatal(err)
   200  		} else {
   201  			fmt.Println("ok")
   202  		}
   203  	}
   204  }
   205  
   206  // AppsDestroyTest destroys a Deis app and checks that it was successful.
   207  func AppsDestroyTest(t *testing.T, params *DeisTestConfig) {
   208  	cmd := "apps:destroy --app={{.AppName}} --confirm={{.AppName}}"
   209  	if err := Chdir(params.ExampleApp); err != nil {
   210  		t.Fatal(err)
   211  	}
   212  	Execute(t, cmd, params, false, "")
   213  	if err := Chdir(".."); err != nil {
   214  		t.Fatal(err)
   215  	}
   216  	if err := Rmdir(params.ExampleApp); err != nil {
   217  		t.Fatal(err)
   218  	}
   219  }
   220  
   221  // GetRandomApp returns a known working example app at random for testing.
   222  func GetRandomApp() string {
   223  	rand.Seed(int64(time.Now().Unix()))
   224  	apps := []string{
   225  		"example-clojure-ring",
   226  		// "example-dart",
   227  		"example-dockerfile-python",
   228  		"example-go",
   229  		"example-java-jetty",
   230  		"example-nodejs-express",
   231  		// "example-php",
   232  		"example-play",
   233  		"example-python-django",
   234  		"example-python-flask",
   235  		"example-ruby-sinatra",
   236  		"example-scala",
   237  	}
   238  	return apps[rand.Intn(len(apps))]
   239  }