github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/namespace/options_test.go (about)

     1  // Copyright (c) 2017 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  	"fmt"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/dbnode/retention"
    29  
    30  	"github.com/golang/mock/gomock"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestOptionsEquals(t *testing.T) {
    35  	o1 := NewOptions()
    36  	require.True(t, o1.Equal(o1))
    37  
    38  	o2 := NewOptions()
    39  	require.True(t, o1.Equal(o2))
    40  	require.True(t, o2.Equal(o1))
    41  }
    42  
    43  func TestOptionsEqualsIndexOpts(t *testing.T) {
    44  	o1 := NewOptions()
    45  	o2 := o1.SetIndexOptions(
    46  		o1.IndexOptions().SetBlockSize(
    47  			o1.IndexOptions().BlockSize() * 2))
    48  	require.True(t, o1.Equal(o1))
    49  	require.True(t, o2.Equal(o2))
    50  	require.False(t, o1.Equal(o2))
    51  	require.False(t, o2.Equal(o1))
    52  }
    53  
    54  func TestOptionsEqualsSchema(t *testing.T) {
    55  	o1 := NewOptions()
    56  	s1, err := LoadSchemaHistory(testSchemaOptions)
    57  	require.NoError(t, err)
    58  	require.NotNil(t, s1)
    59  	o2 := o1.SetSchemaHistory(s1)
    60  	require.True(t, o1.Equal(o1))
    61  	require.True(t, o2.Equal(o2))
    62  	require.False(t, o1.Equal(o2))
    63  	require.False(t, o2.Equal(o1))
    64  }
    65  
    66  func TestOptionsEqualsRetention(t *testing.T) {
    67  	ctrl := gomock.NewController(t)
    68  	defer ctrl.Finish()
    69  
    70  	r1 := retention.NewMockOptions(ctrl)
    71  	o1 := NewOptions().SetRetentionOptions(r1)
    72  
    73  	r1.EXPECT().Equal(r1).Return(true)
    74  	require.True(t, o1.Equal(o1))
    75  
    76  	r2 := retention.NewMockOptions(ctrl)
    77  	o2 := NewOptions().SetRetentionOptions(r2)
    78  
    79  	r1.EXPECT().Equal(r2).Return(true)
    80  	require.True(t, o1.Equal(o2))
    81  
    82  	r1.EXPECT().Equal(r2).Return(false)
    83  	require.False(t, o1.Equal(o2))
    84  
    85  	r2.EXPECT().Equal(r1).Return(false)
    86  	require.False(t, o2.Equal(o1))
    87  
    88  	r2.EXPECT().Equal(r1).Return(true)
    89  	require.True(t, o2.Equal(o1))
    90  }
    91  
    92  func TestOptionsValidate(t *testing.T) {
    93  	ctrl := gomock.NewController(t)
    94  	defer ctrl.Finish()
    95  
    96  	rOpts := retention.NewMockOptions(ctrl)
    97  	iOpts := NewMockIndexOptions(ctrl)
    98  	o1 := NewOptions().
    99  		SetRetentionOptions(rOpts).
   100  		SetIndexOptions(iOpts)
   101  
   102  	iOpts.EXPECT().Enabled().Return(true).AnyTimes()
   103  
   104  	rOpts.EXPECT().Validate().Return(nil)
   105  	rOpts.EXPECT().RetentionPeriod().Return(time.Hour)
   106  	rOpts.EXPECT().FutureRetentionPeriod().Return(time.Duration(0))
   107  	rOpts.EXPECT().BlockSize().Return(time.Hour)
   108  	iOpts.EXPECT().BlockSize().Return(time.Hour)
   109  	require.NoError(t, o1.Validate())
   110  
   111  	rOpts.EXPECT().Validate().Return(nil)
   112  	rOpts.EXPECT().RetentionPeriod().Return(time.Hour)
   113  	rOpts.EXPECT().FutureRetentionPeriod().Return(time.Duration(0))
   114  	rOpts.EXPECT().BlockSize().Return(time.Hour)
   115  	iOpts.EXPECT().BlockSize().Return(2 * time.Hour)
   116  	require.Error(t, o1.Validate())
   117  
   118  	rOpts.EXPECT().Validate().Return(fmt.Errorf("test error"))
   119  	require.Error(t, o1.Validate())
   120  }
   121  
   122  func TestOptionsValidateWithExtendedOptions(t *testing.T) {
   123  	ctrl := gomock.NewController(t)
   124  	defer ctrl.Finish()
   125  
   126  	extendedOpts := NewMockExtendedOptions(ctrl)
   127  	opts := NewOptions().SetExtendedOptions(extendedOpts)
   128  
   129  	extendedOpts.EXPECT().Validate().Return(nil)
   130  	require.NoError(t, opts.Validate())
   131  
   132  	extendedOpts.EXPECT().Validate().Return(fmt.Errorf("test error"))
   133  	require.Error(t, opts.Validate())
   134  }
   135  
   136  func TestOptionsValidateBlockSizeMustBeMultiple(t *testing.T) {
   137  	ctrl := gomock.NewController(t)
   138  	defer ctrl.Finish()
   139  
   140  	rOpts := retention.NewMockOptions(ctrl)
   141  	iOpts := NewMockIndexOptions(ctrl)
   142  	o1 := NewOptions().
   143  		SetRetentionOptions(rOpts).
   144  		SetIndexOptions(iOpts)
   145  
   146  	iOpts.EXPECT().Enabled().Return(true).AnyTimes()
   147  
   148  	rOpts.EXPECT().Validate().Return(nil)
   149  	rOpts.EXPECT().RetentionPeriod().Return(4 * time.Hour).AnyTimes()
   150  	rOpts.EXPECT().FutureRetentionPeriod().Return(time.Duration(0)).AnyTimes()
   151  	rOpts.EXPECT().BlockSize().Return(2 * time.Hour).AnyTimes()
   152  	iOpts.EXPECT().BlockSize().Return(3 * time.Hour).AnyTimes()
   153  	require.Error(t, o1.Validate())
   154  }
   155  
   156  func TestOptionsValidateBlockSizePositive(t *testing.T) {
   157  	ctrl := gomock.NewController(t)
   158  	defer ctrl.Finish()
   159  
   160  	rOpts := retention.NewMockOptions(ctrl)
   161  	iOpts := NewMockIndexOptions(ctrl)
   162  	o1 := NewOptions().
   163  		SetRetentionOptions(rOpts).
   164  		SetIndexOptions(iOpts)
   165  
   166  	iOpts.EXPECT().Enabled().Return(true).AnyTimes()
   167  
   168  	rOpts.EXPECT().Validate().Return(nil)
   169  	rOpts.EXPECT().RetentionPeriod().Return(4 * time.Hour).AnyTimes()
   170  	rOpts.EXPECT().FutureRetentionPeriod().Return(time.Duration(0)).AnyTimes()
   171  	rOpts.EXPECT().BlockSize().Return(2 * time.Hour).AnyTimes()
   172  	iOpts.EXPECT().BlockSize().Return(0 * time.Hour).AnyTimes()
   173  	require.Error(t, o1.Validate())
   174  
   175  	rOpts.EXPECT().Validate().Return(nil)
   176  	rOpts.EXPECT().RetentionPeriod().Return(4 * time.Hour).AnyTimes()
   177  	rOpts.EXPECT().BlockSize().Return(2 * time.Hour).AnyTimes()
   178  	iOpts.EXPECT().BlockSize().Return(-2 * time.Hour).AnyTimes()
   179  	require.Error(t, o1.Validate())
   180  }
   181  
   182  func TestOptionsValidateNoIndexing(t *testing.T) {
   183  	ctrl := gomock.NewController(t)
   184  	defer ctrl.Finish()
   185  
   186  	rOpts := retention.NewMockOptions(ctrl)
   187  	iOpts := NewMockIndexOptions(ctrl)
   188  	o1 := NewOptions().
   189  		SetRetentionOptions(rOpts).
   190  		SetIndexOptions(iOpts)
   191  
   192  	iOpts.EXPECT().Enabled().Return(false).AnyTimes()
   193  
   194  	rOpts.EXPECT().Validate().Return(nil)
   195  	require.NoError(t, o1.Validate())
   196  }
   197  
   198  func TestOptionsValidateStagingStatus(t *testing.T) {
   199  	ctrl := gomock.NewController(t)
   200  	defer ctrl.Finish()
   201  
   202  	rOpts := retention.NewMockOptions(ctrl)
   203  	iOpts := NewMockIndexOptions(ctrl)
   204  	o1 := NewOptions().
   205  		SetRetentionOptions(rOpts).
   206  		SetIndexOptions(iOpts)
   207  
   208  	iOpts.EXPECT().Enabled().Return(true).AnyTimes()
   209  
   210  	rOpts.EXPECT().Validate().Return(nil).AnyTimes()
   211  	rOpts.EXPECT().RetentionPeriod().Return(time.Hour).AnyTimes()
   212  	rOpts.EXPECT().FutureRetentionPeriod().Return(time.Duration(0)).AnyTimes()
   213  	rOpts.EXPECT().BlockSize().Return(time.Hour).AnyTimes()
   214  	iOpts.EXPECT().BlockSize().Return(time.Hour).AnyTimes()
   215  	require.NoError(t, o1.Validate())
   216  
   217  	o1 = o1.SetStagingState(StagingState{status: StagingStatus(12)})
   218  	require.Error(t, o1.Validate())
   219  }