github.com/miketheprogrammer/deis@v1.12.2/tests/apps_test.go (about)

     1  // +build integration
     2  
     3  package tests
     4  
     5  import (
     6  	"fmt"
     7  	"math/rand"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/deis/deis/tests/utils"
    12  )
    13  
    14  var (
    15  	appsCreateCmd          = "apps:create {{.AppName}}"
    16  	appsCreateCmdNoRemote  = "apps:create {{.AppName}} --no-remote"
    17  	appsCreateCmdBuildpack = "apps:create {{.AppName}} --buildpack https://example.com"
    18  	appsListCmd            = "apps:list"
    19  	appsRunCmd             = "apps:run echo Hello, 世界"
    20  	appsOpenCmd            = "apps:open --app={{.AppName}}"
    21  	appsLogsCmd            = "apps:logs --app={{.AppName}}"
    22  	appsLogsLimitCmd       = "apps:logs --app={{.AppName}} -n 1"
    23  	appsInfoCmd            = "apps:info --app={{.AppName}}"
    24  	appsDestroyCmd         = "apps:destroy --app={{.AppName}} --confirm={{.AppName}}"
    25  	appsDestroyCmdNoApp    = "apps:destroy --confirm={{.AppName}}"
    26  	appsTransferCmd        = "apps:transfer {{.NewOwner}} --app={{.AppName}}"
    27  )
    28  
    29  func randomString(n int) string {
    30  	var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    31  	b := make([]rune, n)
    32  	for i := range b {
    33  		b[i] = letters[rand.Intn(len(letters))]
    34  	}
    35  	return string(b)
    36  }
    37  
    38  func TestApps(t *testing.T) {
    39  	params := appsSetup(t)
    40  	appsCreateTest(t, params)
    41  	appsListTest(t, params, false)
    42  	appsLogsTest(t, params)
    43  	appsInfoTest(t, params)
    44  	appsRunTest(t, params)
    45  	appsOpenTest(t, params)
    46  	appsDestroyTest(t, params)
    47  	appsListTest(t, params, true)
    48  	appsTransferTest(t, params)
    49  }
    50  
    51  func appsSetup(t *testing.T) *utils.DeisTestConfig {
    52  	cfg := utils.GetGlobalConfig()
    53  	cfg.AppName = "appssample"
    54  	utils.Execute(t, authLoginCmd, cfg, false, "")
    55  	utils.Execute(t, gitCloneCmd, cfg, false, "")
    56  	return cfg
    57  }
    58  
    59  func appsCreateTest(t *testing.T, params *utils.DeisTestConfig) {
    60  	wd, _ := os.Getwd()
    61  	defer os.Chdir(wd)
    62  	if err := utils.Chdir(params.ExampleApp); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	// TODO: move --buildpack to client unit tests
    66  	utils.Execute(t, appsCreateCmdBuildpack, params, false, "BUILDPACK_URL")
    67  	utils.Execute(t, appsDestroyCmdNoApp, params, false, "")
    68  	utils.Execute(t, appsCreateCmd, params, false, "")
    69  	utils.Execute(t, appsCreateCmd, params, true, "This field must be unique.")
    70  }
    71  
    72  func appsDestroyTest(t *testing.T, params *utils.DeisTestConfig) {
    73  	if err := utils.Chdir(params.ExampleApp); err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	utils.Execute(t, appsDestroyCmd, params, false, "")
    77  	if err := utils.Chdir(".."); err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	if err := utils.Rmdir(params.ExampleApp); err != nil {
    81  		t.Fatal(err)
    82  	}
    83  }
    84  
    85  func appsInfoTest(t *testing.T, params *utils.DeisTestConfig) {
    86  	utils.Execute(t, appsInfoCmd, params, false, "")
    87  }
    88  
    89  func appsListTest(t *testing.T, params *utils.DeisTestConfig, notflag bool) {
    90  	utils.CheckList(t, appsListCmd, params, params.AppName, notflag)
    91  }
    92  
    93  func appsLogsTest(t *testing.T, params *utils.DeisTestConfig) {
    94  	cmd := appsLogsCmd
    95  	// test for application lifecycle logs
    96  	utils.Execute(t, cmd, params, false, "204 NO CONTENT")
    97  	if err := utils.Chdir(params.ExampleApp); err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	utils.Execute(t, gitPushCmd, params, false, "")
   101  	utils.CurlApp(t, *params)
   102  	utils.Execute(t, cmd, params, false, "created initial release")
   103  	utils.Execute(t, cmd, params, false, "listening on 5000...")
   104  
   105  	utils.Execute(t, appsLogsLimitCmd, params, false, "")
   106  
   107  	if err := utils.Chdir(".."); err != nil {
   108  		t.Fatal(err)
   109  	}
   110  }
   111  
   112  func appsOpenTest(t *testing.T, params *utils.DeisTestConfig) {
   113  	utils.CurlApp(t, *params)
   114  	utils.CurlWithFail(t, fmt.Sprintf("http://%s.%s", "this-app-does-not-exist", params.Domain), true, "404 Not Found")
   115  }
   116  
   117  func appsRunTest(t *testing.T, params *utils.DeisTestConfig) {
   118  	cmd := appsRunCmd
   119  	if err := utils.Chdir(params.ExampleApp); err != nil {
   120  		t.Fatal(err)
   121  	}
   122  	utils.CheckList(t, cmd, params, "Hello, 世界", false)
   123  	utils.Execute(t, "apps:run env", params, true, "GIT_SHA")
   124  	// run a REALLY large command to test https://github.com/deis/deis/issues/2046
   125  	largeString := randomString(1024)
   126  	utils.Execute(t, "apps:run echo "+largeString, params, false, largeString)
   127  	if err := utils.Chdir(".."); err != nil {
   128  		t.Fatal(err)
   129  	}
   130  	utils.Execute(t, cmd, params, true, "Not found")
   131  }
   132  
   133  func appsTransferTest(t *testing.T, params *utils.DeisTestConfig) {
   134  	user := utils.GetGlobalConfig()
   135  	user.UserName, user.Password = "app-transfer-test", "test"
   136  	user.AppName = "transfer-test"
   137  	user.NewOwner = params.UserName
   138  	utils.Execute(t, authRegisterCmd, user, false, "")
   139  	utils.Execute(t, authLoginCmd, user, false, "")
   140  	utils.Execute(t, appsCreateCmdNoRemote, user, false, "")
   141  	utils.Execute(t, appsTransferCmd, user, false, "")
   142  	utils.Execute(t, appsInfoCmd, user, true, "403 FORBIDDEN")
   143  	utils.Execute(t, authLoginCmd, params, false, "")
   144  	params.AppName = user.AppName
   145  	utils.CheckList(t, appsInfoCmd, params, params.UserName, false)
   146  }