github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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 12 "code.cloudfoundry.org/cli/cf/api/apifakes" 13 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 14 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 15 16 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 17 18 "code.cloudfoundry.org/cli/cf/commands/domain" 19 "code.cloudfoundry.org/cli/cf/models" 20 . "github.com/onsi/ginkgo" 21 . "github.com/onsi/gomega" 22 ) 23 24 type passingRequirement struct { 25 Name string 26 } 27 28 func (r passingRequirement) Execute() error { 29 return nil 30 } 31 32 var _ = Describe("CreateSharedDomain", func() { 33 var ( 34 ui *testterm.FakeUI 35 routingAPIRepo *apifakes.FakeRoutingAPIRepository 36 domainRepo *apifakes.FakeDomainRepository 37 configRepo coreconfig.Repository 38 39 cmd domain.CreateSharedDomain 40 deps commandregistry.Dependency 41 factory *requirementsfakes.FakeFactory 42 flagContext flags.FlagContext 43 44 loginRequirement requirements.Requirement 45 46 routerGroups models.RouterGroups 47 ) 48 49 BeforeEach(func() { 50 ui = &testterm.FakeUI{} 51 configRepo = testconfig.NewRepositoryWithDefaults() 52 routingAPIRepo = new(apifakes.FakeRoutingAPIRepository) 53 repoLocator := deps.RepoLocator.SetRoutingAPIRepository(routingAPIRepo) 54 55 domainRepo = new(apifakes.FakeDomainRepository) 56 repoLocator = repoLocator.SetDomainRepository(domainRepo) 57 58 deps = commandregistry.Dependency{ 59 UI: ui, 60 Config: configRepo, 61 RepoLocator: repoLocator, 62 } 63 64 cmd = domain.CreateSharedDomain{} 65 cmd.SetDependency(deps, false) 66 67 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 68 69 factory = new(requirementsfakes.FakeFactory) 70 71 loginRequirement = &passingRequirement{Name: "Login"} 72 factory.NewLoginRequirementReturns(loginRequirement) 73 74 routingAPIRepo.ListRouterGroupsStub = func(cb func(models.RouterGroup) bool) error { 75 for _, r := range routerGroups { 76 if !cb(r) { 77 break 78 } 79 } 80 return nil 81 } 82 }) 83 84 Describe("Requirements", func() { 85 Context("when not provided exactly one arg", func() { 86 BeforeEach(func() { 87 flagContext.Parse("arg-1", "extra-arg") 88 }) 89 90 It("fails with usage", func() { 91 _, err := cmd.Requirements(factory, flagContext) 92 Expect(err).To(HaveOccurred()) 93 Expect(ui.Outputs()).To(ContainSubstrings( 94 []string{"Incorrect Usage. Requires DOMAIN as an argument"}, 95 []string{"NAME"}, 96 []string{"USAGE"}, 97 )) 98 }) 99 }) 100 101 Context("when provided exactly one arg", func() { 102 BeforeEach(func() { 103 flagContext.Parse("domain-name") 104 }) 105 106 It("does not fail with usage", func() { 107 _, err := cmd.Requirements(factory, flagContext) 108 Expect(err).NotTo(HaveOccurred()) 109 110 Expect(ui.Outputs()).NotTo(ContainSubstrings( 111 []string{"Incorrect Usage. Requires DOMAIN as an argument"}, 112 []string{"NAME"}, 113 []string{"USAGE"}, 114 )) 115 }) 116 117 It("returns a LoginRequirement", func() { 118 actualRequirements, err := cmd.Requirements(factory, flagContext) 119 Expect(err).NotTo(HaveOccurred()) 120 Expect(factory.NewLoginRequirementCallCount()).To(Equal(1)) 121 Expect(actualRequirements).To(ContainElement(loginRequirement)) 122 }) 123 }) 124 }) 125 126 Describe("Execute", func() { 127 var err error 128 129 JustBeforeEach(func() { 130 err = cmd.Execute(flagContext) 131 }) 132 133 Context("when router-group flag is set", func() { 134 BeforeEach(func() { 135 routerGroups = models.RouterGroups{ 136 models.RouterGroup{ 137 Name: "router-group-name", 138 GUID: "router-group-guid", 139 Type: "router-group-type", 140 }, 141 } 142 flagContext.Parse("domain-name", "--router-group", "router-group-name") 143 }) 144 145 It("tries to retrieve the router group", func() { 146 Expect(err).NotTo(HaveOccurred()) 147 Expect(routingAPIRepo.ListRouterGroupsCallCount()).To(Equal(1)) 148 }) 149 150 It("prints a message", func() { 151 Expect(err).NotTo(HaveOccurred()) 152 Expect(ui.Outputs()).To(ContainSubstrings( 153 []string{"Creating shared domain domain-name"}, 154 )) 155 }) 156 157 It("tries to create a shared domain with router group", func() { 158 Expect(err).NotTo(HaveOccurred()) 159 Expect(domainRepo.CreateSharedDomainCallCount()).To(Equal(1)) 160 domainName, routerGroupGUID := domainRepo.CreateSharedDomainArgsForCall(0) 161 Expect(domainName).To(Equal("domain-name")) 162 Expect(routerGroupGUID).To(Equal("router-group-guid")) 163 }) 164 165 It("prints success message", func() { 166 Expect(err).NotTo(HaveOccurred()) 167 Expect(ui.Outputs()).To(ContainSubstrings( 168 []string{"OK"}, 169 )) 170 }) 171 172 Context("when listing router groups returns an error", func() { 173 BeforeEach(func() { 174 routingAPIRepo.ListRouterGroupsReturns(errors.New("router-group-error")) 175 }) 176 177 It("fails with error message", func() { 178 Expect(err).To(HaveOccurred()) 179 Expect(err.Error()).To(Equal("router-group-error")) 180 }) 181 }) 182 183 Context("when router group is not found", func() { 184 BeforeEach(func() { 185 routerGroups = models.RouterGroups{} 186 }) 187 188 It("fails with a message", func() { 189 Expect(err).To(HaveOccurred()) 190 Expect(err.Error()).To(Equal("Router group router-group-name not found")) 191 }) 192 }) 193 }) 194 195 Context("when router-group flag is not set", func() { 196 BeforeEach(func() { 197 flagContext.Parse("domain-name") 198 }) 199 200 It("does not try to retrieve the router group", func() { 201 Expect(err).NotTo(HaveOccurred()) 202 Expect(routingAPIRepo.ListRouterGroupsCallCount()).To(Equal(0)) 203 }) 204 205 It("prints a message", func() { 206 Expect(err).NotTo(HaveOccurred()) 207 Expect(ui.Outputs()).To(ContainSubstrings( 208 []string{"Creating shared domain domain-name"}, 209 )) 210 }) 211 212 It("tries to create a shared domain without router group", func() { 213 Expect(err).NotTo(HaveOccurred()) 214 Expect(domainRepo.CreateSharedDomainCallCount()).To(Equal(1)) 215 domainName, routerGroupGUID := domainRepo.CreateSharedDomainArgsForCall(0) 216 Expect(domainName).To(Equal("domain-name")) 217 Expect(routerGroupGUID).To(Equal("")) 218 }) 219 220 It("prints success message", func() { 221 Expect(err).NotTo(HaveOccurred()) 222 Expect(ui.Outputs()).To(ContainSubstrings( 223 []string{"OK"}, 224 )) 225 }) 226 }) 227 228 Context("when creating shared domain returns error", func() { 229 BeforeEach(func() { 230 flagContext.Parse("domain-name") 231 domainRepo.CreateSharedDomainReturns(errors.New("create-domain-error")) 232 }) 233 234 It("fails with error", func() { 235 Expect(err).To(HaveOccurred()) 236 Expect(err.Error()).To(Equal("create-domain-error")) 237 }) 238 }) 239 }) 240 })