github.com/niteshexa/cloudfoundry_cli@v7.1.0+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/resources"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("apps Command", func() {
    22  	var (
    23  		cmd             v7.AppsCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v7fakes.FakeActor
    28  		binaryName      string
    29  		executeErr      error
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v7fakes.FakeActor)
    37  
    38  		binaryName = "faceman"
    39  		fakeConfig.BinaryNameReturns(binaryName)
    40  
    41  		cmd = v7.AppsCommand{
    42  			BaseCommand: v7.BaseCommand{
    43  				UI:          testUI,
    44  				Config:      fakeConfig,
    45  				Actor:       fakeActor,
    46  				SharedActor: fakeSharedActor,
    47  			},
    48  		}
    49  
    50  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    51  			Name: "some-org",
    52  			GUID: "some-org-guid",
    53  		})
    54  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    55  			Name: "some-space",
    56  			GUID: "some-space-guid",
    57  		})
    58  
    59  		fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    60  	})
    61  
    62  	JustBeforeEach(func() {
    63  		executeErr = cmd.Execute(nil)
    64  	})
    65  
    66  	It("displays the experimental warning", func() {
    67  		Expect(testUI.Err).NotTo(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    68  	})
    69  
    70  	When("checking target fails", func() {
    71  		BeforeEach(func() {
    72  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    73  		})
    74  
    75  		It("returns an error", func() {
    76  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    77  
    78  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    79  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    80  			Expect(checkTargetedOrg).To(BeTrue())
    81  			Expect(checkTargetedSpace).To(BeTrue())
    82  		})
    83  	})
    84  
    85  	When("the user is not logged in", func() {
    86  		var expectedErr error
    87  
    88  		BeforeEach(func() {
    89  			expectedErr = errors.New("some current user error")
    90  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    91  		})
    92  
    93  		It("return an error", func() {
    94  			Expect(executeErr).To(Equal(expectedErr))
    95  		})
    96  	})
    97  
    98  	When("getting the applications returns an error", func() {
    99  		var expectedErr error
   100  
   101  		BeforeEach(func() {
   102  			expectedErr = ccerror.RequestError{}
   103  			fakeActor.GetAppSummariesForSpaceReturns([]v7action.ApplicationSummary{}, v7action.Warnings{"warning-1", "warning-2"}, expectedErr)
   104  		})
   105  
   106  		It("returns the error and prints warnings", func() {
   107  			Expect(executeErr).To(Equal(ccerror.RequestError{}))
   108  
   109  			Expect(testUI.Out).To(Say(`Getting apps in org some-org / space some-space as steve\.\.\.`))
   110  
   111  			Expect(testUI.Err).To(Say("warning-1"))
   112  			Expect(testUI.Err).To(Say("warning-2"))
   113  		})
   114  	})
   115  
   116  	When("getting routes returns an error", func() {
   117  		var expectedErr error
   118  
   119  		BeforeEach(func() {
   120  			expectedErr = ccerror.RequestError{}
   121  			fakeActor.GetAppSummariesForSpaceReturns([]v7action.ApplicationSummary{
   122  				{
   123  					Application: resources.Application{
   124  						GUID:  "app-guid",
   125  						Name:  "some-app",
   126  						State: constant.ApplicationStarted,
   127  					},
   128  					ProcessSummaries: []v7action.ProcessSummary{{Process: v7action.Process{Type: "process-type"}}},
   129  					Routes:           []resources.Route{},
   130  				},
   131  			}, v7action.Warnings{"warning-1", "warning-2"}, expectedErr)
   132  		})
   133  
   134  		It("returns the error and prints warnings", func() {
   135  			Expect(executeErr).To(Equal(ccerror.RequestError{}))
   136  
   137  			Expect(testUI.Out).To(Say(`Getting apps in org some-org / space some-space as steve\.\.\.`))
   138  
   139  			Expect(testUI.Err).To(Say("warning-1"))
   140  			Expect(testUI.Err).To(Say("warning-2"))
   141  		})
   142  	})
   143  
   144  	When("the route actor does not return any errors", func() {
   145  		Context("with existing apps", func() {
   146  			BeforeEach(func() {
   147  				appSummaries := []v7action.ApplicationSummary{
   148  					{
   149  						Application: resources.Application{
   150  							GUID:  "app-guid-1",
   151  							Name:  "some-app-1",
   152  							State: constant.ApplicationStarted,
   153  						},
   154  						ProcessSummaries: []v7action.ProcessSummary{
   155  							{
   156  								Process: v7action.Process{
   157  									Type: "console",
   158  								},
   159  								InstanceDetails: []v7action.ProcessInstance{},
   160  							},
   161  							{
   162  								Process: v7action.Process{
   163  									Type: "worker",
   164  								},
   165  								InstanceDetails: []v7action.ProcessInstance{
   166  									{
   167  										Index: 0,
   168  										State: constant.ProcessInstanceDown,
   169  									},
   170  								},
   171  							},
   172  							{
   173  								Process: v7action.Process{
   174  									Type: constant.ProcessTypeWeb,
   175  								},
   176  								InstanceDetails: []v7action.ProcessInstance{
   177  									v7action.ProcessInstance{
   178  										Index: 0,
   179  										State: constant.ProcessInstanceRunning,
   180  									},
   181  									v7action.ProcessInstance{
   182  										Index: 1,
   183  										State: constant.ProcessInstanceRunning,
   184  									},
   185  								},
   186  							},
   187  						},
   188  						Routes: []resources.Route{
   189  							{
   190  								Host: "some-app-1",
   191  								URL:  "some-app-1.some-other-domain",
   192  							},
   193  							{
   194  								Host: "some-app-1",
   195  								URL:  "some-app-1.some-domain",
   196  							},
   197  						},
   198  					},
   199  					{
   200  						Application: resources.Application{
   201  							GUID:  "app-guid-2",
   202  							Name:  "some-app-2",
   203  							State: constant.ApplicationStopped,
   204  						},
   205  						ProcessSummaries: []v7action.ProcessSummary{
   206  							{
   207  								Process: v7action.Process{
   208  									Type: constant.ProcessTypeWeb,
   209  								},
   210  								InstanceDetails: []v7action.ProcessInstance{
   211  									v7action.ProcessInstance{
   212  										Index: 0,
   213  										State: constant.ProcessInstanceDown,
   214  									},
   215  									v7action.ProcessInstance{
   216  										Index: 1,
   217  										State: constant.ProcessInstanceDown,
   218  									},
   219  								},
   220  							},
   221  						},
   222  						Routes: []resources.Route{
   223  							{
   224  								Host: "some-app-2",
   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: resources.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  })