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

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