github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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/sharedaction"
     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  
    84  	Context("when checking target fails", func() {
    85  		BeforeEach(func() {
    86  			fakeSharedActor.CheckTargetReturns(sharedaction.NoOrganizationTargetedError{BinaryName: binaryName})
    87  		})
    88  
    89  		It("returns an error", func() {
    90  			Expect(executeErr).To(MatchError(translatableerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    91  
    92  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    93  			_, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    94  			Expect(checkTargetedOrg).To(BeTrue())
    95  			Expect(checkTargetedSpace).To(BeTrue())
    96  		})
    97  	})
    98  
    99  	Context("when the user is not logged in", func() {
   100  		var expectedErr error
   101  
   102  		BeforeEach(func() {
   103  			expectedErr = errors.New("some current user error")
   104  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   105  		})
   106  
   107  		It("return an error", func() {
   108  			Expect(executeErr).To(Equal(expectedErr))
   109  		})
   110  	})
   111  
   112  	Context("when getting the applications returns an error", func() {
   113  		var expectedErr error
   114  
   115  		BeforeEach(func() {
   116  			expectedErr = ccerror.RequestError{}
   117  			fakeActor.GetApplicationsWithProcessesBySpaceReturns([]v3action.ApplicationWithProcessSummary{}, v3action.Warnings{"warning-1", "warning-2"}, expectedErr)
   118  		})
   119  
   120  		It("returns the error and prints warnings", func() {
   121  			Expect(executeErr).To(Equal(translatableerror.APIRequestError{}))
   122  
   123  			Expect(testUI.Out).To(Say("Getting apps in org some-org / space some-space as steve\\.\\.\\."))
   124  
   125  			Expect(testUI.Err).To(Say("warning-1"))
   126  			Expect(testUI.Err).To(Say("warning-2"))
   127  		})
   128  	})
   129  
   130  	Context("when getting routes returns an error", func() {
   131  		var expectedErr error
   132  
   133  		BeforeEach(func() {
   134  			expectedErr = ccerror.RequestError{}
   135  			fakeActor.GetApplicationsWithProcessesBySpaceReturns([]v3action.ApplicationWithProcessSummary{
   136  				{
   137  					Application: v3action.Application{
   138  						GUID:  "app-guid",
   139  						Name:  "some-app",
   140  						State: "STARTED",
   141  					},
   142  					ProcessSummaries: []v3action.ProcessSummary{{Process: v3action.Process{Type: "process-type"}}},
   143  				},
   144  			}, v3action.Warnings{"warning-1", "warning-2"}, nil)
   145  
   146  			fakeV2Actor.GetApplicationRoutesReturns([]v2action.Route{}, v2action.Warnings{"route-warning-1", "route-warning-2"}, expectedErr)
   147  		})
   148  
   149  		It("returns the error and prints warnings", func() {
   150  			Expect(executeErr).To(Equal(translatableerror.APIRequestError{}))
   151  
   152  			Expect(testUI.Out).To(Say("Getting apps in org some-org / space some-space as steve\\.\\.\\."))
   153  
   154  			Expect(testUI.Err).To(Say("warning-1"))
   155  			Expect(testUI.Err).To(Say("warning-2"))
   156  			Expect(testUI.Err).To(Say("route-warning-1"))
   157  			Expect(testUI.Err).To(Say("route-warning-2"))
   158  		})
   159  	})
   160  
   161  	Context("when the route actor does not return any errors", func() {
   162  		BeforeEach(func() {
   163  			fakeV2Actor.GetApplicationRoutesStub = func(appGUID string) (v2action.Routes, v2action.Warnings, error) {
   164  				switch appGUID {
   165  				case "app-guid-1":
   166  					return []v2action.Route{
   167  							{
   168  								Host:   "some-app-1",
   169  								Domain: v2action.Domain{Name: "some-other-domain"},
   170  							},
   171  							{
   172  								Host:   "some-app-1",
   173  								Domain: v2action.Domain{Name: "some-domain"},
   174  							},
   175  						},
   176  						v2action.Warnings{"route-warning-1", "route-warning-2"},
   177  						nil
   178  				case "app-guid-2":
   179  					return []v2action.Route{
   180  							{
   181  								Host:   "some-app-2",
   182  								Domain: v2action.Domain{Name: "some-domain"},
   183  							},
   184  						},
   185  						v2action.Warnings{"route-warning-3", "route-warning-4"},
   186  						nil
   187  				default:
   188  					panic("unknown app guid")
   189  				}
   190  			}
   191  		})
   192  
   193  		Context("with existing apps", func() {
   194  			BeforeEach(func() {
   195  				appSummaries := []v3action.ApplicationWithProcessSummary{
   196  					{
   197  						Application: v3action.Application{
   198  							GUID:  "app-guid-1",
   199  							Name:  "some-app-1",
   200  							State: "STARTED",
   201  						},
   202  						ProcessSummaries: []v3action.ProcessSummary{
   203  							{
   204  								Process: v3action.Process{
   205  									Type: "console",
   206  								},
   207  								InstanceDetails: []v3action.Instance{},
   208  							},
   209  							{
   210  								Process: v3action.Process{
   211  									Type: "worker",
   212  								},
   213  								InstanceDetails: []v3action.Instance{
   214  									{
   215  										Index: 0,
   216  										State: "DOWN",
   217  									},
   218  								},
   219  							},
   220  							{
   221  								Process: v3action.Process{
   222  									Type: constant.ProcessTypeWeb,
   223  								},
   224  								InstanceDetails: []v3action.Instance{
   225  									v3action.Instance{
   226  										Index: 0,
   227  										State: "RUNNING",
   228  									},
   229  									v3action.Instance{
   230  										Index: 1,
   231  										State: "RUNNING",
   232  									},
   233  								},
   234  							},
   235  						},
   236  					},
   237  					{
   238  						Application: v3action.Application{
   239  							GUID:  "app-guid-2",
   240  							Name:  "some-app-2",
   241  							State: "STOPPED",
   242  						},
   243  						ProcessSummaries: []v3action.ProcessSummary{
   244  							{
   245  								Process: v3action.Process{
   246  									Type: constant.ProcessTypeWeb,
   247  								},
   248  								InstanceDetails: []v3action.Instance{
   249  									v3action.Instance{
   250  										Index: 0,
   251  										State: "DOWN",
   252  									},
   253  									v3action.Instance{
   254  										Index: 1,
   255  										State: "DOWN",
   256  									},
   257  								},
   258  							},
   259  						},
   260  					},
   261  				}
   262  				fakeActor.GetApplicationsWithProcessesBySpaceReturns(appSummaries, v3action.Warnings{"warning-1", "warning-2"}, nil)
   263  			})
   264  
   265  			It("prints the application summary and outputs warnings", func() {
   266  				Expect(executeErr).ToNot(HaveOccurred())
   267  
   268  				Expect(testUI.Out).To(Say("Getting apps in org some-org / space some-space as steve\\.\\.\\."))
   269  
   270  				Expect(testUI.Out).To(Say("name\\s+requested state\\s+processes\\s+routes"))
   271  				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"))
   272  				Expect(testUI.Out).To(Say("some-app-2\\s+stopped\\s+web:0/2\\s+some-app-2.some-domain"))
   273  
   274  				Expect(testUI.Err).To(Say("warning-1"))
   275  				Expect(testUI.Err).To(Say("warning-2"))
   276  
   277  				Expect(testUI.Err).To(Say("route-warning-1"))
   278  				Expect(testUI.Err).To(Say("route-warning-2"))
   279  				Expect(testUI.Err).To(Say("route-warning-3"))
   280  				Expect(testUI.Err).To(Say("route-warning-4"))
   281  
   282  				Expect(fakeActor.GetApplicationsWithProcessesBySpaceCallCount()).To(Equal(1))
   283  				spaceGUID := fakeActor.GetApplicationsWithProcessesBySpaceArgsForCall(0)
   284  				Expect(spaceGUID).To(Equal("some-space-guid"))
   285  
   286  				Expect(fakeV2Actor.GetApplicationRoutesCallCount()).To(Equal(2))
   287  				appGUID := fakeV2Actor.GetApplicationRoutesArgsForCall(0)
   288  				Expect(appGUID).To(Equal("app-guid-1"))
   289  				appGUID = fakeV2Actor.GetApplicationRoutesArgsForCall(1)
   290  				Expect(appGUID).To(Equal("app-guid-2"))
   291  			})
   292  		})
   293  
   294  		Context("when app does not have processes", func() {
   295  			BeforeEach(func() {
   296  				appSummaries := []v3action.ApplicationWithProcessSummary{
   297  					{
   298  						Application: v3action.Application{
   299  							GUID:  "app-guid",
   300  							Name:  "some-app",
   301  							State: "STARTED",
   302  						},
   303  						ProcessSummaries: []v3action.ProcessSummary{},
   304  					},
   305  				}
   306  				fakeActor.GetApplicationsWithProcessesBySpaceReturns(appSummaries, v3action.Warnings{"warning"}, nil)
   307  			})
   308  
   309  			It("it does not request or display routes information for app", func() {
   310  				Expect(executeErr).ToNot(HaveOccurred())
   311  
   312  				Expect(testUI.Out).To(Say("Getting apps in org some-org / space some-space as steve\\.\\.\\."))
   313  
   314  				Expect(testUI.Out).To(Say("name\\s+requested state\\s+processes\\s+routes"))
   315  				Expect(testUI.Out).To(Say("some-app\\s+started\\s+$"))
   316  				Expect(testUI.Err).To(Say("warning"))
   317  
   318  				Expect(fakeActor.GetApplicationsWithProcessesBySpaceCallCount()).To(Equal(1))
   319  				spaceGUID := fakeActor.GetApplicationsWithProcessesBySpaceArgsForCall(0)
   320  				Expect(spaceGUID).To(Equal("some-space-guid"))
   321  
   322  				Expect(fakeV2Actor.GetApplicationRoutesCallCount()).To(Equal(0))
   323  			})
   324  		})
   325  
   326  		Context("with no apps", func() {
   327  			BeforeEach(func() {
   328  				fakeActor.GetApplicationsWithProcessesBySpaceReturns([]v3action.ApplicationWithProcessSummary{}, v3action.Warnings{"warning-1", "warning-2"}, nil)
   329  			})
   330  
   331  			It("displays there are no apps", func() {
   332  				Expect(executeErr).ToNot(HaveOccurred())
   333  
   334  				Expect(testUI.Out).To(Say("Getting apps in org some-org / space some-space as steve\\.\\.\\."))
   335  				Expect(testUI.Out).To(Say("No apps found"))
   336  			})
   337  		})
   338  	})
   339  })