github.com/m3db/m3@v1.5.0/src/cluster/placementhandler/delete_all_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package placementhandler 22 23 import ( 24 "errors" 25 "fmt" 26 "net/http" 27 "net/http/httptest" 28 "testing" 29 30 "github.com/m3db/m3/src/aggregator/aggregator" 31 "github.com/m3db/m3/src/cluster/kv" 32 "github.com/m3db/m3/src/cluster/placement" 33 "github.com/m3db/m3/src/cluster/placementhandler/handleroptions" 34 "github.com/m3db/m3/src/x/instrument" 35 36 "github.com/golang/mock/gomock" 37 "github.com/stretchr/testify/assert" 38 "github.com/stretchr/testify/require" 39 ) 40 41 func TestPlacementDeleteAllHandler(t *testing.T) { 42 ctrl := gomock.NewController(t) 43 defer ctrl.Finish() 44 45 runForAllAllowedServices(func(serviceName string) { 46 mockClient, mockPlacementService := SetupPlacementTest(t, ctrl) 47 handlerOpts, err := NewHandlerOptions( 48 mockClient, placement.Configuration{}, nil, instrument.NewOptions()) 49 require.NoError(t, err) 50 handler := NewDeleteAllHandler(handlerOpts) 51 52 svcDefaults := handleroptions.ServiceNameAndDefaults{ 53 ServiceName: serviceName, 54 } 55 56 inst0 := placement.NewInstance(). 57 SetID("foo"). 58 SetEndpoint("foo:123"). 59 SetHostname("foo"). 60 SetPort(123). 61 SetIsolationGroup("foo-group"). 62 SetWeight(1). 63 SetZone("default") 64 if serviceName == handleroptions.M3AggregatorServiceName { 65 inst0 = inst0.SetShardSetID(0) 66 } 67 68 inst1 := placement.NewInstance(). 69 SetID("bar"). 70 SetEndpoint("bar:123"). 71 SetHostname("bar"). 72 SetPort(123). 73 SetIsolationGroup("bar-group"). 74 SetWeight(1). 75 SetZone("default") 76 if serviceName == handleroptions.M3AggregatorServiceName { 77 inst1 = inst1.SetShardSetID(0) 78 } 79 80 instances := []placement.Instance{inst0, inst1} 81 82 existing := placement.NewPlacement(). 83 SetInstances(instances) 84 85 mockKVStore := kv.NewMockStore(ctrl) 86 87 // Test delete success 88 w := httptest.NewRecorder() 89 req := httptest.NewRequest(DeleteAllHTTPMethod, M3DBDeleteAllURL, nil) 90 require.NotNil(t, req) 91 mockPlacementService.EXPECT().Placement().Return(existing, nil) 92 mockPlacementService.EXPECT().Delete() 93 if serviceName == handleroptions.M3AggregatorServiceName { 94 flushTimesMgrOpts := aggregator.NewFlushTimesManagerOptions() 95 electionMgrOpts := aggregator.NewElectionManagerOptions() 96 mockClient.EXPECT().Store(gomock.Any()).Return(mockKVStore, nil) 97 mockKVStore.EXPECT(). 98 Get(gomock.Any()). 99 DoAndReturn(func(k string) (kv.Value, error) { 100 for _, inst := range instances { 101 switch k { 102 case fmt.Sprintf(flushTimesMgrOpts.FlushTimesKeyFmt(), inst.ShardSetID()): 103 return nil, nil 104 case fmt.Sprintf(electionMgrOpts.ElectionKeyFmt(), inst.ShardSetID()): 105 return nil, nil 106 } 107 } 108 return nil, errors.New("unexpected") 109 }). 110 AnyTimes() 111 mockKVStore.EXPECT(). 112 Delete(gomock.Any()). 113 DoAndReturn(func(k string) (kv.Value, error) { 114 for _, inst := range instances { 115 switch k { 116 case fmt.Sprintf(flushTimesMgrOpts.FlushTimesKeyFmt(), inst.ShardSetID()): 117 return nil, nil 118 case fmt.Sprintf(electionMgrOpts.ElectionKeyFmt(), inst.ShardSetID()): 119 return nil, nil 120 } 121 } 122 return nil, errors.New("unexpected") 123 }). 124 AnyTimes() 125 } 126 127 handler.ServeHTTP(svcDefaults, w, req) 128 129 resp := w.Result() 130 assert.Equal(t, http.StatusOK, resp.StatusCode) 131 132 // Test delete error 133 w = httptest.NewRecorder() 134 req = httptest.NewRequest(DeleteAllHTTPMethod, M3DBDeleteAllURL, nil) 135 require.NotNil(t, req) 136 mockPlacementService.EXPECT().Placement().Return(existing, nil) 137 mockPlacementService.EXPECT().Delete().Return(errors.New("error")) 138 handler.ServeHTTP(svcDefaults, w, req) 139 140 resp = w.Result() 141 assert.Equal(t, http.StatusInternalServerError, resp.StatusCode) 142 143 // Test delete not found error 144 w = httptest.NewRecorder() 145 req = httptest.NewRequest(DeleteAllHTTPMethod, M3DBDeleteAllURL, nil) 146 require.NotNil(t, req) 147 mockPlacementService.EXPECT().Placement().Return(nil, kv.ErrNotFound) 148 handler.ServeHTTP(svcDefaults, w, req) 149 150 resp = w.Result() 151 assert.Equal(t, http.StatusNotFound, resp.StatusCode) 152 }) 153 }