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