github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/logs_command_test.go (about)

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