Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benraskin92 committed Oct 7, 2019
1 parent 9d8415a commit 84d86ad
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 42 deletions.
5 changes: 4 additions & 1 deletion src/query/api/v1/handler/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ func TestNewServiceOptions(t *testing.T) {
for k, v := range test.headers {
h.Add(k, v)
}
opts := NewServiceOptions(test.service, h, test.aggOpts)
svcDefaults := ServiceNameAndDefaults{
ServiceName: test.service,
}
opts := NewServiceOptions(svcDefaults, h, test.aggOpts)
assert.Equal(t, test.exp, opts)
}
}
Expand Down
27 changes: 21 additions & 6 deletions src/query/api/v1/handler/placement/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ func TestPlacementAddHandler_Force(t *testing.T) {
}
require.NotNil(t, req)

svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}
mockPlacementService.EXPECT().AddInstances(gomock.Any()).Return(placement.NewPlacement(), nil, errors.New("no new instances found in the valid zone"))
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
Expand All @@ -83,7 +86,7 @@ func TestPlacementAddHandler_Force(t *testing.T) {
require.NotNil(t, req)

mockPlacementService.EXPECT().AddInstances(gomock.Not(nil)).Return(placement.NewPlacement(), nil, nil)
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp = w.Result()
body, _ = ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -116,7 +119,11 @@ func TestPlacementAddHandler_SafeErr_NoNewInstance(t *testing.T) {
}
require.NotNil(t, req)

handler.ServeHTTP(serviceName, w, req)
svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

handler.ServeHTTP(svcDefaults, w, req)

resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -148,7 +155,11 @@ func TestPlacementAddHandler_SafeErr_NotAllAvailable(t *testing.T) {
}
require.NotNil(t, req)

handler.ServeHTTP(serviceName, w, req)
svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

handler.ServeHTTP(svcDefaults, w, req)

resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -207,7 +218,11 @@ func TestPlacementAddHandler_SafeOK(t *testing.T) {
}

mockPlacementService.EXPECT().AddInstances(gomock.Any()).Return(nil, nil, errors.New("test err"))
handler.ServeHTTP(serviceName, w, req)
svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

handler.ServeHTTP(svcDefaults, w, req)

resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -240,7 +255,7 @@ func TestPlacementAddHandler_SafeOK(t *testing.T) {
} else {
mockPlacementService.EXPECT().AddInstances(gomock.Any()).Return(existingPlacement.Clone().SetVersion(1), nil, nil)
}
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp = w.Result()
body, _ = ioutil.ReadAll(resp.Body)
Expand Down
18 changes: 13 additions & 5 deletions src/query/api/v1/handler/placement/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/m3db/m3/src/cluster/services"
"github.com/m3db/m3/src/cluster/shard"
"github.com/m3db/m3/src/query/api/v1/handler"
apihandler "github.com/m3db/m3/src/query/api/v1/handler"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
Expand All @@ -52,24 +53,28 @@ func TestPlacementService(t *testing.T) {
mockClient.EXPECT().Services(gomock.Not(nil)).Return(mockServices, nil)
mockServices.EXPECT().PlacementService(gomock.Not(nil), gomock.Not(nil)).Return(mockPlacementService, nil)

svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

placementService, algo, err := ServiceWithAlgo(
mockClient, handler.NewServiceOptions(handler.M3DBServiceName, nil, nil), time.Time{}, nil)
mockClient, handler.NewServiceOptions(svcDefaults, nil, nil), time.Time{}, nil)
assert.NoError(t, err)
assert.NotNil(t, placementService)
assert.NotNil(t, algo)

// Test Services returns error
mockClient.EXPECT().Services(gomock.Not(nil)).Return(nil, errors.New("dummy service error"))
placementService, err = Service(
mockClient, handler.NewServiceOptions(handler.M3DBServiceName, nil, nil), time.Time{}, nil)
mockClient, handler.NewServiceOptions(svcDefaults, nil, nil), time.Time{}, nil)
assert.Nil(t, placementService)
assert.EqualError(t, err, "dummy service error")

// Test PlacementService returns error
mockClient.EXPECT().Services(gomock.Not(nil)).Return(mockServices, nil)
mockServices.EXPECT().PlacementService(gomock.Not(nil), gomock.Not(nil)).Return(nil, errors.New("dummy placement error"))
placementService, err = Service(
mockClient, handler.NewServiceOptions(handler.M3DBServiceName, nil, nil), time.Time{}, nil)
mockClient, handler.NewServiceOptions(svcDefaults, nil, nil), time.Time{}, nil)
assert.Nil(t, placementService)
assert.EqualError(t, err, "dummy placement error")
})
Expand Down Expand Up @@ -100,10 +105,13 @@ func TestPlacementServiceWithClusterHeaders(t *testing.T) {
})

