diff options
author | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2018-11-20 14:32:50 -0800 |
---|---|---|
committer | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2018-11-21 11:36:41 -0800 |
commit | f54fee24c32465478e2c6fc4cd6fc6bfb44fe096 (patch) | |
tree | ebe95c5f51972d2cacb8c304abace2423b7952db /src/k8splugin/api | |
parent | 3f780f7973081903f1ab6ea01a855fb6c5512a48 (diff) |
Add upload backend implementation
Upload is a seperate API where it takes
a binary stream and stores it. The api supports
tar.gz file format only.
P2: Check if ID is valid before trying upload
Add test with an invalid ID
Issue-ID: MULTICLOUD-393
Change-Id: Id636a95823a046e1795d3be72d0214e953a8c5fc
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
Diffstat (limited to 'src/k8splugin/api')
-rw-r--r-- | src/k8splugin/api/api.go | 2 | ||||
-rw-r--r-- | src/k8splugin/api/defhandler.go | 25 | ||||
-rw-r--r-- | src/k8splugin/api/defhandler_test.go | 65 |
3 files changed, 89 insertions, 3 deletions
diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go index 571a9576..530537dc 100644 --- a/src/k8splugin/api/api.go +++ b/src/k8splugin/api/api.go @@ -109,7 +109,7 @@ func NewRouter(kubeconfig string) *mux.Router { resRouter := router.PathPrefix("/v1/rb").Subrouter() rbdef := rbDefinitionHandler{client: rb.NewDefinitionClient()} resRouter.HandleFunc("/definition", rbdef.createHandler).Methods("POST") - resRouter.HandleFunc("/definition/{rbdID}/upload", rbdef.uploadHandler).Methods("POST") + resRouter.HandleFunc("/definition/{rbdID}/content", rbdef.uploadHandler).Methods("POST") resRouter.HandleFunc("/definition", rbdef.listHandler).Methods("GET") resRouter.HandleFunc("/definition/{rbdID}", rbdef.getHandler).Methods("GET") resRouter.HandleFunc("/definition/{rbdID}", rbdef.deleteHandler).Methods("DELETE") diff --git a/src/k8splugin/api/defhandler.go b/src/k8splugin/api/defhandler.go index c8c03496..222baaee 100644 --- a/src/k8splugin/api/defhandler.go +++ b/src/k8splugin/api/defhandler.go @@ -18,9 +18,9 @@ package api import ( "encoding/json" - "net/http" - + "io/ioutil" "k8splugin/rb" + "net/http" "github.com/gorilla/mux" ) @@ -72,6 +72,27 @@ func (h rbDefinitionHandler) createHandler(w http.ResponseWriter, r *http.Reques // uploadHandler handles upload of the bundle tar file into the database // Note: This will be implemented in a different patch func (h rbDefinitionHandler) uploadHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + uuid := vars["rbdID"] + + if r.Body == nil { + http.Error(w, "Empty Body", http.StatusBadRequest) + return + } + + inpBytes, err := ioutil.ReadAll(r.Body) + if err != nil { + http.Error(w, "Unable to read body", http.StatusBadRequest) + return + } + + err = h.client.Upload(uuid, inpBytes) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) } // listHandler handles GET (list) operations on the endpoint diff --git a/src/k8splugin/api/defhandler_test.go b/src/k8splugin/api/defhandler_test.go index b83f0b7a..9739ab12 100644 --- a/src/k8splugin/api/defhandler_test.go +++ b/src/k8splugin/api/defhandler_test.go @@ -68,6 +68,10 @@ func (m *mockRBDefinition) Delete(id string) error { return m.Err } +func (m *mockRBDefinition) Upload(id string, inp []byte) error { + return m.Err +} + func TestRBDefCreateHandler(t *testing.T) { testCases := []struct { label string @@ -334,3 +338,64 @@ func TestRBDefDeleteHandler(t *testing.T) { }) } } + +func TestRBDefUploadHandler(t *testing.T) { + + testCases := []struct { + label string + inpUUID string + body io.Reader + expectedCode int + rbDefClient *mockRBDefinition + }{ + { + label: "Upload Bundle Definition Content", + expectedCode: http.StatusOK, + inpUUID: "123e4567-e89b-12d3-a456-426655441111", + body: bytes.NewBuffer([]byte{ + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xff, 0xf2, 0x48, 0xcd, + }), + rbDefClient: &mockRBDefinition{}, + }, + { + label: "Upload Invalid Bundle Definition Content", + expectedCode: http.StatusInternalServerError, + inpUUID: "123e4567-e89b-12d3-a456-426655440000", + body: bytes.NewBuffer([]byte{ + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xff, 0xf2, 0x48, 0xcd, + }), + rbDefClient: &mockRBDefinition{ + Err: pkgerrors.New("Internal Error"), + }, + }, + { + label: "Upload Empty Body Content", + expectedCode: http.StatusBadRequest, + inpUUID: "123e4567-e89b-12d3-a456-426655440000", + rbDefClient: &mockRBDefinition{}, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + vh := rbDefinitionHandler{client: testCase.rbDefClient} + req, err := http.NewRequest("POST", + "/v1/resource/definition/"+testCase.inpUUID+"/content", testCase.body) + + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + hr := http.HandlerFunc(vh.uploadHandler) + + hr.ServeHTTP(rr, req) + //Check returned code + if rr.Code != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code) + } + }) + } +} |