github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/v7/logs_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/actor/actionerror"
     9  	"code.cloudfoundry.org/cli/actor/sharedaction"
    10  	"code.cloudfoundry.org/cli/actor/sharedaction/sharedactionfakes"
    11  	"code.cloudfoundry.org/cli/actor/v7action"
    12  	"code.cloudfoundry.org/cli/command/commandfakes"
    13  	. "code.cloudfoundry.org/cli/command/v7"
    14  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    15  	"code.cloudfoundry.org/cli/util/configv3"
    16  	"code.cloudfoundry.org/cli/util/ui"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  	. "github.com/onsi/gomega/gbytes"
    20  )
    21  
    22  var _ = Describe("logs command", func() {
    23  	var (
    24  		cmd             LogsCommand
    25  		testUI          *ui.UI
    26  		fakeConfig      *commandfakes.FakeConfig
    27  		fakeSharedActor *commandfakes.FakeSharedActor
    28  		fakeActor       *v7fakes.FakeLogsActor
    29  		logCacheClient  *sharedactionfakes.FakeLogCacheClient
    30  		binaryName      string
    31  		executeErr      error
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    36  		fakeConfig = new(commandfakes.FakeConfig)
    37  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    38  		fakeActor = new(v7fakes.FakeLogsActor)
    39  		logCacheClient = new(sharedactionfakes.FakeLogCacheClient)
    40  
    41  		cmd = LogsCommand{
    42  			UI:             testUI,
    43  			Config:         fakeConfig,
    44  			SharedActor:    fakeSharedActor,
    45  			Actor:          fakeActor,
    46  			LogCacheClient: logCacheClient,
    47  		}
    48  
    49  		binaryName = "faceman"
    50  		fakeConfig.BinaryNameReturns(binaryName)
    51  		cmd.RequiredArgs.AppName = "some-app"
    52  		fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    53  	})
    54  
    55  	JustBeforeEach(func() {
    56  		executeErr = cmd.Execute(nil)
    57  	})
    58  
    59  	When("the checkTarget fails", func() {
    60  		BeforeEach(func() {
    61  			fakeSharedActor.CheckTargetReturns(
    62  				actionerror.NotLoggedInError{BinaryName: binaryName})
    63  		})
    64  		It("returns an error", func() {
    65  			orgRequired, spaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
    66  			Expect(orgRequired).To(BeTrue())
    67  			Expect(spaceRequired).To(BeTrue())
    68  
    69  			Expect(executeErr).To(MatchError(
    70  				actionerror.NotLoggedInError{BinaryName: binaryName}))
    71  		})
    72  	})
    73  
    74  	When("checkTarget succeeds", func() {
    75  		BeforeEach(func() {
    76  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    77  				Name: "some-space-name",
    78  				GUID: "some-space-guid",
    79  			})
    80  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    81  				Name: "some-org-name",
    82  			})
    83  		})
    84  
    85  		When("the --recent flag is provided", func() {
    86  			BeforeEach(func() {
    87  				cmd.Recent = true
    88  			})
    89  
    90  			It("displays flavor text", func() {
    91  				Expect(testUI.Out).To(Say("Retrieving logs for app some-app in org some-org-name / space some-space-name as some-user..."))
    92  			})
    93  
    94  			When("the logs actor returns an error", func() {
    95  				var expectedErr error
    96  				BeforeEach(func() {
    97  					expectedErr = errors.New("some-error")
    98  					fakeActor.GetRecentLogsForApplicationByNameAndSpaceReturns(
    99  						[]sharedaction.LogMessage{
   100  							*sharedaction.NewLogMessage(
   101  								"all your base are belong to us",
   102  								"1",
   103  								time.Unix(0, 0),
   104  								"app",
   105  								"1",
   106  							),
   107  						},
   108  						v7action.Warnings{"some-warning-1", "some-warning-2"},
   109  						expectedErr)
   110  				})
   111  
   112  				It("displays the errors along with the logs and warnings", func() {
   113  					Expect(executeErr).To(MatchError(expectedErr))
   114  					Expect(testUI.Out).To(Say("all your base are belong to us"))
   115  					Expect(testUI.Err).To(Say("some-warning-1"))
   116  					Expect(testUI.Err).To(Say("some-warning-2"))
   117  				})
   118  			})
   119  
   120  			When("the logs actor returns logs", func() {
   121  				BeforeEach(func() {
   122  					fakeActor.GetRecentLogsForApplicationByNameAndSpaceReturns(
   123  						[]sharedaction.LogMessage{
   124  							*sharedaction.NewLogMessage(
   125  								"i am message 1",
   126  								"1",
   127  								time.Unix(0, 0),
   128  								"app",
   129  								"1",
   130  							),
   131  							*sharedaction.NewLogMessage(
   132  								"i am message 2",
   133  								"1",
   134  								time.Unix(1, 0),
   135  								"another-app",
   136  								"2",
   137  							),
   138  						},
   139  						v7action.Warnings{"some-warning-1", "some-warning-2"},
   140  						nil)
   141  				})
   142  
   143  				It("displays the recent log messages and warnings", func() {
   144  					Expect(executeErr).NotTo(HaveOccurred())
   145  					Expect(testUI.Err).To(Say("some-warning-1"))
   146  					Expect(testUI.Err).To(Say("some-warning-2"))
   147  
   148  					Expect(testUI.Out).To(Say("i am message 1"))
   149  					Expect(testUI.Out).To(Say("i am message 2"))
   150  
   151  					Expect(fakeActor.GetRecentLogsForApplicationByNameAndSpaceCallCount()).To(Equal(1))
   152  					appName, spaceGUID, client := fakeActor.GetRecentLogsForApplicationByNameAndSpaceArgsForCall(0)
   153  
   154  					Expect(appName).To(Equal("some-app"))
   155  					Expect(spaceGUID).To(Equal("some-space-guid"))
   156  					Expect(client).To(Equal(logCacheClient))
   157  				})
   158  			})
   159  		})
   160  
   161  		When("the --recent flag is not provided", func() {
   162  			BeforeEach(func() {
   163  				cmd.Recent = false
   164  				fakeActor.ScheduleTokenRefreshStub = func() (chan bool, error) {
   165  					quitNowChannel := make(chan bool, 1)
   166  					go func() {
   167  						<-quitNowChannel
   168  					}()
   169  					return quitNowChannel, nil
   170  				}
   171  			})
   172  
   173  			When("the logs setup returns an error", func() {
   174  				var expectedErr error
   175  
   176  				BeforeEach(func() {
   177  					expectedErr = errors.New("some-error")
   178  					fakeActor.GetStreamingLogsForApplicationByNameAndSpaceReturns(nil,
   179  						nil,
   180  						nil,
   181  						v7action.Warnings{"some-warning-1",
   182  							"some-warning-2"},
   183  						expectedErr)
   184  				})
   185  
   186  				It("displays the error and all warnings", func() {
   187  					Expect(executeErr).To(MatchError(expectedErr))
   188  					Expect(testUI.Err).To(Say("some-warning-1"))
   189  					Expect(testUI.Err).To(Say("some-warning-2"))
   190  				})
   191  			})
   192  
   193  			When("the logs stream returns an error", func() {
   194  				var (
   195  					expectedErr                 error
   196  					cancelFunctionHasBeenCalled bool
   197  				)
   198  
   199  				BeforeEach(func() {
   200  					expectedErr = errors.New("banana")
   201  
   202  					fakeActor.GetStreamingLogsForApplicationByNameAndSpaceStub =
   203  						func(appName string, spaceGUID string, client sharedaction.LogCacheClient) (
   204  							<-chan sharedaction.LogMessage,
   205  							<-chan error,
   206  							context.CancelFunc,
   207  							v7action.Warnings, error) {
   208  							logStream := make(chan sharedaction.LogMessage)
   209  							errorStream := make(chan error)
   210  							cancelFunctionHasBeenCalled = false
   211  
   212  							cancelFunc := func() {
   213  								if cancelFunctionHasBeenCalled {
   214  									return
   215  								}
   216  								cancelFunctionHasBeenCalled = true
   217  								close(logStream)
   218  								close(errorStream)
   219  							}
   220  							go func() {
   221  								errorStream <- expectedErr
   222  							}()
   223  
   224  							return logStream, errorStream, cancelFunc, v7action.Warnings{"steve for all I care"}, nil
   225  						}
   226  				})
   227  
   228  				When("the token refresher returns an error", func() {
   229  					BeforeEach(func() {
   230  						cmd.Recent = false
   231  						fakeActor.ScheduleTokenRefreshReturns(nil, errors.New("firs swimming"))
   232  					})
   233  					It("displays the errors", func() {
   234  						Expect(executeErr).To(MatchError("firs swimming"))
   235  						Expect(fakeActor.GetStreamingLogsForApplicationByNameAndSpaceCallCount()).To(Equal(0))
   236  					})
   237  				})
   238  
   239  				It("displays the error and all warnings", func() {
   240  					Expect(executeErr).To(MatchError("banana"))
   241  					Expect(testUI.Err).To(Say("steve for all I care"))
   242  					Expect(cancelFunctionHasBeenCalled).To(BeTrue())
   243  				})
   244  			})
   245  
   246  			When("the logs actor returns logs", func() {
   247  				BeforeEach(func() {
   248  					fakeActor.GetStreamingLogsForApplicationByNameAndSpaceStub =
   249  						func(_ string, _ string, _ sharedaction.LogCacheClient) (
   250  							<-chan sharedaction.LogMessage,
   251  							<-chan error, context.CancelFunc,
   252  							v7action.Warnings,
   253  							error) {
   254  
   255  							logStream := make(chan sharedaction.LogMessage)
   256  							errorStream := make(chan error)
   257  
   258  							go func() {
   259  								logStream <- *sharedaction.NewLogMessage("Here are some staging logs!", "OUT", time.Now(), sharedaction.StagingLog, "sourceInstance") //TODO: is it ok to leave staging logs here?
   260  								logStream <- *sharedaction.NewLogMessage("Here are some other staging logs!", "OUT", time.Now(), sharedaction.StagingLog, "sourceInstance")
   261  								close(logStream)
   262  								close(errorStream)
   263  							}()
   264  
   265  							return logStream, errorStream, func() {}, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil
   266  						}
   267  				})
   268  
   269  				It("displays flavor text", func() {
   270  					Expect(testUI.Out).To(Say("Retrieving logs for app some-app in org some-org-name / space some-space-name as some-user..."))
   271  				})
   272  
   273  				It("displays all streaming log messages and warnings", func() {
   274  					Expect(executeErr).NotTo(HaveOccurred())
   275  					Expect(testUI.Err).To(Say("some-warning-1"))
   276  					Expect(testUI.Err).To(Say("some-warning-2"))
   277  
   278  					Expect(testUI.Out).To(Say("Here are some staging logs!"))
   279  					Expect(testUI.Out).To(Say("Here are some other staging logs!"))
   280  
   281  					Expect(fakeActor.GetStreamingLogsForApplicationByNameAndSpaceCallCount()).To(Equal(1))
   282  					appName, spaceGUID, client := fakeActor.GetStreamingLogsForApplicationByNameAndSpaceArgsForCall(0)
   283  
   284  					Expect(appName).To(Equal("some-app"))
   285  					Expect(spaceGUID).To(Equal("some-space-guid"))
   286  					Expect(client).To(Equal(logCacheClient))
   287  				})
   288  			})
   289  		})
   290  	})
   291  })