github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/isolation_segments_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v6"
    10  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("isolation-segments Command", func() {
    19  	var (
    20  		cmd             IsolationSegmentsCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v6fakes.FakeIsolationSegmentsActor
    25  		binaryName      string
    26  		executeErr      error
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    31  		fakeConfig = new(commandfakes.FakeConfig)
    32  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    33  		fakeActor = new(v6fakes.FakeIsolationSegmentsActor)
    34  
    35  		cmd = IsolationSegmentsCommand{
    36  			UI:          testUI,
    37  			Config:      fakeConfig,
    38  			SharedActor: fakeSharedActor,
    39  			Actor:       fakeActor,
    40  		}
    41  
    42  		binaryName = "faceman"
    43  		fakeConfig.BinaryNameReturns(binaryName)
    44  	})
    45  
    46  	JustBeforeEach(func() {
    47  		executeErr = cmd.Execute(nil)
    48  	})
    49  
    50  	When("checking target fails", func() {
    51  		BeforeEach(func() {
    52  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    53  		})
    54  
    55  		It("returns an error", func() {
    56  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    57  
    58  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    59  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    60  			Expect(checkTargetedOrg).To(BeFalse())
    61  			Expect(checkTargetedSpace).To(BeFalse())
    62  		})
    63  	})
    64  
    65  	When("checking target does not fail", func() {
    66  		BeforeEach(func() {
    67  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    68  		})
    69  
    70  		When("an error is not encountered getting the isolation segment summaries", func() {
    71  			When("there are isolation segments", func() {
    72  				BeforeEach(func() {
    73  					fakeActor.GetIsolationSegmentSummariesReturns(
    74  						[]v3action.IsolationSegmentSummary{
    75  							{
    76  								Name:         "some-iso-1",
    77  								EntitledOrgs: []string{},
    78  							},
    79  							{
    80  								Name:         "some-iso-2",
    81  								EntitledOrgs: []string{"some-org-1"},
    82  							},
    83  							{
    84  								Name:         "some-iso-3",
    85  								EntitledOrgs: []string{"some-org-1", "some-org-2"},
    86  							},
    87  						},
    88  						v3action.Warnings{"warning-1", "warning-2"},
    89  						nil,
    90  					)
    91  				})
    92  
    93  				It("displays the isolation segment summaries and all warnings", func() {
    94  					Expect(executeErr).ToNot(HaveOccurred())
    95  
    96  					Expect(testUI.Out).To(Say("Getting isolation segments as banana..."))
    97  					Expect(testUI.Out).To(Say("OK\n\n"))
    98  					Expect(testUI.Out).To(Say(`name\s+orgs`))
    99  					Expect(testUI.Out).To(Say("some-iso-1"))
   100  					Expect(testUI.Out).To(Say(`some-iso-2\s+some-org-1`))
   101  					Expect(testUI.Out).To(Say(`some-iso-3\s+some-org-1, some-org-2`))
   102  
   103  					Expect(testUI.Err).To(Say("warning-1"))
   104  					Expect(testUI.Err).To(Say("warning-2"))
   105  
   106  					Expect(fakeActor.GetIsolationSegmentSummariesCallCount()).To(Equal(1))
   107  				})
   108  			})
   109  
   110  			When("there are no isolation segments", func() {
   111  				BeforeEach(func() {
   112  					fakeActor.GetIsolationSegmentSummariesReturns(
   113  						[]v3action.IsolationSegmentSummary{},
   114  						nil,
   115  						nil,
   116  					)
   117  				})
   118  				It("displays the empty table", func() {
   119  					Expect(executeErr).ToNot(HaveOccurred())
   120  					Expect(testUI.Out).To(Say("Getting isolation segments as banana..."))
   121  					Expect(testUI.Out).To(Say("OK\n\n"))
   122  					Expect(testUI.Out).To(Say(`name\s+orgs`))
   123  					Expect(testUI.Out).NotTo(Say("[a-zA-Z]+"))
   124  
   125  					Expect(fakeActor.GetIsolationSegmentSummariesCallCount()).To(Equal(1))
   126  				})
   127  			})
   128  		})
   129  
   130  		When("an error is encountered getting the isolation segment summaries", func() {
   131  			var expectedError error
   132  			BeforeEach(func() {
   133  				expectedError = errors.New("some-error")
   134  				fakeActor.GetIsolationSegmentSummariesReturns(
   135  					[]v3action.IsolationSegmentSummary{},
   136  					v3action.Warnings{"warning-1", "warning-2"},
   137  					expectedError,
   138  				)
   139  			})
   140  
   141  			It("displays warnings and returns the error", func() {
   142  				Expect(executeErr).To(MatchError(expectedError))
   143  
   144  				Expect(testUI.Out).To(Say("Getting isolation segments as banana..."))
   145  				Expect(testUI.Out).NotTo(Say("OK"))
   146  
   147  				Expect(testUI.Err).To(Say("warning-1"))
   148  				Expect(testUI.Err).To(Say("warning-2"))
   149  			})
   150  		})
   151  	})
   152  })