github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/isolation_segments_command_test.go (about)

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