github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+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  		It("fails with the appropriate errors", func() {
    40  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "run-task", "app-name", "some-command")
    41  		})
    42  
    43  		Context("when the v3 api does not exist", func() {
    44  			var server *Server
    45  
    46  			BeforeEach(func() {
    47  				server = helpers.StartAndTargetServerWithoutV3API()
    48  			})
    49  
    50  			AfterEach(func() {
    51  				server.Close()
    52  			})
    53  
    54  			It("fails with error message that the minimum version is not met", func() {
    55  				session := helpers.CF("run-task", "app-name", "some command")
    56  				Eventually(session).Should(Say("FAILED"))
    57  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.0\\.0 or higher\\."))
    58  				Eventually(session).Should(Exit(1))
    59  			})
    60  		})
    61  	})
    62  
    63  	Context("when the environment is setup correctly", func() {
    64  		var (
    65  			orgName   string
    66  			spaceName string
    67  			appName   string
    68  		)
    69  
    70  		BeforeEach(func() {
    71  			orgName = helpers.NewOrgName()
    72  			spaceName = helpers.NewSpaceName()
    73  			appName = helpers.PrefixedRandomName("APP")
    74  
    75  			helpers.SetupCF(orgName, spaceName)
    76  		})
    77  
    78  		AfterEach(func() {
    79  			helpers.QuickDeleteOrg(orgName)
    80  		})
    81  
    82  		Context("when the application exists", func() {
    83  			BeforeEach(func() {
    84  				helpers.WithHelloWorldApp(func(appDir string) {
    85  					Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
    86  				})
    87  			})
    88  
    89  			Context("when the task name is not provided", func() {
    90  				It("creates a new task", func() {
    91  					session := helpers.CF("run-task", appName, "echo hi")
    92  					userName, _ := helpers.GetCredentials()
    93  					Eventually(session).Should(Say("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
    94  					Eventually(session).Should(Say("OK"))
    95  					Eventually(session).Should(Say("Task has been submitted successfully for execution."))
    96  					Eventually(session).Should(Say("task name:\\s+.+"))
    97  					Eventually(session).Should(Say("task id:\\s+1"))
    98  					Eventually(session).Should(Exit(0))
    99  				})
   100  			})
   101  
   102  			Context("when the task name is provided", func() {
   103  				It("creates a new task with the provided name", func() {
   104  					session := helpers.CF("run-task", appName, "echo hi", "--name", "some-task-name")
   105  					userName, _ := helpers.GetCredentials()
   106  					Eventually(session).Should(Say("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   107  					Eventually(session).Should(Say("OK"))
   108  					Eventually(session).Should(Say("Task has been submitted successfully for execution."))
   109  					Eventually(session).Should(Say("task name:\\s+some-task-name"))
   110  					Eventually(session).Should(Say("task id:\\s+1"))
   111  					Eventually(session).Should(Exit(0))
   112  
   113  					taskSession := helpers.CF("tasks", appName)
   114  					Eventually(taskSession).Should(Say("1\\s+some-task-name"))
   115  					Eventually(taskSession).Should(Exit(0))
   116  				})
   117  			})
   118  
   119  			Context("when disk space is provided", func() {
   120  				Context("when the provided disk space is invalid", func() {
   121  					It("displays error and exits 1", func() {
   122  						session := helpers.CF("run-task", appName, "echo hi", "-k", "invalid")
   123  						Eventually(session.Err).Should(Say("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB"))
   124  
   125  						Eventually(session).Should(Exit(1))
   126  					})
   127  				})
   128  
   129  				Context("when the provided disk space is valid", func() {
   130  					It("runs the task with the provided disk space", func() {
   131  						diskSpace := 123
   132  						session := helpers.CF("run-task", appName, "echo hi", "-k", fmt.Sprintf("%dM", diskSpace))
   133  						Eventually(session).Should(Exit(0))
   134  
   135  						session = helpers.CF("tasks", appName, "-v")
   136  						Eventually(session).Should(Say("\"disk_in_mb\": %d", diskSpace))
   137  						Eventually(session).Should(Exit(0))
   138  					})
   139  				})
   140  			})
   141  
   142  			Context("when task memory is provided", func() {
   143  				Context("when the provided memory is invalid", func() {
   144  					It("displays error and exits 1", func() {
   145  						session := helpers.CF("run-task", appName, "echo hi", "-m", "invalid")
   146  						Eventually(session.Err).Should(Say("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB"))
   147  						Eventually(session).Should(Exit(1))
   148  					})
   149  				})
   150  
   151  				Context("when the provided memory is valid", func() {
   152  					It("runs the task with the provided memory", func() {
   153  						taskMemory := 123
   154  						session := helpers.CF("run-task", appName, "echo hi", "-m", fmt.Sprintf("%dM", taskMemory))
   155  						Eventually(session).Should(Exit(0))
   156  
   157  						session = helpers.CF("tasks", appName, "-v")
   158  						Eventually(session).Should(Say("\"memory_in_mb\": %d", taskMemory))
   159  						Eventually(session).Should(Exit(0))
   160  					})
   161  				})
   162  			})
   163  		})
   164  
   165  		Context("when the application is not staged", func() {
   166  			BeforeEach(func() {
   167  				helpers.WithHelloWorldApp(func(appDir string) {
   168  					Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   169  				})
   170  			})
   171  
   172  			It("fails and outputs task must have a droplet message", func() {
   173  				session := helpers.CF("run-task", appName, "echo hi")
   174  				Eventually(session).Should(Say("FAILED"))
   175  				Eventually(session.Err).Should(Say(`Error running task: App is not staged.`))
   176  				Eventually(session).Should(Exit(1))
   177  			})
   178  		})
   179  
   180  		Context("when the application is staged but stopped", func() {
   181  			BeforeEach(func() {
   182  				helpers.WithHelloWorldApp(func(appDir string) {
   183  					Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   184  				})
   185  				session := helpers.CF("stop", appName)
   186  				Eventually(session).Should(Exit(0))
   187  			})
   188  
   189  			It("creates a new task", func() {
   190  				session := helpers.CF("run-task", appName, "echo hi")
   191  				userName, _ := helpers.GetCredentials()
   192  				Eventually(session).Should(Say("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   193  				Eventually(session).Should(Say("OK"))
   194  				Eventually(session).Should(Say("Task has been submitted successfully for execution."))
   195  				Eventually(session).Should(Say("task name:\\s+.+"))
   196  				Eventually(session).Should(Say("task id:\\s+1"))
   197  				Eventually(session).Should(Exit(0))
   198  			})
   199  		})
   200  
   201  		Context("when the application does not exist", func() {
   202  			It("fails and outputs an app not found message", func() {
   203  				session := helpers.CF("run-task", appName, "echo hi")
   204  				Eventually(session).Should(Say("FAILED"))
   205  				Eventually(session.Err).Should(Say(fmt.Sprintf("App %s not found", appName)))
   206  				Eventually(session).Should(Exit(1))
   207  			})
   208  		})
   209  	})
   210  })