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