github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/shared/poll_start_test.go (about) 1 package shared_test 2 3 import ( 4 "errors" 5 "time" 6 7 "code.cloudfoundry.org/cli/actor/actionerror" 8 "code.cloudfoundry.org/cli/actor/sharedaction" 9 "code.cloudfoundry.org/cli/actor/v2action" 10 "code.cloudfoundry.org/cli/command/commandfakes" 11 "code.cloudfoundry.org/cli/command/translatableerror" 12 . "code.cloudfoundry.org/cli/command/v6/shared" 13 14 "code.cloudfoundry.org/cli/util/ui" 15 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/ginkgo/extensions/table" 18 . "github.com/onsi/gomega" 19 . "github.com/onsi/gomega/gbytes" 20 ) 21 22 var _ = Describe("Poll Start", func() { 23 var ( 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 messages chan sharedaction.LogMessage 27 logErrs chan error 28 appState chan v2action.ApplicationStateChange 29 apiWarnings chan string 30 apiErrs chan error 31 err error 32 block chan bool 33 ) 34 35 BeforeEach(func() { 36 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 37 fakeConfig = new(commandfakes.FakeConfig) 38 fakeConfig.BinaryNameReturns("FiveThirtyEight") 39 40 messages = make(chan sharedaction.LogMessage) 41 logErrs = make(chan error) 42 appState = make(chan v2action.ApplicationStateChange) 43 apiWarnings = make(chan string) 44 apiErrs = make(chan error) 45 block = make(chan bool) 46 47 err = errors.New("This should never occur.") 48 }) 49 50 JustBeforeEach(func() { 51 go func() { 52 err = PollStart(testUI, fakeConfig, messages, logErrs, appState, apiWarnings, apiErrs, func() {}) 53 close(block) 54 }() 55 }) 56 57 When("no API errors appear", func() { 58 It("passes and exits with no errors", func() { 59 appState <- v2action.ApplicationStateStopping 60 appState <- v2action.ApplicationStateStaging 61 appState <- v2action.ApplicationStateStarting 62 logErrs <- actionerror.LogCacheTimeoutError{} 63 apiWarnings <- "some warning" 64 logErrs <- errors.New("some logErrhea") 65 messages <- *sharedaction.NewLogMessage( 66 "some log message", 67 "1", 68 time.Unix(0, 0), 69 "STG", 70 "some source instance") 71 messages <- *sharedaction.NewLogMessage( 72 "some other log message", 73 "1", 74 time.Unix(0, 0), 75 "APP", 76 "some other source instance") 77 close(appState) 78 apiWarnings <- "some other warning" 79 close(apiWarnings) 80 close(apiErrs) 81 close(logErrs) 82 close(messages) 83 84 Eventually(testUI.Out).Should(Say("\nStopping app...")) 85 Eventually(testUI.Out).Should(Say("\nStaging app and tracing logs...")) 86 Eventually(testUI.Out).Should(Say("some log message")) 87 Eventually(testUI.Out).Should(Say("\nWaiting for app to start...")) 88 Consistently(testUI.Out).ShouldNot(Say("some other log message")) 89 90 Eventually(testUI.Err).Should(Say("timeout connecting to log server, no log will be shown")) 91 Eventually(testUI.Err).Should(Say("some warning")) 92 Eventually(testUI.Err).Should(Say("some logErrhea")) 93 Eventually(testUI.Err).Should(Say("some other warning")) 94 95 Eventually(block).Should(BeClosed()) 96 Expect(err).ToNot(HaveOccurred()) 97 }) 98 99 When("state channel is not set", func() { 100 BeforeEach(func() { 101 appState = nil 102 }) 103 104 It("does not wait for it", func() { 105 close(apiWarnings) 106 close(apiErrs) 107 close(logErrs) 108 close(messages) 109 110 Eventually(block).Should(BeClosed()) 111 Expect(err).ToNot(HaveOccurred()) 112 }) 113 }) 114 }) 115 116 DescribeTable("API Errors", 117 func(apiErr error, expectedErr error) { 118 apiErrs <- apiErr 119 Eventually(block).Should(BeClosed()) 120 Expect(err).To(MatchError(expectedErr)) 121 }, 122 123 Entry("StagingFailedNoAppDetectedError", 124 actionerror.StagingFailedNoAppDetectedError{ 125 Reason: "some staging failure reason", 126 }, 127 translatableerror.StagingFailedNoAppDetectedError{ 128 Message: "some staging failure reason", 129 BinaryName: "FiveThirtyEight", 130 }, 131 ), 132 133 Entry("StagingFailedError", 134 actionerror.StagingFailedError{ 135 Reason: "some staging failure reason", 136 }, 137 translatableerror.StagingFailedError{ 138 Message: "some staging failure reason", 139 }, 140 ), 141 142 Entry("StagingTimeoutError", 143 actionerror.StagingTimeoutError{ 144 AppName: "some staging timeout name", 145 Timeout: time.Second, 146 }, 147 translatableerror.StagingTimeoutError{ 148 AppName: "some staging timeout name", 149 Timeout: time.Second, 150 }, 151 ), 152 153 Entry("ApplicationInstanceCrashedError", 154 actionerror.ApplicationInstanceCrashedError{ 155 Name: "some application crashed name", 156 }, 157 translatableerror.ApplicationUnableToStartError{ 158 AppName: "some application crashed name", 159 BinaryName: "FiveThirtyEight", 160 }, 161 ), 162 163 Entry("ApplicationInstanceFlappingError", 164 actionerror.ApplicationInstanceFlappingError{ 165 Name: "some application flapping name", 166 }, 167 translatableerror.ApplicationUnableToStartError{ 168 AppName: "some application flapping name", 169 BinaryName: "FiveThirtyEight", 170 }, 171 ), 172 173 Entry("StartupTimeoutError", 174 actionerror.StartupTimeoutError{ 175 Name: "some application timeout name", 176 }, 177 translatableerror.StartupTimeoutError{ 178 AppName: "some application timeout name", 179 BinaryName: "FiveThirtyEight", 180 }, 181 ), 182 183 Entry("any other error", 184 actionerror.HTTPHealthCheckInvalidError{}, 185 actionerror.HTTPHealthCheckInvalidError{}, 186 ), 187 ) 188 })