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