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