github.com/loafoe/cli@v7.1.0+incompatible/command/v7/space_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     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/integration/helpers"
    13  	"code.cloudfoundry.org/cli/resources"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("space command", func() {
    22  	var (
    23  		cmd             SpaceCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v7fakes.FakeActor
    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(v7fakes.FakeActor)
    37  
    38  		cmd = SpaceCommand{
    39  			BaseCommand: BaseCommand{
    40  				UI:          testUI,
    41  				Config:      fakeConfig,
    42  				SharedActor: fakeSharedActor,
    43  				Actor:       fakeActor,
    44  			},
    45  			RequiredArgs: flag.Space{
    46  				Space: "some-space",
    47  			},
    48  		}
    49  
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  
    53  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    54  			Name: "some-org",
    55  			GUID: "some-org-guid",
    56  		})
    57  
    58  		fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    59  	})
    60  
    61  	JustBeforeEach(func() {
    62  		executeErr = cmd.Execute(nil)
    63  	})
    64  
    65  	When("checking target fails", func() {
    66  		BeforeEach(func() {
    67  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    68  		})
    69  
    70  		It("returns an error", func() {
    71  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    72  
    73  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    74  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    75  			Expect(checkTargetedOrg).To(BeTrue())
    76  			Expect(checkTargetedSpace).To(BeFalse())
    77  		})
    78  	})
    79  
    80  	When("the user is not logged in", func() {
    81  		var expectedErr error
    82  
    83  		BeforeEach(func() {
    84  			expectedErr = errors.New("some current user error")
    85  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    86  		})
    87  
    88  		It("return an error", func() {
    89  			Expect(executeErr).To(Equal(expectedErr))
    90  		})
    91  	})
    92  
    93  	When("the --guid flag is passed", func() {
    94  		BeforeEach(func() {
    95  			cmd.GUID = true
    96  		})
    97  
    98  		When("getting the space succeeds", func() {
    99  			BeforeEach(func() {
   100  				fakeActor.GetSpaceByNameAndOrganizationReturns(
   101  					resources.Space{GUID: "some-space-guid"},
   102  					v7action.Warnings{"some-warning"},
   103  					nil,
   104  				)
   105  			})
   106  
   107  			It("displays warnings and the space guid", func() {
   108  				Expect(executeErr).NotTo(HaveOccurred())
   109  				Expect(testUI.Err).To(Say("some-warning"))
   110  				Expect(testUI.Out).To(Say("some-space-guid"))
   111  			})
   112  		})
   113  
   114  		When("getting the space fails", func() {
   115  			BeforeEach(func() {
   116  				fakeActor.GetSpaceByNameAndOrganizationReturns(
   117  					resources.Space{},
   118  					v7action.Warnings{"some-warning"},
   119  					errors.New("space-error"),
   120  				)
   121  			})
   122  
   123  			It("displays warnings and returns the error", func() {
   124  				Expect(testUI.Err).To(Say("some-warning"))
   125  				Expect(executeErr).To(MatchError("space-error"))
   126  			})
   127  		})
   128  	})
   129  
   130  	When("the --guid flag is not passed", func() {
   131  		var (
   132  			runningSecurityGroup1 resources.SecurityGroup
   133  			runningSecurityGroup2 resources.SecurityGroup
   134  			stagingSecurityGroup  resources.SecurityGroup
   135  			description           string
   136  			ports                 string
   137  		)
   138  
   139  		BeforeEach(func() {
   140  			ports = "8080"
   141  			description = "Test security group"
   142  			runningSecurityGroup1 = helpers.NewSecurityGroup(
   143  				"dns",
   144  				"tcp",
   145  				"10.244.1.18",
   146  				&ports,
   147  				&description,
   148  			)
   149  			runningSecurityGroup2 = helpers.NewSecurityGroup(
   150  				"credhub",
   151  				"all",
   152  				"0.0.0.0-5.6.7.8",
   153  				nil,
   154  				&description,
   155  			)
   156  			stagingSecurityGroup = helpers.NewSecurityGroup(
   157  				"sarah",
   158  				"udp",
   159  				"127.0.0.1",
   160  				&ports,
   161  				nil,
   162  			)
   163  		})
   164  
   165  		When("the --security-group-rules flag is passed", func() {
   166  			BeforeEach(func() {
   167  				cmd.SecurityGroupRules = true
   168  
   169  				fakeActor.GetSpaceSummaryByNameAndOrganizationReturns(
   170  					v7action.SpaceSummary{
   171  						Name:                  "some-space",
   172  						OrgName:               "some-org",
   173  						AppNames:              []string{"app1", "app2", "app3"},
   174  						ServiceInstanceNames:  []string{"instance1", "instance2"},
   175  						RunningSecurityGroups: []resources.SecurityGroup{runningSecurityGroup1, runningSecurityGroup2},
   176  						StagingSecurityGroups: []resources.SecurityGroup{stagingSecurityGroup},
   177  					},
   178  					v7action.Warnings{"some-warning"},
   179  					nil,
   180  				)
   181  			})
   182  
   183  			It("displays flavor text", func() {
   184  				Expect(testUI.Out).To(Say("Getting info for space some-space in org some-org as steve..."))
   185  			})
   186  
   187  			It("displays warnings", func() {
   188  				Expect(testUI.Err).To(Say("some-warning"))
   189  			})
   190  
   191  			It("displays a table of values and security group rules", func() {
   192  				Expect(testUI.Out).To(Say(`name:\s+some-space`))
   193  				Expect(testUI.Out).To(Say(`org:\s+some-org`))
   194  				Expect(testUI.Out).To(Say(`apps:\s+app1, app2, app3`))
   195  				Expect(testUI.Out).To(Say(`services:\s+instance1, instance2`))
   196  				Expect(testUI.Out).To(Say(`isolation segment:`))
   197  				Expect(testUI.Out).To(Say(`running security groups:\s+%s, %s`, runningSecurityGroup1.Name, runningSecurityGroup2.Name))
   198  				Expect(testUI.Out).To(Say(`staging security groups:\s+%s`, stagingSecurityGroup.Name))
   199  
   200  				Expect(testUI.Out).To(Say(`security group\s+destination\s+ports\s+protocol\s+lifecycle\s+description`))
   201  				Expect(testUI.Out).To(Say(`%s\s+%s\s+%s\s+%s\s+%s\s+%s`,
   202  					runningSecurityGroup2.Name, runningSecurityGroup2.Rules[0].Destination, "", runningSecurityGroup2.Rules[0].Protocol, "running", description,
   203  				))
   204  				Expect(testUI.Out).To(Say(`%s\s+%s\s+%s\s+%s\s+%s\s+%s`,
   205  					runningSecurityGroup1.Name, runningSecurityGroup1.Rules[0].Destination, ports, runningSecurityGroup1.Rules[0].Protocol, "running", description,
   206  				))
   207  				Expect(testUI.Out).To(Say(`%s\s+%s\s+%s\s+%s\s+%s\s+%s`,
   208  					stagingSecurityGroup.Name, stagingSecurityGroup.Rules[0].Destination, ports, stagingSecurityGroup.Rules[0].Protocol, "staging", "",
   209  				))
   210  			})
   211  
   212  			When("there are no security groups to show", func() {
   213  				BeforeEach(func() {
   214  					fakeActor.GetSpaceSummaryByNameAndOrganizationReturns(
   215  						v7action.SpaceSummary{},
   216  						v7action.Warnings{"some-warning"},
   217  						nil,
   218  					)
   219  				})
   220  
   221  				It("displays an empty message", func() {
   222  					Expect(testUI.Out).To(Say(`running security groups:\s*\n`))
   223  					Expect(testUI.Out).To(Say(`staging security groups:\s*\n`))
   224  					Expect(testUI.Out).To(Say("No security group rules found."))
   225  				})
   226  			})
   227  		})
   228  
   229  		When("fetching the space summary succeeds with an isolation segment", func() {
   230  			BeforeEach(func() {
   231  				fakeActor.GetSpaceSummaryByNameAndOrganizationReturns(
   232  					v7action.SpaceSummary{
   233  						Name:                  "some-space",
   234  						OrgName:               "some-org",
   235  						AppNames:              []string{"app1", "app2", "app3"},
   236  						ServiceInstanceNames:  []string{"instance1", "instance2"},
   237  						IsolationSegmentName:  "iso-seg-name",
   238  						RunningSecurityGroups: []resources.SecurityGroup{runningSecurityGroup1, runningSecurityGroup2},
   239  						StagingSecurityGroups: []resources.SecurityGroup{stagingSecurityGroup},
   240  					},
   241  					v7action.Warnings{"some-warning"},
   242  					nil,
   243  				)
   244  			})
   245  
   246  			It("displays flavor text", func() {
   247  				Expect(testUI.Out).To(Say("Getting info for space some-space in org some-org as steve..."))
   248  			})
   249  
   250  			It("displays warnings", func() {
   251  				Expect(testUI.Err).To(Say("some-warning"))
   252  			})
   253  
   254  			It("displays a table of values", func() {
   255  				Expect(testUI.Out).To(Say(`name:\s+some-space`))
   256  				Expect(testUI.Out).To(Say(`org:\s+some-org`))
   257  				Expect(testUI.Out).To(Say(`apps:\s+app1, app2, app3`))
   258  				Expect(testUI.Out).To(Say(`services:\s+instance1, instance2`))
   259  				Expect(testUI.Out).To(Say(`isolation segment:\s+iso-seg-name`))
   260  				Expect(testUI.Out).To(Say(`running security groups:\s+%s, %s`, runningSecurityGroup1.Name, runningSecurityGroup2.Name))
   261  				Expect(testUI.Out).To(Say(`staging security groups:\s+%s`, stagingSecurityGroup.Name))
   262  			})
   263  		})
   264  
   265  		When("fetching the space summary succeeds without an isolation segment", func() {
   266  			BeforeEach(func() {
   267  				fakeActor.GetSpaceSummaryByNameAndOrganizationReturns(
   268  					v7action.SpaceSummary{
   269  						Name:                  "some-space",
   270  						OrgName:               "some-org",
   271  						AppNames:              []string{"app1", "app2", "app3"},
   272  						ServiceInstanceNames:  []string{"instance1", "instance2"},
   273  						RunningSecurityGroups: []resources.SecurityGroup{runningSecurityGroup1, runningSecurityGroup2},
   274  						StagingSecurityGroups: []resources.SecurityGroup{stagingSecurityGroup},
   275  					},
   276  					v7action.Warnings{"some-warning"},
   277  					nil,
   278  				)
   279  			})
   280  
   281  			It("displays flavor text", func() {
   282  				Expect(testUI.Out).To(Say("Getting info for space some-space in org some-org as steve..."))
   283  			})
   284  
   285  			It("displays warnings", func() {
   286  				Expect(testUI.Err).To(Say("some-warning"))
   287  			})
   288  
   289  			It("displays a table of values", func() {
   290  				Expect(testUI.Out).To(Say(`name:\s+some-space`))
   291  				Expect(testUI.Out).To(Say(`org:\s+some-org`))
   292  				Expect(testUI.Out).To(Say(`apps:\s+app1, app2, app3`))
   293  				Expect(testUI.Out).To(Say(`services:\s+instance1, instance2`))
   294  				Expect(testUI.Out).To(Say(`isolation segment:`))
   295  				Expect(testUI.Out).To(Say(`running security groups:\s+%s, %s`, runningSecurityGroup1.Name, runningSecurityGroup2.Name))
   296  				Expect(testUI.Out).To(Say(`staging security groups:\s+%s`, stagingSecurityGroup.Name))
   297  			})
   298  		})
   299  
   300  		When("fetching a space with an applied quota", func() {
   301  			BeforeEach(func() {
   302  				fakeActor.GetSpaceSummaryByNameAndOrganizationReturns(
   303  					v7action.SpaceSummary{
   304  						Name:                 "some-space",
   305  						OrgName:              "some-org",
   306  						AppNames:             []string{"app1", "app2", "app3"},
   307  						ServiceInstanceNames: []string{"instance1", "instance2"},
   308  						IsolationSegmentName: "iso-seg-name",
   309  						QuotaName:            "applied-quota-name",
   310  					},
   311  					v7action.Warnings{"some-warning"},
   312  					nil,
   313  				)
   314  			})
   315  
   316  			It("displays the applied quota", func() {
   317  				Expect(testUI.Out).To(Say(`quota:\s+applied-quota-name`))
   318  			})
   319  		})
   320  
   321  		When("fetching a space that has no quota applied", func() {
   322  			BeforeEach(func() {
   323  				fakeActor.GetSpaceSummaryByNameAndOrganizationReturns(
   324  					v7action.SpaceSummary{
   325  						Name:                 "some-space",
   326  						OrgName:              "some-org",
   327  						AppNames:             []string{"app1", "app2", "app3"},
   328  						ServiceInstanceNames: []string{"instance1", "instance2"},
   329  					},
   330  					v7action.Warnings{"some-warning"},
   331  					nil,
   332  				)
   333  			})
   334  
   335  			It("displays a quota row with no value", func() {
   336  				Expect(testUI.Out).To(Say(`quota:\s*\n`))
   337  			})
   338  		})
   339  
   340  		When("fetching the space summary fails", func() {
   341  			BeforeEach(func() {
   342  				fakeActor.GetSpaceSummaryByNameAndOrganizationReturns(
   343  					v7action.SpaceSummary{},
   344  					v7action.Warnings{"some-warning"},
   345  					errors.New("get-summary-error"),
   346  				)
   347  			})
   348  
   349  			It("displays warnings and returns the error", func() {
   350  				Expect(testUI.Err).To(Say("some-warning"))
   351  				Expect(executeErr).To(MatchError("get-summary-error"))
   352  			})
   353  		})
   354  	})
   355  })