github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/namespace/delete_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 namespace
    22  
    23  import (
    24  	"io/ioutil"
    25  	"net/http"
    26  	"net/http/httptest"
    27  	"testing"
    28  
    29  	"github.com/m3db/m3/src/cluster/kv"
    30  	nsproto "github.com/m3db/m3/src/dbnode/generated/proto/namespace"
    31  	"github.com/m3db/m3/src/x/instrument"
    32  
    33  	"github.com/golang/mock/gomock"
    34  	"github.com/gorilla/mux"
    35  	"github.com/stretchr/testify/assert"
    36  	"github.com/stretchr/testify/require"
    37  )
    38  
    39  func TestNamespaceDeleteHandlerNotFound(t *testing.T) {
    40  	ctrl := gomock.NewController(t)
    41  	defer ctrl.Finish()
    42  
    43  	mockClient, mockKV := setupNamespaceTest(t, ctrl)
    44  	mockClient.EXPECT().Store(gomock.Any()).Return(mockKV, nil).AnyTimes()
    45  	deleteHandler := NewDeleteHandler(mockClient, instrument.NewOptions())
    46  
    47  	w := httptest.NewRecorder()
    48  
    49  	req := httptest.NewRequest("DELETE", "/namespace/nope", nil)
    50  	req = mux.SetURLVars(req, map[string]string{"id": "nope"})
    51  	require.NotNil(t, req)
    52  
    53  	mockKV.EXPECT().Get(M3DBNodeNamespacesKey).Return(nil, kv.ErrNotFound)
    54  	deleteHandler.ServeHTTP(svcDefaults, w, req)
    55  
    56  	resp := w.Result()
    57  	body, _ := ioutil.ReadAll(resp.Body)
    58  	assert.Equal(t, http.StatusNotFound, resp.StatusCode)
    59  	assert.JSONEq(t,
    60  		`{"status":"error","error":"unable to find a namespace with specified name"}`,
    61  		string(body))
    62  }
    63  
    64  func TestNamespaceDeleteHandlerDeleteAll(t *testing.T) {
    65  	ctrl := gomock.NewController(t)
    66  	defer ctrl.Finish()
    67  
    68  	mockClient, mockKV := setupNamespaceTest(t, ctrl)
    69  	mockClient.EXPECT().Store(gomock.Any()).Return(mockKV, nil).AnyTimes()
    70  	deleteHandler := NewDeleteHandler(mockClient, instrument.NewOptions())
    71  
    72  	w := httptest.NewRecorder()
    73  
    74  	req := httptest.NewRequest("DELETE", "/namespace/testNamespace", nil)
    75  	req = mux.SetURLVars(req, map[string]string{"id": "testNamespace"})
    76  	require.NotNil(t, req)
    77  
    78  	registry := nsproto.Registry{
    79  		Namespaces: map[string]*nsproto.NamespaceOptions{
    80  			"testNamespace": &nsproto.NamespaceOptions{
    81  				BootstrapEnabled:  true,
    82  				FlushEnabled:      true,
    83  				WritesToCommitLog: true,
    84  				CleanupEnabled:    false,
    85  				RepairEnabled:     false,
    86  				RetentionOptions: &nsproto.RetentionOptions{
    87  					RetentionPeriodNanos:                     172800000000000,
    88  					BlockSizeNanos:                           7200000000000,
    89  					BufferFutureNanos:                        600000000000,
    90  					BufferPastNanos:                          600000000000,
    91  					BlockDataExpiry:                          true,
    92  					BlockDataExpiryAfterNotAccessPeriodNanos: 3600000000000,
    93  				},
    94  			},
    95  		},
    96  	}
    97  
    98  	mockValue := kv.NewMockValue(ctrl)
    99  	mockValue.EXPECT().Unmarshal(gomock.Any()).Return(nil).SetArg(0, registry)
   100  	mockValue.EXPECT().Version().Return(0)
   101  
   102  	mockKV.EXPECT().Get(M3DBNodeNamespacesKey).Return(mockValue, nil)
   103  	mockKV.EXPECT().Delete(M3DBNodeNamespacesKey).Return(nil, nil)
   104  	deleteHandler.ServeHTTP(svcDefaults, w, req)
   105  
   106  	resp := w.Result()
   107  	body, _ := ioutil.ReadAll(resp.Body)
   108  	assert.Equal(t, http.StatusOK, resp.StatusCode)
   109  	assert.Equal(t, "{\"deleted\":true}\n", string(body))
   110  }
   111  
   112  func TestNamespaceDeleteHandler(t *testing.T) {
   113  	ctrl := gomock.NewController(t)
   114  	defer ctrl.Finish()
   115  
   116  	mockClient, mockKV := setupNamespaceTest(t, ctrl)
   117  	mockClient.EXPECT().Store(gomock.Any()).Return(mockKV, nil).AnyTimes()
   118  	deleteHandler := NewDeleteHandler(mockClient, instrument.NewOptions())
   119  
   120  	w := httptest.NewRecorder()
   121  
   122  	req := httptest.NewRequest("DELETE", "/namespace/testNamespace", nil)
   123  	req = mux.SetURLVars(req, map[string]string{"id": "testNamespace"})
   124  	require.NotNil(t, req)
   125  
   126  	registry := nsproto.Registry{
   127  		Namespaces: map[string]*nsproto.NamespaceOptions{
   128  			"otherNamespace": &nsproto.NamespaceOptions{
   129  				BootstrapEnabled:  true,
   130  				FlushEnabled:      true,
   131  				WritesToCommitLog: true,
   132  				CleanupEnabled:    false,
   133  				RepairEnabled:     false,
   134  				RetentionOptions: &nsproto.RetentionOptions{
   135  					RetentionPeriodNanos:                     172800000000000,
   136  					BlockSizeNanos:                           7200000000000,
   137  					BufferFutureNanos:                        600000000000,
   138  					BufferPastNanos:                          600000000000,
   139  					BlockDataExpiry:                          true,
   140  					BlockDataExpiryAfterNotAccessPeriodNanos: 3600000000000,
   141  				},
   142  			},
   143  			"testNamespace": &nsproto.NamespaceOptions{
   144  				BootstrapEnabled:  true,
   145  				FlushEnabled:      true,
   146  				WritesToCommitLog: true,
   147  				CleanupEnabled:    false,
   148  				RepairEnabled:     false,
   149  				RetentionOptions: &nsproto.RetentionOptions{
   150  					RetentionPeriodNanos:                     172800000000000,
   151  					BlockSizeNanos:                           7200000000000,
   152  					BufferFutureNanos:                        600000000000,
   153  					BufferPastNanos:                          600000000000,
   154  					BlockDataExpiry:                          true,
   155  					BlockDataExpiryAfterNotAccessPeriodNanos: 3600000000000,
   156  				},
   157  			},
   158  		},
   159  	}
   160  
   161  	mockValue := kv.NewMockValue(ctrl)
   162  	mockValue.EXPECT().Unmarshal(gomock.Any()).Return(nil).SetArg(0, registry)
   163  	mockValue.EXPECT().Version().Return(0)
   164  
   165  	mockKV.EXPECT().Get(M3DBNodeNamespacesKey).Return(mockValue, nil)
   166  	mockKV.EXPECT().CheckAndSet(M3DBNodeNamespacesKey, gomock.Any(), gomock.Any()).Return(1, nil)
   167  	deleteHandler.ServeHTTP(svcDefaults, w, req)
   168  
   169  	resp := w.Result()
   170  	body, _ := ioutil.ReadAll(resp.Body)
   171  	assert.Equal(t, http.StatusOK, resp.StatusCode)
   172  	assert.Equal(t, "{\"deleted\":true}\n", string(body))
   173  }