github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/run_task_command_test.go (about)

     1  package v3_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     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/flag"
    12  	"code.cloudfoundry.org/cli/command/translatableerror"
    13  	"code.cloudfoundry.org/cli/command/v3"
    14  	"code.cloudfoundry.org/cli/command/v3/v3fakes"
    15  	"code.cloudfoundry.org/cli/types"
    16  	"code.cloudfoundry.org/cli/util/configv3"
    17  	"code.cloudfoundry.org/cli/util/ui"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	. "github.com/onsi/gomega/gbytes"
    21  )
    22  
    23  var _ = Describe("run-task Command", func() {
    24  	var (
    25  		cmd             v3.RunTaskCommand
    26  		testUI          *ui.UI
    27  		fakeConfig      *commandfakes.FakeConfig
    28  		fakeSharedActor *commandfakes.FakeSharedActor
    29  		fakeActor       *v3fakes.FakeRunTaskActor
    30  		binaryName      string
    31  		executeErr      error
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    36  		fakeConfig = new(commandfakes.FakeConfig)
    37  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    38  		fakeActor = new(v3fakes.FakeRunTaskActor)
    39  
    40  		cmd = v3.RunTaskCommand{
    41  			UI:          testUI,
    42  			Config:      fakeConfig,
    43  			SharedActor: fakeSharedActor,
    44  			Actor:       fakeActor,
    45  		}
    46  
    47  		cmd.RequiredArgs.AppName = "some-app-name"
    48  		cmd.RequiredArgs.Command = "some command"
    49  
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionRunTaskV3)
    53  	})
    54  
    55  	JustBeforeEach(func() {
    56  		executeErr = cmd.Execute(nil)
    57  	})
    58  
    59  	Context("when the API version is below the minimum", func() {
    60  		BeforeEach(func() {
    61  			fakeActor.CloudControllerAPIVersionReturns("0.0.0")
    62  		})
    63  
    64  		It("returns a MinimumAPIVersionNotMetError", func() {
    65  			Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{
    66  				CurrentVersion: "0.0.0",
    67  				MinimumVersion: ccversion.MinVersionRunTaskV3,
    68  			}))
    69  		})
    70  	})
    71  
    72  	Context("when checking target fails", func() {
    73  		BeforeEach(func() {
    74  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    75  		})
    76  
    77  		It("returns an error", func() {
    78  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    79  
    80  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    81  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    82  			Expect(checkTargetedOrg).To(BeTrue())
    83  			Expect(checkTargetedSpace).To(BeTrue())
    84  		})
    85  	})
    86  
    87  	Context("when the user is logged in, and a space and org are targeted", func() {
    88  		BeforeEach(func() {
    89  			fakeConfig.HasTargetedOrganizationReturns(true)
    90  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    91  				GUID: "some-org-guid",
    92  				Name: "some-org",
    93  			})
    94  			fakeConfig.HasTargetedSpaceReturns(true)
    95  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    96  				GUID: "some-space-guid",
    97  				Name: "some-space",
    98  			})
    99  		})
   100  
   101  		Context("when getting the current user returns an error", func() {
   102  			var expectedErr error
   103  
   104  			BeforeEach(func() {
   105  				expectedErr = errors.New("got bananapants??")
   106  				fakeConfig.CurrentUserReturns(
   107  					configv3.User{},
   108  					expectedErr)
   109  			})
   110  
   111  			It("returns the error", func() {
   112  				Expect(executeErr).To(MatchError(expectedErr))
   113  			})
   114  		})
   115  
   116  		Context("when getting the current user does not return an error", func() {
   117  			BeforeEach(func() {
   118  				fakeConfig.CurrentUserReturns(
   119  					configv3.User{Name: "some-user"},
   120  					nil)
   121  			})
   122  
   123  			Context("when provided a valid application name", func() {
   124  				BeforeEach(func() {
   125  					fakeActor.GetApplicationByNameAndSpaceReturns(
   126  						v3action.Application{GUID: "some-app-guid"},
   127  						v3action.Warnings{"get-application-warning-1", "get-application-warning-2"},
   128  						nil)
   129  				})
   130  
   131  				Context("when the task name is not provided", func() {
   132  					BeforeEach(func() {
   133  						fakeActor.RunTaskReturns(
   134  							v3action.Task{
   135  								Name:       "31337ddd",
   136  								SequenceID: 3,
   137  							},
   138  							v3action.Warnings{"get-application-warning-3"},
   139  							nil)
   140  					})
   141  
   142  					It("creates a new task and displays all warnings", func() {
   143  						Expect(executeErr).ToNot(HaveOccurred())
   144  
   145  						Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   146  						appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   147  						Expect(appName).To(Equal("some-app-name"))
   148  						Expect(spaceGUID).To(Equal("some-space-guid"))
   149  
   150  						Expect(fakeActor.RunTaskCallCount()).To(Equal(1))
   151  						appGUID, task := fakeActor.RunTaskArgsForCall(0)
   152  						Expect(appGUID).To(Equal("some-app-guid"))
   153  						Expect(task).To(Equal(v3action.Task{Command: "some command"}))
   154  
   155  						Expect(testUI.Out).To(Say("Creating task for app some-app-name in org some-org / space some-space as some-user..."))
   156  						Expect(testUI.Out).To(Say("OK"))
   157  
   158  						Expect(testUI.Out).To(Say("Task has been submitted successfully for execution."))
   159  						Expect(testUI.Out).To(Say("task name:\\s+31337ddd"))
   160  						Expect(testUI.Out).To(Say("task id:\\s+3"))
   161  
   162  						Expect(testUI.Err).To(Say("get-application-warning-1"))
   163  						Expect(testUI.Err).To(Say("get-application-warning-2"))
   164  						Expect(testUI.Err).To(Say("get-application-warning-3"))
   165  					})
   166  				})
   167  
   168  				Context("when task disk space is provided", func() {
   169  					BeforeEach(func() {
   170  						cmd.Name = "some-task-name"
   171  						cmd.Disk = flag.Megabytes{NullUint64: types.NullUint64{Value: 321, IsSet: true}}
   172  						cmd.Memory = flag.Megabytes{NullUint64: types.NullUint64{Value: 123, IsSet: true}}
   173  						fakeActor.RunTaskReturns(
   174  							v3action.Task{
   175  								Name:       "some-task-name",
   176  								SequenceID: 3,
   177  							},
   178  							v3action.Warnings{"get-application-warning-3"},
   179  							nil)
   180  					})
   181  
   182  					It("creates a new task and outputs all warnings", func() {
   183  						Expect(executeErr).ToNot(HaveOccurred())
   184  
   185  						Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   186  						appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   187  						Expect(appName).To(Equal("some-app-name"))
   188  						Expect(spaceGUID).To(Equal("some-space-guid"))
   189  
   190  						Expect(fakeActor.RunTaskCallCount()).To(Equal(1))
   191  						appGUID, task := fakeActor.RunTaskArgsForCall(0)
   192  						Expect(appGUID).To(Equal("some-app-guid"))
   193  						Expect(task).To(Equal(v3action.Task{
   194  							Command:    "some command",
   195  							Name:       "some-task-name",
   196  							DiskInMB:   321,
   197  							MemoryInMB: 123,
   198  						}))
   199  
   200  						Expect(testUI.Out).To(Say("Creating task for app some-app-name in org some-org / space some-space as some-user..."))
   201  						Expect(testUI.Out).To(Say("OK"))
   202  
   203  						Expect(testUI.Out).To(Say("Task has been submitted successfully for execution."))
   204  						Expect(testUI.Out).To(Say("task name:\\s+some-task-name"))
   205  						Expect(testUI.Out).To(Say("task id:\\s+3"))
   206  
   207  						Expect(testUI.Err).To(Say("get-application-warning-1"))
   208  						Expect(testUI.Err).To(Say("get-application-warning-2"))
   209  						Expect(testUI.Err).To(Say("get-application-warning-3"))
   210  					})
   211  				})
   212  			})
   213  
   214  			Context("when there are errors", func() {
   215  				Context("when the error is translatable", func() {
   216  					Context("when getting the app returns the error", func() {
   217  						var (
   218  							returnedErr error
   219  							expectedErr error
   220  						)
   221  
   222  						BeforeEach(func() {
   223  							expectedErr = errors.New("request-error")
   224  							returnedErr = ccerror.RequestError{Err: expectedErr}
   225  							fakeActor.GetApplicationByNameAndSpaceReturns(
   226  								v3action.Application{GUID: "some-app-guid"},
   227  								nil,
   228  								returnedErr)
   229  						})
   230  
   231  						It("returns a translatable error", func() {
   232  							Expect(executeErr).To(MatchError(ccerror.RequestError{Err: expectedErr}))
   233  						})
   234  					})
   235  
   236  					Context("when running the task returns the error", func() {
   237  						var returnedErr error
   238  
   239  						BeforeEach(func() {
   240  							returnedErr = ccerror.UnverifiedServerError{URL: "some-url"}
   241  							fakeActor.GetApplicationByNameAndSpaceReturns(
   242  								v3action.Application{GUID: "some-app-guid"},
   243  								nil,
   244  								nil)
   245  							fakeActor.RunTaskReturns(
   246  								v3action.Task{},
   247  								nil,
   248  								returnedErr)
   249  						})
   250  
   251  						It("returns a translatable error", func() {
   252  							Expect(executeErr).To(MatchError(returnedErr))
   253  						})
   254  					})
   255  				})
   256  
   257  				Context("when the error is not translatable", func() {
   258  					Context("when getting the app returns the error", func() {
   259  						var expectedErr error
   260  
   261  						BeforeEach(func() {
   262  							expectedErr = errors.New("got bananapants??")
   263  							fakeActor.GetApplicationByNameAndSpaceReturns(
   264  								v3action.Application{GUID: "some-app-guid"},
   265  								v3action.Warnings{"get-application-warning-1", "get-application-warning-2"},
   266  								expectedErr)
   267  						})
   268  
   269  						It("return the error and all warnings", func() {
   270  							Expect(executeErr).To(MatchError(expectedErr))
   271  
   272  							Expect(testUI.Err).To(Say("get-application-warning-1"))
   273  							Expect(testUI.Err).To(Say("get-application-warning-2"))
   274  						})
   275  					})
   276  
   277  					Context("when running the task returns an error", func() {
   278  						var expectedErr error
   279  
   280  						BeforeEach(func() {
   281  							expectedErr = errors.New("got bananapants??")
   282  							fakeActor.GetApplicationByNameAndSpaceReturns(
   283  								v3action.Application{GUID: "some-app-guid"},
   284  								v3action.Warnings{"get-application-warning-1", "get-application-warning-2"},
   285  								nil)
   286  							fakeActor.RunTaskReturns(
   287  								v3action.Task{},
   288  								v3action.Warnings{"run-task-warning-1", "run-task-warning-2"},
   289  								expectedErr)
   290  						})
   291  
   292  						It("returns the error and all warnings", func() {
   293  							Expect(executeErr).To(MatchError(expectedErr))
   294  
   295  							Expect(testUI.Err).To(Say("get-application-warning-1"))
   296  							Expect(testUI.Err).To(Say("get-application-warning-2"))
   297  							Expect(testUI.Err).To(Say("run-task-warning-1"))
   298  							Expect(testUI.Err).To(Say("run-task-warning-2"))
   299  						})
   300  					})
   301  				})
   302  			})
   303  		})
   304  	})
   305  })