github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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 AfterEach(func() { 100 helpers.QuickDeleteOrg(orgName) 101 }) 102 103 Context("when input is invalid", func() { 104 Context("because no app name is provided", func() { 105 It("gives an incorrect usage message", func() { 106 session := helpers.CF("logs") 107 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided")) 108 Eventually(session).Should(Say("NAME:")) 109 Eventually(session).Should(Say("logs - Tail or show recent logs for an app")) 110 Eventually(session).Should(Say("USAGE:")) 111 Eventually(session).Should(Say("cf logs APP_NAME")) 112 Eventually(session).Should(Say("OPTIONS:")) 113 Eventually(session).Should(Say("--recent\\s+Dump recent logs instead of tailing")) 114 Eventually(session).Should(Say("SEE ALSO:")) 115 Eventually(session).Should(Say("app, apps, ssh")) 116 Eventually(session).Should(Exit(1)) 117 }) 118 }) 119 120 Context("because the app does not exist", func() { 121 It("fails with an app not found message", func() { 122 session := helpers.CF("logs", "dora") 123 Eventually(session).Should(Say("FAILED")) 124 Eventually(session.Err).Should(Say("Application 'dora' not found.")) 125 Eventually(session).Should(Exit(1)) 126 }) 127 }) 128 }) 129 130 Context("when the specified app exists", func() { 131 var appName string 132 133 BeforeEach(func() { 134 appName = helpers.PrefixedRandomName("app") 135 helpers.WithHelloWorldApp(func(appDir string) { 136 Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "-u", "http")).Should(Exit(0)) 137 }) 138 }) 139 140 Context("without the --recent flag", func() { 141 It("streams logs out to the screen", func() { 142 session := helpers.CF("logs", appName) 143 defer session.Terminate() 144 145 userName, _ := helpers.GetCredentials() 146 Eventually(session).Should(Say("Retrieving logs for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 147 148 response, err := http.Get(fmt.Sprintf("http://%s.%s", appName, defaultSharedDomain())) 149 Expect(err).NotTo(HaveOccurred()) 150 Expect(response.StatusCode).To(Equal(http.StatusOK)) 151 Eventually(session).Should(Say("%s \\[APP/PROC/WEB/0\\]\\s+OUT .*? \"GET / HTTP/1.1\" 200 \\d+", helpers.ISO8601Regex)) 152 }) 153 }) 154 155 Context("with the --recent flag", func() { 156 It("displays the most recent logs and closes the stream", func() { 157 session := helpers.CF("logs", appName, "--recent") 158 userName, _ := helpers.GetCredentials() 159 Eventually(session).Should(Say("Retrieving logs for app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) 160 Eventually(session).Should(Say("%s \\[API/\\d+\\]\\s+OUT Created app with guid %s", helpers.ISO8601Regex, helpers.GUIDRegex)) 161 Eventually(session).Should(Exit(0)) 162 }) 163 }) 164 }) 165 }) 166 })