github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/v3_apps_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/v2action"
     8  	"code.cloudfoundry.org/cli/actor/v3action"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    12  	"code.cloudfoundry.org/cli/command/commandfakes"
    13  	"code.cloudfoundry.org/cli/command/translatableerror"
    14  	"code.cloudfoundry.org/cli/command/v3"
    15  	"code.cloudfoundry.org/cli/command/v3/shared/sharedfakes"
    16  	"code.cloudfoundry.org/cli/command/v3/v3fakes"
    17  	"code.cloudfoundry.org/cli/util/configv3"
    18  	"code.cloudfoundry.org/cli/util/ui"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  	. "github.com/onsi/gomega/gbytes"
    22  )
    23  
    24  var _ = Describe("v3-apps Command", func() {
    25  	var (
    26  		cmd             v3.V3AppsCommand
    27  		testUI          *ui.UI
    28  		fakeConfig      *commandfakes.FakeConfig
    29  		fakeSharedActor *commandfakes.FakeSharedActor
    30  		fakeActor       *v3fakes.FakeV3AppsActor
    31  		fakeV2Actor     *sharedfakes.FakeV2AppRouteActor
    32  		binaryName      string
    33  		executeErr      error
    34  	)
    35  
    36  	BeforeEach(func() {
    37  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    38  		fakeConfig = new(commandfakes.FakeConfig)
    39  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    40  		fakeActor = new(v3fakes.FakeV3AppsActor)
    41  		fakeV2Actor = new(sharedfakes.FakeV2AppRouteActor)
    42  
    43  		binaryName = "faceman"
    44  		fakeConfig.BinaryNameReturns(binaryName)
    45  
    46  		cmd = v3.V3AppsCommand{
    47  			UI:              testUI,
    48  			Config:          fakeConfig,
    49  			Actor:           fakeActor,
    50  			V2AppRouteActor: fakeV2Actor,
    51  			SharedActor:     fakeSharedActor,
    52  		}
    53  
    54  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    55  			Name: "some-org",
    56  			GUID: "some-org-guid",
    57  		})
    58  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    59  			Name: "some-space",
    60  			GUID: "some-space-guid",
    61  		})
    62  
    63  		fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    64  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionV3)
    65  	})
    66  
    67  	JustBeforeEach(func() {
    68  		executeErr = cmd.Execute(nil)
    69  	})
    70  
    71  	Context("when the API version is below the minimum", func() {
    72  		BeforeEach(func() {
    73  			fakeActor.CloudControllerAPIVersionReturns("0.0.0")
    74  		})
    75  
    76  		It("returns a MinimumAPIVersionNotMetError", func() {
    77  			Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{
    78  				CurrentVersion: "0.0.0",
    79  				MinimumVersion: ccversion.MinVersionV3,
    80  			}))
    81  		})
    82  
    83  		It("displays the experimental warning", func() {
    84  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    85  		})
    86  	})
    87  
    88  	Context("when checking target fails", func() {
    89  		BeforeEach(func() {
    90  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    91  		})
    92  
    93  		It("returns an error", func() {
    94  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    95  
    96  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    97  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    98  			Expect(checkTargetedOrg).To(BeTrue())
    99  			Expect(checkTargetedSpace).To(BeTrue())
   100  		})
   101  	})
   102  
   103  	Context("when the user is not logged in", func() {
   104  		var expectedErr error
   105  
   106  		BeforeEach(func() {
   107  			expectedErr = errors.New("some current user error")
   108  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   109  		})
   110  
   111  		It("return an error", func() {
   112  			Expect(executeErr).To(Equal(expectedErr))
   113  		})
   114  	})
   115  
   116  	Context("when getting the applications returns an error", func() {
   117  		var expectedErr error
   118  
   119  		BeforeEach(func() {
   120  			expectedErr = ccerror.RequestError{}
   121  			fakeActor.GetApplicationsWithProcessesBySpaceReturns([]v3action.ApplicationWithProcessSummary{}, v3action.Warnings{"warning-1", "warning-2"}, expectedErr)
   122  		})
   123  
   124  		It("returns the error and prints warnings", func() {
   125  			Expect(executeErr).To(Equal(ccerror.RequestError{}))
   126  
   127  			Expect(testUI.Out).To(Say("Getting apps in org some-org / space some-space as steve\\.\\.\\."))
   128  
   129  			Expect(testUI.Err).To(Say("warning-1"))
   130  			Expect(testUI.Err).To(Say("warning-2"))
   131  		})
   132  	})
   133  
   134  	Context("when getting routes returns an error", func() {
   135  		var expectedErr error
   136  
   137  		BeforeEach(func() {
   138  			expectedErr = ccerror.RequestError{}
   139  			fakeActor.GetApplicationsWithProcessesBySpaceReturns([]v3action.ApplicationWithProcessSummary{
   140  				{
   141  					Application: v3action.Application{
   142  						GUID:  "app-guid",
   143  						Name:  "some-app",
   144  						State: constant.ApplicationStarted,
   145  					},
   146  					ProcessSummaries: []v3action.ProcessSummary{{Process: v3action.Process{Type: "process-type"}}},
   147  				},
   148  			}, v3action.Warnings{"warning-1", "warning-2"}, nil)
   149  
   150  			fakeV2Actor.GetApplicationRoutesReturns([]v2action.Route{}, v2action.Warnings{"route-warning-1", "route-warning-2"}, expectedErr)
   151  		})
   152  
   153  		It("returns the error and prints warnings", func() {
   154  			Expect(executeErr).To(Equal(ccerror.RequestError{}))
   155  
   156  			Expect(testUI.Out).To(Say("Getting apps in org some-org / space some-space as steve\\.\\.\\."))
   157  
   158  			Expect(testUI.Err).To(Say("warning-1"))
   159  			Expect(testUI.Err).To(Say("warning-2"))
   160  			Expect(testUI.Err).To(Say("route-warning-1"))
   161  			Expect(testUI.Err).To(Say("route-warning-2"))
   162  		})
   163  	})
   164  
   165  	Context("when the route actor does not return any errors", func() {
   166  		BeforeEach(func() {
   167  			fakeV2Actor.GetApplicationRoutesStub = func(appGUID string) (v2action.Routes, v2action.Warnings, error) {
   168  				switch appGUID {
   169  				case "app-guid-1":
   170  					return []v2action.Route{
   171  							{
   172  								Host:   "some-app-1",
   173  								Domain: v2action.Domain{Name: "some-other-domain"},
   174  							},
   175  							{
   176  								Host:   "some-app-1",
   177  								Domain: v2action.Domain{Name: "some-domain"},
   178  							},
   179  						},
   180  						v2action.Warnings{"route-warning-1", "route-warning-2"},
   181  						nil
   182  				case "app-guid-2":
   183  					return []v2action.Route{
   184  							{
   185  								Host:   "some-app-2",
   186  								Domain: v2action.Domain{Name: "some-domain"},
   187  							},
   188  						},
   189  						v2action.Warnings{"route-warning-3", "route-warning-4"},
   190  						nil
   191  				default:
   192  					panic("unknown app guid")
   193  				}
   194  			}
   195  		})
   196  
   197  		Context("with existing apps", func() {
   198  			BeforeEach(func() {
   199  				appSummaries := []v3action.ApplicationWithProcessSummary{
   200  					{
   201  						Application: v3action.Application{
   202  							GUID:  "app-guid-1",
   203  							Name:  "some-app-1",
   204  							State: constant.ApplicationStarted,
   205  						},
   206  						ProcessSummaries: []v3action.ProcessSummary{
   207  							{
   208  								Process: v3action.Process{
   209  									Type: "console",
   210  								},
   211  								InstanceDetails: []v3action.ProcessInstance{},
   212  							},
   213  							{
   214  								Process: v3action.Process{
   215  									Type: "worker",
   216  								},
   217  								InstanceDetails: []v3action.ProcessInstance{
   218  									{
   219  										Index: 0,
   220  										State: constant.ProcessInstanceDown,
   221  									},
   222  								},
   223  							},
   224  							{
   225  								Process: v3action.Process{
   226  									Type: constant.ProcessTypeWeb,
   227  								},
   228  								InstanceDetails: []v3action.ProcessInstance{
   229  									v3action.ProcessInstance{
   230  										Index: 0,
   231  										State: constant.ProcessInstanceRunning,
   232  									},
   233  									v3action.ProcessInstance{
   234  										Index: 1,
   235  										State: constant.ProcessInstanceRunning,
   236  									},
   237  								},
   238  							},
   239  						},
   240  					},
   241  					{
   242  						Application: v3action.Application{
   243  							GUID:  "app-guid-2",
   244  							Name:  "some-app-2",
   245  							State: constant.ApplicationStopped,
   246  						},
   247  						ProcessSummaries: []v3action.ProcessSummary{
   248  							{
   249  								Process: v3action.Process{
   250  									Type: constant.ProcessTypeWeb,
   251  								},
   252  								InstanceDetails: []v3action.ProcessInstance{
   253  									v3action.ProcessInstance{
   254  										Index: 0,
   255  										State: constant.ProcessInstanceDown,
   256  									},
   257  									v3action.ProcessInstance{
   258  										Index: 1,
   259  										State: constant.ProcessInstanceDown,
   260  									},
   261  								},
   262  							},
   263  						},
   264  					},
   265  				}
   266  				fakeActor.GetApplicationsWithProcessesBySpaceReturns(appSummaries, v3action.Warnings{"warning-1", "warning-2"}, nil)
   267  			})
   268  
   269  			It("prints the application summary and outputs warnings", func() {
   270  				Expect(executeErr).ToNot(HaveOccurred())
   271  
   272  				Expect(testUI.Out).To(Say("Getting apps in org some-org / space some-space as steve\\.\\.\\."))
   273  
   274  				Expect(testUI.Out).To(Say("name\\s+requested state\\s+processes\\s+routes"))
   275  				Expect(testUI.Out).To(Say("some-app-1\\s+started\\s+web:2/2, console:0/0, worker:0/1\\s+some-app-1.some-other-domain, some-app-1.some-domain"))
   276  				Expect(testUI.Out).To(Say("some-app-2\\s+stopped\\s+web:0/2\\s+some-app-2.some-domain"))
   277  
   278  				Expect(testUI.Err).To(Say("warning-1"))
   279  				Expect(testUI.Err).To(Say("warning-2"))
   280  
   281  				Expect(testUI.Err).To(Say("route-warning-1"))
   282  				Expect(testUI.Err).To(Say("route-warning-2"))
   283  				Expect(testUI.Err).To(Say("route-warning-3"))
   284  				Expect(testUI.Err).To(Say("route-warning-4"))
   285  
   286  				Expect(fakeActor.GetApplicationsWithProcessesBySpaceCallCount()).To(Equal(1))
   287  				spaceGUID := fakeActor.GetApplicationsWithProcessesBySpaceArgsForCall(0)
   288  				Expect(spaceGUID).To(Equal("some-space-guid"))
   289  
   290  				Expect(fakeV2Actor.GetApplicationRoutesCallCount()).To(Equal(2))
   291  				appGUID := fakeV2Actor.GetApplicationRoutesArgsForCall(0)
   292  				Expect(appGUID).To(Equal("app-guid-1"))
   293  				appGUID = fakeV2Actor.GetApplicationRoutesArgsForCall(1)
   294  				Expect(appGUID).To(Equal("app-guid-2"))
   295  			})
   296  		})
   297  
   298  		Context("when app does not have processes", func() {
   299  			BeforeEach(func() {
   300  				appSummaries := []v3action.ApplicationWithProcessSummary{
   301  					{
   302  						Application: v3action.Application{
   303  							GUID:  "app-guid",
   304  							Name:  "some-app",
   305  							State: constant.ApplicationStarted,
   306  						},
   307  						ProcessSummaries: []v3action.ProcessSummary{},
   308  					},
   309  				}
   310  				fakeActor.GetApplicationsWithProcessesBySpaceReturns(appSummaries, v3action.Warnings{"warning"}, nil)
   311  			})
   312  
   313  			It("it does not request or display routes information for app", func() {
   314  				Expect(executeErr).ToNot(HaveOccurred())
   315  
   316  				Expect(testUI.Out).To(Say("Getting apps in org some-org / space some-space as steve\\.\\.\\."))
   317  
   318  				Expect(testUI.Out).To(Say("name\\s+requested state\\s+processes\\s+routes"))
   319  				Expect(testUI.Out).To(Say("some-app\\s+started\\s+$"))
   320  				Expect(testUI.Err).To(Say("warning"))
   321  
   322  				Expect(fakeActor.GetApplicationsWithProcessesBySpaceCallCount()).To(Equal(1))
   323  				spaceGUID := fakeActor.GetApplicationsWithProcessesBySpaceArgsForCall(0)
   324  				Expect(spaceGUID).To(Equal("some-space-guid"))
   325  
   326  				Expect(fakeV2Actor.GetApplicationRoutesCallCount()).To(Equal(0))
   327  			})
   328  		})
   329  
   330  		Context("with no apps", func() {
   331  			BeforeEach(func() {
   332  				fakeActor.GetApplicationsWithProcessesBySpaceReturns([]v3action.ApplicationWithProcessSummary{}, v3action.Warnings{"warning-1", "warning-2"}, nil)
   333  			})
   334  
   335  			It("displays there are no apps", func() {
   336  				Expect(executeErr).ToNot(HaveOccurred())
   337  
   338  				Expect(testUI.Out).To(Say("Getting apps in org some-org / space some-space as steve\\.\\.\\."))
   339  				Expect(testUI.Out).To(Say("No apps found"))
   340  			})
   341  		})
   342  	})
   343  })