github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/integration/isolated/run_task_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("run-task command", func() {
    14  	Context("when --help flag is set", func() {
    15  		It("Displays command usage to output", func() {
    16  			session := helpers.CF("run-task", "--help")
    17  			Eventually(session).Should(Say("NAME:"))
    18  			Eventually(session).Should(Say("   run-task - Run a one-off task on an app"))
    19  			Eventually(session).Should(Say("USAGE:"))
    20  			Eventually(session).Should(Say("   cf run-task APP_NAME COMMAND \\[-k DISK] \\[-m MEMORY\\] \\[--name TASK_NAME\\]"))
    21  			Eventually(session).Should(Say("TIP:"))
    22  			Eventually(session).Should(Say("   Use 'cf logs' to display the logs of the app and all its tasks. If your task name is unique, grep this command's output for the task name to view task-specific logs."))
    23  			Eventually(session).Should(Say("EXAMPLES:"))
    24  			Eventually(session).Should(Say(`   cf run-task my-app "bundle exec rake db:migrate" --name migrate`))
    25  			Eventually(session).Should(Say("ALIAS:"))
    26  			Eventually(session).Should(Say("   rt"))
    27  			Eventually(session).Should(Say("OPTIONS:"))
    28  			Eventually(session).Should(Say("   -k          Disk limit \\(e\\.g\\. 256M, 1024M, 1G\\)"))
    29  			Eventually(session).Should(Say("   -m          Memory limit \\(e\\.g\\. 256M, 1024M, 1G\\)"))
    30  			Eventually(session).Should(Say("   --name      Name to give the task \\(generated if omitted\\)"))
    31  			Eventually(session).Should(Say("SEE ALSO:"))
    32  			Eventually(session).Should(Say("   logs, tasks, terminate-task"))
    33  			Eventually(session).Should(Exit(0))
    34  		})
    35  	})
    36  
    37  	Context("when the environment is not setup correctly", func() {
    38  		Context("when no API endpoint is set", func() {
    39  			BeforeEach(func() {
    40  				helpers.UnsetAPI()
    41  			})
    42  
    43  			It("fails with no API endpoint set message", func() {
    44  				session := helpers.CF("run-task", "app-name", "some command")
    45  				Eventually(session).Should(Say("FAILED"))
    46  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    47  				Eventually(session).Should(Exit(1))
    48  			})
    49  		})
    50  
    51  		Context("when not logged in", func() {
    52  			BeforeEach(func() {
    53  				helpers.LogoutCF()
    54  			})
    55  
    56  			It("fails with not logged in message", func() {
    57  				session := helpers.CF("run-task", "app-name", "some command")
    58  				Eventually(session).Should(Say("FAILED"))
    59  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    60  				Eventually(session).Should(Exit(1))
    61  			})
    62  		})
    63  
    64  		Context("when there no org set", func() {
    65  			BeforeEach(func() {
    66  				helpers.LogoutCF()
    67  				helpers.LoginCF()
    68  			})
    69  
    70  			It("fails with no targeted org error message", func() {
    71  				session := helpers.CF("run-task", "app-name", "some command")
    72  				Eventually(session).Should(Say("FAILED"))
    73  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
    74  				Eventually(session).Should(Exit(1))
    75  			})
    76  		})
    77  
    78  		Context("when there no space set", func() {
    79  			BeforeEach(func() {
    80  				helpers.LogoutCF()
    81  				helpers.LoginCF()
    82  				helpers.TargetOrg(ReadOnlyOrg)
    83  			})
    84  
    85  			It("fails with no space targeted error message", func() {
    86  				session := helpers.CF("run-task", "app-name", "some command")
    87  				Eventually(session).Should(Say("FAILED"))
    88  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space"))
    89  				Eventually(session).Should(Exit(1))
    90  			})
    91  		})
    92  	})
    93  
    94  	Context("when the environment is setup correctly", func() {
    95  		var (
    96  			orgName   string
    97  			spaceName string
    98  			appName   string
    99  		)
   100  
   101  		BeforeEach(func() {
   102  			orgName = helpers.NewOrgName()
   103  			spaceName = helpers.NewSpaceName()
   104  			appName = helpers.PrefixedRandomName("APP")
   105  
   106  			setupCF(orgName, spaceName)
   107  		})
   108  
   109  		Context("when the application exists", func() {
   110  			BeforeEach(func() {
   111  				helpers.WithHelloWorldApp(func(appDir string) {
   112  					Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   113  				})
   114  			})
   115  
   116  			Context("when the task name is not provided", func() {
   117  				It("creates a new task", func() {
   118  					session := helpers.CF("run-task", appName, "echo hi")
   119  					userName, _ := helpers.GetCredentials()
   120  					Eventually(session).Should(Say("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   121  					Eventually(session).Should(Say("OK"))
   122  					Eventually(session).Should(Say("Task has been submitted successfully for execution."))
   123  					Eventually(session).Should(Say("task name:\\s+.+"))
   124  					Eventually(session).Should(Say("task id:\\s+1"))
   125  					Eventually(session).Should(Exit(0))
   126  				})
   127  			})
   128  
   129  			Context("when the task name is provided", func() {
   130  				It("creates a new task with the provided name", func() {
   131  					session := helpers.CF("run-task", appName, "echo hi", "--name", "some-task-name")
   132  					userName, _ := helpers.GetCredentials()
   133  					Eventually(session).Should(Say("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   134  					Eventually(session).Should(Say("OK"))
   135  					Eventually(session).Should(Say("Task has been submitted successfully for execution."))
   136  					Eventually(session).Should(Say("task name:\\s+some-task-name"))
   137  					Eventually(session).Should(Say("task id:\\s+1"))
   138  					Eventually(session).Should(Exit(0))
   139  
   140  					taskSession := helpers.CF("tasks", appName)
   141  					Eventually(taskSession).Should(Say("1\\s+some-task-name"))
   142  					Eventually(taskSession).Should(Exit(0))
   143  				})
   144  			})
   145  
   146  			Context("when disk space is provided", func() {
   147  				Context("when the provided disk space is invalid", func() {
   148  					It("displays error and exits 1", func() {
   149  						session := helpers.CF("run-task", appName, "echo hi", "-k", "invalid")
   150  						Eventually(session.Err).Should(Say("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB"))
   151  
   152  						Eventually(session).Should(Exit(1))
   153  					})
   154  				})
   155  
   156  				Context("when the provided disk space is valid", func() {
   157  					It("runs the task with the provided disk space", func() {
   158  						diskSpace := 123
   159  						session := helpers.CF("run-task", appName, "echo hi", "-k", fmt.Sprintf("%dM", diskSpace))
   160  						Eventually(session).Should(Exit(0))
   161  
   162  						session = helpers.CF("tasks", appName, "-v")
   163  						Eventually(session).Should(Say("\"disk_in_mb\": %d", diskSpace))
   164  						Eventually(session).Should(Exit(0))
   165  					})
   166  				})
   167  			})
   168  
   169  			Context("when task memory is provided", func() {
   170  				Context("when the provided memory is invalid", func() {
   171  					It("displays error and exits 1", func() {
   172  						session := helpers.CF("run-task", appName, "echo hi", "-m", "invalid")
   173  						Eventually(session.Err).Should(Say("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB"))
   174  						Eventually(session).Should(Exit(1))
   175  					})
   176  				})
   177  
   178  				Context("when the provided memory is valid", func() {
   179  					It("runs the task with the provided memory", func() {
   180  						taskMemory := 123
   181  						session := helpers.CF("run-task", appName, "echo hi", "-m", fmt.Sprintf("%dM", taskMemory))
   182  						Eventually(session).Should(Exit(0))
   183  
   184  						session = helpers.CF("tasks", appName, "-v")
   185  						Eventually(session).Should(Say("\"memory_in_mb\": %d", taskMemory))
   186  						Eventually(session).Should(Exit(0))
   187  					})
   188  				})
   189  			})
   190  		})
   191  
   192  		Context("when the application is not staged", func() {
   193  			BeforeEach(func() {
   194  				helpers.WithHelloWorldApp(func(appDir string) {
   195  					Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   196  				})
   197  			})
   198  
   199  			It("fails and outputs task must have a droplet message", func() {
   200  				session := helpers.CF("run-task", appName, "echo hi")
   201  				Eventually(session).Should(Say("FAILED"))
   202  				Eventually(session.Err).Should(Say(`Error running task: App is not staged.`))
   203  				Eventually(session).Should(Exit(1))
   204  			})
   205  		})
   206  
   207  		Context("when the application is staged but stopped", func() {
   208  			BeforeEach(func() {
   209  				helpers.WithHelloWorldApp(func(appDir string) {
   210  					Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   211  				})
   212  				session := helpers.CF("stop", appName)
   213  				Eventually(session).Should(Exit(0))
   214  			})
   215  
   216  			It("creates a new task", func() {
   217  				session := helpers.CF("run-task", appName, "echo hi")
   218  				userName, _ := helpers.GetCredentials()
   219  				Eventually(session).Should(Say("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   220  				Eventually(session).Should(Say("OK"))
   221  				Eventually(session).Should(Say("Task has been submitted successfully for execution."))
   222  				Eventually(session).Should(Say("task name:\\s+.+"))
   223  				Eventually(session).Should(Say("task id:\\s+1"))
   224  				Eventually(session).Should(Exit(0))
   225  			})
   226  		})
   227  
   228  		Context("when the application does not exist", func() {
   229  			It("fails and outputs an app not found message", func() {
   230  				session := helpers.CF("run-task", appName, "echo hi")
   231  				Eventually(session).Should(Say("FAILED"))
   232  				Eventually(session.Err).Should(Say(fmt.Sprintf("App %s not found", appName)))
   233  				Eventually(session).Should(Exit(1))
   234  			})
   235  		})
   236  	})
   237  })