github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/app_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	v7 "code.cloudfoundry.org/cli/command/v7"
    12  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    13  	"code.cloudfoundry.org/cli/resources"
    14  	"code.cloudfoundry.org/cli/types"
    15  	"code.cloudfoundry.org/cli/util/configv3"
    16  	"code.cloudfoundry.org/cli/util/ui"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  	. "github.com/onsi/gomega/gbytes"
    20  )
    21  
    22  var _ = Describe("app Command", func() {
    23  	var (
    24  		cmd             v7.AppCommand
    25  		testUI          *ui.UI
    26  		fakeConfig      *commandfakes.FakeConfig
    27  		fakeSharedActor *commandfakes.FakeSharedActor
    28  		fakeActor       *v7fakes.FakeActor
    29  		binaryName      string
    30  		executeErr      error
    31  		app             string
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    36  		fakeConfig = new(commandfakes.FakeConfig)
    37  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    38  		fakeActor = new(v7fakes.FakeActor)
    39  
    40  		binaryName = "faceman"
    41  		fakeConfig.BinaryNameReturns(binaryName)
    42  		app = "some-app"
    43  
    44  		cmd = v7.AppCommand{
    45  			RequiredArgs: flag.AppName{AppName: app},
    46  			BaseCommand: v7.BaseCommand{
    47  				UI:          testUI,
    48  				Config:      fakeConfig,
    49  				SharedActor: fakeSharedActor,
    50  				Actor:       fakeActor,
    51  			},
    52  		}
    53  
    54  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    55  			Name: "some-org",
    56  			GUID: "some-org-guid",
    57  		})
    58  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    59  			Name: "some-space",
    60  			GUID: "some-space-guid",
    61  		})
    62  
    63  		fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    64  	})
    65  
    66  	JustBeforeEach(func() {
    67  		executeErr = cmd.Execute(nil)
    68  	})
    69  
    70  	When("checking target fails", func() {
    71  		BeforeEach(func() {
    72  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    73  		})
    74  
    75  		It("returns an error", func() {
    76  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    77  
    78  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    79  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    80  			Expect(checkTargetedOrg).To(BeTrue())
    81  			Expect(checkTargetedSpace).To(BeTrue())
    82  		})
    83  	})
    84  
    85  	When("the user is not logged in", func() {
    86  		var expectedErr error
    87  
    88  		BeforeEach(func() {
    89  			expectedErr = errors.New("some current user error")
    90  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    91  		})
    92  
    93  		It("return an error", func() {
    94  			Expect(executeErr).To(Equal(expectedErr))
    95  		})
    96  	})
    97  
    98  	When("the --guid flag is provided", func() {
    99  		BeforeEach(func() {
   100  			cmd.GUID = true
   101  		})
   102  
   103  		When("no errors occur", func() {
   104  			BeforeEach(func() {
   105  				fakeActor.GetApplicationByNameAndSpaceReturns(
   106  					resources.Application{GUID: "some-guid"},
   107  					v7action.Warnings{"warning-1", "warning-2"},
   108  					nil)
   109  			})
   110  
   111  			It("displays the application guid and all warnings", func() {
   112  				Expect(executeErr).ToNot(HaveOccurred())
   113  
   114  				Expect(testUI.Out).To(Say("some-guid"))
   115  				Expect(testUI.Err).To(Say("warning-1"))
   116  				Expect(testUI.Err).To(Say("warning-2"))
   117  
   118  				Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   119  				appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   120  				Expect(appName).To(Equal("some-app"))
   121  				Expect(spaceGUID).To(Equal("some-space-guid"))
   122  			})
   123  		})
   124  
   125  		When("an error is encountered getting the app", func() {
   126  			When("the error is translatable", func() {
   127  				BeforeEach(func() {
   128  					fakeActor.GetApplicationByNameAndSpaceReturns(
   129  						resources.Application{},
   130  						v7action.Warnings{"warning-1", "warning-2"},
   131  						actionerror.ApplicationNotFoundError{Name: "some-app"})
   132  				})
   133  
   134  				It("returns a translatable error and all warnings", func() {
   135  					Expect(executeErr).To(MatchError(actionerror.ApplicationNotFoundError{Name: "some-app"}))
   136  
   137  					Expect(testUI.Err).To(Say("warning-1"))
   138  					Expect(testUI.Err).To(Say("warning-2"))
   139  				})
   140  			})
   141  
   142  			When("the error is not translatable", func() {
   143  				var expectedErr error
   144  
   145  				BeforeEach(func() {
   146  					expectedErr = errors.New("get app summary error")
   147  					fakeActor.GetApplicationByNameAndSpaceReturns(
   148  						resources.Application{},
   149  						v7action.Warnings{"warning-1", "warning-2"},
   150  						expectedErr)
   151  				})
   152  
   153  				It("returns the error and all warnings", func() {
   154  					Expect(executeErr).To(MatchError(expectedErr))
   155  
   156  					Expect(testUI.Err).To(Say("warning-1"))
   157  					Expect(testUI.Err).To(Say("warning-2"))
   158  				})
   159  			})
   160  		})
   161  	})
   162  
   163  	When("the --guid is not passed", func() {
   164  		When("getting the application summary returns an error", func() {
   165  			var expectedErr error
   166  
   167  			BeforeEach(func() {
   168  				expectedErr = actionerror.ApplicationNotFoundError{Name: app}
   169  				fakeActor.GetDetailedAppSummaryReturns(v7action.DetailedApplicationSummary{}, v7action.Warnings{"warning-1", "warning-2"}, expectedErr)
   170  			})
   171  
   172  			It("returns the error and prints warnings", func() {
   173  				Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: app}))
   174  
   175  				Expect(testUI.Out).To(Say(`Showing health and status for app some-app in org some-org / space some-space as steve\.\.\.`))
   176  
   177  				Expect(testUI.Err).To(Say("warning-1"))
   178  				Expect(testUI.Err).To(Say("warning-2"))
   179  			})
   180  		})
   181  
   182  		When("getting the application summary is successful", func() {
   183  			BeforeEach(func() {
   184  				summary := v7action.DetailedApplicationSummary{
   185  					ApplicationSummary: v7action.ApplicationSummary{
   186  						Application: resources.Application{
   187  							Name:  "some-app",
   188  							State: constant.ApplicationStarted,
   189  						},
   190  						ProcessSummaries: v7action.ProcessSummaries{
   191  							{
   192  								Process: v7action.Process{
   193  									Type:    constant.ProcessTypeWeb,
   194  									Command: *types.NewFilteredString("some-command-1"),
   195  								},
   196  							},
   197  							{
   198  								Process: v7action.Process{
   199  									Type:    "console",
   200  									Command: *types.NewFilteredString("some-command-2"),
   201  								},
   202  							},
   203  						},
   204  					},
   205  					CurrentDroplet: resources.Droplet{
   206  						Stack: "cflinuxfs2",
   207  						Buildpacks: []resources.DropletBuildpack{
   208  							{
   209  								Name:         "ruby_buildpack",
   210  								DetectOutput: "some-detect-output",
   211  							},
   212  							{
   213  								Name:         "some-buildpack",
   214  								DetectOutput: "",
   215  							},
   216  						},
   217  					},
   218  				}
   219  				fakeActor.GetDetailedAppSummaryReturns(summary, v7action.Warnings{"warning-1", "warning-2"}, nil)
   220  			})
   221  
   222  			It("prints the application summary and outputs warnings", func() {
   223  				Expect(executeErr).ToNot(HaveOccurred())
   224  
   225  				Expect(testUI.Out).To(Say(`(?m)Showing health and status for app some-app in org some-org / space some-space as steve\.\.\.\n\n`))
   226  				Expect(testUI.Out).To(Say(`name:\s+some-app`))
   227  				Expect(testUI.Out).To(Say(`requested state:\s+started`))
   228  				Expect(testUI.Out).ToNot(Say("start command:"))
   229  
   230  				Expect(testUI.Err).To(Say("warning-1"))
   231  				Expect(testUI.Err).To(Say("warning-2"))
   232  
   233  				Expect(fakeActor.GetDetailedAppSummaryCallCount()).To(Equal(1))
   234  				appName, spaceGUID, withObfuscatedValues := fakeActor.GetDetailedAppSummaryArgsForCall(0)
   235  				Expect(appName).To(Equal("some-app"))
   236  				Expect(spaceGUID).To(Equal("some-space-guid"))
   237  				Expect(withObfuscatedValues).To(BeFalse())
   238  			})
   239  		})
   240  	})
   241  })