github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/v6/isolated/create_shared_domain_test.go (about) 1 package isolated 2 3 import ( 4 "fmt" 5 "regexp" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 8 "code.cloudfoundry.org/cli/integration/helpers" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/gbytes" 12 . "github.com/onsi/gomega/gexec" 13 . "github.com/onsi/gomega/ghttp" 14 ) 15 16 var _ = Describe("create-shared-domain command", func() { 17 Context("Help", func() { 18 It("displays the help information", func() { 19 session := helpers.CF("create-shared-domain", "--help") 20 Eventually(session).Should(Say("NAME:\n")) 21 Eventually(session).Should(Say(regexp.QuoteMeta("create-shared-domain - Create a domain that can be used by all orgs (admin-only)"))) 22 Eventually(session).Should(Say("USAGE:\n")) 23 Eventually(session).Should(Say(regexp.QuoteMeta("cf create-shared-domain DOMAIN [--router-group ROUTER_GROUP | --internal]"))) 24 Eventually(session).Should(Say("OPTIONS:\n")) 25 Eventually(session).Should(Say(`--router-group\s+Routes for this domain will be configured only on the specified router group`)) 26 Eventually(session).Should(Say(`--internal\s+Applications that use internal routes communicate directly on the container network`)) 27 Eventually(session).Should(Say("SEE ALSO:\n")) 28 Eventually(session).Should(Say("create-domain, domains, router-groups")) 29 Eventually(session).Should(Exit(0)) 30 }) 31 }) 32 33 var ( 34 orgName string 35 spaceName string 36 domainName string 37 ) 38 39 BeforeEach(func() { 40 orgName = helpers.NewOrgName() 41 spaceName = helpers.NewSpaceName() 42 helpers.SetupCF(orgName, spaceName) 43 domainName = helpers.NewDomainName() 44 }) 45 46 When("user is logged in as a privileged user", func() { 47 48 var username string 49 50 BeforeEach(func() { 51 username, _ = helpers.GetCredentials() 52 }) 53 54 When("No optional flags are specified", func() { 55 When("domain name is valid", func() { 56 It("should create the shared domain", func() { 57 session := helpers.CF("create-shared-domain", domainName) 58 59 Eventually(session).Should(Say("Creating shared domain %s as %s...", domainName, username)) 60 Eventually(session).Should(Say("OK")) 61 Eventually(session).Should(Exit(0)) 62 63 session = helpers.CF("domains") 64 Eventually(session).Should(Say(`%s\s+shared`, domainName)) 65 Eventually(session).Should(Exit(0)) 66 }) 67 }) 68 69 When("domain name is invalid", func() { 70 BeforeEach(func() { 71 domainName = "invalid-domain-name%*$$#)*" + helpers.RandomName() 72 }) 73 74 It("should fail and return an error", func() { 75 session := helpers.CF("create-shared-domain", domainName) 76 77 Eventually(session).Should(Say("Creating shared domain %s as %s...", regexp.QuoteMeta(domainName), username)) 78 Eventually(session).Should(Say("FAILED")) 79 Eventually(session.Err).Should(Say(regexp.QuoteMeta("The domain is invalid: name can contain multiple subdomains, each having only alphanumeric characters and hyphens of up to 63 characters, see RFC 1035."))) 80 Eventually(session).Should(Exit(1)) 81 }) 82 }) 83 84 When("domain name is already taken", func() { 85 BeforeEach(func() { 86 session := helpers.CF("create-shared-domain", domainName) 87 Eventually(session).Should(Exit(0)) 88 }) 89 90 It("should fail and return an error", func() { 91 session := helpers.CF("create-shared-domain", domainName) 92 Eventually(session).Should(Say("Creating shared domain %s as %s...", domainName, username)) 93 Eventually(session).Should(Say("FAILED")) 94 Eventually(session.Err).Should(Say("The domain name is taken: %s", domainName)) 95 Eventually(session).Should(Exit(1)) 96 }) 97 }) 98 }) 99 100 When("the --internal flag is specified", func() { 101 When("the CC API version is less than the minimum version specified", func() { 102 var server *Server 103 104 BeforeEach(func() { 105 server = helpers.StartAndTargetMockServerWithAPIVersions(ccversion.MinSupportedV2ClientVersion, ccversion.MinSupportedV3ClientVersion) 106 }) 107 108 AfterEach(func() { 109 server.Close() 110 }) 111 112 It("fails with error message that the minimum version is not met", func() { 113 session := helpers.CF("create-shared-domain", domainName, "--internal", "-v") 114 Eventually(session).Should(Say("FAILED")) 115 Eventually(session.Err).Should(Say(`Option '--internal' requires CF API version 2\.115\.0 or higher\. Your target is %s`, ccversion.MinSupportedV2ClientVersion)) 116 Eventually(session).Should(Exit(1)) 117 }) 118 }) 119 120 When("the CC API version meets the minimum version requirement", func() { 121 BeforeEach(func() { 122 helpers.SkipIfVersionLessThan(ccversion.MinVersionInternalDomainV2) 123 }) 124 125 When("things work as expected", func() { 126 It("creates a domain with internal flag", func() { 127 session := helpers.CF("create-shared-domain", domainName, "--internal") 128 129 Eventually(session).Should(Say("Creating shared domain %s as %s...", domainName, username)) 130 Eventually(session).Should(Say("OK")) 131 Eventually(session).Should(Exit(0)) 132 133 session = helpers.CF("domains") 134 135 var sharedDomainResponse struct { 136 Resources []struct { 137 Entity struct { 138 Internal bool `json:"internal"` 139 Name string `json:"name"` 140 } 141 } 142 } 143 144 helpers.Curl(&sharedDomainResponse, "/v2/shared_domains?q=name:%s", domainName) 145 Expect(sharedDomainResponse.Resources).To(HaveLen(1)) 146 isInternal := sharedDomainResponse.Resources[0].Entity.Internal 147 Expect(isInternal).To(BeTrue()) 148 }) 149 }) 150 151 When("both --internal and --router-group flags are specified", func() { 152 It("returns an argument error", func() { 153 session := helpers.CF("create-shared-domain", domainName, "--router-group", "my-router-group", "--internal") 154 Eventually(session.Err).Should(Say("Incorrect Usage: The following arguments cannot be used together: --router-group, --internal")) 155 Eventually(session).Should(Say("FAILED")) 156 Eventually(session).Should(Exit(1)) 157 }) 158 }) 159 }) 160 }) 161 162 When("With the --router-group flag", func() { 163 var routerGroupName string 164 165 BeforeEach(func() { 166 helpers.SkipIfNoRoutingAPI() 167 }) 168 169 When("router-group exists", func() { 170 BeforeEach(func() { 171 routerGroupName = helpers.FindOrCreateTCPRouterGroup(GinkgoParallelNode()) 172 }) 173 174 It("should create a new shared domain", func() { 175 session := helpers.CF("create-shared-domain", domainName, "--router-group", routerGroupName) 176 177 Eventually(session).Should(Say("Creating shared domain %s as %s...", domainName, username)) 178 Eventually(session).Should(Say("OK")) 179 Eventually(session).Should(Exit(0)) 180 181 session = helpers.CF("domains") 182 Eventually(session).Should(Say(`%s\s+shared`, domainName)) 183 184 var sharedDomainResponse struct { 185 Resources []struct { 186 Entity struct { 187 RouterGroupGUID string `json:"router_group_guid"` 188 } 189 } 190 } 191 192 helpers.Curl(&sharedDomainResponse, "/v2/shared_domains?q=name:%s", domainName) 193 Expect(sharedDomainResponse.Resources).To(HaveLen(1)) 194 currentRouterGroupGUID := sharedDomainResponse.Resources[0].Entity.RouterGroupGUID 195 196 var routerGroupListResponse []struct{ GUID string } 197 198 helpers.Curl(&routerGroupListResponse, "/routing/v1/router_groups?name=%s", routerGroupName) 199 Expect(routerGroupListResponse).To(HaveLen(1)) 200 expectedRouterGroupGUID := routerGroupListResponse[0].GUID 201 Expect(currentRouterGroupGUID).Should(Equal(expectedRouterGroupGUID)) 202 }) 203 }) 204 205 When("router-group does not exist", func() { 206 BeforeEach(func() { 207 routerGroupName = "not-a-real-router-group" 208 session := helpers.CF("router-groups") 209 Consistently(session).ShouldNot(Say(routerGroupName)) 210 Eventually(session).Should(Exit(0)) 211 }) 212 213 It("should fail and return an error", func() { 214 session := helpers.CF("create-shared-domain", domainName, "--router-group", routerGroupName) 215 Eventually(session).Should(Say("FAILED")) 216 Eventually(session.Err).Should(Say("Router group not-a-real-router-group not found")) 217 Eventually(session).Should(Exit(1)) 218 }) 219 }) 220 }) 221 }) 222 223 When("user is not logged in as a privileged user", func() { 224 var ( 225 username string 226 password string 227 routerGroupName string 228 ) 229 230 BeforeEach(func() { 231 helpers.LoginCF() 232 username, password = helpers.CreateUser() 233 helpers.LogoutCF() 234 helpers.LoginAs(username, password) 235 }) 236 237 It("should not be able to create shared domain", func() { 238 session := helpers.CF("create-shared-domain", domainName) 239 Eventually(session).Should(Say(fmt.Sprintf("Creating shared domain %s as %s...", domainName, username))) 240 Eventually(session).Should(Say("FAILED")) 241 Eventually(session.Err).Should(Say("You are not authorized to perform the requested action")) 242 Eventually(session).Should(Exit(1)) 243 }) 244 245 When("with --internal flag", func() { 246 BeforeEach(func() { 247 helpers.SkipIfVersionLessThan(ccversion.MinVersionInternalDomainV2) 248 }) 249 250 It("should fail and return an unauthorized message", func() { 251 session := helpers.CF("create-shared-domain", domainName, "--internal") 252 Eventually(session).Should(Say("FAILED")) 253 Eventually(session.Err).Should(Say("You are not authorized to perform the requested action")) 254 Eventually(session).Should(Exit(1)) 255 }) 256 }) 257 258 When("with --router-group flag", func() { 259 BeforeEach(func() { 260 helpers.SkipIfNoRoutingAPI() 261 }) 262 263 When("router-group exists", func() { 264 BeforeEach(func() { 265 routerGroupName = helpers.FindOrCreateTCPRouterGroup(GinkgoParallelNode()) 266 }) 267 268 It("should fail and return an unauthorized message", func() { 269 session := helpers.CF("create-shared-domain", domainName, "--router-group", routerGroupName) 270 Eventually(session).Should(Say("FAILED")) 271 Eventually(session.Err).ShouldNot(Say("Error Code: 401")) 272 Eventually(session.Err).Should(Say("You are not authorized to perform the requested action")) 273 Eventually(session).Should(Exit(1)) 274 }) 275 }) 276 277 When("router-group does not exists", func() { 278 BeforeEach(func() { 279 routerGroupName = "invalid-router-group" 280 }) 281 282 It("should fail and return an unauthorized message", func() { 283 session := helpers.CF("create-shared-domain", domainName, "--router-group", routerGroupName) 284 Eventually(session).Should(Say("FAILED")) 285 Eventually(session.Err).ShouldNot(Say("Error Code: 401")) 286 Eventually(session.Err).Should(Say("You are not authorized to perform the requested action")) 287 Eventually(session).Should(Exit(1)) 288 }) 289 }) 290 }) 291 292 }) 293 })