github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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(Exit(0))
    18  			Expect(session.Out).To(Say("NAME:"))
    19  			Expect(session.Out).To(Say("   run-task - Run a one-off task on an app"))
    20  			Expect(session.Out).To(Say("USAGE:"))
    21  			Expect(session.Out).To(Say("   cf run-task APP_NAME COMMAND \\[-k DISK] \\[-m MEMORY\\] \\[--name TASK_NAME\\]"))
    22  			Expect(session.Out).To(Say("TIP:"))
    23  			Expect(session.Out).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."))
    24  			Expect(session.Out).To(Say("EXAMPLES:"))
    25  			Expect(session.Out).To(Say(`   cf run-task my-app "bundle exec rake db:migrate" --name migrate`))
    26  			Expect(session.Out).To(Say("ALIAS:"))
    27  			Expect(session.Out).To(Say("   rt"))
    28  			Expect(session.Out).To(Say("OPTIONS:"))
    29  			Expect(session.Out).To(Say("   -k          Disk limit \\(e\\.g\\. 256M, 1024M, 1G\\)"))
    30  			Expect(session.Out).To(Say("   -m          Memory limit \\(e\\.g\\. 256M, 1024M, 1G\\)"))
    31  			Expect(session.Out).To(Say("   --name      Name to give the task \\(generated if omitted\\)"))
    32  			Expect(session.Out).To(Say("SEE ALSO:"))
    33  			Expect(session.Out).To(Say("   logs, tasks, terminate-task"))
    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(Exit(1))
    46  				Expect(session.Out).To(Say("FAILED"))
    47  				Expect(session.Err).To(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    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(Exit(1))
    59  				Expect(session.Out).To(Say("FAILED"))
    60  				Expect(session.Err).To(Say("Not logged in. Use 'cf login' to log in."))
    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(Exit(1))
    73  				Expect(session.Out).To(Say("FAILED"))
    74  				Expect(session.Err).To(Say("No org targeted, use 'cf target -o ORG' to target an org."))
    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(Exit(1))
    88  				Expect(session.Out).To(Say("FAILED"))
    89  				Expect(session.Err).To(Say("No space targeted, use 'cf target -s SPACE' to target a space"))
    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.PrefixedRandomName("SPACE")
   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  					Eventually(session).Should(Exit(0))
   120  					userName, _ := helpers.GetCredentials()
   121  					Expect(session.Out).To(Say(fmt.Sprintf("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)))
   122  					Expect(session.Out).To(Say(`OK
   123  
   124  Task has been submitted successfully for execution.
   125  Task name:   .+
   126  Task id:     1`,
   127  					))
   128  				})
   129  			})
   130  
   131  			Context("when the task name is provided", func() {
   132  				It("creates a new task with the provided name", func() {
   133  					session := helpers.CF("run-task", appName, "echo hi", "--name", "some-task-name")
   134  					Eventually(session).Should(Exit(0))
   135  					userName, _ := helpers.GetCredentials()
   136  					Expect(session.Out).To(Say(fmt.Sprintf("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)))
   137  					Expect(session.Out).To(Say(`OK
   138  
   139  Task has been submitted successfully for execution.
   140  Task name:   some-task-name
   141  Task id:     1`,
   142  					))
   143  
   144  					Eventually(func() *Buffer {
   145  						taskSession := helpers.CF("tasks", appName)
   146  						Eventually(taskSession).Should(Exit(0))
   147  						return taskSession.Out
   148  					}).Should(Say("1\\s+some-task-name"))
   149  				})
   150  			})
   151  
   152  			Context("when disk space is provided", func() {
   153  				Context("when the provided disk space is invalid", func() {
   154  					It("displays error and exits 1", func() {
   155  						session := helpers.CF("run-task", appName, "echo hi", "-k", "invalid")
   156  						Eventually(session.Err).Should(Say("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB"))
   157  
   158  						Eventually(session).Should(Exit(1))
   159  					})
   160  				})
   161  
   162  				Context("when the provided disk space is valid", func() {
   163  					It("runs the task with the provided disk space", func() {
   164  						diskSpace := 123
   165  						session := helpers.CF("run-task", appName, "echo hi", "-k", fmt.Sprintf("%dM", diskSpace))
   166  						Eventually(session).Should(Exit(0))
   167  
   168  						session = helpers.CF("tasks", appName, "-v")
   169  						Eventually(session.Out).Should(Say("\"disk_in_mb\": %d", diskSpace))
   170  						Eventually(session).Should(Exit(0))
   171  					})
   172  				})
   173  			})
   174  
   175  			Context("when task memory is provided", func() {
   176  				Context("when the provided memory is invalid", func() {
   177  					It("displays error and exits 1", func() {
   178  						session := helpers.CF("run-task", appName, "echo hi", "-m", "invalid")
   179  						Eventually(session.Err).Should(Say("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB"))
   180  
   181  						Eventually(session).Should(Exit(1))
   182  					})
   183  				})
   184  
   185  				Context("when the provided memory is valid", func() {
   186  					It("runs the task with the provided memory", func() {
   187  						taskMemory := 123
   188  						session := helpers.CF("run-task", appName, "echo hi", "-m", fmt.Sprintf("%dM", taskMemory))
   189  						Eventually(session).Should(Exit(0))
   190  
   191  						session = helpers.CF("tasks", appName, "-v")
   192  						Eventually(session.Out).Should(Say("\"memory_in_mb\": %d", taskMemory))
   193  						Eventually(session).Should(Exit(0))
   194  					})
   195  				})
   196  			})
   197  		})
   198  
   199  		Context("when the application is not staged", func() {
   200  			BeforeEach(func() {
   201  				helpers.WithHelloWorldApp(func(appDir string) {
   202  					Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   203  				})
   204  			})
   205  
   206  			It("fails and outputs task must have a droplet message", func() {
   207  				session := helpers.CF("run-task", appName, "echo hi")
   208  				Eventually(session).Should(Exit(1))
   209  				Expect(session.Out).To(Say("FAILED"))
   210  				Expect(session.Err).To(Say(`Error running task: App is not staged.`))
   211  			})
   212  		})
   213  
   214  		Context("when the application is staged but stopped", func() {
   215  			BeforeEach(func() {
   216  				helpers.WithHelloWorldApp(func(appDir string) {
   217  					Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   218  				})
   219  				session := helpers.CF("stop", appName)
   220  				Eventually(session).Should(Exit(0))
   221  			})
   222  
   223  			It("creates a new task", func() {
   224  				session := helpers.CF("run-task", appName, "echo hi")
   225  				Eventually(session).Should(Exit(0))
   226  				userName, _ := helpers.GetCredentials()
   227  				Expect(session.Out).To(Say(fmt.Sprintf("Creating task for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)))
   228  				Expect(session.Out).To(Say(`OK
   229  
   230  Task has been submitted successfully for execution.
   231  Task name:   .+
   232  Task id:     1`,
   233  				))
   234  			})
   235  		})
   236  
   237  		Context("when the application does not exist", func() {
   238  			It("fails and outputs an app not found message", func() {
   239  				session := helpers.CF("run-task", appName, "echo hi")
   240  				Eventually(session).Should(Exit(1))
   241  				Expect(session.Out).To(Say("FAILED"))
   242  				Expect(session.Err).To(Say(fmt.Sprintf("App %s not found", appName)))
   243  			})
   244  		})
   245  	})
   246  })