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