github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/shared/app_summary_displayer_test.go (about)

     1  package shared_test
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/actor/v3action"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    11  	"code.cloudfoundry.org/cli/command/commandfakes"
    12  	. "code.cloudfoundry.org/cli/command/v6/shared"
    13  	"code.cloudfoundry.org/cli/command/v6/shared/sharedfakes"
    14  	"code.cloudfoundry.org/cli/integration/helpers"
    15  	"code.cloudfoundry.org/cli/types"
    16  	"code.cloudfoundry.org/cli/util/configv3"
    17  	"code.cloudfoundry.org/cli/util/ui"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	. "github.com/onsi/gomega/gbytes"
    21  )
    22  
    23  var _ = Describe("app summary displayer", func() {
    24  	var (
    25  		appSummaryDisplayer AppSummaryDisplayer
    26  		output              *Buffer
    27  		testUI              *ui.UI
    28  		fakeConfig          *commandfakes.FakeConfig
    29  		fakeV2Actor         *sharedfakes.FakeV2AppActor
    30  		fakeActor           *sharedfakes.FakeV3AppSummaryActor
    31  		appName             string
    32  		executeErr          error
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		output = NewBuffer()
    37  		testUI = ui.NewTestUI(nil, output, NewBuffer())
    38  		fakeConfig = new(commandfakes.FakeConfig)
    39  		fakeActor = new(sharedfakes.FakeV3AppSummaryActor)
    40  		fakeV2Actor = new(sharedfakes.FakeV2AppActor)
    41  		appName = "some-app"
    42  
    43  		appSummaryDisplayer = AppSummaryDisplayer{
    44  			UI:         testUI,
    45  			Config:     fakeConfig,
    46  			Actor:      fakeActor,
    47  			V2AppActor: fakeV2Actor,
    48  			AppName:    appName,
    49  		}
    50  
    51  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    52  			GUID: "some-space-guid",
    53  			Name: "some-space"})
    54  	})
    55  
    56  	Describe("DisplayAppInfo", func() {
    57  		JustBeforeEach(func() {
    58  			executeErr = appSummaryDisplayer.DisplayAppInfo()
    59  		})
    60  
    61  		When("getting the app summary succeeds", func() {
    62  			When("the app has instances", func() {
    63  				BeforeEach(func() {
    64  					appSummary := v3action.ApplicationSummary{
    65  						Application: v3action.Application{
    66  							GUID:  "some-app-guid",
    67  							State: constant.ApplicationStarted,
    68  						},
    69  						ProcessSummaries: v3action.ProcessSummaries{
    70  							{
    71  								Process: v3action.Process{
    72  									Type:       "console",
    73  									MemoryInMB: types.NullUint64{Value: 16, IsSet: true},
    74  									DiskInMB:   types.NullUint64{Value: 512, IsSet: true},
    75  								},
    76  								InstanceDetails: []v3action.ProcessInstance{
    77  									v3action.ProcessInstance{
    78  										Index:       0,
    79  										State:       constant.ProcessInstanceRunning,
    80  										MemoryUsage: 1000000,
    81  										DiskUsage:   1000000,
    82  										MemoryQuota: 33554432,
    83  										DiskQuota:   8000000,
    84  										Uptime:      time.Now().Sub(time.Unix(167572800, 0)),
    85  									},
    86  								},
    87  							},
    88  							{
    89  								Process: v3action.Process{
    90  									Type:       constant.ProcessTypeWeb,
    91  									MemoryInMB: types.NullUint64{Value: 32, IsSet: true},
    92  									DiskInMB:   types.NullUint64{Value: 1024, IsSet: true},
    93  								},
    94  								InstanceDetails: []v3action.ProcessInstance{
    95  									v3action.ProcessInstance{
    96  										Index:       0,
    97  										State:       constant.ProcessInstanceRunning,
    98  										MemoryUsage: 1000000,
    99  										DiskUsage:   1000000,
   100  										MemoryQuota: 33554432,
   101  										DiskQuota:   2000000,
   102  										Uptime:      time.Now().Sub(time.Unix(267321600, 0)),
   103  									},
   104  									v3action.ProcessInstance{
   105  										Index:       1,
   106  										State:       constant.ProcessInstanceRunning,
   107  										MemoryUsage: 2000000,
   108  										DiskUsage:   2000000,
   109  										MemoryQuota: 33554432,
   110  										DiskQuota:   4000000,
   111  										Uptime:      time.Now().Sub(time.Unix(330480000, 0)),
   112  									},
   113  									v3action.ProcessInstance{
   114  										Index:       2,
   115  										State:       constant.ProcessInstanceRunning,
   116  										MemoryUsage: 3000000,
   117  										DiskUsage:   3000000,
   118  										MemoryQuota: 33554432,
   119  										DiskQuota:   6000000,
   120  										Uptime:      time.Now().Sub(time.Unix(1277164800, 0)),
   121  									},
   122  								},
   123  							},
   124  						},
   125  					}
   126  
   127  					fakeActor.GetApplicationSummaryByNameAndSpaceReturns(
   128  						appSummary,
   129  						v3action.Warnings{"get-app-summary-warning"},
   130  						nil)
   131  				})
   132  
   133  				When("the isolation segment name is provided", func() {
   134  					var isolationSegmentName string
   135  					BeforeEach(func() {
   136  						isolationSegmentName = "potato beans"
   137  						fakeV2Actor.GetApplicationInstancesWithStatsByApplicationReturns(
   138  							[]v2action.ApplicationInstanceWithStats{
   139  								{IsolationSegment: isolationSegmentName},
   140  							},
   141  							v2action.Warnings{"some-instance-stats-warning"}, nil)
   142  					})
   143  
   144  					It("should output the isolation segment name", func() {
   145  						Expect(executeErr).ToNot(HaveOccurred())
   146  						Expect(testUI.Out).To(Say(`isolation segment:\s+%s`, isolationSegmentName))
   147  
   148  						Expect(testUI.Err).To(Say("get-app-summary-warning"))
   149  						Expect(testUI.Err).To(Say("some-instance-stats-warning"))
   150  
   151  						Expect(fakeV2Actor.GetApplicationInstancesWithStatsByApplicationCallCount()).To(Equal(1))
   152  						Expect(fakeV2Actor.GetApplicationInstancesWithStatsByApplicationArgsForCall(0)).To(Equal("some-app-guid"))
   153  					})
   154  				})
   155  
   156  				When("the isolation segment name is missing", func() {
   157  					BeforeEach(func() {
   158  						fakeV2Actor.GetApplicationInstancesWithStatsByApplicationReturns(
   159  							[]v2action.ApplicationInstanceWithStats{{}},
   160  							v2action.Warnings{"some-instance-stats-warning"}, nil)
   161  					})
   162  
   163  					It("should not output the isolation segment row", func() {
   164  						Expect(executeErr).ToNot(HaveOccurred())
   165  						Expect(testUI.Out).ToNot(Say("isolation segment:"))
   166  
   167  						Expect(testUI.Err).To(Say("get-app-summary-warning"))
   168  						Expect(testUI.Err).To(Say("some-instance-stats-warning"))
   169  					})
   170  				})
   171  
   172  				When("getting the isolation segment information errors", func() {
   173  					When("a random error is returned", func() {
   174  						var expectedErr error
   175  
   176  						BeforeEach(func() {
   177  							expectedErr = errors.New("knaslfnasldfnasdfnasdkj")
   178  							fakeV2Actor.GetApplicationInstancesWithStatsByApplicationReturns(nil, v2action.Warnings{"some-instance-stats-warning"}, expectedErr)
   179  						})
   180  
   181  						It("displays the warnings and returns an error", func() {
   182  							Expect(executeErr).To(MatchError(expectedErr))
   183  
   184  							Expect(testUI.Err).To(Say("get-app-summary-warning"))
   185  							Expect(testUI.Err).To(Say("some-instance-stats-warning"))
   186  						})
   187  					})
   188  
   189  					When("a random error is returned", func() {
   190  						BeforeEach(func() {
   191  							fakeV2Actor.GetApplicationInstancesWithStatsByApplicationReturns(nil, v2action.Warnings{"some-instance-stats-warning"}, ccerror.ResourceNotFoundError{})
   192  						})
   193  
   194  						It("displays the warnings and continues", func() {
   195  							Expect(executeErr).ToNot(HaveOccurred())
   196  							Expect(testUI.Out).ToNot(Say("isolation segment:"))
   197  
   198  							Expect(testUI.Err).To(Say("get-app-summary-warning"))
   199  							Expect(testUI.Err).To(Say("some-instance-stats-warning"))
   200  						})
   201  					})
   202  				})
   203  
   204  				It("lists information for each of the processes", func() {
   205  					Expect(executeErr).ToNot(HaveOccurred())
   206  
   207  					processTable := helpers.ParseV3AppProcessTable(output.Contents())
   208  					Expect(len(processTable.Processes)).To(Equal(2))
   209  
   210  					webProcessSummary := processTable.Processes[0]
   211  					Expect(webProcessSummary.Type).To(Equal("web"))
   212  					Expect(webProcessSummary.InstanceCount).To(Equal("3/3"))
   213  					Expect(webProcessSummary.MemUsage).To(Equal("32M"))
   214  
   215  					Expect(webProcessSummary.Instances[0].Memory).To(Equal("976.6K of 32M"))
   216  					Expect(webProcessSummary.Instances[0].Disk).To(Equal("976.6K of 1.9M"))
   217  					Expect(webProcessSummary.Instances[0].CPU).To(Equal("0.0%"))
   218  
   219  					Expect(webProcessSummary.Instances[1].Memory).To(Equal("1.9M of 32M"))
   220  					Expect(webProcessSummary.Instances[1].Disk).To(Equal("1.9M of 3.8M"))
   221  					Expect(webProcessSummary.Instances[1].CPU).To(Equal("0.0%"))
   222  
   223  					Expect(webProcessSummary.Instances[2].Memory).To(Equal("2.9M of 32M"))
   224  					Expect(webProcessSummary.Instances[2].Disk).To(Equal("2.9M of 5.7M"))
   225  					Expect(webProcessSummary.Instances[2].CPU).To(Equal("0.0%"))
   226  
   227  					consoleProcessSummary := processTable.Processes[1]
   228  					Expect(consoleProcessSummary.Type).To(Equal("console"))
   229  					Expect(consoleProcessSummary.InstanceCount).To(Equal("1/1"))
   230  					Expect(consoleProcessSummary.MemUsage).To(Equal("16M"))
   231  
   232  					Expect(consoleProcessSummary.Instances[0].Memory).To(Equal("976.6K of 32M"))
   233  					Expect(consoleProcessSummary.Instances[0].Disk).To(Equal("976.6K of 7.6M"))
   234  					Expect(consoleProcessSummary.Instances[0].CPU).To(Equal("0.0%"))
   235  
   236  					Expect(testUI.Err).To(Say("get-app-summary-warning"))
   237  
   238  					Expect(fakeActor.GetApplicationSummaryByNameAndSpaceCallCount()).To(Equal(1))
   239  					passedAppName, spaceName, withObfuscatedValues := fakeActor.GetApplicationSummaryByNameAndSpaceArgsForCall(0)
   240  					Expect(passedAppName).To(Equal("some-app"))
   241  					Expect(spaceName).To(Equal("some-space-guid"))
   242  					Expect(withObfuscatedValues).To(BeFalse())
   243  				})
   244  			})
   245  
   246  			When("the app has no instances", func() {
   247  				BeforeEach(func() {
   248  					appSummary := v3action.ApplicationSummary{
   249  						Application: v3action.Application{
   250  							GUID: "some-app-guid",
   251  						},
   252  					}
   253  
   254  					fakeActor.GetApplicationSummaryByNameAndSpaceReturns(
   255  						appSummary,
   256  						v3action.Warnings{"get-app-summary-warning"},
   257  						nil)
   258  					fakeV2Actor.GetApplicationInstancesWithStatsByApplicationReturns(nil, v2action.Warnings{"some-instance-stats-warning"}, nil)
   259  				})
   260  
   261  				It("should not output isolation segment header", func() {
   262  					Expect(executeErr).ToNot(HaveOccurred())
   263  					Expect(testUI.Out).ToNot(Say("isolation segment:"))
   264  
   265  					Expect(fakeV2Actor.GetApplicationInstancesWithStatsByApplicationCallCount()).To(Equal(0))
   266  				})
   267  			})
   268  
   269  			When("the app is stopped", func() {
   270  				BeforeEach(func() {
   271  					appSummary := v3action.ApplicationSummary{
   272  						Application: v3action.Application{
   273  							GUID:  "some-app-guid",
   274  							State: constant.ApplicationStopped,
   275  						},
   276  						ProcessSummaries: v3action.ProcessSummaries{{}},
   277  					}
   278  
   279  					fakeActor.GetApplicationSummaryByNameAndSpaceReturns(
   280  						appSummary,
   281  						v3action.Warnings{"get-app-summary-warning"},
   282  						nil)
   283  					fakeV2Actor.GetApplicationInstancesWithStatsByApplicationReturns(nil, v2action.Warnings{"some-instance-stats-warning"}, nil)
   284  				})
   285  
   286  				It("should not output isolation segment header", func() {
   287  					Expect(executeErr).ToNot(HaveOccurred())
   288  					Expect(testUI.Out).ToNot(Say("isolation segment:"))
   289  
   290  					Expect(fakeV2Actor.GetApplicationInstancesWithStatsByApplicationCallCount()).To(Equal(0))
   291  				})
   292  			})
   293  		})
   294  
   295  		When("getting the app summary fails", func() {
   296  			BeforeEach(func() {
   297  				fakeActor.GetApplicationSummaryByNameAndSpaceReturns(
   298  					v3action.ApplicationSummary{},
   299  					v3action.Warnings{"get-app-summary-warning"},
   300  					errors.New("some-app-summary-error"),
   301  				)
   302  			})
   303  
   304  			It("returns the error and displays all warnings", func() {
   305  				Expect(executeErr).To(MatchError("some-app-summary-error"))
   306  				Expect(testUI.Err).To(Say("get-app-summary-warning"))
   307  				Expect(output.Contents()).To(HaveLen(0))
   308  			})
   309  		})
   310  	})
   311  })