Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Unified Diff: go/src/infra/appengine/test-results/frontend/upload_test.go

Issue 2240473004: test-results: package frontend: add upload handler (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Address estaab@ comments, set dependent CL Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: go/src/infra/appengine/test-results/frontend/upload_test.go
diff --git a/go/src/infra/appengine/test-results/frontend/upload_test.go b/go/src/infra/appengine/test-results/frontend/upload_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..6a8f3eb28c7b6afd1e6491cd6edbe9ac0a80f420
--- /dev/null
+++ b/go/src/infra/appengine/test-results/frontend/upload_test.go
@@ -0,0 +1,86 @@
+package frontend
+
+import (
+ "bytes"
+ "io"
+ "io/ioutil"
+ "mime/multipart"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "golang.org/x/net/context"
+
+ "github.com/luci/gae/impl/memory"
+ "github.com/luci/gae/service/datastore"
+ "github.com/luci/luci-go/server/router"
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+func withTestingContext(c *router.Context, next router.Handler) {
+ ctx := memory.Use(context.Background())
+ ds := datastore.Get(ctx)
+ testFileIdx, err := datastore.FindAndParseIndexYAML(filepath.Join("..", "model", "testdata"))
+ if err != nil {
+ panic(err)
+ }
+ ds.Testable().AddIndexes(testFileIdx...)
+ ds.Testable().CatchupIndexes()
+
+ c.Context = ctx
+ next(c)
+}
+
+func TestUpload(t *testing.T) {
+ t.Parallel()
+
+ r := router.New()
+ mw := router.NewMiddlewareChain(withTestingContext)
+ r.POST("/testfile/upload", mw.Extend(withParsedUploadForm), uploadHandler)
+ srv := httptest.NewServer(r)
+ client := &http.Client{}
+
+ Convey("upload", t, func() {
+ Convey("no matching aggregate file in datastore", func() {
+ var buf bytes.Buffer
+ multi := multipart.NewWriter(&buf)
+ // Form files.
+ f, err := os.Open(filepath.Join("testdata", "full_results_0.json"))
+ So(err, ShouldBeNil)
+ defer f.Close()
+ multiFile, err := multi.CreateFormFile("file", "full_results.json")
+ So(err, ShouldBeNil)
+ _, err = io.Copy(multiFile, f)
+ So(err, ShouldBeNil)
+ // Form fields.
+ fields := []struct {
+ key, val string
+ }{
+ {"master", "chromium.chromiumos"},
+ {"builder", "test-builder"},
+ {"test_type", "test-type"},
+ }
+ for _, field := range fields {
+ f, err := multi.CreateFormField(field.key)
+ So(err, ShouldBeNil)
+ _, err = f.Write([]byte(field.val))
+ So(err, ShouldBeNil)
+ }
+ multi.Close()
+
+ req, err := http.NewRequest("POST", srv.URL+"/testfile/upload", &buf)
+ So(err, ShouldBeNil)
+ req.Header.Set("Content-Type", multi.FormDataContentType())
+ resp, err := client.Do(req)
+ So(err, ShouldBeNil)
+ defer resp.Body.Close()
+ So(resp.StatusCode, ShouldEqual, http.StatusOK)
+
+ b, err := ioutil.ReadAll(resp.Body)
+ So(err, ShouldBeNil)
+ So(string(b), ShouldEqual, "OK")
+ })
+ })
+}
« no previous file with comments | « go/src/infra/appengine/test-results/frontend/upload.go ('k') | go/src/infra/appengine/test-results/model/test_file.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698