github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/application/events_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/commands/application"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  
    14  	"code.cloudfoundry.org/cli/cf/api/appevents/appeventsfakes"
    15  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig/coreconfigfakes"
    16  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    17  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    18  
    19  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  const TIMESTAMP_FORMAT = "2006-01-02T15:04:05.00-0700"
    25  
    26  var _ = Describe("events command", func() {
    27  	var (
    28  		reqFactory  *requirementsfakes.FakeFactory
    29  		eventsRepo  *appeventsfakes.FakeAppEventsRepository
    30  		ui          *testterm.FakeUI
    31  		config      *coreconfigfakes.FakeRepository
    32  		deps        commandregistry.Dependency
    33  		flagContext flags.FlagContext
    34  
    35  		loginRequirement         requirements.Requirement
    36  		targetedSpaceRequirement requirements.Requirement
    37  		applicationRequirement   *requirementsfakes.FakeApplicationRequirement
    38  
    39  		cmd *application.Events
    40  	)
    41  
    42  	BeforeEach(func() {
    43  		cmd = &application.Events{}
    44  
    45  		ui = new(testterm.FakeUI)
    46  		eventsRepo = new(appeventsfakes.FakeAppEventsRepository)
    47  		config = new(coreconfigfakes.FakeRepository)
    48  
    49  		config.OrganizationFieldsReturns(models.OrganizationFields{Name: "my-org"})
    50  		config.SpaceFieldsReturns(models.SpaceFields{Name: "my-space"})
    51  		config.UsernameReturns("my-user")
    52  
    53  		deps = commandregistry.Dependency{
    54  			UI:          ui,
    55  			RepoLocator: api.RepositoryLocator{}.SetAppEventsRepository(eventsRepo),
    56  			Config:      config,
    57  		}
    58  
    59  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    60  
    61  		reqFactory = new(requirementsfakes.FakeFactory)
    62  		loginRequirement = &passingRequirement{Name: "login-requirement"}
    63  		reqFactory.NewLoginRequirementReturns(loginRequirement)
    64  		targetedSpaceRequirement = &passingRequirement{Name: "targeted-space-requirement"}
    65  		reqFactory.NewTargetedSpaceRequirementReturns(targetedSpaceRequirement)
    66  		applicationRequirement = new(requirementsfakes.FakeApplicationRequirement)
    67  		applicationRequirement.ExecuteReturns(nil)
    68  		reqFactory.NewApplicationRequirementReturns(applicationRequirement)
    69  	})
    70  
    71  	Describe("Requirements", func() {
    72  		BeforeEach(func() {
    73  			cmd.SetDependency(deps, false)
    74  		})
    75  
    76  		Context("when not provided exactly 1 argument", func() {
    77  			It("fails", func() {
    78  				err := flagContext.Parse("too", "many")
    79  				Expect(err).NotTo(HaveOccurred())
    80  				_, err = cmd.Requirements(reqFactory, flagContext)
    81  				Expect(err).To(HaveOccurred())
    82  				Expect(ui.Outputs()).To(ContainSubstrings(
    83  					[]string{"Incorrect Usage", "Requires an argument"},
    84  				))
    85  			})
    86  		})
    87  
    88  		Context("when provided exactly one arg", func() {
    89  			var actualRequirements []requirements.Requirement
    90  
    91  			BeforeEach(func() {
    92  				err := flagContext.Parse("service-name")
    93  				Expect(err).NotTo(HaveOccurred())
    94  				actualRequirements, err = cmd.Requirements(reqFactory, flagContext)
    95  				Expect(err).NotTo(HaveOccurred())
    96  			})
    97  
    98  			It("returns a LoginRequirement", func() {
    99  				Expect(reqFactory.NewLoginRequirementCallCount()).To(Equal(1))
   100  				Expect(actualRequirements).To(ContainElement(loginRequirement))
   101  			})
   102  
   103  			It("returns a TargetedSpaceRequirement", func() {
   104  				Expect(reqFactory.NewTargetedSpaceRequirementCallCount()).To(Equal(1))
   105  				Expect(actualRequirements).To(ContainElement(targetedSpaceRequirement))
   106  			})
   107  
   108  			It("returns a ApplicationRequirement", func() {
   109  				Expect(reqFactory.NewApplicationRequirementCallCount()).To(Equal(1))
   110  				Expect(actualRequirements).To(ContainElement(applicationRequirement))
   111  			})
   112  		})
   113  	})
   114  
   115  	Describe("Execute", func() {
   116  		var executeCmdErr error
   117  
   118  		BeforeEach(func() {
   119  			applicationRequirement.GetApplicationReturns(models.Application{
   120  				ApplicationFields: models.ApplicationFields{
   121  					Name: "my-app",
   122  					GUID: "my-app-guid",
   123  				},
   124  			})
   125  
   126  			err := flagContext.Parse("my-app")
   127  			Expect(err).NotTo(HaveOccurred())
   128  		})
   129  
   130  		JustBeforeEach(func() {
   131  			executeCmdErr = cmd.Execute(flagContext)
   132  		})
   133  
   134  		Context("when no events exist", func() {
   135  			BeforeEach(func() {
   136  				eventsRepo.RecentEventsReturns([]models.EventFields{}, nil)
   137  
   138  				cmd.SetDependency(deps, false)
   139  				cmd.Requirements(reqFactory, flagContext)
   140  			})
   141  
   142  			It("tells the user", func() {
   143  				Expect(executeCmdErr).NotTo(HaveOccurred())
   144  				Expect(ui.Outputs()).To(ContainSubstrings(
   145  					[]string{"events", "my-app"},
   146  					[]string{"No events", "my-app"},
   147  				))
   148  			})
   149  		})
   150  
   151  		Context("when events exist", func() {
   152  			var (
   153  				earlierTimestamp time.Time
   154  				timestamp        time.Time
   155  			)
   156  
   157  			BeforeEach(func() {
   158  				var err error
   159  
   160  				earlierTimestamp, err = time.Parse(TIMESTAMP_FORMAT, "1999-12-31T23:59:11.00-0000")
   161  				Expect(err).NotTo(HaveOccurred())
   162  
   163  				timestamp, err = time.Parse(TIMESTAMP_FORMAT, "2000-01-01T00:01:11.00-0000")
   164  				Expect(err).NotTo(HaveOccurred())
   165  
   166  				eventsRepo.RecentEventsReturns([]models.EventFields{
   167  					{
   168  						GUID:        "event-guid-1",
   169  						Name:        "app crashed",
   170  						Timestamp:   earlierTimestamp,
   171  						Description: "reason: app instance exited, exit_status: 78",
   172  						Actor:       "george-clooney",
   173  						ActorName:   "George Clooney",
   174  					},
   175  					{
   176  						GUID:        "event-guid-2",
   177  						Name:        "app crashed",
   178  						Timestamp:   timestamp,
   179  						Description: "reason: app instance was stopped, exit_status: 77",
   180  						Actor:       "marcel-marceau",
   181  					},
   182  				}, nil)
   183  
   184  				cmd.SetDependency(deps, false)
   185  				cmd.Requirements(reqFactory, flagContext)
   186  			})
   187  
   188  			It("lists events given an app name", func() {
   189  				Expect(executeCmdErr).NotTo(HaveOccurred())
   190  				Expect(eventsRepo.RecentEventsCallCount()).To(Equal(1))
   191  				appGUID, limit := eventsRepo.RecentEventsArgsForCall(0)
   192  				Expect(limit).To(Equal(int64(50)))
   193  				Expect(appGUID).To(Equal("my-app-guid"))
   194  
   195  				Expect(ui.Outputs()).To(ContainSubstrings(
   196  					[]string{"Getting events for app", "my-app", "my-org", "my-space", "my-user"},
   197  					[]string{"time", "event", "actor", "description"},
   198  					[]string{earlierTimestamp.Local().Format(TIMESTAMP_FORMAT), "app crashed", "George Clooney", "app instance exited", "78"},
   199  					[]string{timestamp.Local().Format(TIMESTAMP_FORMAT), "app crashed", "marcel-marceau", "app instance was stopped", "77"},
   200  				))
   201  			})
   202  		})
   203  
   204  		Context("when the request fails", func() {
   205  			BeforeEach(func() {
   206  				eventsRepo.RecentEventsReturns([]models.EventFields{}, errors.New("welp"))
   207  
   208  				cmd.SetDependency(deps, false)
   209  				cmd.Requirements(reqFactory, flagContext)
   210  			})
   211  
   212  			It("tells the user when an error occurs", func() {
   213  				Expect(executeCmdErr).To(HaveOccurred())
   214  				Expect(ui.Outputs()).To(ContainSubstrings(
   215  					[]string{"events", "my-app"},
   216  				))
   217  				errStr := executeCmdErr.Error()
   218  				Expect(errStr).To(ContainSubstring("welp"))
   219  			})
   220  		})
   221  	})
   222  })