github.com/spotahome/redis-operator@v1.2.4/api/redisfailover/v1/validate_test.go (about)

     1  package v1
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     8  )
     9  
    10  func TestValidate(t *testing.T) {
    11  	tests := []struct {
    12  		name                   string
    13  		rfName                 string
    14  		rfBootstrapNode        *BootstrapSettings
    15  		rfRedisCustomConfig    []string
    16  		rfSentinelCustomConfig []string
    17  		expectedError          string
    18  		expectedBootstrapNode  *BootstrapSettings
    19  	}{
    20  		{
    21  			name:   "populates default values",
    22  			rfName: "test",
    23  		},
    24  		{
    25  			name:          "errors on too long of name",
    26  			rfName:        "some-super-absurdely-unnecessarily-long-name-that-will-most-definitely-fail",
    27  			expectedError: "name length can't be higher than 48",
    28  		},
    29  		{
    30  			name:                   "SentinelCustomConfig provided",
    31  			rfName:                 "test",
    32  			rfSentinelCustomConfig: []string{"failover-timeout 500"},
    33  		},
    34  		{
    35  			name:            "BootstrapNode provided without a host",
    36  			rfName:          "test",
    37  			rfBootstrapNode: &BootstrapSettings{},
    38  			expectedError:   "BootstrapNode must include a host when provided",
    39  		},
    40  		{
    41  			name:   "SentinelCustomConfig provided",
    42  			rfName: "test",
    43  		},
    44  		{
    45  			name:                  "Populates default bootstrap port when valid",
    46  			rfName:                "test",
    47  			rfBootstrapNode:       &BootstrapSettings{Host: "127.0.0.1"},
    48  			expectedBootstrapNode: &BootstrapSettings{Host: "127.0.0.1", Port: "6379"},
    49  		},
    50  		{
    51  			name:                  "Allows for specifying boostrap port",
    52  			rfName:                "test",
    53  			rfBootstrapNode:       &BootstrapSettings{Host: "127.0.0.1", Port: "6380"},
    54  			expectedBootstrapNode: &BootstrapSettings{Host: "127.0.0.1", Port: "6380"},
    55  		},
    56  		{
    57  			name:                "Appends applied custom config to default initial values",
    58  			rfName:              "test",
    59  			rfRedisCustomConfig: []string{"tcp-keepalive 60"},
    60  		},
    61  		{
    62  			name:                  "Appends applied custom config to default initial values when bootstrapping",
    63  			rfName:                "test",
    64  			rfRedisCustomConfig:   []string{"tcp-keepalive 60"},
    65  			rfBootstrapNode:       &BootstrapSettings{Host: "127.0.0.1"},
    66  			expectedBootstrapNode: &BootstrapSettings{Host: "127.0.0.1", Port: "6379"},
    67  		},
    68  	}
    69  
    70  	for _, test := range tests {
    71  		t.Run(test.name, func(t *testing.T) {
    72  			assert := assert.New(t)
    73  			rf := generateRedisFailover(test.rfName, test.rfBootstrapNode)
    74  			rf.Spec.Redis.CustomConfig = test.rfRedisCustomConfig
    75  			rf.Spec.Sentinel.CustomConfig = test.rfSentinelCustomConfig
    76  
    77  			err := rf.Validate()
    78  
    79  			if test.expectedError == "" {
    80  				assert.NoError(err)
    81  
    82  				expectedRedisCustomConfig := []string{
    83  					"replica-priority 100",
    84  				}
    85  
    86  				if test.rfBootstrapNode != nil {
    87  					expectedRedisCustomConfig = []string{
    88  						"replica-priority 0",
    89  					}
    90  				}
    91  
    92  				expectedRedisCustomConfig = append(expectedRedisCustomConfig, test.rfRedisCustomConfig...)
    93  				expectedSentinelCustomConfig := defaultSentinelCustomConfig
    94  				if len(test.rfSentinelCustomConfig) > 0 {
    95  					expectedSentinelCustomConfig = test.rfSentinelCustomConfig
    96  				}
    97  
    98  				expectedRF := &RedisFailover{
    99  					ObjectMeta: metav1.ObjectMeta{
   100  						Name:      test.rfName,
   101  						Namespace: "namespace",
   102  					},
   103  					Spec: RedisFailoverSpec{
   104  						Redis: RedisSettings{
   105  							Image:    defaultImage,
   106  							Replicas: defaultRedisNumber,
   107  							Port:     defaultRedisPort,
   108  							Exporter: Exporter{
   109  								Image: defaultExporterImage,
   110  							},
   111  							CustomConfig: expectedRedisCustomConfig,
   112  						},
   113  						Sentinel: SentinelSettings{
   114  							Image:        defaultImage,
   115  							Replicas:     defaultSentinelNumber,
   116  							CustomConfig: expectedSentinelCustomConfig,
   117  							Exporter: Exporter{
   118  								Image: defaultSentinelExporterImage,
   119  							},
   120  						},
   121  						BootstrapNode: test.expectedBootstrapNode,
   122  					},
   123  				}
   124  				assert.Equal(expectedRF, rf)
   125  			} else {
   126  				if assert.Error(err) {
   127  					assert.Contains(test.expectedError, err.Error())
   128  				}
   129  			}
   130  		})
   131  	}
   132  }