github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/integration/isolated/logs_command_test.go (about) 1 package isolated 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "code.cloudfoundry.org/cli/integration/helpers" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 . "github.com/onsi/gomega/gbytes" 11 . "github.com/onsi/gomega/gexec" 12 ) 13 14 var _ = Describe("Logs Command", func() { 15 Describe("help", func() { 16 It("displays command usage to output", func() { 17 session := helpers.CF("logs", "--help") 18 Eventually(session).Should(Say("NAME:")) 19 Eventually(session).Should(Say("logs - Tail or show recent logs for an app")) 20 Eventually(session).Should(Say("USAGE:")) 21 Eventually(session).Should(Say("cf logs APP_NAME")) 22 Eventually(session).Should(Say("OPTIONS:")) 23 Eventually(session).Should(Say("--recent\\s+Dump recent logs instead of tailing")) 24 Eventually(session).Should(Say("SEE ALSO:")) 25 Eventually(session).Should(Say("app, apps, ssh")) 26 Eventually(session).Should(Exit(0)) 27 }) 28 }) 29 30 Context("when the environment is not setup correctly", func() { 31 Context("when no API endpoint is set", func() { 32 BeforeEach(func() { 33 helpers.UnsetAPI() 34 }) 35 36 It("fails with no API endpoint message", func() { 37 session := helpers.CF("logs", "dora") 38 Eventually(session).Should(Say("FAILED")) 39 Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint")) 40 Eventually(session).Should(Exit(1)) 41 }) 42 }) 43 44 Context("not logged in", func() { 45 BeforeEach(func() { 46 helpers.LogoutCF() 47 }) 48 49 It("fails with not logged in message", func() { 50 session := helpers.CF("logs", "dora") 51 Eventually(session).Should(Say("FAILED")) 52 Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in.")) 53 Eventually(session).Should(Exit(1)) 54 }) 55 }) 56 57 Context("when no org is targeted", func() { 58 BeforeEach(func() { 59 helpers.LogoutCF() 60 helpers.LoginCF() // uses the "cf auth" command, which loses the targeted org and space (cf login does not) 61 }) 62 63 It("fails with no org or space targeted message", func() { 64 session := helpers.CF("logs", "dora") 65 Eventually(session).Should(Say("FAILED")) 66 Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org.")) 67 Eventually(session).Should(Exit(1)) 68 }) 69 }) 70 71 Context("when no space is targeted", func() { 72 BeforeEach(func() { 73 helpers.LogoutCF() 74 helpers.LoginCF() // uses the "cf auth" command, which loses the targeted org and space (cf login does not) 75 helpers.TargetOrg(ReadOnlyOrg) 76 }) 77 78 It("fails with no space targeted message", func() { 79 session := helpers.CF("logs", "dora") 80 Eventually(session.Out).Should(Say("FAILED")) 81 Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space")) 82 Eventually(session).Should(Exit(1)) 83 }) 84 }) 85 }) 86 87 Context("when the environment is set up correctly", func() { 88 var ( 89 orgName string 90 spaceName string 91 ) 92 93 BeforeEach(func() { 94 orgName = helpers.NewOrgName() 95 spaceName = helpers.NewSpaceName() 96 setupCF(orgName, spaceName) 97 }) 98 99 Context("when input is invalid", func() { 100 Context("because no app name is provided", func() { 101 It("gives an incorrect usage message", func() { 102 session := helpers.CF("logs") 103 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided")) 104 Eventually(session).Should(Say("NAME:")) 105 Eventually(session).Should(Say("logs - Tail or show recent logs for an app")) 106 Eventually(session).Should(Say("USAGE:")) 107 Eventually(session).Should(Say("cf logs APP_NAME")) 108 Eventually(session).Should(Say("OPTIONS:")) 109 Eventually(session).Should(Say("--recent\\s+Dump recent logs instead of tailing")) 110 Eventually(session).Should(Say("SEE ALSO:")) 111 Eventually(session).Should(Say("app, apps, ssh")) 112 Eventually(session).Should(Exit(1)) 113 }) 114 }) 115 116 Context("because the app does not exist", func() { 117 It("fails with an app not found message", func() { 118 session := helpers.CF("logs", "dora") 119 Eventually(session).Should(Say("FAILED")) 120 Eventually(session.Err).Should(Say("Application 'dora' not found.")) 121 Eventually(session).Should(Exit(1)) 122 }) 123 }) 124 }) 125 126 Context("when the specified app exists", func() { 127 var appName string 128 129 BeforeEach(func() { 130 appName = helpers.PrefixedRandomName("app") 131 helpers.WithHelloWorldApp(func(appDir string) { 132 Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "-u", "http")).Should(Exit(0)) 133 }) 134 }) 135 136 Context("without the --recent flag", func() { 137 It("streams logs out to the screen", func() { 138 session := helpers.CF("logs", appName) 139 defer session.Terminate() 140 141 userName, _ := helpers.GetCredentials() 142 Eventually(session).Should(Say("Retrieving logs for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 143 144 response, err := http.Get(fmt.Sprintf("http://%s.%s", appName, defaultSharedDomain())) 145 Expect(err).NotTo(HaveOccurred()) 146 Expect(response.StatusCode).To(Equal(http.StatusOK)) 147 Eventually(session).Should(Say("%s \\[APP/PROC/WEB/0\\]\\s+OUT .*? \"GET / HTTP/1.1\" 200 \\d+", helpers.ISO8601Regex)) 148 }) 149 }) 150 151 Context("with the --recent flag", func() { 152 It("displays the most recent logs and closes the stream", func() { 153 session := helpers.CF("logs", appName, "--recent") 154 userName, _ := helpers.GetCredentials() 155 Eventually(session).Should(Say("Retrieving logs for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 156 Eventually(session).Should(Say("%s \\[API/\\d+\\]\\s+OUT Created app with guid %s", helpers.ISO8601Regex, helpers.GUIDRegex)) 157 Eventually(session).Should(Exit(0)) 158 }) 159 }) 160 }) 161 }) 162 })