github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/terminate_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("terminate-task command", func() {
    14  	When("the environment is not setup correctly", func() {
    15  		It("fails with the appropriate errors", func() {
    16  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "terminate-task", "app-name", "3")
    17  		})
    18  	})
    19  
    20  	When("the environment is setup correctly", func() {
    21  		var (
    22  			orgName   string
    23  			spaceName string
    24  			appName   string
    25  		)
    26  
    27  		BeforeEach(func() {
    28  			orgName = helpers.NewOrgName()
    29  			spaceName = helpers.NewSpaceName()
    30  			appName = helpers.PrefixedRandomName("APP")
    31  
    32  			helpers.SetupCF(orgName, spaceName)
    33  		})
    34  
    35  		AfterEach(func() {
    36  			helpers.QuickDeleteOrg(orgName)
    37  		})
    38  
    39  		When("the application does not exist", func() {
    40  			It("fails to terminate task and outputs an error message", func() {
    41  				session := helpers.CF("terminate-task", appName, "1")
    42  				Eventually(session.Err).Should(Say(fmt.Sprintf("App '%s' not found", appName)))
    43  				Eventually(session).Should(Say("FAILED"))
    44  				Eventually(session).Should(Exit(1))
    45  			})
    46  		})
    47  
    48  		When("the application exists", func() {
    49  			BeforeEach(func() {
    50  				helpers.WithHelloWorldApp(func(appDir string) {
    51  					Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
    52  				})
    53  			})
    54  
    55  			When("the wrong data type is provided to terminate-task", func() {
    56  				It("outputs an error message to the user, provides help text, and exits 1", func() {
    57  					session := helpers.CF("terminate-task", appName, "not-an-integer")
    58  					Eventually(session.Err).Should(Say("Incorrect usage: Value for TASK_ID must be integer"))
    59  					Eventually(session).Should(Say("FAILED"))
    60  					Eventually(session).Should(Say("terminate-task APP_NAME TASK_ID")) // help
    61  					Eventually(session).Should(Exit(1))
    62  				})
    63  			})
    64  
    65  			When("the task is in the RUNNING state", func() {
    66  				BeforeEach(func() {
    67  					helpers.WithHelloWorldApp(func(appDir string) {
    68  						Eventually(helpers.CF("run-task", appName, "--command", "sleep 1000")).Should(Exit(0))
    69  					})
    70  				})
    71  
    72  				It("terminates the task", func() {
    73  					tasksSession := helpers.CF("tasks", appName)
    74  					Eventually(tasksSession).Should(Exit(0))
    75  					Expect(tasksSession).To(Say(`1\s+[a-zA-Z-0-9]+\s+RUNNING`))
    76  
    77  					session := helpers.CF("terminate-task", appName, "1")
    78  					userName, _ := helpers.GetCredentials()
    79  					Eventually(session).Should(Say(
    80  						fmt.Sprintf("Terminating task 1 of app %s in org %s / space %s as %s..", appName, orgName, spaceName, userName)))
    81  					Eventually(session).Should(Say("OK"))
    82  					Eventually(session).Should(Exit(0))
    83  				})
    84  			})
    85  
    86  			When("the task is in the SUCCEEDED state", func() {
    87  				BeforeEach(func() {
    88  					helpers.WithHelloWorldApp(func(appDir string) {
    89  						Eventually(helpers.CF("run-task", appName, "--command", "echo test")).Should(Exit(0))
    90  					})
    91  				})
    92  
    93  				It("fails to terminate the task and prints an error", func() {
    94  					Eventually(func() *Buffer {
    95  						taskSession := helpers.CF("tasks", appName)
    96  						Eventually(taskSession).Should(Exit(0))
    97  						return taskSession.Out
    98  					}).Should(Say(`1\s+[a-zA-Z-0-9]+\s+SUCCEEDED`))
    99  
   100  					session := helpers.CF("terminate-task", appName, "1")
   101  					Eventually(session.Err).Should(Say("Task state is SUCCEEDED and therefore cannot be canceled"))
   102  					Eventually(session).Should(Say("FAILED"))
   103  					Eventually(session).Should(Exit(1))
   104  				})
   105  			})
   106  
   107  			When("the task is in the FAILED state", func() {
   108  				BeforeEach(func() {
   109  					helpers.WithHelloWorldApp(func(appDir string) {
   110  						Eventually(helpers.CF("run-task", appName, "--command", "false")).Should(Exit(0))
   111  					})
   112  				})
   113  
   114  				It("fails to terminate the task and prints an error", func() {
   115  					Eventually(func() *Buffer {
   116  						taskSession := helpers.CF("tasks", appName)
   117  						Eventually(taskSession).Should(Exit(0))
   118  						return taskSession.Out
   119  					}).Should(Say(`1\s+[a-zA-Z-0-9]+\s+FAILED`))
   120  
   121  					session := helpers.CF("terminate-task", appName, "1")
   122  					Eventually(session.Err).Should(Say("Task state is FAILED and therefore cannot be canceled"))
   123  					Eventually(session).Should(Say("FAILED"))
   124  					Eventually(session).Should(Exit(1))
   125  				})
   126  			})
   127  
   128  			When("the task ID does not exist", func() {
   129  				It("fails to terminate the task and prints an error", func() {
   130  					session := helpers.CF("terminate-task", appName, "1")
   131  					Eventually(session.Err).Should(Say("Task sequence ID 1 not found."))
   132  					Eventually(session).Should(Say("FAILED"))
   133  					Eventually(session).Should(Exit(1))
   134  				})
   135  			})
   136  		})
   137  	})
   138  })