github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/tasks_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("tasks command", func() {
    15  	var (
    16  		appName string
    17  	)
    18  
    19  	BeforeEach(func() {
    20  		appName = helpers.PrefixedRandomName("APP")
    21  	})
    22  
    23  	Context("when --help flag is set", func() {
    24  		It("Displays command usage to output", func() {
    25  			session := helpers.CF("tasks", "--help")
    26  			Eventually(session.Out).Should(Say("NAME:"))
    27  			Eventually(session.Out).Should(Say("   tasks - List tasks of an app"))
    28  			Eventually(session.Out).Should(Say("USAGE:"))
    29  			Eventually(session.Out).Should(Say("   cf tasks APP_NAME"))
    30  			Eventually(session.Out).Should(Say("SEE ALSO:"))
    31  			Eventually(session.Out).Should(Say("   apps, logs, run-task, terminate-task"))
    32  			Eventually(session).Should(Exit(0))
    33  		})
    34  	})
    35  
    36  	Context("when the environment is not setup correctly", func() {
    37  		Context("when no API endpoint is set", func() {
    38  			BeforeEach(func() {
    39  				helpers.UnsetAPI()
    40  			})
    41  
    42  			It("fails with no API endpoint set message", func() {
    43  				session := helpers.CF("tasks", appName)
    44  				Eventually(session.Out).Should(Say("FAILED"))
    45  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    46  				Eventually(session).Should(Exit(1))
    47  			})
    48  		})
    49  
    50  		Context("when the v3 api does not exist", func() {
    51  			var server *Server
    52  
    53  			BeforeEach(func() {
    54  				server = helpers.StartAndTargetServerWithoutV3API()
    55  			})
    56  
    57  			AfterEach(func() {
    58  				server.Close()
    59  			})
    60  
    61  			It("fails with error message that the minimum version is not met", func() {
    62  				session := helpers.CF("tasks", appName)
    63  				Eventually(session).Should(Say("FAILED"))
    64  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.0\\.0 or higher\\."))
    65  				Eventually(session).Should(Exit(1))
    66  			})
    67  		})
    68  
    69  		Context("when not logged in", func() {
    70  			BeforeEach(func() {
    71  				helpers.LogoutCF()
    72  			})
    73  
    74  			It("fails with not logged in message", func() {
    75  				session := helpers.CF("tasks", appName)
    76  				Eventually(session.Out).Should(Say("FAILED"))
    77  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    78  				Eventually(session).Should(Exit(1))
    79  			})
    80  		})
    81  
    82  		Context("when there is no org set", func() {
    83  			BeforeEach(func() {
    84  				helpers.LogoutCF()
    85  				helpers.LoginCF()
    86  			})
    87  
    88  			It("fails with no targeted org error message", func() {
    89  				session := helpers.CF("tasks", appName)
    90  				Eventually(session.Out).Should(Say("FAILED"))
    91  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
    92  				Eventually(session).Should(Exit(1))
    93  			})
    94  		})
    95  
    96  		Context("when there is no space set", func() {
    97  			BeforeEach(func() {
    98  				helpers.LogoutCF()
    99  				helpers.LoginCF()
   100  				helpers.TargetOrg(ReadOnlyOrg)
   101  			})
   102  
   103  			It("fails with no space targeted error message", func() {
   104  				session := helpers.CF("tasks", appName)
   105  				Eventually(session.Out).Should(Say("FAILED"))
   106  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space"))
   107  				Eventually(session).Should(Exit(1))
   108  			})
   109  		})
   110  	})
   111  
   112  	Context("when the environment is setup correctly", func() {
   113  		var (
   114  			orgName   string
   115  			spaceName string
   116  		)
   117  
   118  		BeforeEach(func() {
   119  			orgName = helpers.NewOrgName()
   120  			spaceName = helpers.NewSpaceName()
   121  
   122  			setupCF(orgName, spaceName)
   123  		})
   124  
   125  		AfterEach(func() {
   126  			helpers.LoginCF()
   127  			helpers.QuickDeleteOrg(orgName)
   128  		})
   129  
   130  		Context("when the application does not exist", func() {
   131  			It("fails and outputs an app not found message", func() {
   132  				session := helpers.CF("tasks", appName)
   133  				Eventually(session.Out).Should(Say("FAILED"))
   134  				Eventually(session.Err).Should(Say(fmt.Sprintf("App %s not found", appName)))
   135  				Eventually(session).Should(Exit(1))
   136  			})
   137  		})
   138  
   139  		Context("when the application exists", func() {
   140  			BeforeEach(func() {
   141  				helpers.WithHelloWorldApp(func(appDir string) {
   142  					Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   143  				})
   144  			})
   145  
   146  			Context("when the application does not have associated tasks", func() {
   147  				It("displays an empty table", func() {
   148  					session := helpers.CF("tasks", appName)
   149  					Eventually(session.Out).Should(Say(`
   150  id   name   state   start time   command
   151  `,
   152  					))
   153  					Consistently(session.Out).ShouldNot(Say("1"))
   154  					Eventually(session).Should(Exit(0))
   155  				})
   156  			})
   157  
   158  			Context("when the application has associated tasks", func() {
   159  				BeforeEach(func() {
   160  					Eventually(helpers.CF("run-task", appName, "echo hello world")).Should(Exit(0))
   161  					Eventually(helpers.CF("run-task", appName, "echo foo bar")).Should(Exit(0))
   162  				})
   163  
   164  				It("displays all the tasks in descending order", func() {
   165  					session := helpers.CF("tasks", appName)
   166  					userName, _ := helpers.GetCredentials()
   167  					Eventually(session.Out).Should(Say(fmt.Sprintf("Getting tasks for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)))
   168  					Eventually(session.Out).Should(Say("OK\n"))
   169  					Eventually(session.Out).Should(Say(`id\s+name\s+state\s+start time\s+command
   170  2\s+[a-zA-Z-0-9 ,:]+echo foo bar
   171  1\s+[a-zA-Z-0-9 ,:]+echo hello world`))
   172  					Eventually(session).Should(Exit(0))
   173  				})
   174  
   175  				Context("when the logged in user does not have authorization to see task commands", func() {
   176  					var user string
   177  
   178  					BeforeEach(func() {
   179  						user = helpers.NewUsername()
   180  						password := helpers.NewPassword()
   181  						Eventually(helpers.CF("create-user", user, password)).Should(Exit(0))
   182  						Eventually(helpers.CF("set-space-role", user, orgName, spaceName, "SpaceAuditor")).Should(Exit(0))
   183  						Eventually(helpers.CF("auth", user, password)).Should(Exit(0))
   184  						Eventually(helpers.CF("target", "-o", orgName, "-s", spaceName)).Should(Exit(0))
   185  					})
   186  
   187  					It("does not display task commands", func() {
   188  						session := helpers.CF("tasks", appName)
   189  						Eventually(session.Out).Should(Say("2\\s+[a-zA-Z-0-9 ,:]+\\[hidden\\]"))
   190  						Eventually(session.Out).Should(Say("1\\s+[a-zA-Z-0-9 ,:]+\\[hidden\\]"))
   191  						Eventually(session).Should(Exit(0))
   192  					})
   193  				})
   194  			})
   195  		})
   196  	})
   197  })