github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/v3_apps_command_test.go (about)

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