github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/v7/isolated/logs_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     8  
     9  	"code.cloudfoundry.org/cli/integration/helpers"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gbytes"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("logs Command", func() {
    17  	Describe("help", func() {
    18  		When("--help flag is set", func() {
    19  			It("appears in cf help -a", func() {
    20  				session := helpers.CF("help", "-a")
    21  				Eventually(session).Should(Exit(0))
    22  				Expect(session).To(HaveCommandInCategoryWithDescription("logs", "APPS", "Tail or show recent logs for an app"))
    23  			})
    24  
    25  			It("displays command usage to output", func() {
    26  				session := helpers.CF("logs", "--help")
    27  				Eventually(session).Should(Say("NAME:"))
    28  				Eventually(session).Should(Say("logs - Tail or show recent logs for an app"))
    29  				Eventually(session).Should(Say("USAGE:"))
    30  				Eventually(session).Should(Say("cf logs APP_NAME"))
    31  				Eventually(session).Should(Say("OPTIONS:"))
    32  				Eventually(session).Should(Say(`--recent\s+Dump recent logs instead of tailing`))
    33  				Eventually(session).Should(Say("SEE ALSO:"))
    34  				Eventually(session).Should(Say("app, apps, ssh"))
    35  				Eventually(session).Should(Exit(0))
    36  			})
    37  		})
    38  	})
    39  
    40  	When("the environment is not setup correctly", func() {
    41  		It("fails with the appropriate errors", func() {
    42  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "logs", "app-name")
    43  		})
    44  	})
    45  
    46  	When("the environment is set up correctly", func() {
    47  		var (
    48  			orgName   string
    49  			spaceName string
    50  		)
    51  
    52  		BeforeEach(func() {
    53  			orgName = helpers.NewOrgName()
    54  			spaceName = helpers.NewSpaceName()
    55  			helpers.SetupCF(orgName, spaceName)
    56  		})
    57  
    58  		AfterEach(func() {
    59  			helpers.QuickDeleteOrg(orgName)
    60  		})
    61  
    62  		When("input is invalid", func() {
    63  			Context("because no app name is provided", func() {
    64  				It("gives an incorrect usage message", func() {
    65  					session := helpers.CF("logs")
    66  					Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    67  					Eventually(session).Should(Say("NAME:"))
    68  					Eventually(session).Should(Say("logs - Tail or show recent logs for an app"))
    69  					Eventually(session).Should(Say("USAGE:"))
    70  					Eventually(session).Should(Say("cf logs APP_NAME"))
    71  					Eventually(session).Should(Say("OPTIONS:"))
    72  					Eventually(session).Should(Say(`--recent\s+Dump recent logs instead of tailing`))
    73  					Eventually(session).Should(Say("SEE ALSO:"))
    74  					Eventually(session).Should(Say("app, apps, ssh"))
    75  					Eventually(session).Should(Exit(1))
    76  				})
    77  			})
    78  
    79  			Context("because the app does not exist", func() {
    80  				It("fails with an app not found message", func() {
    81  					session := helpers.CF("logs", "dora")
    82  					Eventually(session).Should(Say("FAILED"))
    83  					Eventually(session.Err).Should(Say("App 'dora' not found"))
    84  					Eventually(session).Should(Exit(1))
    85  				})
    86  			})
    87  		})
    88  
    89  		When("the specified app exists", func() {
    90  			var appName string
    91  
    92  			BeforeEach(func() {
    93  				appName = helpers.PrefixedRandomName("app")
    94  				helpers.WithHelloWorldApp(func(appDir string) {
    95  					Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "-u", "http", "--endpoint", "/")).Should(Exit(0))
    96  				})
    97  			})
    98  
    99  			Context("without the --recent flag", func() {
   100  				It("streams logs out to the screen", func() {
   101  					session := helpers.CF("logs", appName)
   102  					defer session.Terminate()
   103  
   104  					userName, _ := helpers.GetCredentials()
   105  					Eventually(session).Should(Say("Retrieving logs for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   106  
   107  					response, err := http.Get(fmt.Sprintf("http://%s.%s", appName, helpers.DefaultSharedDomain()))
   108  					Expect(err).NotTo(HaveOccurred())
   109  					Expect(response.StatusCode).To(Equal(http.StatusOK))
   110  					Eventually(session).Should(Say(`%s \[APP/PROC/WEB/0\]\s+OUT .*? \"GET / HTTP/1.1\" 200 \d+`, helpers.ISO8601Regex))
   111  				})
   112  			})
   113  
   114  			Context("with the --recent flag", func() {
   115  				It("displays the most recent logs and closes the stream", func() {
   116  					session := helpers.CF("logs", appName, "--recent")
   117  					userName, _ := helpers.GetCredentials()
   118  					Eventually(session).Should(Say("Retrieving logs for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   119  					Eventually(session).Should(Say(`%s \[API/\d+\]\s+OUT Created app with guid %s`, helpers.ISO8601Regex, helpers.GUIDRegex))
   120  					Eventually(session).Should(Exit(0))
   121  				})
   122  			})
   123  		})
   124  	})
   125  })