github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/actor/v3action/application_summary_test.go (about)

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