var (
serviceValue = handler.M3DBServiceName
serviceValue = handler.M3DBServiceName
svcDefaults = apihandler.ServiceNameAndDefaults{
ServiceName: handler.M3DBServiceName,
}
environmentValue = "bar_env"
zoneValue = "baz_zone"
opts = handler.NewServiceOptions(serviceValue, nil, nil)
opts = handler.NewServiceOptions(svcDefaults, nil, nil)
)
opts.ServiceEnvironment = environmentValue
opts.ServiceZone = zoneValue
Expand Down
11 changes: 8 additions & 3 deletions src/query/api/v1/handler/placement/delete_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/m3db/m3/src/cluster/kv"
"github.com/m3db/m3/src/cmd/services/m3query/config"
apihandler "github.com/m3db/m3/src/query/api/v1/handler"
"github.com/m3db/m3/src/x/instrument"

"github.com/golang/mock/gomock"
Expand All @@ -46,12 +47,16 @@ func TestPlacementDeleteAllHandler(t *testing.T) {
require.NoError(t, err)
handler := NewDeleteAllHandler(handlerOpts)

svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

// Test delete success
w := httptest.NewRecorder()
req := httptest.NewRequest(DeleteAllHTTPMethod, M3DBDeleteAllURL, nil)
require.NotNil(t, req)
mockPlacementService.EXPECT().Delete()
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
Expand All @@ -61,7 +66,7 @@ func TestPlacementDeleteAllHandler(t *testing.T) {
req = httptest.NewRequest(DeleteAllHTTPMethod, M3DBDeleteAllURL, nil)
require.NotNil(t, req)
mockPlacementService.EXPECT().Delete().Return(errors.New("error"))
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp = w.Result()
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
Expand All @@ -71,7 +76,7 @@ func TestPlacementDeleteAllHandler(t *testing.T) {
req = httptest.NewRequest(DeleteAllHTTPMethod, M3DBDeleteAllURL, nil)
require.NotNil(t, req)
mockPlacementService.EXPECT().Delete().Return(kv.ErrNotFound)
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp = w.Result()
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
Expand Down
18 changes: 13 additions & 5 deletions src/query/api/v1/handler/placement/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ func TestPlacementDeleteHandler_Force(t *testing.T) {
config.Configuration{}, nil, instrument.NewOptions())
handler := NewDeleteHandler(handlerOpts)

svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

// Test remove success
w := httptest.NewRecorder()
req := httptest.NewRequest(DeleteHTTPMethod, "/placement/host1?force=true", nil)
req = mux.SetURLVars(req, map[string]string{"id": "host1"})
require.NotNil(t, req)
mockPlacementService.EXPECT().RemoveInstances([]string{"host1"}).Return(placement.NewPlacement(), nil)
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp := w.Result()
body, err := ioutil.ReadAll(resp.Body)
Expand All @@ -70,7 +74,7 @@ func TestPlacementDeleteHandler_Force(t *testing.T) {
req = mux.SetURLVars(req, map[string]string{"id": "nope"})
require.NotNil(t, req)
mockPlacementService.EXPECT().RemoveInstances([]string{"nope"}).Return(placement.NewPlacement(), errors.New("ID does not exist"))
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp = w.Result()
body, err = ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -128,12 +132,16 @@ func testDeleteHandlerSafe(t *testing.T, serviceName string) {
SetIsMirrored(true)
}

svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

req = mux.SetURLVars(req, map[string]string{"id": "host1"})
require.NotNil(t, req)
if !isStateless(serviceName) {
mockPlacementService.EXPECT().Placement().Return(basePlacement, nil)
}
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp := w.Result()
body, err := ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -165,7 +173,7 @@ func testDeleteHandlerSafe(t *testing.T, serviceName string) {
req = mux.SetURLVars(req, map[string]string{"id": "host1"})
require.NotNil(t, req)
mockPlacementService.EXPECT().Placement().Return(basePlacement, nil)
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp = w.Result()
body, err = ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -259,7 +267,7 @@ func testDeleteHandlerSafe(t *testing.T, serviceName string) {
mockPlacementService.EXPECT().CheckAndSet(gomock.Any(), 0).Return(returnPlacement, nil)
}

handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp = w.Result()
body, err = ioutil.ReadAll(resp.Body)
Expand Down
13 changes: 9 additions & 4 deletions src/query/api/v1/handler/placement/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/m3db/m3/src/cluster/placement/storage"
"github.com/m3db/m3/src/cluster/services"
"github.com/m3db/m3/src/cmd/services/m3query/config"
apihandler "github.com/m3db/m3/src/query/api/v1/handler"
"github.com/m3db/m3/src/x/instrument"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -125,8 +126,12 @@ func TestPlacementGetHandler(t *testing.T) {
placementObj, err := placement.NewPlacementFromProto(placementProto)
require.NoError(t, err)

svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

mockPlacementService.EXPECT().Placement().Return(placementObj, nil)
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
Expand All @@ -139,7 +144,7 @@ func TestPlacementGetHandler(t *testing.T) {
require.NotNil(t, req)

mockPlacementService.EXPECT().Placement().Return(nil, errors.New("key not found"))
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp = w.Result()
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
Expand All @@ -149,7 +154,7 @@ func TestPlacementGetHandler(t *testing.T) {
req = httptest.NewRequest(GetHTTPMethod, "/placement/get?version=foo", nil)
require.NotNil(t, req)

handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)
resp = w.Result()
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)

Expand All @@ -160,7 +165,7 @@ func TestPlacementGetHandler(t *testing.T) {

mockPlacementService.EXPECT().PlacementForVersion(12).Return(placementObj.Clone().SetVersion(12), nil)

handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)
resp = w.Result()
body, _ = ioutil.ReadAll(resp.Body)
assert.Equal(t, http.StatusOK, resp.StatusCode)
Expand Down
10 changes: 7 additions & 3 deletions src/query/api/v1/handler/placement/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ func TestPlacementInitHandler(t *testing.T) {
newPlacement, err := placement.NewPlacementFromProto(initTestPlacementProto)
require.NoError(t, err)

svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

mockPlacementService.EXPECT().BuildInitialPlacement(gomock.Not(nil), 16, 1).Return(newPlacement, nil)
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)
resp := w.Result()
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
Expand All @@ -119,7 +123,7 @@ func TestPlacementInitHandler(t *testing.T) {
BuildInitialPlacement(gomock.Not(nil), 64, 2).
Return(nil, errors.New("unable to build initial placement"))
}
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)
resp = w.Result()
body, err = ioutil.ReadAll(resp.Body)
require.NoError(t, err)
Expand All @@ -146,7 +150,7 @@ func TestPlacementInitHandler(t *testing.T) {
Return(nil, kv.ErrAlreadyExists)
}

handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)
resp = w.Result()
_, err = ioutil.ReadAll(resp.Body)
require.NoError(t, err)
Expand Down
20 changes: 16 additions & 4 deletions src/query/api/v1/handler/placement/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ func testPlacementReplaceHandlerForce(t *testing.T, serviceName string) {
w := httptest.NewRecorder()
req := newReplaceRequest(`{"force": true, "leavingInstanceIDs": []}`)

svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

mockPlacementService.EXPECT().ReplaceInstances([]string{}, gomock.Any()).Return(placement.NewPlacement(), nil, errors.New("test"))
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
Expand All @@ -93,7 +97,7 @@ func testPlacementReplaceHandlerForce(t *testing.T, serviceName string) {
w = httptest.NewRecorder()
req = newReplaceRequest(`{"force": true, "leavingInstanceIDs": ["a"]}`)
mockPlacementService.EXPECT().ReplaceInstances([]string{"a"}, gomock.Not(nil)).Return(placement.NewPlacement(), nil, nil)
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)
resp = w.Result()
body, _ = ioutil.ReadAll(resp.Body)
assert.Equal(t, `{"placement":{"instances":{},"replicaFactor":0,"numShards":0,"isSharded":false,"cutoverTime":"0","isMirrored":false,"maxShardSetId":0},"version":0}`, string(body))
Expand All @@ -113,12 +117,16 @@ func testPlacementReplaceHandlerSafeErr(t *testing.T, serviceName string) {
w := httptest.NewRecorder()
req := newReplaceRequest("{}")

svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

mockPlacementService.EXPECT().Placement().Return(newInitPlacement(), nil)
if serviceName == apihandler.M3CoordinatorServiceName {
mockPlacementService.EXPECT().CheckAndSet(gomock.Any(), 0).
Return(newInitPlacement().SetVersion(1), nil)
}
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -238,8 +246,12 @@ func testPlacementReplaceHandlerSafeOk(t *testing.T, serviceName string) {
SetInstances(instances).
SetVersion(2)

svcDefaults := apihandler.ServiceNameAndDefaults{
ServiceName: serviceName,
}

mockPlacementService.EXPECT().CheckAndSet(matcher, 1).Return(returnPl, nil)
handler.ServeHTTP(serviceName, w, req)
handler.ServeHTTP(svcDefaults, w, req)

resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
Expand Down
Loading

0 comments on commit 84d86ad

Please sign in to comment.