github.com/deis/workflow-e2e@v2.12.2-0.20180227201524-4105be7001fe+incompatible/tests/cmd/apps/commands.go (about) 1 package apps 2 3 import ( 4 "errors" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "runtime" 9 "strings" 10 11 "github.com/deis/workflow-e2e/shims" 12 "github.com/deis/workflow-e2e/tests/cmd" 13 "github.com/deis/workflow-e2e/tests/model" 14 "github.com/deis/workflow-e2e/tests/settings" 15 16 . "github.com/onsi/gomega" 17 . "github.com/onsi/gomega/gbytes" 18 . "github.com/onsi/gomega/gexec" 19 ) 20 21 var ErrNoAppMatch = errors.New("\"No App matches the given query.\"") 22 23 // The functions in this file implement SUCCESS CASES for commonly used `deis apps` subcommands. 24 // This allows each of these to be re-used easily in multiple contexts. 25 26 // Create executes `deis apps:create` as the specified user with the specified, arvitrary options. 27 func Create(user model.User, options ...string) model.App { 28 noRemote := false 29 app := model.NewApp() 30 sess, err := cmd.Start("deis apps:create %s %s", &user, app.Name, strings.Join(options, " ")) 31 Expect(err).NotTo(HaveOccurred()) 32 sess.Wait(settings.MaxEventuallyTimeout) 33 Eventually(sess).Should(Say("created %s", app.Name)) 34 35 for _, option := range options { 36 if option == "--no-remote" { 37 noRemote = true 38 break 39 } 40 } 41 42 if noRemote { 43 Eventually(sess).Should(Say("If you want to add a git remote for this app later, use ")) 44 } else { 45 Eventually(sess).Should(Say("Git remote deis successfully created for app")) 46 } 47 Eventually(sess).Should(Exit(0)) 48 return app 49 } 50 51 // Open executes `deis apps:open` on the specified app as the specified user. A shim is used to 52 // intercept the execution of `open` (Darwin) or `xdg-open` (Linux) and verify that the browser 53 // would have navigated to the correct address. 54 func Open(user model.User, app model.App) { 55 // The underlying utility that `deis open` looks for: 56 toShim := "open" //darwin 57 if runtime.GOOS == "linux" { 58 toShim = "xdg-open" 59 } 60 myShim, err := shims.CreateSystemShim(toShim) 61 if err != nil { 62 panic(err) 63 } 64 defer shims.RemoveShim(myShim) 65 66 // Create custom env with location of open shim prepended to the PATH env var. 67 env := shims.PrependPath(os.Environ(), os.TempDir()) 68 69 sess, err := cmd.StartCmd(model.Cmd{Env: env, CommandLineString: fmt.Sprintf("DEIS_PROFILE=%s deis open -a %s", user.Username, app.Name)}) 70 Expect(err).NotTo(HaveOccurred()) 71 Eventually(sess).Should(Exit(0)) 72 73 output, err := ioutil.ReadFile(myShim.OutFile.Name()) 74 Expect(err).NotTo(HaveOccurred()) 75 Expect(strings.TrimSpace(string(output))).To(ContainSubstring(app.URL)) 76 } 77 78 // Destroy executes `deis apps:destroy` on the specified app as the specified user. 79 func Destroy(user model.User, app model.App) *Session { 80 sess, err := cmd.Start("deis apps:destroy --app=%s --confirm=%s", &user, app.Name, app.Name) 81 Expect(err).NotTo(HaveOccurred()) 82 sess.Wait(settings.MaxEventuallyTimeout) 83 Eventually(sess).Should(Say("Destroying %s...", app.Name)) 84 Eventually(sess).Should(Say(`done in `)) 85 Eventually(sess).Should(Exit(0)) 86 return sess 87 }