github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/domain/create_shared_domain_test.go (about)

     1  package domain_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     8  	"code.cloudfoundry.org/cli/cf/flags"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    11  	"github.com/blang/semver"
    12  
    13  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    14  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    15  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    16  
    17  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    18  
    19  	"code.cloudfoundry.org/cli/cf/commands/domain"
    20  	"code.cloudfoundry.org/cli/cf/models"
    21  	. "github.com/onsi/ginkgo"
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  type passingRequirement struct {
    26  	Name string
    27  }
    28  
    29  func (r passingRequirement) Execute() error {
    30  	return nil
    31  }
    32  
    33  var _ = Describe("CreateSharedDomain", func() {
    34  	var (
    35  		ui             *testterm.FakeUI
    36  		routingAPIRepo *apifakes.FakeRoutingAPIRepository
    37  		domainRepo     *apifakes.FakeDomainRepository
    38  		configRepo     coreconfig.Repository
    39  
    40  		cmd         domain.CreateSharedDomain
    41  		deps        commandregistry.Dependency
    42  		factory     *requirementsfakes.FakeFactory
    43  		flagContext flags.FlagContext
    44  
    45  		loginRequirement         requirements.Requirement
    46  		routingAPIRequirement    requirements.Requirement
    47  		minAPIVersionRequirement requirements.Requirement
    48  
    49  		routerGroups models.RouterGroups
    50  	)
    51  
    52  	BeforeEach(func() {
    53  		ui = &testterm.FakeUI{}
    54  		configRepo = testconfig.NewRepositoryWithDefaults()
    55  		routingAPIRepo = new(apifakes.FakeRoutingAPIRepository)
    56  		repoLocator := deps.RepoLocator.SetRoutingAPIRepository(routingAPIRepo)
    57  
    58  		domainRepo = new(apifakes.FakeDomainRepository)
    59  		repoLocator = repoLocator.SetDomainRepository(domainRepo)
    60  
    61  		deps = commandregistry.Dependency{
    62  			UI:          ui,
    63  			Config:      configRepo,
    64  			RepoLocator: repoLocator,
    65  		}
    66  
    67  		cmd = domain.CreateSharedDomain{}
    68  		cmd.SetDependency(deps, false)
    69  
    70  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    71  
    72  		factory = new(requirementsfakes.FakeFactory)
    73  
    74  		loginRequirement = &passingRequirement{Name: "Login"}
    75  		factory.NewLoginRequirementReturns(loginRequirement)
    76  
    77  		routingAPIRequirement = &passingRequirement{Name: "RoutingApi"}
    78  		factory.NewRoutingAPIRequirementReturns(routingAPIRequirement)
    79  
    80  		minAPIVersionRequirement = &passingRequirement{"MinAPIVersionRequirement"}
    81  		factory.NewMinAPIVersionRequirementReturns(minAPIVersionRequirement)
    82  
    83  		routingAPIRepo.ListRouterGroupsStub = func(cb func(models.RouterGroup) bool) error {
    84  			for _, r := range routerGroups {
    85  				if !cb(r) {
    86  					break
    87  				}
    88  			}
    89  			return nil
    90  		}
    91  	})
    92  
    93  	Describe("Requirements", func() {
    94  		Context("when not provided exactly one arg", func() {
    95  			BeforeEach(func() {
    96  				flagContext.Parse("arg-1", "extra-arg")
    97  			})
    98  
    99  			It("fails with usage", func() {
   100  				_, err := cmd.Requirements(factory, flagContext)
   101  				Expect(err).To(HaveOccurred())
   102  				Expect(ui.Outputs()).To(ContainSubstrings(
   103  					[]string{"Incorrect Usage. Requires DOMAIN as an argument"},
   104  					[]string{"NAME"},
   105  					[]string{"USAGE"},
   106  				))
   107  			})
   108  		})
   109  
   110  		Context("when provided exactly one arg", func() {
   111  			BeforeEach(func() {
   112  				flagContext.Parse("domain-name")
   113  			})
   114  
   115  			It("does not fail with usage", func() {
   116  				_, err := cmd.Requirements(factory, flagContext)
   117  				Expect(err).NotTo(HaveOccurred())
   118  
   119  				Expect(ui.Outputs()).NotTo(ContainSubstrings(
   120  					[]string{"Incorrect Usage. Requires DOMAIN as an argument"},
   121  					[]string{"NAME"},
   122  					[]string{"USAGE"},
   123  				))
   124  			})
   125  
   126  			It("returns a LoginRequirement", func() {
   127  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   128  				Expect(err).NotTo(HaveOccurred())
   129  				Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
   130  				Expect(actualRequirements).To(ContainElement(loginRequirement))
   131  			})
   132  
   133  			It("does not return a RoutingAPIRequirement", func() {
   134  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   135  				Expect(err).NotTo(HaveOccurred())
   136  				Expect(factory.NewRoutingAPIRequirementCallCount()).To(Equal(0))
   137  				Expect(actualRequirements).ToNot(ContainElement(routingAPIRequirement))
   138  			})
   139  
   140  			It("does not return a MinAPIVersionRequirement", func() {
   141  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   142  				Expect(err).NotTo(HaveOccurred())
   143  				Expect(actualRequirements).NotTo(ContainElement(minAPIVersionRequirement))
   144  			})
   145  
   146  			Context("when router-group flag is set", func() {
   147  				BeforeEach(func() {
   148  					flagContext.Parse("domain-name", "--router-group", "route-group-name")
   149  				})
   150  
   151  				It("returns a LoginRequirement", func() {
   152  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   153  					Expect(err).NotTo(HaveOccurred())
   154  					Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
   155  					Expect(actualRequirements).To(ContainElement(loginRequirement))
   156  				})
   157  
   158  				It("returns a RoutingAPIRequirement", func() {
   159  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   160  					Expect(err).NotTo(HaveOccurred())
   161  
   162  					Expect(factory.NewRoutingAPIRequirementCallCount()).To(Equal(1))
   163  					Expect(actualRequirements).To(ContainElement(routingAPIRequirement))
   164  				})
   165  
   166  				It("returns a MinAPIVersionRequirement", func() {
   167  					expectedVersion, err := semver.Make("2.36.0")
   168  					Expect(err).NotTo(HaveOccurred())
   169  
   170  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   171  					Expect(err).NotTo(HaveOccurred())
   172  
   173  					Expect(factory.NewMinAPIVersionRequirementCallCount()).To(Equal(1))
   174  					feature, requiredVersion := factory.NewMinAPIVersionRequirementArgsForCall(0)
   175  					Expect(feature).To(Equal("Option '--router-group'"))
   176  					Expect(requiredVersion).To(Equal(expectedVersion))
   177  					Expect(actualRequirements).To(ContainElement(minAPIVersionRequirement))
   178  				})
   179  			})
   180  		})
   181  	})
   182  
   183  	Describe("Execute", func() {
   184  		var err error
   185  
   186  		JustBeforeEach(func() {
   187  			err = cmd.Execute(flagContext)
   188  		})
   189  
   190  		Context("when router-group flag is set", func() {
   191  			BeforeEach(func() {
   192  				routerGroups = models.RouterGroups{
   193  					models.RouterGroup{
   194  						Name: "router-group-name",
   195  						GUID: "router-group-guid",
   196  						Type: "router-group-type",
   197  					},
   198  				}
   199  				flagContext.Parse("domain-name", "--router-group", "router-group-name")
   200  			})
   201  
   202  			It("tries to retrieve the router group", func() {
   203  				Expect(err).NotTo(HaveOccurred())
   204  				Expect(routingAPIRepo.ListRouterGroupsCallCount()).To(Equal(1))
   205  			})
   206  
   207  			It("prints a message", func() {
   208  				Expect(err).NotTo(HaveOccurred())
   209  				Expect(ui.Outputs()).To(ContainSubstrings(
   210  					[]string{"Creating shared domain domain-name"},
   211  				))
   212  			})
   213  
   214  			It("tries to create a shared domain with router group", func() {
   215  				Expect(err).NotTo(HaveOccurred())
   216  				Expect(domainRepo.CreateSharedDomainCallCount()).To(Equal(1))
   217  				domainName, routerGroupGUID := domainRepo.CreateSharedDomainArgsForCall(0)
   218  				Expect(domainName).To(Equal("domain-name"))
   219  				Expect(routerGroupGUID).To(Equal("router-group-guid"))
   220  			})
   221  
   222  			It("prints success message", func() {
   223  				Expect(err).NotTo(HaveOccurred())
   224  				Expect(ui.Outputs()).To(ContainSubstrings(
   225  					[]string{"OK"},
   226  				))
   227  			})
   228  
   229  			Context("when listing router groups returns an error", func() {
   230  				BeforeEach(func() {
   231  					routingAPIRepo.ListRouterGroupsReturns(errors.New("router-group-error"))
   232  				})
   233  
   234  				It("fails with error message", func() {
   235  					Expect(err).To(HaveOccurred())
   236  					Expect(err.Error()).To(Equal("router-group-error"))
   237  				})
   238  			})
   239  
   240  			Context("when router group is not found", func() {
   241  				BeforeEach(func() {
   242  					routerGroups = models.RouterGroups{}
   243  				})
   244  
   245  				It("fails with a message", func() {
   246  					Expect(err).To(HaveOccurred())
   247  					Expect(err.Error()).To(Equal("Router group router-group-name not found"))
   248  				})
   249  			})
   250  		})
   251  
   252  		Context("when router-group flag is not set", func() {
   253  			BeforeEach(func() {
   254  				flagContext.Parse("domain-name")
   255  			})
   256  
   257  			It("does not try to retrieve the router group", func() {
   258  				Expect(err).NotTo(HaveOccurred())
   259  				Expect(routingAPIRepo.ListRouterGroupsCallCount()).To(Equal(0))
   260  			})
   261  
   262  			It("prints a message", func() {
   263  				Expect(err).NotTo(HaveOccurred())
   264  				Expect(ui.Outputs()).To(ContainSubstrings(
   265  					[]string{"Creating shared domain domain-name"},
   266  				))
   267  			})
   268  
   269  			It("tries to create a shared domain without router group", func() {
   270  				Expect(err).NotTo(HaveOccurred())
   271  				Expect(domainRepo.CreateSharedDomainCallCount()).To(Equal(1))
   272  				domainName, routerGroupGUID := domainRepo.CreateSharedDomainArgsForCall(0)
   273  				Expect(domainName).To(Equal("domain-name"))
   274  				Expect(routerGroupGUID).To(Equal(""))
   275  			})
   276  
   277  			It("prints success message", func() {
   278  				Expect(err).NotTo(HaveOccurred())
   279  				Expect(ui.Outputs()).To(ContainSubstrings(
   280  					[]string{"OK"},
   281  				))
   282  			})
   283  		})
   284  
   285  		Context("when creating shared domain returns error", func() {
   286  			BeforeEach(func() {
   287  				flagContext.Parse("domain-name")
   288  				domainRepo.CreateSharedDomainReturns(errors.New("create-domain-error"))
   289  			})
   290  
   291  			It("fails with error", func() {
   292  				Expect(err).To(HaveOccurred())
   293  				Expect(err.Error()).To(Equal("create-domain-error"))
   294  			})
   295  		})
   296  	})
   297  })