github.com/m3db/m3@v1.5.0/src/dbnode/namespace/metadata_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  
    27  	"github.com/m3db/m3/src/dbnode/retention"
    28  	"github.com/m3db/m3/src/x/ident"
    29  
    30  	"github.com/golang/mock/gomock"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestMetadataEqualsTrue(t *testing.T) {
    35  
    36  	testID := ident.StringID("some-string")
    37  	testOpts := NewOptions()
    38  	md1, err := NewMetadata(testID, testOpts)
    39  	require.NoError(t, err)
    40  	md2, err := NewMetadata(testID, testOpts)
    41  	require.NoError(t, err)
    42  
    43  	require.True(t, md1.Equal(md1))
    44  	require.True(t, md1.Equal(md2))
    45  	require.True(t, md2.Equal(md1))
    46  }
    47  
    48  func TestMetadataEqualsIDsDiffer(t *testing.T) {
    49  	testID1 := ident.StringID("some-string-1")
    50  	testID2 := ident.StringID("some-string-2")
    51  	testOpts := NewOptions()
    52  	md1, err := NewMetadata(testID1, testOpts)
    53  	require.NoError(t, err)
    54  	md2, err := NewMetadata(testID2, testOpts)
    55  	require.NoError(t, err)
    56  	require.False(t, md1.Equal(md2))
    57  	require.False(t, md2.Equal(md1))
    58  }
    59  
    60  func TestMetadataEqualsOptsDiffer(t *testing.T) {
    61  	testID := ident.StringID("some-string")
    62  	testOpts1 := NewOptions()
    63  	testOpts2 := testOpts1.SetBootstrapEnabled(!testOpts1.BootstrapEnabled())
    64  	md1, err := NewMetadata(testID, testOpts1)
    65  	require.NoError(t, err)
    66  	md2, err := NewMetadata(testID, testOpts2)
    67  	require.NoError(t, err)
    68  	require.False(t, md1.Equal(md2))
    69  	require.False(t, md2.Equal(md1))
    70  }
    71  
    72  func TestMetadataEqualsRetentionOptsDiffer(t *testing.T) {
    73  	testID := ident.StringID("some-string")
    74  	testOpts1 := NewOptions()
    75  	ropts := testOpts1.RetentionOptions()
    76  	testOpts2 := testOpts1.SetRetentionOptions(ropts.SetBlockSize(ropts.BlockSize() * 2))
    77  	md1, err := NewMetadata(testID, testOpts1)
    78  	require.NoError(t, err)
    79  	md2, err := NewMetadata(testID, testOpts2)
    80  	require.NoError(t, err)
    81  	require.False(t, md1.Equal(md2))
    82  	require.False(t, md2.Equal(md1))
    83  }
    84  
    85  func TestMetadataValidateEmptyID(t *testing.T) {
    86  	testID := ident.StringID("")
    87  	testOpts1 := NewOptions()
    88  	_, err := NewMetadata(testID, testOpts1)
    89  	require.Error(t, err)
    90  }
    91  
    92  func TestMetadataValidateRetentionErr(t *testing.T) {
    93  	ctrl := gomock.NewController(t)
    94  	defer ctrl.Finish()
    95  
    96  	mockROpts := retention.NewMockOptions(ctrl)
    97  	testID := ident.StringID("some-string")
    98  	testOpts1 := NewOptions().SetRetentionOptions(mockROpts)
    99  
   100  	mockROpts.EXPECT().Validate().Return(nil)
   101  	_, err := NewMetadata(testID, testOpts1)
   102  	require.NoError(t, err)
   103  
   104  	mockROpts.EXPECT().Validate().Return(fmt.Errorf("some-error"))
   105  	_, err = NewMetadata(testID, testOpts1)
   106  	require.Error(t, err)
   107  }