github.com/arunkumar7540/cli@v6.45.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/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 admin", func() {
    47  		When("No optional flags are specified", func() {
    48  			When("domain name is valid", func() {
    49  				It("should create the shared domain", func() {
    50  					session := helpers.CF("create-shared-domain", domainName)
    51  
    52  					Eventually(session).Should(Say("Creating shared domain %s as admin...", domainName))
    53  					Eventually(session).Should(Say("OK"))
    54  					Eventually(session).Should(Exit(0))
    55  
    56  					session = helpers.CF("domains")
    57  					Eventually(session).Should(Say(`%s\s+shared`, domainName))
    58  					Eventually(session).Should(Exit(0))
    59  				})
    60  			})
    61  
    62  			When("domain name is invalid", func() {
    63  				BeforeEach(func() {
    64  					domainName = "invalid-domain-name%*$$#)*" + helpers.RandomName()
    65  				})
    66  
    67  				It("should fail and return an error", func() {
    68  					session := helpers.CF("create-shared-domain", domainName)
    69  
    70  					Eventually(session).Should(Say("Creating shared domain %s as admin...", regexp.QuoteMeta(domainName)))
    71  					Eventually(session).Should(Say("FAILED"))
    72  					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.")))
    73  					Eventually(session).Should(Exit(1))
    74  				})
    75  			})
    76  
    77  			When("domain name is already taken", func() {
    78  				BeforeEach(func() {
    79  					session := helpers.CF("create-shared-domain", domainName)
    80  					Eventually(session).Should(Exit(0))
    81  				})
    82  
    83  				It("should fail and return an error", func() {
    84  					session := helpers.CF("create-shared-domain", domainName)
    85  					Eventually(session).Should(Say("Creating shared domain %s as admin...", domainName))
    86  					Eventually(session).Should(Say("FAILED"))
    87  					Eventually(session.Err).Should(Say("The domain name is taken: %s", domainName))
    88  					Eventually(session).Should(Exit(1))
    89  				})
    90  			})
    91  		})
    92  
    93  		When("the --internal flag is specified", func() {
    94  			When("the CC API version is less than the minimum version specified", func() {
    95  				var server *Server
    96  
    97  				BeforeEach(func() {
    98  					server = helpers.StartAndTargetServerWithAPIVersions(ccversion.MinSupportedV2ClientVersion, ccversion.MinSupportedV3ClientVersion)
    99  				})
   100  
   101  				AfterEach(func() {
   102  					server.Close()
   103  				})
   104  
   105  				It("fails with error message that the minimum version is not met", func() {
   106  					session := helpers.CF("create-shared-domain", domainName, "--internal", "-v")
   107  					Eventually(session).Should(Say("FAILED"))
   108  					Eventually(session.Err).Should(Say(`Option '--internal' requires CF API version 2\.115\.0 or higher\. Your target is %s`, ccversion.MinSupportedV2ClientVersion))
   109  					Eventually(session).Should(Exit(1))
   110  				})
   111  			})
   112  
   113  			When("the CC API version meets the minimum version requirement", func() {
   114  				BeforeEach(func() {
   115  					helpers.SkipIfVersionLessThan(ccversion.MinVersionInternalDomainV2)
   116  				})
   117  
   118  				When("things work as expected", func() {
   119  					It("creates a domain with internal flag", func() {
   120  						session := helpers.CF("create-shared-domain", domainName, "--internal")
   121  
   122  						Eventually(session).Should(Say("Creating shared domain %s as admin...", domainName))
   123  						Eventually(session).Should(Say("OK"))
   124  						Eventually(session).Should(Exit(0))
   125  
   126  						session = helpers.CF("domains")
   127  
   128  						var sharedDomainResponse struct {
   129  							Resources []struct {
   130  								Entity struct {
   131  									Internal bool   `json:"internal"`
   132  									Name     string `json:"name"`
   133  								}
   134  							}
   135  						}
   136  
   137  						helpers.Curl(&sharedDomainResponse, "/v2/shared_domains?q=name:%s", domainName)
   138  						Expect(sharedDomainResponse.Resources).To(HaveLen(1))
   139  						isInternal := sharedDomainResponse.Resources[0].Entity.Internal
   140  						Expect(isInternal).To(BeTrue())
   141  					})
   142  				})
   143  
   144  				When("both --internal and --router-group flags are specified", func() {
   145  					It("returns an argument error", func() {
   146  						session := helpers.CF("create-shared-domain", domainName, "--router-group", "my-router-group", "--internal")
   147  						Eventually(session.Err).Should(Say("Incorrect Usage: The following arguments cannot be used together: --router-group, --internal"))
   148  						Eventually(session).Should(Say("FAILED"))
   149  						Eventually(session).Should(Exit(1))
   150  					})
   151  				})
   152  			})
   153  		})
   154  
   155  		When("With the --router-group flag", func() {
   156  			var routerGroupName string
   157  
   158  			BeforeEach(func() {
   159  				helpers.SkipIfNoRoutingAPI()
   160  			})
   161  
   162  			When("router-group exists", func() {
   163  				BeforeEach(func() {
   164  					routerGroupName = helpers.FindOrCreateTCPRouterGroup(GinkgoParallelNode())
   165  				})
   166  
   167  				It("should create a new shared domain", func() {
   168  					session := helpers.CF("create-shared-domain", domainName, "--router-group", routerGroupName)
   169  
   170  					Eventually(session).Should(Say("Creating shared domain %s as admin...", domainName))
   171  					Eventually(session).Should(Say("OK"))
   172  					Eventually(session).Should(Exit(0))
   173  
   174  					session = helpers.CF("domains")
   175  					Eventually(session).Should(Say(`%s\s+shared`, domainName))
   176  
   177  					var sharedDomainResponse struct {
   178  						Resources []struct {
   179  							Entity struct {
   180  								RouterGroupGUID string `json:"router_group_guid"`
   181  							}
   182  						}
   183  					}
   184  
   185  					helpers.Curl(&sharedDomainResponse, "/v2/shared_domains?q=name:%s", domainName)
   186  					Expect(sharedDomainResponse.Resources).To(HaveLen(1))
   187  					currentRouterGroupGUID := sharedDomainResponse.Resources[0].Entity.RouterGroupGUID
   188  
   189  					var routerGroupListResponse []struct{ GUID string }
   190  
   191  					helpers.Curl(&routerGroupListResponse, "/routing/v1/router_groups?name=%s", routerGroupName)
   192  					Expect(routerGroupListResponse).To(HaveLen(1))
   193  					expectedRouterGroupGUID := routerGroupListResponse[0].GUID
   194  					Expect(currentRouterGroupGUID).Should(Equal(expectedRouterGroupGUID))
   195  				})
   196  			})
   197  
   198  			When("router-group does not exist", func() {
   199  				BeforeEach(func() {
   200  					routerGroupName = "not-a-real-router-group"
   201  					session := helpers.CF("router-groups")
   202  					Consistently(session).ShouldNot(Say(routerGroupName))
   203  					Eventually(session).Should(Exit(0))
   204  				})
   205  
   206  				It("should fail and return an error", func() {
   207  					session := helpers.CF("create-shared-domain", domainName, "--router-group", routerGroupName)
   208  					Eventually(session).Should(Say("FAILED"))
   209  					Eventually(session.Err).Should(Say("Router group not-a-real-router-group not found"))
   210  					Eventually(session).Should(Exit(1))
   211  				})
   212  			})
   213  		})
   214  	})
   215  
   216  	When("user is not logged in as admin", func() {
   217  		var (
   218  			username        string
   219  			password        string
   220  			routerGroupName string
   221  		)
   222  
   223  		BeforeEach(func() {
   224  			helpers.LoginCF()
   225  			username, password = helpers.CreateUser()
   226  			helpers.LoginAs(username, password)
   227  		})
   228  
   229  		It("should not be able to create shared domain", func() {
   230  			session := helpers.CF("create-shared-domain", domainName)
   231  			Eventually(session).Should(Say(fmt.Sprintf("Creating shared domain %s as %s...", domainName, username)))
   232  			Eventually(session).Should(Say("FAILED"))
   233  			Eventually(session.Err).Should(Say("You are not authorized to perform the requested action"))
   234  			Eventually(session).Should(Exit(1))
   235  		})
   236  
   237  		When("with --internal flag", func() {
   238  			BeforeEach(func() {
   239  				helpers.SkipIfVersionLessThan(ccversion.MinVersionInternalDomainV2)
   240  			})
   241  
   242  			It("should fail and return an unauthorized message", func() {
   243  				session := helpers.CF("create-shared-domain", domainName, "--internal")
   244  				Eventually(session).Should(Say("FAILED"))
   245  				Eventually(session.Err).Should(Say("You are not authorized to perform the requested action"))
   246  				Eventually(session).Should(Exit(1))
   247  			})
   248  		})
   249  
   250  		When("with --router-group flag", func() {
   251  			BeforeEach(func() {
   252  				helpers.SkipIfNoRoutingAPI()
   253  			})
   254  
   255  			When("router-group exists", func() {
   256  				BeforeEach(func() {
   257  					routerGroupName = helpers.FindOrCreateTCPRouterGroup(GinkgoParallelNode())
   258  				})
   259  
   260  				It("should fail and return an unauthorized message", func() {
   261  					session := helpers.CF("create-shared-domain", domainName, "--router-group", routerGroupName)
   262  					Eventually(session).Should(Say("FAILED"))
   263  					Eventually(session.Err).ShouldNot(Say("Error Code: 401"))
   264  					Eventually(session.Err).Should(Say("You are not authorized to perform the requested action"))
   265  					Eventually(session).Should(Exit(1))
   266  				})
   267  			})
   268  
   269  			When("router-group does not exists", func() {
   270  				BeforeEach(func() {
   271  					routerGroupName = "invalid-router-group"
   272  				})
   273  
   274  				It("should fail and return an unauthorized message", func() {
   275  					session := helpers.CF("create-shared-domain", domainName, "--router-group", routerGroupName)
   276  					Eventually(session).Should(Say("FAILED"))
   277  					Eventually(session.Err).ShouldNot(Say("Error Code: 401"))
   278  					Eventually(session.Err).Should(Say("You are not authorized to perform the requested action"))
   279  					Eventually(session).Should(Exit(1))
   280  				})
   281  			})
   282  		})
   283  
   284  	})
   285  })