From c8275d5d7995baaffd24e2aa497beb2f84e6a8f9 Mon Sep 17 00:00:00 2001 From: Lyle Franklin Date: Tue, 4 Dec 2018 08:41:41 -0800 Subject: [PATCH] Reduce duplication on upload-product test [#160181845] pivotal-cf/om #240: Intermittent `POST .../api/v0/available_products: EOF` when uploading tile --- acceptance/upload_product_test.go | 89 ++++++++++--------------------- 1 file changed, 29 insertions(+), 60 deletions(-) diff --git a/acceptance/upload_product_test.go b/acceptance/upload_product_test.go index db72730ef..73f1009f5 100644 --- a/acceptance/upload_product_test.go +++ b/acceptance/upload_product_test.go @@ -21,9 +21,10 @@ import ( var _ = Describe("upload-product command", func() { var ( - product string - productFile *os.File - server *httptest.Server + product string + productFile *os.File + server *httptest.Server + uploadHandler func(http.ResponseWriter, *http.Request) ) BeforeEach(func() { @@ -52,7 +53,15 @@ name: some-product`) err = zipper.Close() Expect(err).NotTo(HaveOccurred()) - server = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + uploadHandler = func(w http.ResponseWriter, req *http.Request) { + err := req.ParseMultipartForm(100) + Expect(err).NotTo(HaveOccurred()) + + product = req.MultipartForm.File["product[file]"][0].Filename + w.Write([]byte("{}")) + } + + server = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { var responseString string w.Header().Set("Content-Type", "application/json") @@ -75,11 +84,8 @@ name: some-product`) return } - err := req.ParseMultipartForm(100) - Expect(err).NotTo(HaveOccurred()) - - product = req.MultipartForm.File["product[file]"][0].Filename - responseString = "{}" + uploadHandler(w, req) + return } default: out, err := httputil.DumpRequest(req, true) @@ -91,10 +97,6 @@ name: some-product`) })) }) - JustBeforeEach(func() { - server.StartTLS() - }) - AfterEach(func() { os.Remove(productFile.Name()) server.Close() @@ -181,58 +183,25 @@ name: some-product`) Context("when the server returns EOF during upload", func() { BeforeEach(func() { uploadCallCount := 0 - server = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - var responseString string - w.Header().Set("Content-Type", "application/json") + uploadHandler = func(w http.ResponseWriter, req *http.Request) { + uploadCallCount++ - switch req.URL.Path { - case "/uaa/oauth/token": - responseString = `{ - "access_token": "some-opsman-token", - "token_type": "bearer", - "expires_in": 3600 - }` - case "/api/v0/diagnostic_report": - responseString = "{}" - case "/api/v0/available_products": - if req.Method == "GET" { - responseString = "[]" - } else if req.Method == "POST" { - auth := req.Header.Get("Authorization") - if auth != "Bearer some-opsman-token" { - w.WriteHeader(http.StatusUnauthorized) - return - } - - uploadCallCount++ - - if uploadCallCount == 1 { - server.CloseClientConnections() - return - } else { - err := req.ParseMultipartForm(100) - if err != nil { - http.Error(w, fmt.Sprintf("failed to parse request body: %s", err), http.StatusInternalServerError) - return - } - - product = req.MultipartForm.File["product[file]"][0].Filename - responseString = "{}" - } + if uploadCallCount == 1 { + server.CloseClientConnections() + return + } else { + err := req.ParseMultipartForm(100) + if err != nil { + http.Error(w, fmt.Sprintf("failed to parse request body: %s", err), http.StatusInternalServerError) + return } - default: - out, err := httputil.DumpRequest(req, true) - Expect(err).NotTo(HaveOccurred()) - Fail(fmt.Sprintf("unexpected request: %s", out)) - } - w.Write([]byte(responseString)) - })) + product = req.MultipartForm.File["product[file]"][0].Filename + w.Write([]byte("{}")) + } + } }) - // TODO: remove server duplication - // TODO: test that if 3 retries fail then om returns EOF error - It("retries the upload", func() { command := exec.Command(pathToMain, "--target", server.URL,