github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/space_command_test.go (about)

     1  package v2_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/actor/v3action"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    10  	"code.cloudfoundry.org/cli/command/commandfakes"
    11  	. "code.cloudfoundry.org/cli/command/v2"
    12  	"code.cloudfoundry.org/cli/command/v2/v2fakes"
    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("space Command", func() {
    21  	var (
    22  		cmd             SpaceCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v2fakes.FakeSpaceActor
    27  		fakeActorV3     *v2fakes.FakeSpaceActorV3
    28  		binaryName      string
    29  		executeErr      error
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v2fakes.FakeSpaceActor)
    37  		fakeActorV3 = new(v2fakes.FakeSpaceActorV3)
    38  
    39  		cmd = SpaceCommand{
    40  			UI:          testUI,
    41  			Config:      fakeConfig,
    42  			SharedActor: fakeSharedActor,
    43  			Actor:       fakeActor,
    44  			ActorV3:     fakeActorV3,
    45  		}
    46  
    47  		binaryName = "faceman"
    48  		fakeConfig.BinaryNameReturns(binaryName)
    49  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionLifecyleStagingV2)
    50  		fakeActorV3.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(nil)
    55  	})
    56  
    57  	Context("when checking the target fails", func() {
    58  		BeforeEach(func() {
    59  			fakeSharedActor.CheckTargetReturns(
    60  				actionerror.NotLoggedInError{BinaryName: binaryName})
    61  		})
    62  
    63  		It("returns an error", func() {
    64  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    65  
    66  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    67  			targetedOrganizationRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
    68  			Expect(targetedOrganizationRequired).To(Equal(true))
    69  			Expect(targetedSpaceRequired).To(Equal(false))
    70  		})
    71  	})
    72  
    73  	Context("when the --guid flag is provided", func() {
    74  		BeforeEach(func() {
    75  			cmd.RequiredArgs.Space = "some-space"
    76  			cmd.GUID = true
    77  		})
    78  
    79  		Context("when no errors occur", func() {
    80  			BeforeEach(func() {
    81  				fakeConfig.TargetedOrganizationReturns(
    82  					configv3.Organization{GUID: "some-org-guid"},
    83  				)
    84  				fakeActor.GetSpaceByOrganizationAndNameReturns(
    85  					v2action.Space{GUID: "some-space-guid"},
    86  					v2action.Warnings{"warning-1", "warning-2"},
    87  					nil)
    88  			})
    89  
    90  			It("displays the space guid and outputs all warnings", func() {
    91  				Expect(executeErr).ToNot(HaveOccurred())
    92  
    93  				Expect(testUI.Out).To(Say("some-space-guid"))
    94  				Expect(testUI.Err).To(Say("warning-1"))
    95  				Expect(testUI.Err).To(Say("warning-2"))
    96  
    97  				Expect(fakeActor.GetSpaceByOrganizationAndNameCallCount()).To(Equal(1))
    98  				orgGUID, spaceName := fakeActor.GetSpaceByOrganizationAndNameArgsForCall(0)
    99  				Expect(orgGUID).To(Equal("some-org-guid"))
   100  				Expect(spaceName).To(Equal("some-space"))
   101  			})
   102  		})
   103  
   104  		Context("when getting the space returns an error", func() {
   105  			Context("when the error is translatable", func() {
   106  				BeforeEach(func() {
   107  					fakeActor.GetSpaceByOrganizationAndNameReturns(
   108  						v2action.Space{},
   109  						v2action.Warnings{"warning-1", "warning-2"},
   110  						actionerror.SpaceNotFoundError{Name: "some-space"})
   111  				})
   112  
   113  				It("returns a translatable error and outputs all warnings", func() {
   114  					Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"}))
   115  
   116  					Expect(testUI.Err).To(Say("warning-1"))
   117  					Expect(testUI.Err).To(Say("warning-2"))
   118  				})
   119  			})
   120  
   121  			Context("when the error is not translatable", func() {
   122  				var expectedErr error
   123  
   124  				BeforeEach(func() {
   125  					expectedErr = errors.New("get space error")
   126  					fakeActor.GetSpaceByOrganizationAndNameReturns(
   127  						v2action.Space{},
   128  						v2action.Warnings{"warning-1", "warning-2"},
   129  						expectedErr)
   130  				})
   131  
   132  				It("returns the error and all warnings", func() {
   133  					Expect(executeErr).To(MatchError(expectedErr))
   134  
   135  					Expect(testUI.Err).To(Say("warning-1"))
   136  					Expect(testUI.Err).To(Say("warning-2"))
   137  				})
   138  			})
   139  		})
   140  	})
   141  
   142  	Context("when the --guid flag is not provided", func() {
   143  		Context("when no errors occur", func() {
   144  			BeforeEach(func() {
   145  				fakeConfig.CurrentUserReturns(
   146  					configv3.User{
   147  						Name: "some-user",
   148  					},
   149  					nil)
   150  
   151  				cmd.RequiredArgs.Space = "some-space"
   152  
   153  				fakeConfig.TargetedOrganizationReturns(
   154  					configv3.Organization{
   155  						GUID: "some-org-guid",
   156  						Name: "some-org",
   157  					},
   158  				)
   159  
   160  				fakeActor.GetSpaceSummaryByOrganizationAndNameReturns(
   161  					v2action.SpaceSummary{
   162  						Space: v2action.Space{
   163  							Name: "some-space",
   164  							GUID: "some-space-guid",
   165  						},
   166  						OrgName: "some-org",
   167  						OrgDefaultIsolationSegmentGUID: "some-org-default-isolation-segment-guid",
   168  						AppNames:                       []string{"app1", "app2", "app3"},
   169  						ServiceInstanceNames:           []string{"service1", "service2", "service3"},
   170  						SpaceQuotaName:                 "some-space-quota",
   171  						RunningSecurityGroupNames:      []string{"public_networks", "dns", "load_balancer"},
   172  						StagingSecurityGroupNames:      []string{"staging-sec-1", "staging-sec-2"},
   173  					},
   174  					v2action.Warnings{"warning-1", "warning-2"},
   175  					nil,
   176  				)
   177  			})
   178  
   179  			Context("when there is no v3 API", func() {
   180  				BeforeEach(func() {
   181  					cmd.ActorV3 = nil
   182  				})
   183  
   184  				It("displays the space summary with no isolation segment row", func() {
   185  					Expect(executeErr).To(BeNil())
   186  					Expect(testUI.Out).ToNot(Say("isolation segment:"))
   187  				})
   188  			})
   189  
   190  			Context("when there is a v3 API", func() {
   191  				BeforeEach(func() {
   192  					fakeActorV3.GetEffectiveIsolationSegmentBySpaceReturns(
   193  						v3action.IsolationSegment{
   194  							Name: "some-isolation-segment",
   195  						},
   196  						v3action.Warnings{"v3-warning-1", "v3-warning-2"},
   197  						nil,
   198  					)
   199  				})
   200  
   201  				It("displays warnings and a table with space name, org, apps, services, isolation segment, space quota and security groups", func() {
   202  					Expect(executeErr).To(BeNil())
   203  
   204  					Expect(testUI.Out).To(Say("Getting info for space some-space in org some-org as some-user\\.\\.\\."))
   205  					Expect(testUI.Out).To(Say("name:\\s+some-space"))
   206  					Expect(testUI.Out).To(Say("org:\\s+some-org"))
   207  					Expect(testUI.Out).To(Say("apps:\\s+app1, app2, app3"))
   208  					Expect(testUI.Out).To(Say("services:\\s+service1, service2, service3"))
   209  					Expect(testUI.Out).To(Say("isolation segment:\\s+some-isolation-segment"))
   210  					Expect(testUI.Out).To(Say("space quota:\\s+some-space-quota"))
   211  					Expect(testUI.Out).To(Say("running security groups:\\s+public_networks, dns, load_balancer"))
   212  					Expect(testUI.Out).To(Say("staging security groups:\\s+staging-sec-1, staging-sec-2"))
   213  
   214  					Expect(testUI.Err).To(Say("warning-1"))
   215  					Expect(testUI.Err).To(Say("warning-2"))
   216  					Expect(testUI.Err).To(Say("v3-warning-1"))
   217  					Expect(testUI.Err).To(Say("v3-warning-2"))
   218  
   219  					Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   220  					Expect(fakeActor.GetSpaceSummaryByOrganizationAndNameCallCount()).To(Equal(1))
   221  					orgGUID, spaceName, includeStagingSecurityGroupRules := fakeActor.GetSpaceSummaryByOrganizationAndNameArgsForCall(0)
   222  					Expect(orgGUID).To(Equal("some-org-guid"))
   223  					Expect(spaceName).To(Equal("some-space"))
   224  					Expect(includeStagingSecurityGroupRules).To(BeTrue())
   225  					Expect(fakeActorV3.GetEffectiveIsolationSegmentBySpaceCallCount()).To(Equal(1))
   226  					spaceGUID, orgDefaultIsolationSegmentGUID := fakeActorV3.GetEffectiveIsolationSegmentBySpaceArgsForCall(0)
   227  					Expect(spaceGUID).To(Equal("some-space-guid"))
   228  					Expect(orgDefaultIsolationSegmentGUID).To(Equal("some-org-default-isolation-segment-guid"))
   229  				})
   230  			})
   231  
   232  			Context("when v3 api version is below 3.11.0 and the v2 api version is no less than 2.68.0", func() {
   233  				BeforeEach(func() {
   234  					fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionLifecyleStagingV2)
   235  					fakeActorV3.CloudControllerAPIVersionReturns("3.10.0")
   236  				})
   237  
   238  				It("displays warnings and a table with space name, org, apps, services, space quota and security groups", func() {
   239  					Expect(executeErr).To(BeNil())
   240  
   241  					Expect(testUI.Out).NotTo(Say("isolation segment:"))
   242  
   243  					orgGUID, spaceName, includeStagingSecurityGroupRules := fakeActor.GetSpaceSummaryByOrganizationAndNameArgsForCall(0)
   244  					Expect(orgGUID).To(Equal("some-org-guid"))
   245  					Expect(spaceName).To(Equal("some-space"))
   246  					Expect(includeStagingSecurityGroupRules).To(BeTrue())
   247  					Expect(fakeActorV3.GetEffectiveIsolationSegmentBySpaceCallCount()).To(Equal(0))
   248  				})
   249  			})
   250  
   251  			Context("when v3 api version is below 3.11.0 and the v2 api version is less than 2.68.0 (v2 will never be above 2.68.0 if v3 is lower than 3.11.0)", func() {
   252  				BeforeEach(func() {
   253  					fakeActor.CloudControllerAPIVersionReturns("2.54.0")
   254  					fakeActorV3.CloudControllerAPIVersionReturns("3.10.0")
   255  				})
   256  
   257  				It("displays warnings and a table with no values for staging security groups", func() {
   258  					Expect(executeErr).To(BeNil())
   259  
   260  					Expect(testUI.Out).NotTo(Say("isolation segment:"))
   261  
   262  					orgGUID, spaceName, includeStagingSecurityGroupRules := fakeActor.GetSpaceSummaryByOrganizationAndNameArgsForCall(0)
   263  					Expect(orgGUID).To(Equal("some-org-guid"))
   264  					Expect(spaceName).To(Equal("some-space"))
   265  					Expect(includeStagingSecurityGroupRules).To(BeFalse())
   266  					Expect(fakeActorV3.GetEffectiveIsolationSegmentBySpaceCallCount()).To(Equal(0))
   267  				})
   268  			})
   269  		})
   270  	})
   271  
   272  	Context("when getting the current user returns an error", func() {
   273  		var expectedErr error
   274  
   275  		BeforeEach(func() {
   276  			expectedErr = errors.New("getting current user error")
   277  			fakeConfig.CurrentUserReturns(
   278  				configv3.User{},
   279  				expectedErr)
   280  		})
   281  
   282  		It("returns the error", func() {
   283  			Expect(executeErr).To(MatchError(expectedErr))
   284  		})
   285  	})
   286  
   287  	Context("when getting the space summary returns an error", func() {
   288  		Context("when the error is translatable", func() {
   289  			BeforeEach(func() {
   290  				fakeActor.GetSpaceSummaryByOrganizationAndNameReturns(
   291  					v2action.SpaceSummary{},
   292  					v2action.Warnings{"warning-1", "warning-2"},
   293  					actionerror.SpaceNotFoundError{Name: "some-space"})
   294  			})
   295  
   296  			It("returns a translatable error and outputs all warnings", func() {
   297  				Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"}))
   298  
   299  				Expect(testUI.Err).To(Say("warning-1"))
   300  				Expect(testUI.Err).To(Say("warning-2"))
   301  			})
   302  		})
   303  
   304  		Context("when the error is not translatable", func() {
   305  			var expectedErr error
   306  
   307  			BeforeEach(func() {
   308  				expectedErr = errors.New("get space summary error")
   309  				fakeActor.GetSpaceSummaryByOrganizationAndNameReturns(
   310  					v2action.SpaceSummary{},
   311  					v2action.Warnings{"warning-1", "warning-2"},
   312  					expectedErr)
   313  			})
   314  
   315  			It("returns the error and all warnings", func() {
   316  				Expect(executeErr).To(MatchError(expectedErr))
   317  
   318  				Expect(testUI.Err).To(Say("warning-1"))
   319  				Expect(testUI.Err).To(Say("warning-2"))
   320  			})
   321  		})
   322  	})
   323  
   324  	Context("when getting the isolation segment returns an error", func() {
   325  		Context("a generic error", func() {
   326  			var expectedErr error
   327  
   328  			BeforeEach(func() {
   329  				expectedErr = errors.New("get isolation segment error")
   330  				fakeActorV3.GetEffectiveIsolationSegmentBySpaceReturns(
   331  					v3action.IsolationSegment{},
   332  					v3action.Warnings{"v3-warning-1", "v3-warning-2"},
   333  					expectedErr)
   334  			})
   335  
   336  			It("returns the error and all warnings", func() {
   337  				Expect(executeErr).To(MatchError(expectedErr))
   338  
   339  				Expect(testUI.Err).To(Say("v3-warning-1"))
   340  				Expect(testUI.Err).To(Say("v3-warning-2"))
   341  			})
   342  		})
   343  
   344  		Context("a NoRelationshipError", func() {
   345  			BeforeEach(func() {
   346  				fakeActorV3.GetEffectiveIsolationSegmentBySpaceReturns(
   347  					v3action.IsolationSegment{},
   348  					v3action.Warnings{"v3-warning-1", "v3-warning-2"},
   349  					actionerror.NoRelationshipError{})
   350  			})
   351  
   352  			It("does not fill in the isolation segment", func() {
   353  				Expect(executeErr).ToNot(HaveOccurred())
   354  				Expect(testUI.Out).To(Say("(?m)isolation segment:\\s*$"))
   355  			})
   356  		})
   357  	})
   358  
   359  	Context("when the --security-group-rules flag is provided", func() {
   360  		BeforeEach(func() {
   361  			fakeConfig.CurrentUserReturns(
   362  				configv3.User{
   363  					Name: "some-user",
   364  				},
   365  				nil)
   366  
   367  			cmd.RequiredArgs.Space = "some-space"
   368  			cmd.SecurityGroupRules = true
   369  
   370  			fakeConfig.TargetedOrganizationReturns(
   371  				configv3.Organization{
   372  					GUID: "some-org-guid",
   373  					Name: "some-org",
   374  				},
   375  			)
   376  
   377  			fakeActor.GetSpaceSummaryByOrganizationAndNameReturns(
   378  				v2action.SpaceSummary{
   379  					Space: v2action.Space{
   380  						Name: "some-space",
   381  					},
   382  					OrgName:                   "some-org",
   383  					AppNames:                  []string{"app1", "app2", "app3"},
   384  					ServiceInstanceNames:      []string{"service1", "service2", "service3"},
   385  					SpaceQuotaName:            "some-space-quota",
   386  					RunningSecurityGroupNames: []string{"public_networks", "dns", "load_balancer"},
   387  					StagingSecurityGroupNames: []string{"staging-sec-1", "staging-sec-2"},
   388  					SecurityGroupRules: []v2action.SecurityGroupRule{
   389  						{
   390  							Description: "Public networks",
   391  							Destination: "0.0.0.0-9.255.255.255",
   392  							Lifecycle:   "staging",
   393  							Name:        "public_networks",
   394  							Ports:       "12345",
   395  							Protocol:    "tcp",
   396  						},
   397  						{
   398  							Description: "Public networks",
   399  							Destination: "0.0.0.0-9.255.255.255",
   400  							Lifecycle:   "running",
   401  							Name:        "public_networks",
   402  							Ports:       "12345",
   403  							Protocol:    "tcp",
   404  						},
   405  						{
   406  							Description: "More public networks",
   407  							Destination: "11.0.0.0-169.253.255.255",
   408  							Lifecycle:   "staging",
   409  							Name:        "more_public_networks",
   410  							Ports:       "54321",
   411  							Protocol:    "udp",
   412  						},
   413  						{
   414  							Description: "More public networks",
   415  							Destination: "11.0.0.0-169.253.255.255",
   416  							Lifecycle:   "running",
   417  							Name:        "more_public_networks",
   418  							Ports:       "54321",
   419  							Protocol:    "udp",
   420  						},
   421  					},
   422  				},
   423  				v2action.Warnings{"warning-1", "warning-2"},
   424  				nil,
   425  			)
   426  		})
   427  
   428  		It("displays warnings and security group rules", func() {
   429  			Expect(executeErr).To(BeNil())
   430  
   431  			orgGUID, spaceName, includeStagingSecurityGroupRules := fakeActor.GetSpaceSummaryByOrganizationAndNameArgsForCall(0)
   432  			Expect(orgGUID).To(Equal("some-org-guid"))
   433  			Expect(spaceName).To(Equal("some-space"))
   434  			Expect(includeStagingSecurityGroupRules).To(BeTrue())
   435  
   436  			Expect(testUI.Out).To(Say("name:\\s+some-space"))
   437  			Expect(testUI.Out).To(Say("running security groups:\\s+public_networks, dns, load_balancer"))
   438  			Expect(testUI.Out).To(Say("staging security groups:\\s+staging-sec-1, staging-sec-2"))
   439  			Expect(testUI.Out).To(Say("(?m)^\n^\\s+security group\\s+destination\\s+ports\\s+protocol\\s+lifecycle\\s+description$"))
   440  			Expect(testUI.Out).To(Say("#0\\s+public_networks\\s+0.0.0.0-9.255.255.255\\s+12345\\s+tcp\\s+staging\\s+Public networks"))
   441  			Expect(testUI.Out).To(Say("(?m)^\\s+public_networks\\s+0.0.0.0-9.255.255.255\\s+12345\\s+tcp\\s+running\\s+Public networks"))
   442  			Expect(testUI.Out).To(Say("#1\\s+more_public_networks\\s+11.0.0.0-169.253.255.255\\s+54321\\s+udp\\s+staging\\s+More public networks"))
   443  			Expect(testUI.Out).To(Say("(?m)\\s+more_public_networks\\s+11.0.0.0-169.253.255.255\\s+54321\\s+udp\\s+running\\s+More public networks"))
   444  		})
   445  	})
   446  })