From 251c0eed8fa580f4b124f97360f6ef5cab021d38 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 3 Aug 2016 20:03:44 +0700 Subject: [PATCH] add an integration test downloading a small file with Chrome --- integrationtests/chrome_test.go | 23 +++++++++- .../integrationtests_suite_test.go | 45 +++++++++++++++++-- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/integrationtests/chrome_test.go b/integrationtests/chrome_test.go index c929d4df4..19b75fc20 100644 --- a/integrationtests/chrome_test.go +++ b/integrationtests/chrome_test.go @@ -4,6 +4,8 @@ import ( "fmt" "io" "net/http" + "os" + "path/filepath" "github.com/tebeka/selenium" @@ -114,9 +116,28 @@ var _ = Describe("Chrome tests", func() { } } return nil - }).ShouldNot(HaveOccurred()) + }, 5).ShouldNot(HaveOccurred()) close(done) }, 10) + + It("downloads a small file", func(done Done) { + var err error + err = wd.Get("https://quic.clemente.io/data") + Expect(err).NotTo(HaveOccurred()) + var file *os.File + Eventually(func() error { + file, err = os.Open(filepath.Join(downloadDir, "data")) + return err + }, 30, 0.1).ShouldNot(HaveOccurred()) + fi, err := file.Stat() + Expect(err).ToNot(HaveOccurred()) + Eventually(fi.Size(), 10, 0.1).Should(Equal(int64(dataLen))) + contents := make([]byte, dataLen) + _, err = file.Read(contents) + Expect(err).ToNot(HaveOccurred()) + Expect(contents).To(Equal(data)) + close(done) + }, 60) }) } }) diff --git a/integrationtests/integrationtests_suite_test.go b/integrationtests/integrationtests_suite_test.go index 6c00a462b..dd179fe79 100644 --- a/integrationtests/integrationtests_suite_test.go +++ b/integrationtests/integrationtests_suite_test.go @@ -4,9 +4,12 @@ import ( "crypto/rand" "fmt" "io" + "io/ioutil" "net" "net/http" + "os" "os/exec" + "path/filepath" "strconv" "time" @@ -28,9 +31,10 @@ const ( ) var ( - server *h2quic.Server - data []byte - port string + server *h2quic.Server + data []byte + port string + downloadDir string docker *gexec.Session ) @@ -43,6 +47,7 @@ func TestIntegration(t *testing.T) { var _ = BeforeSuite(func() { setupHTTPHandlers() setupQuicServer() + setupDownloadDir() setupSelenium() }) @@ -51,8 +56,13 @@ var _ = AfterSuite(func() { Expect(err).NotTo(HaveOccurred()) stopSelenium() + removeDownloadDir() }, 5) +var _ = AfterEach(func() { + clearDownloadDir() +}) + func setupHTTPHandlers() { defer GinkgoRecover() data = make([]byte, dataLen) @@ -111,6 +121,7 @@ func setupSelenium() { "-i", "--rm", "-p=4444:4444", + fmt.Sprintf("-v=%s/:/home/seluser/Downloads/", downloadDir), "lclemente/standalone-chrome:latest", ) docker, err = gexec.Start(dockerCmd, GinkgoWriter, GinkgoWriter) @@ -161,3 +172,31 @@ func GetLocalIP() string { } panic("no addr") } + +// create a temporary directory for Chrome downloads +// Docker will mount the Chrome download directory here +func setupDownloadDir() { + var err error + downloadDir, err = ioutil.TempDir("/tmp", "quicgodownloads") + Expect(err).ToNot(HaveOccurred()) +} + +// delete all files in the download directory +func clearDownloadDir() { + d, err := os.Open(downloadDir) + defer d.Close() + Expect(err).ToNot(HaveOccurred()) + filenames, err := d.Readdirnames(-1) + Expect(err).ToNot(HaveOccurred()) + for _, filename := range filenames { + err := os.Remove(filepath.Join(downloadDir, filename)) + Expect(err).ToNot(HaveOccurred()) + } +} + +// delete the download directory +// must be empty when calling this function +func removeDownloadDir() { + err := os.Remove(downloadDir) + Expect(err).ToNot(HaveOccurred()) +}