github.com/wanddynosios/cli@v7.1.0+incompatible/integration/v6/isolated/logs_command_test.go (about)

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