github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/tasks_command_test.go (about)

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