github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/v3action/application_with_process_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  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Application with ProcessSummary Actions", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v3actionfakes.FakeCloudControllerClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeCloudControllerClient = new(v3actionfakes.FakeCloudControllerClient)
    23  		actor = NewActor(nil, fakeCloudControllerClient, nil)
    24  	})
    25  
    26  	Describe("GetApplicationsWithProcessesBySpace", func() {
    27  		Context("when there are apps", func() {
    28  			BeforeEach(func() {
    29  				fakeCloudControllerClient.GetApplicationsReturns(
    30  					[]ccv3.Application{
    31  						{
    32  							Name:  "some-app-name-1",
    33  							GUID:  "some-app-guid-1",
    34  							State: "RUNNING",
    35  						},
    36  						{
    37  							Name:  "some-app-name-2",
    38  							GUID:  "some-app-guid-2",
    39  							State: "STOPPED",
    40  						},
    41  					},
    42  					ccv3.Warnings{"some-warning"},
    43  					nil,
    44  				)
    45  
    46  				fakeCloudControllerClient.GetApplicationProcessesReturnsOnCall(
    47  					0,
    48  					[]ccv3.Process{
    49  						{
    50  							GUID: "some-process-guid-1",
    51  							Type: "some-process-type-1",
    52  						},
    53  						{
    54  							GUID: "some-process-guid-2",
    55  							Type: "some-process-type-2",
    56  						},
    57  					},
    58  					ccv3.Warnings{"some-process-warning-1"},
    59  					nil,
    60  				)
    61  				fakeCloudControllerClient.GetApplicationProcessesReturnsOnCall(
    62  					1,
    63  					[]ccv3.Process{
    64  						{
    65  							GUID: "some-process-guid-3",
    66  							Type: "some-process-type-3",
    67  						},
    68  					},
    69  					ccv3.Warnings{"some-process-warning-2"},
    70  					nil,
    71  				)
    72  
    73  				fakeCloudControllerClient.GetProcessInstancesReturnsOnCall(
    74  					0,
    75  					[]ccv3.Instance{{State: "RUNNING"}, {State: "DOWN"}, {State: "RUNNING"}},
    76  					ccv3.Warnings{"some-process-stats-warning-1"},
    77  					nil,
    78  				)
    79  				fakeCloudControllerClient.GetProcessInstancesReturnsOnCall(
    80  					1,
    81  					[]ccv3.Instance{{State: "RUNNING"}, {State: "RUNNING"}},
    82  					ccv3.Warnings{"some-process-stats-warning-2"},
    83  					nil,
    84  				)
    85  				fakeCloudControllerClient.GetProcessInstancesReturnsOnCall(
    86  					2,
    87  					[]ccv3.Instance{{State: "DOWN"}},
    88  					ccv3.Warnings{"some-process-stats-warning-3"},
    89  					nil,
    90  				)
    91  			})
    92  
    93  			It("returns app summaries and warnings", func() {
    94  				summaries, warnings, err := actor.GetApplicationsWithProcessesBySpace("some-space-guid")
    95  				Expect(err).ToNot(HaveOccurred())
    96  				Expect(summaries).To(Equal([]ApplicationWithProcessSummary{
    97  					{
    98  						Application: Application{
    99  							Name:  "some-app-name-1",
   100  							GUID:  "some-app-guid-1",
   101  							State: "RUNNING",
   102  						},
   103  						ProcessSummaries: []ProcessSummary{
   104  							{
   105  								Process:         Process{GUID: "some-process-guid-1", Type: "some-process-type-1"},
   106  								InstanceDetails: []Instance{{State: "RUNNING"}, {State: "DOWN"}, {State: "RUNNING"}},
   107  							},
   108  							{
   109  								Process:         Process{GUID: "some-process-guid-2", Type: "some-process-type-2"},
   110  								InstanceDetails: []Instance{{State: "RUNNING"}, {State: "RUNNING"}},
   111  							},
   112  						},
   113  					},
   114  					{
   115  						Application: Application{
   116  							Name:  "some-app-name-2",
   117  							GUID:  "some-app-guid-2",
   118  							State: "STOPPED",
   119  						},
   120  						ProcessSummaries: []ProcessSummary{
   121  							{
   122  								Process:         Process{GUID: "some-process-guid-3", Type: "some-process-type-3"},
   123  								InstanceDetails: []Instance{{State: "DOWN"}},
   124  							},
   125  						},
   126  					},
   127  				}))
   128  				Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning-1", "some-process-stats-warning-1", "some-process-stats-warning-2", "some-process-warning-2", "some-process-stats-warning-3"}))
   129  
   130  				Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1))
   131  				expectedQuery := url.Values{
   132  					"space_guids": []string{"some-space-guid"},
   133  					"order_by":    []string{"name"},
   134  				}
   135  				query := fakeCloudControllerClient.GetApplicationsArgsForCall(0)
   136  				Expect(query).To(Equal(expectedQuery))
   137  
   138  				Expect(fakeCloudControllerClient.GetApplicationProcessesCallCount()).To(Equal(2))
   139  				appGUID := fakeCloudControllerClient.GetApplicationProcessesArgsForCall(0)
   140  				Expect(appGUID).To(Equal("some-app-guid-1"))
   141  				appGUID = fakeCloudControllerClient.GetApplicationProcessesArgsForCall(1)
   142  				Expect(appGUID).To(Equal("some-app-guid-2"))
   143  
   144  				Expect(fakeCloudControllerClient.GetProcessInstancesCallCount()).To(Equal(3))
   145  				processGUID := fakeCloudControllerClient.GetProcessInstancesArgsForCall(0)
   146  				Expect(processGUID).To(Equal("some-process-guid-1"))
   147  				processGUID = fakeCloudControllerClient.GetProcessInstancesArgsForCall(1)
   148  				Expect(processGUID).To(Equal("some-process-guid-2"))
   149  				processGUID = fakeCloudControllerClient.GetProcessInstancesArgsForCall(2)
   150  				Expect(processGUID).To(Equal("some-process-guid-3"))
   151  			})
   152  		})
   153  
   154  		Context("when getting the app processes returns an error", func() {
   155  			var expectedErr error
   156  
   157  			BeforeEach(func() {
   158  				fakeCloudControllerClient.GetApplicationsReturns(
   159  					[]ccv3.Application{
   160  						{
   161  							Name:  "some-app-name",
   162  							GUID:  "some-app-guid",
   163  							State: "RUNNING",
   164  						},
   165  					},
   166  					ccv3.Warnings{"some-warning"},
   167  					nil,
   168  				)
   169  
   170  				expectedErr = errors.New("some error")
   171  				fakeCloudControllerClient.GetApplicationProcessesReturns(
   172  					[]ccv3.Process{},
   173  					ccv3.Warnings{"some-process-warning"},
   174  					expectedErr,
   175  				)
   176  			})
   177  
   178  			It("returns the error", func() {
   179  				_, warnings, err := actor.GetApplicationsWithProcessesBySpace("some-space-guid")
   180  				Expect(err).To(Equal(expectedErr))
   181  				Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning"}))
   182  			})
   183  		})
   184  
   185  		Context("when getting the app process instances returns an error", func() {
   186  			var expectedErr error
   187  
   188  			BeforeEach(func() {
   189  				fakeCloudControllerClient.GetApplicationsReturns(
   190  					[]ccv3.Application{
   191  						{
   192  							Name:  "some-app-name",
   193  							GUID:  "some-app-guid",
   194  							State: "RUNNING",
   195  						},
   196  					},
   197  					ccv3.Warnings{"some-warning"},
   198  					nil,
   199  				)
   200  
   201  				fakeCloudControllerClient.GetApplicationProcessesReturns(
   202  					[]ccv3.Process{
   203  						{
   204  							GUID: "some-process-guid",
   205  							Type: "some-type",
   206  						},
   207  					},
   208  					ccv3.Warnings{"some-process-warning"},
   209  					nil,
   210  				)
   211  
   212  				expectedErr = errors.New("some error")
   213  				fakeCloudControllerClient.GetProcessInstancesReturns(
   214  					[]ccv3.Instance{},
   215  					ccv3.Warnings{"some-process-stats-warning"},
   216  					expectedErr,
   217  				)
   218  			})
   219  
   220  			It("returns the error", func() {
   221  				_, warnings, err := actor.GetApplicationsWithProcessesBySpace("some-space-guid")
   222  				Expect(err).To(Equal(expectedErr))
   223  				Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning", "some-process-stats-warning"}))
   224  			})
   225  		})
   226  	})
   227  })