github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/run_task_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  
     7  	"code.cloudfoundry.org/cli/integration/helpers"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  	. "github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("run-task command", func() {
    15  	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(Exit(0))
    19  			Expect(session).To(Say("NAME:"))
    20  			Expect(session).To(Say("   run-task - Run a one-off task on an app"))
    21  			Expect(session).To(Say("USAGE:"))
    22  			Expect(session).To(Say(`   cf run-task APP_NAME \[--command COMMAND\] \[-k DISK] \[-m MEMORY\] \[--name TASK_NAME\] \[--process PROCESS_TYPE\]`))
    23  			Expect(session).To(Say("TIP:"))
    24  			Expect(session).To(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."))
    25  			Expect(session).To(Say("EXAMPLES:"))
    26  			Expect(session).To(Say(`   cf run-task my-app --command "bundle exec rake db:migrate" --name migrate`))
    27  			Expect(session).To(Say("ALIAS:"))
    28  			Expect(session).To(Say("   rt"))
    29  			Expect(session).To(Say("OPTIONS:"))
    30  			Expect(session).To(Say(`   --command, -c\s+The command to execute`))
    31  			Expect(session).To(Say(`   -k                 Disk limit \(e\.g\. 256M, 1024M, 1G\)`))
    32  			Expect(session).To(Say(`   -m                 Memory limit \(e\.g\. 256M, 1024M, 1G\)`))
    33  			Expect(session).To(Say(`   --name             Name to give the task \(generated if omitted\)`))
    34  			Expect(session).To(Say(`   --process          Process type to use as a template for command, memory, and disk for the created task`))
    35  			Expect(session).To(Say("SEE ALSO:"))
    36  			Expect(session).To(Say("   logs, tasks, terminate-task"))
    37  		})
    38  	})
    39  
    40  	When("the environment is not setup correctly", func() {
    41  		It("fails with the appropriate errors", func() {
    42  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "run-task", "app-name", "--command", "some-command")
    43  		})
    44  	})
    45  
    46  	When("the environment is setup correctly", func() {
    47  		var (
    48  			orgName   string
    49  			spaceName string
    50  			appName   string
    51  		)
    52  
    53  		BeforeEach(func() {
    54  			orgName = helpers.NewOrgName()
    55  			spaceName = helpers.NewSpaceName()
    56  			appName = helpers.PrefixedRandomName("APP")
    57  
    58  			helpers.SetupCF(orgName, spaceName)
    59  		})
    60  
    61  		AfterEach(func() {
    62  			helpers.QuickDeleteOrg(orgName)
    63  		})
    64  
    65  		When("the application exists", func() {
    66  
    67  			When("the app has a default task process", func() {
    68  				BeforeEach(func() {
    69  					helpers.WithTaskApp(func(appDir string) {
    70  						Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "-f", path.Join(appDir, "manifest.yml"))).Should(Exit(0))
    71  					}, appName)
    72  				})
    73  
    74  				It("creates a new task", func() {
    75  					session := helpers.CF("run-task", appName)
    76  					userName, _ := helpers.GetCredentials()
    77  					Eventually(session).Should(Exit(0))
    78  					Expect(session).To(Say("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
    79  					Expect(session).To(Say("Task has been submitted successfully for execution."))
    80  					Expect(session).To(Say("OK"))
    81  					Expect(session).To(Say(`task name:\s+.+`))
    82  					Expect(session).To(Say(`task id:\s+1`))
    83  				})
    84  			})
    85  
    86  			When("the app is given a command flag", func() {
    87  
    88  				BeforeEach(func() {
    89  					helpers.WithHelloWorldApp(func(appDir string) {
    90  						Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
    91  					})
    92  				})
    93  				When("the task name is provided", func() {
    94  					It("creates a new task with the provided name", func() {
    95  						session := helpers.CF("run-task", appName, "--command", "echo hi", "--name", "some-task-name")
    96  						userName, _ := helpers.GetCredentials()
    97  						Eventually(session).Should(Exit(0))
    98  						Expect(session).To(Say("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
    99  						Expect(session).To(Say("Task has been submitted successfully for execution."))
   100  						Expect(session).To(Say("OK"))
   101  						Expect(session).To(Say(`task name:\s+some-task-name`))
   102  						Expect(session).To(Say(`task id:\s+1`))
   103  
   104  						taskSession := helpers.CF("tasks", appName)
   105  						Eventually(taskSession).Should(Exit(0))
   106  						Expect(taskSession).Should(Say(`1\s+some-task-name`))
   107  					})
   108  				})
   109  
   110  				When("disk space is provided", func() {
   111  					When("the provided disk space is invalid", func() {
   112  						It("displays error and exits 1", func() {
   113  							session := helpers.CF("run-task", appName, "--command", "echo hi", "-k", "invalid")
   114  							Eventually(session).Should(Exit(1))
   115  							Expect(session.Err).Should(Say("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB"))
   116  
   117  						})
   118  					})
   119  
   120  					When("the provided disk space is valid", func() {
   121  						It("runs the task with the provided disk space", func() {
   122  							diskSpace := 123
   123  							session := helpers.CF("run-task", appName, "--command", "echo hi", "-k", fmt.Sprintf("%dM", diskSpace))
   124  							Eventually(session).Should(Exit(0))
   125  
   126  							session = helpers.CF("tasks", appName, "-v")
   127  							Eventually(session).Should(Exit(0))
   128  							Expect(session).To(Say("\"disk_in_mb\": %d", diskSpace))
   129  						})
   130  					})
   131  				})
   132  
   133  				When("task memory is provided", func() {
   134  					When("the provided memory is invalid", func() {
   135  						It("displays error and exits 1", func() {
   136  							session := helpers.CF("run-task", appName, "--command", "echo hi", "-m", "invalid")
   137  							Eventually(session).Should(Exit(1))
   138  							Expect(session.Err).Should(Say("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB"))
   139  						})
   140  					})
   141  
   142  					When("the provided memory is valid", func() {
   143  						It("runs the task with the provided memory", func() {
   144  							taskMemory := 123
   145  							session := helpers.CF("run-task", appName, "--command", " echo hi", "-m", fmt.Sprintf("%dM", taskMemory))
   146  							Eventually(session).Should(Exit(0))
   147  
   148  							session = helpers.CF("tasks", appName, "-v")
   149  							Eventually(session).Should(Exit(0))
   150  							Expect(session).To(Say("\"memory_in_mb\": %d", taskMemory))
   151  						})
   152  					})
   153  				})
   154  			})
   155  
   156  		})
   157  
   158  		When("the application is not staged", func() {
   159  			BeforeEach(func() {
   160  				helpers.WithHelloWorldApp(func(appDir string) {
   161  					Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   162  				})
   163  			})
   164  
   165  			It("fails and outputs task must have a droplet message", func() {
   166  				session := helpers.CF("run-task", appName, "--command", "echo hi")
   167  				Eventually(session).Should(Exit(1))
   168  				Expect(session).To(Say("FAILED"))
   169  				Expect(session.Err).To(Say(`Error running task: App is not staged.`))
   170  			})
   171  		})
   172  
   173  		When("the application is staged but stopped", func() {
   174  			BeforeEach(func() {
   175  				helpers.WithTaskApp(func(appDir string) {
   176  					Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "-f", path.Join(appDir, "manifest.yml"))).Should(Exit(0))
   177  				}, appName)
   178  				session := helpers.CF("stop", appName)
   179  				Eventually(session).Should(Exit(0))
   180  			})
   181  
   182  			It("creates a new task", func() {
   183  				session := helpers.CF("run-task", appName)
   184  				Eventually(session).Should(Exit(0))
   185  				userName, _ := helpers.GetCredentials()
   186  				Expect(session).To(Say("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   187  				Expect(session).To(Say("Task has been submitted successfully for execution."))
   188  				Expect(session).To(Say("OK"))
   189  				Expect(session).To(Say(`task name:\s+.+`))
   190  				Expect(session).To(Say(`task id:\s+1`))
   191  			})
   192  		})
   193  
   194  		When("the application does not exist", func() {
   195  			It("fails and outputs an app not found message", func() {
   196  				session := helpers.CF("run-task", appName)
   197  				Eventually(session).Should(Exit(1))
   198  				Expect(session).To(Say("FAILED"))
   199  				Expect(session.Err).To(Say(fmt.Sprintf("App '%s' not found", appName)))
   200  			})
   201  		})
   202  	})
   203  })