github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/v3action/application_summary_test.go (about)

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