From d6683833ebf1fadd98f3ee9618a4c7931839116d Mon Sep 17 00:00:00 2001 From: Alex DeBrie Date: Fri, 2 Mar 2018 01:48:36 -0600 Subject: [PATCH] Fix for UpdateFunction (#382) * Fix for UpdateFunction * Fix function service mock --- function/service.go | 2 +- httpapi/httpapi.go | 6 +++--- libkv/function.go | 12 ++++++------ libkv/function_test.go | 10 ++++------ mock/function.go | 4 ++-- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/function/service.go b/function/service.go index 04f0191..ac59125 100644 --- a/function/service.go +++ b/function/service.go @@ -3,7 +3,7 @@ package function // Service represents service for managing functions. type Service interface { RegisterFunction(fn *Function) (*Function, error) - UpdateFunction(space string, fn *Function) (*Function, error) + UpdateFunction(fn *Function) (*Function, error) GetFunction(space string, id ID) (*Function, error) GetFunctions(space string) (Functions, error) DeleteFunction(space string, id ID) error diff --git a/httpapi/httpapi.go b/httpapi/httpapi.go index 6cafb5d..6e772c6 100644 --- a/httpapi/httpapi.go +++ b/httpapi/httpapi.go @@ -129,9 +129,9 @@ func (h HTTPAPI) updateFunction(w http.ResponseWriter, r *http.Request, params h return } - space := params.ByName("space") + fn.Space = params.ByName("space") fn.ID = function.ID(params.ByName("id")) - output, err := h.Functions.UpdateFunction(space, fn) + output, err := h.Functions.UpdateFunction(fn) if err != nil { if _, ok := err.(*function.ErrFunctionValidation); ok { w.WriteHeader(http.StatusBadRequest) @@ -146,7 +146,7 @@ func (h HTTPAPI) updateFunction(w http.ResponseWriter, r *http.Request, params h encoder.Encode(output) } - metricFunctionUpdateRequests.WithLabelValues(space).Inc() + metricFunctionUpdateRequests.WithLabelValues(fn.Space).Inc() } func (h HTTPAPI) deleteFunction(w http.ResponseWriter, r *http.Request, params httprouter.Params) { diff --git a/libkv/function.go b/libkv/function.go index 4f24ae3..5b53b8e 100644 --- a/libkv/function.go +++ b/libkv/function.go @@ -53,14 +53,14 @@ func (service Service) RegisterFunction(fn *function.Function) (*function.Functi } // UpdateFunction updates function configuration. -func (service Service) UpdateFunction(space string, fn *function.Function) (*function.Function, error) { - _, err := service.FunctionStore.Get(FunctionKey{space, fn.ID}.String(), &store.ReadOptions{Consistent: true}) - if err != nil { - return nil, &function.ErrFunctionNotFound{ID: fn.ID} +func (service Service) UpdateFunction(fn *function.Function) (*function.Function, error) { + if err := service.validateFunction(fn); err != nil { + return nil, err } - if err = service.validateFunction(fn); err != nil { - return nil, err + _, err := service.FunctionStore.Get(FunctionKey{fn.Space, fn.ID}.String(), &store.ReadOptions{Consistent: true}) + if err != nil { + return nil, &function.ErrFunctionNotFound{ID: fn.ID} } byt, err := json.Marshal(fn) diff --git a/libkv/function_test.go b/libkv/function_test.go index 99df7dc..e8d2015 100644 --- a/libkv/function_test.go +++ b/libkv/function_test.go @@ -93,7 +93,7 @@ func TestUpdateFunction(t *testing.T) { ID: "testid", Space: "default", Provider: &function.Provider{Type: function.HTTPEndpoint, URL: "http://example1.com"}} - _, err := service.UpdateFunction("default", fn) + _, err := service.UpdateFunction(fn) assert.Nil(t, err) } @@ -103,12 +103,10 @@ func TestUpdateFunction_ValidationError(t *testing.T) { defer ctrl.Finish() db := mock.NewMockStore(ctrl) - returned := []byte(`{"space":"default","functionId":"testid","provider":{"type":"http","url":"http://example.com"}}`) - db.EXPECT().Get("default/testid", gomock.Any()).Return(&store.KVPair{Value: returned}, nil) service := &Service{FunctionStore: db, Log: zap.NewNop()} fn := &function.Function{ID: "testid", Space: "default", Provider: &function.Provider{Type: function.HTTPEndpoint}} - _, err := service.UpdateFunction("default", fn) + _, err := service.UpdateFunction(fn) assert.Equal(t, err, &function.ErrFunctionValidation{Message: "Missing required fields for HTTP endpoint."}) } @@ -125,7 +123,7 @@ func TestUpdateFunction_NotFoundError(t *testing.T) { ID: "testid", Space: "default", Provider: &function.Provider{Type: function.HTTPEndpoint, URL: "http://example.com"}} - _, err := service.UpdateFunction("default", fn) + _, err := service.UpdateFunction(fn) assert.Equal(t, err, &function.ErrFunctionNotFound{ID: "testid"}) } @@ -147,7 +145,7 @@ func TestUpdateFunction_PutError(t *testing.T) { ID: "testid", Space: "default", Provider: &function.Provider{Type: function.HTTPEndpoint, URL: "http://example1.com"}} - _, err := service.UpdateFunction("default", fn) + _, err := service.UpdateFunction(fn) assert.EqualError(t, err, "KV put error") } diff --git a/mock/function.go b/mock/function.go index f1e206d..616a9f4 100644 --- a/mock/function.go +++ b/mock/function.go @@ -85,8 +85,8 @@ func (mr *MockFunctionServiceMockRecorder) RegisterFunction(arg0 interface{}) *g } // UpdateFunction mocks base method -func (m *MockFunctionService) UpdateFunction(arg0 string, arg1 *function.Function) (*function.Function, error) { - ret := m.ctrl.Call(m, "UpdateFunction", arg0, arg1) +func (m *MockFunctionService) UpdateFunction(arg1 *function.Function) (*function.Function, error) { + ret := m.ctrl.Call(m, "UpdateFunction", arg1) ret0, _ := ret[0].(*function.Function) ret1, _ := ret[1].(error) return ret0, ret1