github.com/deiscc/workflow-e2e@v0.0.0-20181208071258-117299af888f/tests/domains_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"net/http"
     7  	"strconv"
     8  
     9  	"github.com/deiscc/workflow-e2e/tests/cmd"
    10  	"github.com/deiscc/workflow-e2e/tests/cmd/apps"
    11  	"github.com/deiscc/workflow-e2e/tests/cmd/auth"
    12  	"github.com/deiscc/workflow-e2e/tests/cmd/builds"
    13  	"github.com/deiscc/workflow-e2e/tests/cmd/domains"
    14  	"github.com/deiscc/workflow-e2e/tests/model"
    15  	"github.com/deiscc/workflow-e2e/tests/settings"
    16  	"github.com/deiscc/workflow-e2e/tests/util"
    17  
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	. "github.com/onsi/gomega/gbytes"
    21  	. "github.com/onsi/gomega/gexec"
    22  )
    23  
    24  var _ = Describe("deis domains", func() {
    25  
    26  	Context("with an existing user", func() {
    27  
    28  		var user model.User
    29  
    30  		BeforeEach(func() {
    31  			user = auth.RegisterAndLogin()
    32  		})
    33  
    34  		AfterEach(func() {
    35  			auth.Cancel(user)
    36  		})
    37  
    38  		Context("who owns an existing app", func() {
    39  
    40  			var app model.App
    41  
    42  			BeforeEach(func() {
    43  				app = apps.Create(user, "--no-remote")
    44  			})
    45  
    46  			AfterEach(func() {
    47  				apps.Destroy(user, app)
    48  			})
    49  
    50  			Specify("that user can list that app's domains", func() {
    51  				sess, err := cmd.Start("deis domains:list --app=%s", &user, app.Name)
    52  				Eventually(sess).Should(Say("=== %s Domains", app.Name))
    53  				Eventually(sess).Should(Say("%s", app.Name))
    54  				Expect(err).NotTo(HaveOccurred())
    55  				Eventually(sess).Should(Exit(0))
    56  			})
    57  
    58  			Specify("that user can add domains to that app", func() {
    59  				domain := getRandDomain()
    60  				sess, err := cmd.Start("deis domains:add %s --app=%s", &user, domain, app.Name)
    61  				Eventually(sess).Should(Say("Adding %s to %s...", domain, app.Name))
    62  				Eventually(sess, settings.MaxEventuallyTimeout).Should(Say("done"))
    63  				Expect(err).NotTo(HaveOccurred())
    64  				Eventually(sess).Should(Exit(0))
    65  			})
    66  
    67  			Specify("that user cannot remove a non-existent domain from that app", func() {
    68  				sess, err := cmd.Start("deis domains:remove --app=%s %s", &user, app.Name, "non.existent.domain")
    69  				Eventually(sess.Err, settings.MaxEventuallyTimeout).Should(Say(util.PrependError(domains.ErrNoDomainMatch)))
    70  				Expect(err).NotTo(HaveOccurred())
    71  				Eventually(sess).Should(Exit(1))
    72  			})
    73  
    74  			Context("with a domain added to it", func() {
    75  
    76  				var domain string
    77  
    78  				BeforeEach(func() {
    79  					domain = getRandDomain()
    80  					domains.Add(user, app, domain)
    81  				})
    82  
    83  				Specify("that user can remove that domain from that app", func() {
    84  					sess, err := cmd.Start("deis domains:remove %s --app=%s", &user, domain, app.Name)
    85  					Eventually(sess).Should(Say("Removing %s from %s...", domain, app.Name))
    86  					Eventually(sess, settings.MaxEventuallyTimeout).Should(Say("done"))
    87  					Expect(err).NotTo(HaveOccurred())
    88  					Eventually(sess).Should(Exit(0))
    89  				})
    90  
    91  			})
    92  
    93  		})
    94  
    95  		Context("who owns an existing app that has already been deployed", func() {
    96  
    97  			var app model.App
    98  
    99  			BeforeEach(func() {
   100  				app = apps.Create(user, "--no-remote")
   101  				builds.Create(user, app)
   102  			})
   103  
   104  			AfterEach(func() {
   105  				apps.Destroy(user, app)
   106  			})
   107  
   108  			Context("with a domain added to it", func() {
   109  
   110  				cmdRetryTimeout := 60
   111  
   112  				var domain string
   113  
   114  				BeforeEach(func() {
   115  					domain = getRandDomain()
   116  					domains.Add(user, app, domain)
   117  				})
   118  
   119  				AfterEach(func() {
   120  					domains.Remove(user, app, domain)
   121  					// App can no longer be accessed at the previously associated domain
   122  					curlCmd := model.Cmd{CommandLineString: fmt.Sprintf(`curl -sL -H "Host: %s" -w "%%{http_code}\\n" "%s" -o /dev/null`, domain, app.URL)}
   123  					Eventually(cmd.Retry(curlCmd, strconv.Itoa(http.StatusNotFound), cmdRetryTimeout)).Should(BeTrue())
   124  				})
   125  
   126  				Specify("that app can be accessed at its usual address", func() {
   127  					curlCmd := model.Cmd{CommandLineString: fmt.Sprintf(`curl -sL -w "%%{http_code}\\n" "%s" -o /dev/null`, app.URL)}
   128  					Eventually(cmd.Retry(curlCmd, strconv.Itoa(http.StatusOK), cmdRetryTimeout)).Should(BeTrue())
   129  				})
   130  
   131  				Specify("that app can be accessed at the associated domain", func() {
   132  					curlCmd := model.Cmd{CommandLineString: fmt.Sprintf(`curl -sL -H "Host: %s" -w "%%{http_code}\\n" "%s" -o /dev/null`, domain, app.URL)}
   133  					Eventually(cmd.Retry(curlCmd, strconv.Itoa(http.StatusOK), cmdRetryTimeout)).Should(BeTrue())
   134  				})
   135  
   136  			})
   137  
   138  		})
   139  
   140  	})
   141  
   142  })
   143  
   144  func getRandDomain() string {
   145  	return fmt.Sprintf("my-custom-%d.domain.com", rand.Intn(999999999))
   146  }