github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/command/v7/labels_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  	"regexp"
     6  
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	. "code.cloudfoundry.org/cli/command/v7"
    11  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    12  	"code.cloudfoundry.org/cli/types"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("labels command", func() {
    21  	var (
    22  		cmd             LabelsCommand
    23  		fakeActor       *v7fakes.FakeAppActor
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		testUI          *ui.UI
    27  
    28  		executeErr error
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeActor = new(v7fakes.FakeAppActor)
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		cmd = LabelsCommand{
    37  			Actor:       fakeActor,
    38  			UI:          testUI,
    39  			Config:      fakeConfig,
    40  			SharedActor: fakeSharedActor,
    41  		}
    42  	})
    43  
    44  	JustBeforeEach(func() {
    45  		executeErr = cmd.Execute(nil)
    46  	})
    47  
    48  	Describe("listing labels", func() {
    49  		BeforeEach(func() {
    50  			fakeConfig.CurrentUserNameReturns("some-user", nil)
    51  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
    52  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"})
    53  			cmd.RequiredArgs = flag.LabelsArgs{
    54  				ResourceType: "app",
    55  				ResourceName: "dora",
    56  			}
    57  			fakeActor.GetApplicationByNameAndSpaceReturns(
    58  				v7action.Application{
    59  					Metadata: &v7action.Metadata{
    60  						Labels: map[string]types.NullString{
    61  							"some-other-label": types.NewNullString("some-other-value"),
    62  							"some-label":       types.NewNullString("some-value"),
    63  						},
    64  					},
    65  				},
    66  				v7action.Warnings{},
    67  				nil)
    68  		})
    69  
    70  		It("doesn't error", func() {
    71  			Expect(executeErr).ToNot(HaveOccurred())
    72  		})
    73  
    74  		It("checks that the user is logged in and targeted to an org and space", func() {
    75  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    76  			checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    77  			Expect(checkOrg).To(BeTrue())
    78  			Expect(checkSpace).To(BeTrue())
    79  		})
    80  
    81  		It("displays a message that it is retrieving the labels", func() {
    82  			Expect(executeErr).ToNot(HaveOccurred())
    83  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    84  			Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for app dora in org fake-org / space fake-space as some-user...`)))
    85  		})
    86  
    87  		It("retrieves the labels associated with the application", func() {
    88  			Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
    89  			appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
    90  			Expect(appName).To(Equal("dora"))
    91  			Expect(spaceGUID).To(Equal("some-space-guid"))
    92  		})
    93  
    94  		It("displays the labels that are associated with the application, alphabetically", func() {
    95  			Expect(testUI.Out).To(Say(`Key\s+Value`))
    96  			Expect(testUI.Out).To(Say(`some-label\s+some-value`))
    97  			Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
    98  		})
    99  
   100  		When("CAPI returns warnings", func() {
   101  			BeforeEach(func() {
   102  				fakeActor.GetApplicationByNameAndSpaceReturns(
   103  					v7action.Application{
   104  						Metadata: &v7action.Metadata{
   105  							Labels: map[string]types.NullString{
   106  								"some-other-label": types.NewNullString("some-other-value"),
   107  								"some-label":       types.NewNullString("some-value"),
   108  							},
   109  						},
   110  					},
   111  					v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   112  					nil)
   113  			})
   114  
   115  			It("prints all warnings", func() {
   116  				Expect(testUI.Err).To(Say("some-warning-1"))
   117  				Expect(testUI.Err).To(Say("some-warning-2"))
   118  			})
   119  		})
   120  
   121  		When("there is an error retrieving the application", func() {
   122  			BeforeEach(func() {
   123  				fakeActor.GetApplicationByNameAndSpaceReturns(
   124  					v7action.Application{},
   125  					v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   126  					errors.New("boom"))
   127  			})
   128  
   129  			It("returns the error", func() {
   130  				Expect(executeErr).To(MatchError("boom"))
   131  			})
   132  
   133  			It("still prints all warnings", func() {
   134  				Expect(testUI.Err).To(Say("some-warning-1"))
   135  				Expect(testUI.Err).To(Say("some-warning-2"))
   136  			})
   137  
   138  			It("doesn't say ok", func() {
   139  				Expect(testUI.Out).ToNot(Say("OK"))
   140  			})
   141  		})
   142  
   143  		When("checking targeted org and space fails", func() {
   144  			BeforeEach(func() {
   145  				fakeSharedActor.CheckTargetReturns(errors.New("nope"))
   146  			})
   147  
   148  			It("returns an error", func() {
   149  				Expect(executeErr).To(MatchError("nope"))
   150  			})
   151  		})
   152  
   153  		When("fetching the current user's name fails", func() {
   154  			BeforeEach(func() {
   155  				fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom"))
   156  			})
   157  
   158  			It("returns an error", func() {
   159  				Expect(executeErr).To(MatchError("boom"))
   160  			})
   161  		})
   162  	})
   163  })