github.com/weaviate/weaviate@v1.24.6/usecases/replica/config_test.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package replica
    13  
    14  import (
    15  	"fmt"
    16  	"testing"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/stretchr/testify/require"
    20  	"github.com/weaviate/weaviate/entities/models"
    21  	"github.com/weaviate/weaviate/entities/replication"
    22  )
    23  
    24  func Test_ValidateConfig(t *testing.T) {
    25  	tests := []struct {
    26  		name          string
    27  		initialconfig *models.ReplicationConfig
    28  		resultConfig  *models.ReplicationConfig
    29  		globalConfig  replication.GlobalConfig
    30  		expectedErr   error
    31  	}{
    32  		{
    33  			name:          "config not provided",
    34  			initialconfig: nil,
    35  			resultConfig:  &models.ReplicationConfig{Factor: 1},
    36  			globalConfig:  replication.GlobalConfig{MinimumFactor: 1},
    37  		},
    38  		{
    39  			name:          "config not provided - global minimum is 2",
    40  			initialconfig: nil,
    41  			resultConfig:  &models.ReplicationConfig{Factor: 2},
    42  			globalConfig:  replication.GlobalConfig{MinimumFactor: 2},
    43  		},
    44  		{
    45  			name:          "config provided, factor not provided",
    46  			initialconfig: &models.ReplicationConfig{},
    47  			resultConfig:  &models.ReplicationConfig{Factor: 1},
    48  			globalConfig:  replication.GlobalConfig{MinimumFactor: 1},
    49  		},
    50  		{
    51  			name:          "config provided, factor < 0",
    52  			initialconfig: &models.ReplicationConfig{Factor: -1},
    53  			resultConfig:  &models.ReplicationConfig{Factor: 1},
    54  			globalConfig:  replication.GlobalConfig{MinimumFactor: 1},
    55  		},
    56  		{
    57  			name:          "config provided, valid factor",
    58  			initialconfig: &models.ReplicationConfig{Factor: 7},
    59  			resultConfig:  &models.ReplicationConfig{Factor: 7},
    60  		},
    61  		{
    62  			name:          "explicitly trying to bypass the minimum leads to error",
    63  			initialconfig: &models.ReplicationConfig{Factor: 1},
    64  			resultConfig:  &models.ReplicationConfig{Factor: 1},
    65  			globalConfig:  replication.GlobalConfig{MinimumFactor: 2},
    66  			expectedErr:   fmt.Errorf("invalid replication factor: setup requires a minimum replication factor of 2: got 1"),
    67  		},
    68  	}
    69  
    70  	for _, test := range tests {
    71  		t.Run(test.name, func(t *testing.T) {
    72  			class := &models.Class{
    73  				ReplicationConfig: test.initialconfig,
    74  			}
    75  			err := ValidateConfig(class, test.globalConfig)
    76  			if test.expectedErr != nil {
    77  				assert.EqualError(t, test.expectedErr, err.Error())
    78  			} else {
    79  				assert.Nil(t, err)
    80  				assert.EqualValues(t, test.resultConfig, class.ReplicationConfig)
    81  			}
    82  		})
    83  	}
    84  }
    85  
    86  func Test_ValidateConfigUpdate(t *testing.T) {
    87  	tests := []struct {
    88  		name          string
    89  		initial       *models.ReplicationConfig
    90  		update        *models.ReplicationConfig
    91  		expectedError error
    92  	}{
    93  		{
    94  			name:    "attempting to increase replicas beyond cluster size",
    95  			initial: &models.ReplicationConfig{Factor: 3},
    96  			update:  &models.ReplicationConfig{Factor: 4},
    97  			expectedError: fmt.Errorf(
    98  				"cannot scale to 4 replicas, cluster has only 3 nodes"),
    99  		},
   100  	}
   101  
   102  	for _, test := range tests {
   103  		t.Run(test.name, func(t *testing.T) {
   104  			err := ValidateConfigUpdate(
   105  				&models.Class{ReplicationConfig: test.initial},
   106  				&models.Class{ReplicationConfig: test.update},
   107  				&fakeNodeCounter{3})
   108  			if test.expectedError == nil {
   109  				assert.Nil(t, err)
   110  			} else {
   111  				require.NotNil(t, err, "update validation must error")
   112  				assert.Equal(t, test.expectedError.Error(), err.Error())
   113  			}
   114  		})
   115  	}
   116  }
   117  
   118  type fakeNodeCounter struct{ count int }
   119  
   120  func (f *fakeNodeCounter) NodeCount() int {
   121  	return f.count
   122  }