diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2023-03-12 10:52:44 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2023-04-13 09:15:42 +0300 |
commit | 33b80cf8423ca65fda18c052cfabaaef081b5d84 (patch) | |
tree | f4912094d301f4a5722190e4ea298815d413f6d6 | |
parent | ee57a61d40af8d0220ce489d86c99759c69f8b40 (diff) |
Praefect: RepositoriesCleanup.Validate method
The new 'Validate' method validates values of the
'RepositoriesCleanup' type. It will be used in the later changes.
The 'RepositoriesInBatch' field now has 'uint' type as it is
not allowed to use negative values for it.
-rw-r--r-- | internal/cli/praefect/main.go | 2 | ||||
-rw-r--r-- | internal/praefect/config/config.go | 17 | ||||
-rw-r--r-- | internal/praefect/config/config_test.go | 54 |
3 files changed, 71 insertions, 2 deletions
diff --git a/internal/cli/praefect/main.go b/internal/cli/praefect/main.go index 46877f070..13705a111 100644 --- a/internal/cli/praefect/main.go +++ b/internal/cli/praefect/main.go @@ -587,7 +587,7 @@ func run( cfg := repocleaner.Cfg{ RunInterval: conf.RepositoriesCleanup.RunInterval.Duration(), LivenessInterval: 30 * time.Second, - RepositoriesInBatch: conf.RepositoriesCleanup.RepositoriesInBatch, + RepositoriesInBatch: int(conf.RepositoriesCleanup.RepositoriesInBatch), } repoCleaner := repocleaner.NewRunner(cfg, logger, healthChecker, nodeSet.Connections(), storageSync, storageSync, repocleaner.NewLogWarnAction(logger)) if err := repoCleaner.Run(ctx, helper.NewTimerTicker(conf.RepositoriesCleanup.CheckInterval.Duration())); err != nil && !errors.Is(context.Canceled, err) { diff --git a/internal/praefect/config/config.go b/internal/praefect/config/config.go index 54d505cca..01c8f616e 100644 --- a/internal/praefect/config/config.go +++ b/internal/praefect/config/config.go @@ -508,7 +508,22 @@ type RepositoriesCleanup struct { // RunInterval: the check runs if the previous operation was done at least RunInterval before. RunInterval duration.Duration `toml:"run_interval,omitempty"` // RepositoriesInBatch is the number of repositories to pass as a batch for processing. - RepositoriesInBatch int `toml:"repositories_in_batch,omitempty"` + RepositoriesInBatch uint `toml:"repositories_in_batch,omitempty"` +} + +// Validate runs validation on all fields and compose all found errors. +func (rc RepositoriesCleanup) Validate() error { + if rc.RunInterval == 0 { + // If RunInterval is set to 0 it means it is disabled. The validation doesn't make + // sense then as it won't be used. + return nil + } + + return cfgerror.New(). + Append(cfgerror.Comparable(rc.CheckInterval.Duration()).GreaterOrEqual(minimalSyncCheckInterval), "check_interval"). + Append(cfgerror.Comparable(rc.RunInterval.Duration()).GreaterOrEqual(minimalSyncRunInterval), "run_interval"). + Append(cfgerror.Comparable(rc.RepositoriesInBatch).GreaterOrEqual(1), "repositories_in_batch"). + AsError() } // DefaultRepositoriesCleanup contains default configuration values for the RepositoriesCleanup. diff --git a/internal/praefect/config/config_test.go b/internal/praefect/config/config_test.go index dec2de721..3052e649b 100644 --- a/internal/praefect/config/config_test.go +++ b/internal/praefect/config/config_test.go @@ -808,3 +808,57 @@ func TestVirtualStorage_Validate(t *testing.T) { }) } } + +func TestRepositoriesCleanup_Validate(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + name string + cleanup RepositoriesCleanup + expectedErr error + }{ + { + name: "valid", + cleanup: RepositoriesCleanup{ + CheckInterval: duration.Duration(10 * time.Minute), + RunInterval: duration.Duration(1 * time.Minute), + RepositoriesInBatch: 10, + }, + }, + { + name: "noop because run interval is 0", + cleanup: RepositoriesCleanup{ + CheckInterval: -duration.Duration(10 * time.Minute), + RunInterval: 0, + RepositoriesInBatch: 10, + }, + }, + { + name: "invalid", + cleanup: RepositoriesCleanup{ + CheckInterval: duration.Duration(1), + RunInterval: duration.Duration(50 * time.Second), + RepositoriesInBatch: 0, + }, + expectedErr: cfgerror.ValidationErrors{ + cfgerror.NewValidationError( + fmt.Errorf("%w: 1ns is not greater than or equal to 1m0s", cfgerror.ErrNotInRange), + "check_interval", + ), + cfgerror.NewValidationError( + fmt.Errorf("%w: 50s is not greater than or equal to 1m0s", cfgerror.ErrNotInRange), + "run_interval", + ), + cfgerror.NewValidationError( + fmt.Errorf("%w: 0 is not greater than or equal to 1", cfgerror.ErrNotInRange), + "repositories_in_batch", + ), + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + err := tc.cleanup.Validate() + require.Equal(t, tc.expectedErr, err) + }) + } +} |