github.com/cloudfoundry/cli@v7.1.0+incompatible/actor/v3action/application_summary_test.go (about)

     1  package v3action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/actor/v3action"
     7  	"code.cloudfoundry.org/cli/actor/v3action/v3actionfakes"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    11  	"code.cloudfoundry.org/cli/resources"
    12  	"code.cloudfoundry.org/cli/types"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Application Summary Actions", func() {
    19  	var (
    20  		actor                     *Actor
    21  		fakeCloudControllerClient *v3actionfakes.FakeCloudControllerClient
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		fakeCloudControllerClient = new(v3actionfakes.FakeCloudControllerClient)
    26  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil)
    27  	})
    28  
    29  	Describe("GetApplicationSummaryByNameAndSpace", func() {
    30  		var (
    31  			appName              string
    32  			spaceGUID            string
    33  			withObfuscatedValues bool
    34  
    35  			summary    ApplicationSummary
    36  			warnings   Warnings
    37  			executeErr error
    38  		)
    39  
    40  		BeforeEach(func() {
    41  			appName = "some-app-name"
    42  			spaceGUID = "some-space-guid"
    43  			withObfuscatedValues = true
    44  		})
    45  
    46  		JustBeforeEach(func() {
    47  			summary, warnings, executeErr = actor.GetApplicationSummaryByNameAndSpace(appName, spaceGUID, withObfuscatedValues)
    48  		})
    49  
    50  		When("retrieving the application is successful", func() {
    51  			BeforeEach(func() {
    52  				fakeCloudControllerClient.GetApplicationsReturns(
    53  					[]resources.Application{
    54  						{
    55  							Name:  "some-app-name",
    56  							GUID:  "some-app-guid",
    57  							State: constant.ApplicationStarted,
    58  						},
    59  					},
    60  					ccv3.Warnings{"some-warning"},
    61  					nil,
    62  				)
    63  			})
    64  
    65  			When("retrieving the process information is successful", func() {
    66  				BeforeEach(func() {
    67  					listedProcess := ccv3.Process{
    68  						GUID:       "some-process-guid",
    69  						Type:       "some-type",
    70  						Command:    *types.NewFilteredString("[Redacted Value]"),
    71  						MemoryInMB: types.NullUint64{Value: 32, IsSet: true},
    72  					}
    73  					fakeCloudControllerClient.GetApplicationProcessesReturns(
    74  						[]ccv3.Process{listedProcess},
    75  						ccv3.Warnings{"some-process-warning"},
    76  						nil,
    77  					)
    78  
    79  					explicitlyCalledProcess := listedProcess
    80  					explicitlyCalledProcess.Command = *types.NewFilteredString("some-start-command")
    81  					fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
    82  						explicitlyCalledProcess,
    83  						ccv3.Warnings{"get-process-by-type-warning"},
    84  						nil,
    85  					)
    86  
    87  					fakeCloudControllerClient.GetProcessInstancesReturns(
    88  						[]ccv3.ProcessInstance{
    89  							{
    90  								State:       constant.ProcessInstanceRunning,
    91  								CPU:         0.01,
    92  								MemoryUsage: 1000000,
    93  								DiskUsage:   2000000,
    94  								MemoryQuota: 3000000,
    95  								DiskQuota:   4000000,
    96  								Index:       0,
    97  								Details:     "some details",
    98  							},
    99  						},
   100  						ccv3.Warnings{"some-process-stats-warning"},
   101  						nil,
   102  					)
   103  				})
   104  
   105  				When("app has droplet", func() {
   106  					BeforeEach(func() {
   107  						fakeCloudControllerClient.GetApplicationDropletCurrentReturns(
   108  							resources.Droplet{
   109  								Stack: "some-stack",
   110  								Buildpacks: []resources.DropletBuildpack{
   111  									{
   112  										Name: "some-buildpack",
   113  									},
   114  								},
   115  								Image: "docker/some-image",
   116  							},
   117  							ccv3.Warnings{"some-droplet-warning"},
   118  							nil,
   119  						)
   120  					})
   121  
   122  					It("returns the summary and warnings with droplet information", func() {
   123  						Expect(executeErr).ToNot(HaveOccurred())
   124  						Expect(summary).To(Equal(ApplicationSummary{
   125  							Application: Application{
   126  								Name:  "some-app-name",
   127  								GUID:  "some-app-guid",
   128  								State: constant.ApplicationStarted,
   129  							},
   130  							CurrentDroplet: Droplet{
   131  								Stack: "some-stack",
   132  								Image: "docker/some-image",
   133  								Buildpacks: []Buildpack{
   134  									{
   135  										Name: "some-buildpack",
   136  									},
   137  								},
   138  							},
   139  							ProcessSummaries: []ProcessSummary{
   140  								{
   141  									Process: Process{
   142  										GUID:       "some-process-guid",
   143  										MemoryInMB: types.NullUint64{Value: 32, IsSet: true},
   144  										Type:       "some-type",
   145  										Command:    *types.NewFilteredString("some-start-command"),
   146  									},
   147  									InstanceDetails: []ProcessInstance{
   148  										{
   149  											State:       constant.ProcessInstanceRunning,
   150  											CPU:         0.01,
   151  											MemoryUsage: 1000000,
   152  											DiskUsage:   2000000,
   153  											MemoryQuota: 3000000,
   154  											DiskQuota:   4000000,
   155  											Index:       0,
   156  											Details:     "some details",
   157  										},
   158  									},
   159  								},
   160  							},
   161  						}))
   162  						Expect(warnings).To(ConsistOf("some-warning", "some-process-warning", "get-process-by-type-warning", "some-process-stats-warning", "some-droplet-warning"))
   163  
   164  						Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1))
   165  						Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(ConsistOf(
   166  							ccv3.Query{Key: ccv3.NameFilter, Values: []string{"some-app-name"}},
   167  							ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{"some-space-guid"}},
   168  						))
   169  
   170  						Expect(fakeCloudControllerClient.GetApplicationDropletCurrentCallCount()).To(Equal(1))
   171  						Expect(fakeCloudControllerClient.GetApplicationDropletCurrentArgsForCall(0)).To(Equal("some-app-guid"))
   172  
   173  						Expect(fakeCloudControllerClient.GetApplicationProcessesCallCount()).To(Equal(1))
   174  						Expect(fakeCloudControllerClient.GetApplicationProcessesArgsForCall(0)).To(Equal("some-app-guid"))
   175  
   176  						Expect(fakeCloudControllerClient.GetApplicationProcessByTypeCallCount()).To(Equal(1))
   177  						appGUID, processType := fakeCloudControllerClient.GetApplicationProcessByTypeArgsForCall(0)
   178  						Expect(appGUID).To(Equal("some-app-guid"))
   179  						Expect(processType).To(Equal("some-type"))
   180  
   181  						Expect(fakeCloudControllerClient.GetProcessInstancesCallCount()).To(Equal(1))
   182  						Expect(fakeCloudControllerClient.GetProcessInstancesArgsForCall(0)).To(Equal("some-process-guid"))
   183  					})
   184  
   185  					When("getting the current droplet returns an error", func() {
   186  						var expectedErr error
   187  
   188  						BeforeEach(func() {
   189  							expectedErr = errors.New("some error")
   190  							fakeCloudControllerClient.GetApplicationDropletCurrentReturns(
   191  								resources.Droplet{},
   192  								ccv3.Warnings{"some-droplet-warning"},
   193  								expectedErr,
   194  							)
   195  						})
   196  
   197  						It("returns the error", func() {
   198  							Expect(executeErr).To(Equal(expectedErr))
   199  							Expect(warnings).To(ConsistOf("some-warning", "some-process-warning", "get-process-by-type-warning", "some-process-stats-warning", "some-droplet-warning"))
   200  						})
   201  					})
   202  				})
   203  
   204  				When("app does not have current droplet", func() {
   205  					BeforeEach(func() {
   206  						fakeCloudControllerClient.GetApplicationDropletCurrentReturns(
   207  							resources.Droplet{},
   208  							ccv3.Warnings{"some-droplet-warning"},
   209  							ccerror.DropletNotFoundError{},
   210  						)
   211  					})
   212  
   213  					It("returns the summary and warnings without droplet information", func() {
   214  						Expect(executeErr).ToNot(HaveOccurred())
   215  						Expect(summary).To(Equal(ApplicationSummary{
   216  							Application: Application{
   217  								Name:  "some-app-name",
   218  								GUID:  "some-app-guid",
   219  								State: constant.ApplicationStarted,
   220  							},
   221  							ProcessSummaries: []ProcessSummary{
   222  								{
   223  									Process: Process{
   224  										GUID:       "some-process-guid",
   225  										MemoryInMB: types.NullUint64{Value: 32, IsSet: true},
   226  										Type:       "some-type",
   227  										Command:    *types.NewFilteredString("some-start-command"),
   228  									},
   229  									InstanceDetails: []ProcessInstance{
   230  										{
   231  											State:       constant.ProcessInstanceRunning,
   232  											CPU:         0.01,
   233  											MemoryUsage: 1000000,
   234  											DiskUsage:   2000000,
   235  											MemoryQuota: 3000000,
   236  											DiskQuota:   4000000,
   237  											Index:       0,
   238  											Details:     "some details",
   239  										},
   240  									},
   241  								},
   242  							},
   243  						}))
   244  						Expect(warnings).To(ConsistOf("some-warning", "some-process-warning", "get-process-by-type-warning", "some-process-stats-warning", "some-droplet-warning"))
   245  
   246  						Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1))
   247  						Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(ConsistOf(
   248  							ccv3.Query{Key: ccv3.NameFilter, Values: []string{"some-app-name"}},
   249  							ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{"some-space-guid"}},
   250  						))
   251  
   252  						Expect(fakeCloudControllerClient.GetApplicationDropletCurrentCallCount()).To(Equal(1))
   253  						Expect(fakeCloudControllerClient.GetApplicationDropletCurrentArgsForCall(0)).To(Equal("some-app-guid"))
   254  
   255  						Expect(fakeCloudControllerClient.GetApplicationProcessesCallCount()).To(Equal(1))
   256  						Expect(fakeCloudControllerClient.GetApplicationProcessesArgsForCall(0)).To(Equal("some-app-guid"))
   257  
   258  						Expect(fakeCloudControllerClient.GetApplicationProcessByTypeCallCount()).To(Equal(1))
   259  						appGUID, processType := fakeCloudControllerClient.GetApplicationProcessByTypeArgsForCall(0)
   260  						Expect(appGUID).To(Equal("some-app-guid"))
   261  						Expect(processType).To(Equal("some-type"))
   262  
   263  						Expect(fakeCloudControllerClient.GetProcessInstancesCallCount()).To(Equal(1))
   264  						Expect(fakeCloudControllerClient.GetProcessInstancesArgsForCall(0)).To(Equal("some-process-guid"))
   265  					})
   266  				})
   267  			})
   268  
   269  			When("getting the app process instances returns an error", func() {
   270  				var expectedErr error
   271  
   272  				BeforeEach(func() {
   273  					fakeCloudControllerClient.GetApplicationProcessesReturns(
   274  						[]ccv3.Process{
   275  							{
   276  								GUID: "some-process-guid",
   277  								Type: "some-type",
   278  							},
   279  						},
   280  						ccv3.Warnings{"some-process-warning"},
   281  						nil,
   282  					)
   283  
   284  					fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   285  						ccv3.Process{},
   286  						ccv3.Warnings{"get-process-by-type-warning"},
   287  						nil,
   288  					)
   289  
   290  					expectedErr = errors.New("some error")
   291  					fakeCloudControllerClient.GetProcessInstancesReturns(
   292  						[]ccv3.ProcessInstance{},
   293  						ccv3.Warnings{"some-process-stats-warning"},
   294  						expectedErr,
   295  					)
   296  				})
   297  
   298  				It("returns the error", func() {
   299  					Expect(executeErr).To(Equal(expectedErr))
   300  					Expect(warnings).To(ConsistOf("some-warning", "some-process-warning", "get-process-by-type-warning", "some-process-stats-warning"))
   301  				})
   302  			})
   303  		})
   304  
   305  		When("getting the app processes returns an error", func() {
   306  			var expectedErr error
   307  
   308  			BeforeEach(func() {
   309  				fakeCloudControllerClient.GetApplicationsReturns(
   310  					[]resources.Application{
   311  						{
   312  							Name:  "some-app-name",
   313  							GUID:  "some-app-guid",
   314  							State: constant.ApplicationStarted,
   315  						},
   316  					},
   317  					ccv3.Warnings{"some-warning"},
   318  					nil,
   319  				)
   320  
   321  				expectedErr = errors.New("some error")
   322  				fakeCloudControllerClient.GetApplicationProcessesReturns(
   323  					[]ccv3.Process{{Type: constant.ProcessTypeWeb}},
   324  					ccv3.Warnings{"some-process-warning"},
   325  					expectedErr,
   326  				)
   327  			})
   328  
   329  			It("returns the error", func() {
   330  				Expect(executeErr).To(Equal(expectedErr))
   331  				Expect(warnings).To(ConsistOf("some-warning", "some-process-warning"))
   332  			})
   333  		})
   334  	})
   335  })