github.com/m3db/m3@v1.5.0/src/dbnode/persist/fs/migration/task_options_test.go (about)

     1  // Copyright (c) 2020 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 migration
    22  
    23  import (
    24  	"fmt"
    25  	"testing"
    26  
    27  	"github.com/golang/mock/gomock"
    28  
    29  	"github.com/m3db/m3/src/dbnode/namespace"
    30  	"github.com/m3db/m3/src/dbnode/persist/fs"
    31  	"github.com/m3db/m3/src/dbnode/storage"
    32  	"github.com/m3db/m3/src/x/ident"
    33  	"github.com/stretchr/testify/require"
    34  )
    35  
    36  func defaultTestOptions(t *testing.T, ctrl *gomock.Controller) TaskOptions {
    37  	md, err := namespace.NewMetadata(ident.StringID("ns"), namespace.NewOptions())
    38  	require.NoError(t, err)
    39  
    40  	pm, err := fs.NewPersistManager(fs.NewOptions())
    41  	require.NoError(t, err)
    42  
    43  	mockOpts := storage.NewMockOptions(ctrl)
    44  	mockOpts.EXPECT().Validate().AnyTimes()
    45  
    46  	return NewTaskOptions().
    47  		SetInfoFileResult(fs.ReadInfoFileResult{}).
    48  		SetShard(1).
    49  		SetNamespaceMetadata(md).
    50  		SetPersistManager(pm).
    51  		SetStorageOptions(mockOpts).
    52  		SetFilesystemOptions(fs.NewOptions())
    53  }
    54  
    55  func TestValidateStorageOptions(t *testing.T) {
    56  	ctrl := gomock.NewController(t)
    57  	defer ctrl.Finish()
    58  
    59  	opts := defaultTestOptions(t, ctrl)
    60  	require.NoError(t, opts.Validate())
    61  
    62  	opts = opts.SetStorageOptions(nil)
    63  	require.Error(t, opts.Validate())
    64  }
    65  
    66  func TestValidateNewMergerFn(t *testing.T) {
    67  	ctrl := gomock.NewController(t)
    68  	defer ctrl.Finish()
    69  
    70  	opts := defaultTestOptions(t, ctrl)
    71  	require.NoError(t, opts.Validate())
    72  
    73  	opts = opts.SetNewMergerFn(nil)
    74  	require.Error(t, opts.Validate())
    75  }
    76  
    77  func TestValidateInfoResultErr(t *testing.T) {
    78  	ctrl := gomock.NewController(t)
    79  	defer ctrl.Finish()
    80  
    81  	opts := defaultTestOptions(t, ctrl)
    82  	require.NoError(t, opts.Validate())
    83  
    84  	opts = opts.SetInfoFileResult(fs.ReadInfoFileResult{Err: testReadInfoFileResultError{}})
    85  	require.Error(t, opts.Validate())
    86  }
    87  
    88  func TestValidateNamespaceMetadata(t *testing.T) {
    89  	ctrl := gomock.NewController(t)
    90  	defer ctrl.Finish()
    91  
    92  	opts := defaultTestOptions(t, ctrl)
    93  	require.NoError(t, opts.Validate())
    94  
    95  	opts = opts.SetNamespaceMetadata(nil)
    96  	require.Error(t, opts.Validate())
    97  }
    98  
    99  func TestValidatePersistManager(t *testing.T) {
   100  	ctrl := gomock.NewController(t)
   101  	defer ctrl.Finish()
   102  
   103  	opts := defaultTestOptions(t, ctrl)
   104  	require.NoError(t, opts.Validate())
   105  
   106  	opts = opts.SetPersistManager(nil)
   107  	require.Error(t, opts.Validate())
   108  }
   109  
   110  func TestInfoFileResult(t *testing.T) {
   111  	opts := NewTaskOptions()
   112  	value := fs.ReadInfoFileResult{}
   113  	require.Equal(t, value, opts.SetInfoFileResult(value).InfoFileResult())
   114  }
   115  
   116  func TestShard(t *testing.T) {
   117  	opts := NewTaskOptions()
   118  	value := uint32(1)
   119  	require.Equal(t, value, opts.SetShard(value).Shard())
   120  }
   121  
   122  func TestNamespaceMetadata(t *testing.T) {
   123  	opts := NewTaskOptions()
   124  	value, err := namespace.NewMetadata(ident.StringID("ns"), namespace.NewOptions())
   125  	require.NoError(t, err)
   126  
   127  	require.Equal(t, value, opts.SetNamespaceMetadata(value).NamespaceMetadata())
   128  }
   129  
   130  func TestPersistManager(t *testing.T) {
   131  	opts := NewTaskOptions()
   132  	value, err := fs.NewPersistManager(fs.NewOptions())
   133  	require.NoError(t, err)
   134  
   135  	require.Equal(t, value, opts.SetPersistManager(value).PersistManager())
   136  }
   137  
   138  func TestStorageOptions(t *testing.T) {
   139  	opts := NewTaskOptions()
   140  	value := storage.DefaultTestOptions()
   141  	require.Equal(t, value, opts.SetStorageOptions(value).StorageOptions())
   142  }
   143  
   144  type testReadInfoFileResultError struct {
   145  }
   146  
   147  func (t testReadInfoFileResultError) Error() error {
   148  	return fmt.Errorf("error")
   149  }
   150  
   151  func (t testReadInfoFileResultError) Filepath() string {
   152  	return ""
   153  }