github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/application/events_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"time"
     5  
     6  	testapi "github.com/cloudfoundry/cli/cf/api/app_events/fakes"
     7  	"github.com/cloudfoundry/cli/cf/command_registry"
     8  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     9  	"github.com/cloudfoundry/cli/cf/errors"
    10  	"github.com/cloudfoundry/cli/cf/models"
    11  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    12  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    13  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    14  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  
    18  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    19  )
    20  
    21  var _ = Describe("events command", func() {
    22  	var (
    23  		requirementsFactory *testreq.FakeReqFactory
    24  		eventsRepo          *testapi.FakeAppEventsRepository
    25  		ui                  *testterm.FakeUI
    26  		configRepo          core_config.Repository
    27  		deps                command_registry.Dependency
    28  	)
    29  
    30  	const TIMESTAMP_FORMAT = "2006-01-02T15:04:05.00-0700"
    31  
    32  	updateCommandDependency := func(pluginCall bool) {
    33  		deps.Ui = ui
    34  		deps.Config = configRepo
    35  		deps.RepoLocator = deps.RepoLocator.SetAppEventsRepository(eventsRepo)
    36  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("events").SetDependency(deps, pluginCall))
    37  	}
    38  
    39  	BeforeEach(func() {
    40  		eventsRepo = new(testapi.FakeAppEventsRepository)
    41  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
    42  		ui = new(testterm.FakeUI)
    43  		configRepo = testconfig.NewRepositoryWithDefaults()
    44  	})
    45  
    46  	runCommand := func(args ...string) bool {
    47  		return testcmd.RunCliCommand("events", args, requirementsFactory, updateCommandDependency, false)
    48  	}
    49  
    50  	It("fails with usage when called without an app name", func() {
    51  		passed := runCommand()
    52  		Expect(ui.Outputs).To(ContainSubstrings(
    53  			[]string{"Incorrect Usage", "Requires", "argument"},
    54  		))
    55  		Expect(passed).To(BeFalse())
    56  	})
    57  
    58  	It("lists events given an app name", func() {
    59  		earlierTimestamp, err := time.Parse(TIMESTAMP_FORMAT, "1999-12-31T23:59:11.00-0000")
    60  		Expect(err).NotTo(HaveOccurred())
    61  
    62  		timestamp, err := time.Parse(TIMESTAMP_FORMAT, "2000-01-01T00:01:11.00-0000")
    63  		Expect(err).NotTo(HaveOccurred())
    64  
    65  		app := models.Application{}
    66  		app.Name = "my-app"
    67  		app.Guid = "my-app-guid"
    68  		requirementsFactory.Application = app
    69  
    70  		eventsRepo.RecentEventsReturns([]models.EventFields{
    71  			{
    72  				Guid:        "event-guid-1",
    73  				Name:        "app crashed",
    74  				Timestamp:   earlierTimestamp,
    75  				Description: "reason: app instance exited, exit_status: 78",
    76  				ActorName:   "George Clooney",
    77  			},
    78  			{
    79  				Guid:        "event-guid-2",
    80  				Name:        "app crashed",
    81  				Timestamp:   timestamp,
    82  				Description: "reason: app instance was stopped, exit_status: 77",
    83  				ActorName:   "Marcel Marceau",
    84  			},
    85  		}, nil)
    86  
    87  		runCommand("my-app")
    88  
    89  		Expect(eventsRepo.RecentEventsCallCount()).To(Equal(1))
    90  		appGuid, limit := eventsRepo.RecentEventsArgsForCall(0)
    91  		Expect(limit).To(Equal(int64(50)))
    92  		Expect(appGuid).To(Equal("my-app-guid"))
    93  		Expect(ui.Outputs).To(ContainSubstrings(
    94  			[]string{"Getting events for app", "my-app", "my-org", "my-space", "my-user"},
    95  			[]string{"time", "event", "actor", "description"},
    96  			[]string{earlierTimestamp.Local().Format(TIMESTAMP_FORMAT), "app crashed", "George Clooney", "app instance exited", "78"},
    97  			[]string{timestamp.Local().Format(TIMESTAMP_FORMAT), "app crashed", "Marcel Marceau", "app instance was stopped", "77"},
    98  		))
    99  	})
   100  
   101  	It("tells the user when an error occurs", func() {
   102  		eventsRepo.RecentEventsReturns(nil, errors.New("welp"))
   103  
   104  		app := models.Application{}
   105  		app.Name = "my-app"
   106  		requirementsFactory.Application = app
   107  
   108  		runCommand("my-app")
   109  
   110  		Expect(ui.Outputs).To(ContainSubstrings(
   111  			[]string{"events", "my-app"},
   112  			[]string{"FAILED"},
   113  			[]string{"welp"},
   114  		))
   115  	})
   116  
   117  	It("tells the user when no events exist for that app", func() {
   118  		app := models.Application{}
   119  		app.Name = "my-app"
   120  		requirementsFactory.Application = app
   121  
   122  		runCommand("my-app")
   123  
   124  		Expect(ui.Outputs).To(ContainSubstrings(
   125  			[]string{"events", "my-app"},
   126  			[]string{"No events", "my-app"},
   127  		))
   128  	})
   129  })