github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/cf/commands/space/space_test.go (about)

     1  package space_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/spacequotas/spacequotasfakes"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  
     7  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
     8  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
     9  	"code.cloudfoundry.org/cli/plugin/models"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  
    13  	"code.cloudfoundry.org/cli/cf/api"
    14  	"code.cloudfoundry.org/cli/cf/commands/space"
    15  	"code.cloudfoundry.org/cli/cf/flags"
    16  	"code.cloudfoundry.org/cli/cf/models"
    17  	"code.cloudfoundry.org/cli/cf/requirements"
    18  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    19  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    20  )
    21  
    22  var _ = Describe("space command", func() {
    23  	var (
    24  		ui               *testterm.FakeUI
    25  		loginReq         *requirementsfakes.FakeRequirement
    26  		targetedOrgReq   *requirementsfakes.FakeTargetedOrgRequirement
    27  		reqFactory       *requirementsfakes.FakeFactory
    28  		deps             commandregistry.Dependency
    29  		cmd              space.ShowSpace
    30  		flagContext      flags.FlagContext
    31  		getSpaceModel    *plugin_models.GetSpace_Model
    32  		spaceRequirement *requirementsfakes.FakeSpaceRequirement
    33  		quotaRepo        *spacequotasfakes.FakeSpaceQuotaRepository
    34  	)
    35  
    36  	BeforeEach(func() {
    37  		ui = new(testterm.FakeUI)
    38  		quotaRepo = new(spacequotasfakes.FakeSpaceQuotaRepository)
    39  		repoLocator := api.RepositoryLocator{}
    40  		repoLocator = repoLocator.SetSpaceQuotaRepository(quotaRepo)
    41  		getSpaceModel = new(plugin_models.GetSpace_Model)
    42  
    43  		deps = commandregistry.Dependency{
    44  			UI:          ui,
    45  			Config:      testconfig.NewRepositoryWithDefaults(),
    46  			RepoLocator: repoLocator,
    47  			PluginModels: &commandregistry.PluginModels{
    48  				Space: getSpaceModel,
    49  			},
    50  		}
    51  
    52  		reqFactory = new(requirementsfakes.FakeFactory)
    53  
    54  		loginReq = new(requirementsfakes.FakeRequirement)
    55  		loginReq.ExecuteReturns(nil)
    56  		reqFactory.NewLoginRequirementReturns(loginReq)
    57  
    58  		targetedOrgReq = new(requirementsfakes.FakeTargetedOrgRequirement)
    59  		targetedOrgReq.ExecuteReturns(nil)
    60  		reqFactory.NewTargetedOrgRequirementReturns(targetedOrgReq)
    61  
    62  		spaceRequirement = new(requirementsfakes.FakeSpaceRequirement)
    63  		spaceRequirement.ExecuteReturns(nil)
    64  		reqFactory.NewSpaceRequirementReturns(spaceRequirement)
    65  
    66  		cmd = space.ShowSpace{}
    67  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    68  		cmd.SetDependency(deps, false)
    69  	})
    70  
    71  	Describe("Requirements", func() {
    72  		Context("when the wrong number of args are provided", func() {
    73  			BeforeEach(func() {
    74  				err := flagContext.Parse()
    75  				Expect(err).NotTo(HaveOccurred())
    76  			})
    77  
    78  			It("fails with no args", func() {
    79  				_, err := cmd.Requirements(reqFactory, flagContext)
    80  				Expect(err).To(HaveOccurred())
    81  				Expect(ui.Outputs()).To(ContainSubstrings(
    82  					[]string{"FAILED"},
    83  					[]string{"Incorrect Usage. Requires an argument"},
    84  				))
    85  			})
    86  		})
    87  
    88  		Context("when provided exactly one arg", func() {
    89  			var actualRequirements []requirements.Requirement
    90  
    91  			Context("when no flags are provided", func() {
    92  				BeforeEach(func() {
    93  					err := flagContext.Parse("my-space")
    94  					Expect(err).NotTo(HaveOccurred())
    95  					actualRequirements, err = cmd.Requirements(reqFactory, flagContext)
    96  					Expect(err).NotTo(HaveOccurred())
    97  				})
    98  
    99  				It("returns a login requirement", func() {
   100  					Expect(reqFactory.NewLoginRequirementCallCount()).To(Equal(1))
   101  					Expect(actualRequirements).To(ContainElement(loginReq))
   102  				})
   103  
   104  				It("returns a targeted org requirement", func() {
   105  					Expect(reqFactory.NewTargetedOrgRequirementCallCount()).To(Equal(1))
   106  					Expect(actualRequirements).To(ContainElement(targetedOrgReq))
   107  				})
   108  
   109  				It("returns a space requirement", func() {
   110  					Expect(reqFactory.NewSpaceRequirementCallCount()).To(Equal(1))
   111  					Expect(actualRequirements).To(ContainElement(spaceRequirement))
   112  				})
   113  			})
   114  		})
   115  	})
   116  
   117  	Describe("Execute", func() {
   118  		var (
   119  			space      models.Space
   120  			spaceQuota models.SpaceQuota
   121  			executeErr error
   122  		)
   123  
   124  		BeforeEach(func() {
   125  			org := models.OrganizationFields{
   126  				Name: "my-org",
   127  				GUID: "my-org-guid",
   128  			}
   129  
   130  			app := models.ApplicationFields{
   131  				Name: "app1",
   132  				GUID: "app1-guid",
   133  			}
   134  
   135  			apps := []models.ApplicationFields{app}
   136  
   137  			domain := models.DomainFields{
   138  				Name: "domain1",
   139  				GUID: "domain1-guid",
   140  			}
   141  
   142  			domains := []models.DomainFields{domain}
   143  
   144  			serviceInstance := models.ServiceInstanceFields{
   145  				Name: "service1",
   146  				GUID: "service1-guid",
   147  			}
   148  			services := []models.ServiceInstanceFields{serviceInstance}
   149  
   150  			securityGroup1 := models.SecurityGroupFields{Name: "Nacho Security", Rules: []map[string]interface{}{
   151  				{"protocol": "all", "destination": "0.0.0.0-9.255.255.255", "log": true, "IntTest": 1000},
   152  			}}
   153  			securityGroup2 := models.SecurityGroupFields{Name: "Nacho Prime", Rules: []map[string]interface{}{
   154  				{"protocol": "udp", "ports": "8080-9090", "destination": "198.41.191.47/1"},
   155  			}}
   156  			securityGroups := []models.SecurityGroupFields{securityGroup1, securityGroup2}
   157  
   158  			space = models.Space{
   159  				SpaceFields: models.SpaceFields{
   160  					Name: "whose-space-is-it-anyway",
   161  					GUID: "whose-space-is-it-anyway-guid",
   162  				},
   163  				Organization:     org,
   164  				Applications:     apps,
   165  				Domains:          domains,
   166  				ServiceInstances: services,
   167  				SecurityGroups:   securityGroups,
   168  				SpaceQuotaGUID:   "runaway-guid",
   169  			}
   170  
   171  			spaceRequirement.GetSpaceReturns(space)
   172  
   173  			spaceQuota = models.SpaceQuota{
   174  				Name:                    "runaway",
   175  				GUID:                    "runaway-guid",
   176  				MemoryLimit:             102400,
   177  				InstanceMemoryLimit:     -1,
   178  				RoutesLimit:             111,
   179  				ServicesLimit:           222,
   180  				NonBasicServicesAllowed: false,
   181  				AppInstanceLimit:        7,
   182  				ReservedRoutePortsLimit: "7",
   183  			}
   184  
   185  			quotaRepo.FindByGUIDReturns(spaceQuota, nil)
   186  		})
   187  
   188  		JustBeforeEach(func() {
   189  			executeErr = cmd.Execute(flagContext)
   190  		})
   191  
   192  		Context("when logged in and an org is targeted", func() {
   193  			BeforeEach(func() {
   194  				err := flagContext.Parse("my-space")
   195  				Expect(err).NotTo(HaveOccurred())
   196  				cmd.Requirements(reqFactory, flagContext)
   197  			})
   198  
   199  			Context("when the guid flag is passed", func() {
   200  				BeforeEach(func() {
   201  					err := flagContext.Parse("my-space", "--guid")
   202  					Expect(err).NotTo(HaveOccurred())
   203  				})
   204  
   205  				It("shows only the space guid", func() {
   206  					Expect(executeErr).NotTo(HaveOccurred())
   207  					Expect(ui.Outputs()).To(ContainSubstrings(
   208  						[]string{"whose-space-is-it-anyway-guid"},
   209  					))
   210  
   211  					Expect(ui.Outputs()).ToNot(ContainSubstrings(
   212  						[]string{"Getting info for space", "whose-space-is-it-anyway", "my-org", "my-user"},
   213  					))
   214  				})
   215  			})
   216  
   217  			Context("when the security-group-rules flag is passed", func() {
   218  				BeforeEach(func() {
   219  					err := flagContext.Parse("my-space", "--security-group-rules")
   220  					Expect(err).NotTo(HaveOccurred())
   221  				})
   222  				It("it shows space information and security group rules", func() {
   223  					Expect(executeErr).NotTo(HaveOccurred())
   224  					Expect(ui.Outputs()).To(ContainSubstrings(
   225  						[]string{"Getting rules for the security group", "Nacho Security"},
   226  						[]string{"protocol", "all"},
   227  						[]string{"destination", "0.0.0.0-9.255.255.255"},
   228  						[]string{"Getting rules for the security group", "Nacho Prime"},
   229  						[]string{"protocol", "udp"},
   230  						[]string{"log", "true"},
   231  						[]string{"IntTest", "1000"},
   232  						[]string{"ports", "8080-9090"},
   233  						[]string{"destination", "198.41.191.47/1"},
   234  					))
   235  				})
   236  			})
   237  
   238  			Context("when the space has a space quota", func() {
   239  				It("shows information about the given space", func() {
   240  					Expect(executeErr).NotTo(HaveOccurred())
   241  					Expect(ui.Outputs()).To(ContainSubstrings(
   242  						[]string{"Getting info for space", "whose-space-is-it-anyway", "my-org", "my-user"},
   243  						[]string{"OK"},
   244  						[]string{"whose-space-is-it-anyway"},
   245  						[]string{"Org", "my-org"},
   246  						[]string{"Apps", "app1"},
   247  						[]string{"Domains", "domain1"},
   248  						[]string{"Services", "service1"},
   249  						[]string{"Security Groups", "Nacho Security", "Nacho Prime"},
   250  						[]string{"Space Quota", "runaway (100G memory limit, unlimited instance memory limit, 111 routes, 222 services, paid services disallowed, 7 app instance limit, 7 route ports)"},
   251  					))
   252  				})
   253  
   254  				Context("when the route ports limit is -1", func() {
   255  					BeforeEach(func() {
   256  						spaceQuota.ReservedRoutePortsLimit = "-1"
   257  						quotaRepo.FindByGUIDReturns(spaceQuota, nil)
   258  					})
   259  
   260  					It("displays unlimited as the route ports limit", func() {
   261  						Expect(executeErr).NotTo(HaveOccurred())
   262  						Expect(ui.Outputs()).To(ContainSubstrings(
   263  							[]string{"unlimited route ports"},
   264  						))
   265  					})
   266  				})
   267  
   268  				Context("when the reserved route ports field is not provided by the CC API", func() {
   269  					BeforeEach(func() {
   270  						spaceQuota.ReservedRoutePortsLimit = ""
   271  						quotaRepo.FindByGUIDReturns(spaceQuota, nil)
   272  					})
   273  
   274  					It("should not display route ports", func() {
   275  						Expect(executeErr).NotTo(HaveOccurred())
   276  						Expect(ui.Outputs()).NotTo(ContainSubstrings(
   277  							[]string{"route ports"},
   278  						))
   279  					})
   280  				})
   281  
   282  				Context("when the app instance limit is -1", func() {
   283  					BeforeEach(func() {
   284  						spaceQuota.AppInstanceLimit = -1
   285  						quotaRepo.FindByGUIDReturns(spaceQuota, nil)
   286  					})
   287  
   288  					It("displays unlimited as the app instance limit", func() {
   289  						Expect(executeErr).NotTo(HaveOccurred())
   290  						Expect(ui.Outputs()).To(ContainSubstrings(
   291  							[]string{"unlimited app instance limit"},
   292  						))
   293  					})
   294  				})
   295  			})
   296  
   297  			Context("when the space does not have a space quota", func() {
   298  				BeforeEach(func() {
   299  					space.SpaceQuotaGUID = ""
   300  					spaceRequirement.GetSpaceReturns(space)
   301  				})
   302  
   303  				It("shows information without a space quota", func() {
   304  					Expect(executeErr).NotTo(HaveOccurred())
   305  					Expect(quotaRepo.FindByGUIDCallCount()).To(Equal(0))
   306  					Expect(ui.Outputs()).To(ContainSubstrings(
   307  						[]string{"Getting info for space", "whose-space-is-it-anyway", "my-org", "my-user"},
   308  						[]string{"OK"},
   309  						[]string{"whose-space-is-it-anyway"},
   310  						[]string{"Org", "my-org"},
   311  						[]string{"Apps", "app1"},
   312  						[]string{"Domains", "domain1"},
   313  						[]string{"Services", "service1"},
   314  						[]string{"Security Groups", "Nacho Security", "Nacho Prime"},
   315  						[]string{"Space Quota"},
   316  					))
   317  				})
   318  			})
   319  
   320  			Context("When called as a plugin", func() {
   321  				BeforeEach(func() {
   322  					cmd.SetDependency(deps, true)
   323  				})
   324  
   325  				It("Fills in the PluginModel", func() {
   326  					Expect(executeErr).NotTo(HaveOccurred())
   327  
   328  					Expect(getSpaceModel.Name).To(Equal("whose-space-is-it-anyway"))
   329  					Expect(getSpaceModel.Guid).To(Equal("whose-space-is-it-anyway-guid"))
   330  
   331  					Expect(getSpaceModel.Organization.Name).To(Equal("my-org"))
   332  					Expect(getSpaceModel.Organization.Guid).To(Equal("my-org-guid"))
   333  
   334  					Expect(getSpaceModel.Applications).To(HaveLen(1))
   335  					Expect(getSpaceModel.Applications[0].Name).To(Equal("app1"))
   336  					Expect(getSpaceModel.Applications[0].Guid).To(Equal("app1-guid"))
   337  
   338  					Expect(getSpaceModel.Domains).To(HaveLen(1))
   339  					Expect(getSpaceModel.Domains[0].Name).To(Equal("domain1"))
   340  					Expect(getSpaceModel.Domains[0].Guid).To(Equal("domain1-guid"))
   341  
   342  					Expect(getSpaceModel.ServiceInstances).To(HaveLen(1))
   343  					Expect(getSpaceModel.ServiceInstances[0].Name).To(Equal("service1"))
   344  					Expect(getSpaceModel.ServiceInstances[0].Guid).To(Equal("service1-guid"))
   345  
   346  					Expect(getSpaceModel.SecurityGroups).To(HaveLen(2))
   347  					Expect(getSpaceModel.SecurityGroups[0].Name).To(Equal("Nacho Security"))
   348  					Expect(getSpaceModel.SecurityGroups[0].Rules).To(HaveLen(1))
   349  					Expect(getSpaceModel.SecurityGroups[0].Rules[0]).To(HaveLen(4))
   350  					val := getSpaceModel.SecurityGroups[0].Rules[0]["protocol"]
   351  					Expect(val).To(Equal("all"))
   352  					val = getSpaceModel.SecurityGroups[0].Rules[0]["destination"]
   353  					Expect(val).To(Equal("0.0.0.0-9.255.255.255"))
   354  
   355  					Expect(getSpaceModel.SecurityGroups[1].Name).To(Equal("Nacho Prime"))
   356  					Expect(getSpaceModel.SecurityGroups[1].Rules).To(HaveLen(1))
   357  					Expect(getSpaceModel.SecurityGroups[1].Rules[0]).To(HaveLen(3))
   358  					val = getSpaceModel.SecurityGroups[1].Rules[0]["protocol"]
   359  					Expect(val).To(Equal("udp"))
   360  					val = getSpaceModel.SecurityGroups[1].Rules[0]["destination"]
   361  					Expect(val).To(Equal("198.41.191.47/1"))
   362  					val = getSpaceModel.SecurityGroups[1].Rules[0]["ports"]
   363  					Expect(val).To(Equal("8080-9090"))
   364  
   365  					Expect(getSpaceModel.SpaceQuota.Name).To(Equal("runaway"))
   366  					Expect(getSpaceModel.SpaceQuota.Guid).To(Equal("runaway-guid"))
   367  					Expect(getSpaceModel.SpaceQuota.MemoryLimit).To(Equal(int64(102400)))
   368  					Expect(getSpaceModel.SpaceQuota.InstanceMemoryLimit).To(Equal(int64(-1)))
   369  					Expect(getSpaceModel.SpaceQuota.RoutesLimit).To(Equal(111))
   370  					Expect(getSpaceModel.SpaceQuota.ServicesLimit).To(Equal(222))
   371  					Expect(getSpaceModel.SpaceQuota.NonBasicServicesAllowed).To(BeFalse())
   372  				})
   373  			})
   374  		})
   375  	})
   376  })