vitess.io/vitess@v0.16.2/go/vt/vtorc/config/config_test.go (about)

     1  /*
     2  Copyright 2022 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestUpdateConfigValuesFromFlags(t *testing.T) {
    27  	t.Run("defaults", func(t *testing.T) {
    28  		// Restore the changes we make to the Config parameter
    29  		defer func() {
    30  			Config = newConfiguration()
    31  		}()
    32  		defaultConfig := newConfiguration()
    33  		UpdateConfigValuesFromFlags()
    34  		require.Equal(t, defaultConfig, Config)
    35  	})
    36  
    37  	t.Run("override auditPurgeDuration", func(t *testing.T) {
    38  		oldAuditPurgeDuration := auditPurgeDuration
    39  		auditPurgeDuration = 8 * time.Hour * 24
    40  		auditPurgeDuration += time.Second + 4*time.Minute
    41  		// Restore the changes we make
    42  		defer func() {
    43  			Config = newConfiguration()
    44  			auditPurgeDuration = oldAuditPurgeDuration
    45  		}()
    46  
    47  		testConfig := newConfiguration()
    48  		// auditPurgeDuration is supposed to be in multiples of days.
    49  		// If it is not, then we round down to the nearest number of days.
    50  		testConfig.AuditPurgeDays = 8
    51  		UpdateConfigValuesFromFlags()
    52  		require.Equal(t, testConfig, Config)
    53  	})
    54  
    55  	t.Run("override sqliteDataFile", func(t *testing.T) {
    56  		oldSqliteDataFile := sqliteDataFile
    57  		sqliteDataFile = "newVal"
    58  		// Restore the changes we make
    59  		defer func() {
    60  			Config = newConfiguration()
    61  			sqliteDataFile = oldSqliteDataFile
    62  		}()
    63  
    64  		testConfig := newConfiguration()
    65  		testConfig.SQLite3DataFile = "newVal"
    66  		UpdateConfigValuesFromFlags()
    67  		require.Equal(t, testConfig, Config)
    68  	})
    69  
    70  	t.Run("override instancePollTime", func(t *testing.T) {
    71  		oldInstancePollTime := instancePollTime
    72  		instancePollTime = 7 * time.Second
    73  		// Restore the changes we make
    74  		defer func() {
    75  			Config = newConfiguration()
    76  			instancePollTime = oldInstancePollTime
    77  		}()
    78  
    79  		testConfig := newConfiguration()
    80  		testConfig.InstancePollSeconds = 7
    81  		UpdateConfigValuesFromFlags()
    82  		require.Equal(t, testConfig, Config)
    83  	})
    84  
    85  	t.Run("override snapshotTopologyInterval", func(t *testing.T) {
    86  		oldSnapshotTopologyInterval := snapshotTopologyInterval
    87  		snapshotTopologyInterval = 1 * time.Hour
    88  		// Restore the changes we make
    89  		defer func() {
    90  			Config = newConfiguration()
    91  			snapshotTopologyInterval = oldSnapshotTopologyInterval
    92  		}()
    93  
    94  		testConfig := newConfiguration()
    95  		testConfig.SnapshotTopologiesIntervalHours = 1
    96  		UpdateConfigValuesFromFlags()
    97  		require.Equal(t, testConfig, Config)
    98  	})
    99  
   100  	t.Run("override reasonableReplicationLag", func(t *testing.T) {
   101  		oldReasonableReplicationLag := reasonableReplicationLag
   102  		reasonableReplicationLag = 15 * time.Second
   103  		// Restore the changes we make
   104  		defer func() {
   105  			Config = newConfiguration()
   106  			reasonableReplicationLag = oldReasonableReplicationLag
   107  		}()
   108  
   109  		testConfig := newConfiguration()
   110  		testConfig.ReasonableReplicationLagSeconds = 15
   111  		UpdateConfigValuesFromFlags()
   112  		require.Equal(t, testConfig, Config)
   113  	})
   114  
   115  	t.Run("override auditFileLocation", func(t *testing.T) {
   116  		oldAuditFileLocation := auditFileLocation
   117  		auditFileLocation = "newFile"
   118  		// Restore the changes we make
   119  		defer func() {
   120  			Config = newConfiguration()
   121  			auditFileLocation = oldAuditFileLocation
   122  		}()
   123  
   124  		testConfig := newConfiguration()
   125  		testConfig.AuditLogFile = "newFile"
   126  		UpdateConfigValuesFromFlags()
   127  		require.Equal(t, testConfig, Config)
   128  	})
   129  
   130  	t.Run("override auditToBackend", func(t *testing.T) {
   131  		oldAuditToBackend := auditToBackend
   132  		auditToBackend = true
   133  		// Restore the changes we make
   134  		defer func() {
   135  			Config = newConfiguration()
   136  			auditToBackend = oldAuditToBackend
   137  		}()
   138  
   139  		testConfig := newConfiguration()
   140  		testConfig.AuditToBackendDB = true
   141  		UpdateConfigValuesFromFlags()
   142  		require.Equal(t, testConfig, Config)
   143  	})
   144  
   145  	t.Run("override auditToSyslog", func(t *testing.T) {
   146  		oldAuditToSyslog := auditToSyslog
   147  		auditToSyslog = true
   148  		// Restore the changes we make
   149  		defer func() {
   150  			Config = newConfiguration()
   151  			auditToSyslog = oldAuditToSyslog
   152  		}()
   153  
   154  		testConfig := newConfiguration()
   155  		testConfig.AuditToSyslog = true
   156  		UpdateConfigValuesFromFlags()
   157  		require.Equal(t, testConfig, Config)
   158  	})
   159  
   160  	t.Run("override recoveryPeriodBlockDuration", func(t *testing.T) {
   161  		oldRecoveryPeriodBlockDuration := recoveryPeriodBlockDuration
   162  		recoveryPeriodBlockDuration = 5 * time.Minute
   163  		// Restore the changes we make
   164  		defer func() {
   165  			Config = newConfiguration()
   166  			recoveryPeriodBlockDuration = oldRecoveryPeriodBlockDuration
   167  		}()
   168  
   169  		testConfig := newConfiguration()
   170  		testConfig.RecoveryPeriodBlockSeconds = 300
   171  		UpdateConfigValuesFromFlags()
   172  		require.Equal(t, testConfig, Config)
   173  	})
   174  
   175  	t.Run("override preventCrossCellFailover", func(t *testing.T) {
   176  		oldPreventCrossCellFailover := preventCrossCellFailover
   177  		preventCrossCellFailover = true
   178  		// Restore the changes we make
   179  		defer func() {
   180  			Config = newConfiguration()
   181  			preventCrossCellFailover = oldPreventCrossCellFailover
   182  		}()
   183  
   184  		testConfig := newConfiguration()
   185  		testConfig.PreventCrossDataCenterPrimaryFailover = true
   186  		UpdateConfigValuesFromFlags()
   187  		require.Equal(t, testConfig, Config)
   188  	})
   189  
   190  	t.Run("override waitReplicasTimeout", func(t *testing.T) {
   191  		oldWaitReplicasTimeout := waitReplicasTimeout
   192  		waitReplicasTimeout = 3*time.Minute + 4*time.Second
   193  		// Restore the changes we make
   194  		defer func() {
   195  			Config = newConfiguration()
   196  			waitReplicasTimeout = oldWaitReplicasTimeout
   197  		}()
   198  
   199  		testConfig := newConfiguration()
   200  		testConfig.WaitReplicasTimeoutSeconds = 184
   201  		UpdateConfigValuesFromFlags()
   202  		require.Equal(t, testConfig, Config)
   203  	})
   204  
   205  	t.Run("override topoInformationRefreshDuration", func(t *testing.T) {
   206  		oldTopoInformationRefreshDuration := topoInformationRefreshDuration
   207  		topoInformationRefreshDuration = 1 * time.Second
   208  		// Restore the changes we make
   209  		defer func() {
   210  			Config = newConfiguration()
   211  			topoInformationRefreshDuration = oldTopoInformationRefreshDuration
   212  		}()
   213  
   214  		testConfig := newConfiguration()
   215  		testConfig.TopoInformationRefreshSeconds = 1
   216  		UpdateConfigValuesFromFlags()
   217  		require.Equal(t, testConfig, Config)
   218  	})
   219  
   220  	t.Run("override recoveryPollDuration", func(t *testing.T) {
   221  		oldRecoveryPollDuration := recoveryPollDuration
   222  		recoveryPollDuration = 15 * time.Second
   223  		// Restore the changes we make
   224  		defer func() {
   225  			Config = newConfiguration()
   226  			recoveryPollDuration = oldRecoveryPollDuration
   227  		}()
   228  
   229  		testConfig := newConfiguration()
   230  		testConfig.RecoveryPollSeconds = 15
   231  		UpdateConfigValuesFromFlags()
   232  		require.Equal(t, testConfig, Config)
   233  	})
   234  }