github.com/loafoe/cli@v7.1.0+incompatible/command/v7/tasks_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    10  	"code.cloudfoundry.org/cli/command/commandfakes"
    11  	. "code.cloudfoundry.org/cli/command/v7"
    12  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    13  	"code.cloudfoundry.org/cli/resources"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("tasks Command", func() {
    22  	var (
    23  		cmd             TasksCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v7fakes.FakeActor
    28  		binaryName      string
    29  		executeErr      error
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v7fakes.FakeActor)
    37  
    38  		cmd = TasksCommand{
    39  			BaseCommand: BaseCommand{
    40  				UI:          testUI,
    41  				Config:      fakeConfig,
    42  				SharedActor: fakeSharedActor,
    43  				Actor:       fakeActor,
    44  			},
    45  		}
    46  
    47  		cmd.RequiredArgs.AppName = "some-app-name"
    48  
    49  		binaryName = "faceman"
    50  		fakeConfig.BinaryNameReturns(binaryName)
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(nil)
    55  	})
    56  
    57  	When("checking target fails", func() {
    58  		BeforeEach(func() {
    59  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    60  		})
    61  
    62  		It("returns an error", func() {
    63  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    64  
    65  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    66  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    67  			Expect(checkTargetedOrg).To(BeTrue())
    68  			Expect(checkTargetedSpace).To(BeTrue())
    69  		})
    70  	})
    71  
    72  	When("the user is logged in, and a space and org are targeted", func() {
    73  		BeforeEach(func() {
    74  			fakeConfig.HasTargetedOrganizationReturns(true)
    75  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    76  				GUID: "some-org-guid",
    77  				Name: "some-org",
    78  			})
    79  			fakeConfig.HasTargetedSpaceReturns(true)
    80  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    81  				GUID: "some-space-guid",
    82  				Name: "some-space",
    83  			})
    84  		})
    85  
    86  		When("getting the current user returns an error", func() {
    87  			var expectedErr error
    88  
    89  			BeforeEach(func() {
    90  				expectedErr = errors.New("get current user error")
    91  				fakeConfig.CurrentUserReturns(
    92  					configv3.User{},
    93  					expectedErr)
    94  			})
    95  
    96  			It("returns the error", func() {
    97  				Expect(executeErr).To(MatchError(expectedErr))
    98  			})
    99  		})
   100  
   101  		When("getting the current user does not return an error", func() {
   102  			BeforeEach(func() {
   103  				fakeConfig.CurrentUserReturns(
   104  					configv3.User{Name: "some-user"},
   105  					nil)
   106  			})
   107  
   108  			When("provided a valid application name", func() {
   109  				BeforeEach(func() {
   110  					fakeActor.GetApplicationByNameAndSpaceReturns(
   111  						resources.Application{GUID: "some-app-guid"},
   112  						v7action.Warnings{"get-application-warning-1", "get-application-warning-2"},
   113  						nil)
   114  					fakeActor.GetApplicationTasksReturns(
   115  						[]v7action.Task{
   116  							{
   117  								GUID:       "task-3-guid",
   118  								SequenceID: 3,
   119  								Name:       "task-3",
   120  								State:      constant.TaskRunning,
   121  								CreatedAt:  "2016-11-08T22:26:02Z",
   122  								Command:    "some-command",
   123  							},
   124  							{
   125  								GUID:       "task-2-guid",
   126  								SequenceID: 2,
   127  								Name:       "task-2",
   128  								State:      constant.TaskFailed,
   129  								CreatedAt:  "2016-11-08T22:26:02Z",
   130  								Command:    "some-command",
   131  							},
   132  							{
   133  								GUID:       "task-1-guid",
   134  								SequenceID: 1,
   135  								Name:       "task-1",
   136  								State:      constant.TaskSucceeded,
   137  								CreatedAt:  "2016-11-08T22:26:02Z",
   138  								Command:    "some-command",
   139  							},
   140  						},
   141  						v7action.Warnings{"get-tasks-warning-1"},
   142  						nil)
   143  				})
   144  
   145  				It("outputs all tasks associated with the application and all warnings", func() {
   146  					Expect(executeErr).ToNot(HaveOccurred())
   147  
   148  					Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   149  					appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   150  					Expect(appName).To(Equal("some-app-name"))
   151  					Expect(spaceGUID).To(Equal("some-space-guid"))
   152  
   153  					Expect(fakeActor.GetApplicationTasksCallCount()).To(Equal(1))
   154  					guid, order := fakeActor.GetApplicationTasksArgsForCall(0)
   155  					Expect(guid).To(Equal("some-app-guid"))
   156  					Expect(order).To(Equal(v7action.Descending))
   157  
   158  					Expect(testUI.Out).To(Say("Getting tasks for app some-app-name in org some-org / space some-space as some-user..."))
   159  
   160  					Expect(testUI.Out).To(Say(`id\s+name\s+state\s+start time\s+command`))
   161  					Expect(testUI.Out).To(Say(`3\s+task-3\s+RUNNING\s+Tue, 08 Nov 2016 22:26:02 UTC\s+some-command`))
   162  					Expect(testUI.Out).To(Say(`2\s+task-2\s+FAILED\s+Tue, 08 Nov 2016 22:26:02 UTC\s+some-command`))
   163  					Expect(testUI.Out).To(Say(`1\s+task-1\s+SUCCEEDED\s+Tue, 08 Nov 2016 22:26:02 UTC\s+some-command`))
   164  					Expect(testUI.Err).To(Say("get-application-warning-1"))
   165  					Expect(testUI.Err).To(Say("get-application-warning-2"))
   166  					Expect(testUI.Err).To(Say("get-tasks-warning-1"))
   167  				})
   168  
   169  				When("the tasks' command fields are returned as empty strings", func() {
   170  					BeforeEach(func() {
   171  						fakeActor.GetApplicationTasksReturns(
   172  							[]v7action.Task{
   173  								{
   174  									GUID:       "task-2-guid",
   175  									SequenceID: 2,
   176  									Name:       "task-2",
   177  									State:      constant.TaskFailed,
   178  									CreatedAt:  "2016-11-08T22:26:02Z",
   179  									Command:    "",
   180  								},
   181  								{
   182  									GUID:       "task-1-guid",
   183  									SequenceID: 1,
   184  									Name:       "task-1",
   185  									State:      constant.TaskSucceeded,
   186  									CreatedAt:  "2016-11-08T22:26:02Z",
   187  									Command:    "",
   188  								},
   189  							},
   190  							v7action.Warnings{"get-tasks-warning-1"},
   191  							nil)
   192  					})
   193  
   194  					It("outputs [hidden] for the tasks' commands", func() {
   195  						Expect(executeErr).ToNot(HaveOccurred())
   196  
   197  						Expect(testUI.Out).To(Say(`2\s+task-2\s+FAILED\s+Tue, 08 Nov 2016 22:26:02 UTC\s+\[hidden\]`))
   198  						Expect(testUI.Out).To(Say(`1\s+task-1\s+SUCCEEDED\s+Tue, 08 Nov 2016 22:26:02 UTC\s+\[hidden\]`))
   199  					})
   200  				})
   201  
   202  				When("there are no tasks associated with the application", func() {
   203  					BeforeEach(func() {
   204  						fakeActor.GetApplicationTasksReturns([]v7action.Task{}, nil, nil)
   205  					})
   206  
   207  					It("outputs an empty table", func() {
   208  						Expect(executeErr).ToNot(HaveOccurred())
   209  
   210  						Expect(testUI.Out).To(Say(`No tasks found for application.`))
   211  						Expect(testUI.Out).NotTo(Say("1"))
   212  					})
   213  				})
   214  			})
   215  
   216  			When("there are errors", func() {
   217  				When("the error is translatable", func() {
   218  					When("getting the application returns the error", func() {
   219  						var (
   220  							returnedErr error
   221  							expectedErr error
   222  						)
   223  
   224  						BeforeEach(func() {
   225  							expectedErr = errors.New("request-error")
   226  							returnedErr = ccerror.RequestError{Err: expectedErr}
   227  							fakeActor.GetApplicationByNameAndSpaceReturns(
   228  								resources.Application{GUID: "some-app-guid"},
   229  								nil,
   230  								returnedErr)
   231  						})
   232  
   233  						It("returns a translatable error", func() {
   234  							Expect(executeErr).To(MatchError(ccerror.RequestError{Err: expectedErr}))
   235  						})
   236  					})
   237  
   238  					When("getting the app's tasks returns the error", func() {
   239  						var returnedErr error
   240  
   241  						BeforeEach(func() {
   242  							returnedErr = ccerror.UnverifiedServerError{URL: "some-url"}
   243  							fakeActor.GetApplicationByNameAndSpaceReturns(
   244  								resources.Application{GUID: "some-app-guid"},
   245  								nil,
   246  								nil)
   247  							fakeActor.GetApplicationTasksReturns(
   248  								[]v7action.Task{},
   249  								nil,
   250  								returnedErr)
   251  						})
   252  
   253  						It("returns a translatable error", func() {
   254  							Expect(executeErr).To(MatchError(returnedErr))
   255  						})
   256  					})
   257  				})
   258  
   259  				When("the error is not translatable", func() {
   260  					When("getting the app returns the error", func() {
   261  						var expectedErr error
   262  
   263  						BeforeEach(func() {
   264  							expectedErr = errors.New("bananapants")
   265  							fakeActor.GetApplicationByNameAndSpaceReturns(
   266  								resources.Application{GUID: "some-app-guid"},
   267  								v7action.Warnings{"get-application-warning-1", "get-application-warning-2"},
   268  								expectedErr)
   269  						})
   270  
   271  						It("return the error and outputs all warnings", func() {
   272  							Expect(executeErr).To(MatchError(expectedErr))
   273  
   274  							Expect(testUI.Err).To(Say("get-application-warning-1"))
   275  							Expect(testUI.Err).To(Say("get-application-warning-2"))
   276  						})
   277  					})
   278  
   279  					When("getting the app's tasks returns the error", func() {
   280  						var expectedErr error
   281  
   282  						BeforeEach(func() {
   283  							expectedErr = errors.New("bananapants??")
   284  							fakeActor.GetApplicationByNameAndSpaceReturns(
   285  								resources.Application{GUID: "some-app-guid"},
   286  								v7action.Warnings{"get-application-warning-1", "get-application-warning-2"},
   287  								nil)
   288  							fakeActor.GetApplicationTasksReturns(
   289  								nil,
   290  								v7action.Warnings{"get-tasks-warning-1", "get-tasks-warning-2"},
   291  								expectedErr)
   292  						})
   293  
   294  						It("returns the error and outputs all warnings", func() {
   295  							Expect(executeErr).To(MatchError(expectedErr))
   296  
   297  							Expect(testUI.Err).To(Say("get-application-warning-1"))
   298  							Expect(testUI.Err).To(Say("get-application-warning-2"))
   299  							Expect(testUI.Err).To(Say("get-tasks-warning-1"))
   300  							Expect(testUI.Err).To(Say("get-tasks-warning-2"))
   301  						})
   302  					})
   303  				})
   304  			})
   305  		})
   306  	})
   307  })