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