Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavlo Strokov <pstrokov@gitlab.com>2023-02-14 02:49:18 +0300
committerPavlo Strokov <pstrokov@gitlab.com>2023-04-13 09:15:42 +0300
commita6ec2117755d1db877c9aa85473bfde901840273 (patch)
treeb3aff6887bf3c3eaab6e9630d6c6af9c8f8e0e54
parent20d689f645051ba859feedfd76d35d59bf9600cc (diff)
Praefect: Reconciliation.Validate method
The new 'Validate' method validates values of the 'Reconciliation' type. It will be used in the later changes.
-rw-r--r--internal/praefect/config/config.go15
-rw-r--r--internal/praefect/config/config_test.go40
2 files changed, 55 insertions, 0 deletions
diff --git a/internal/praefect/config/config.go b/internal/praefect/config/config.go
index 577ddb0fb..871a3b6b7 100644
--- a/internal/praefect/config/config.go
+++ b/internal/praefect/config/config.go
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
+ "sort"
"time"
"github.com/hashicorp/yamux"
@@ -151,6 +152,20 @@ type Reconciliation struct {
HistogramBuckets []float64 `toml:"histogram_buckets,omitempty"`
}
+// Validate runs validation on all fields and compose all found errors.
+func (r Reconciliation) Validate() error {
+ errs := cfgerror.New().
+ Append(cfgerror.IsPositive(r.SchedulingInterval.Duration()), "scheduling_interval")
+
+ if r.SchedulingInterval != 0 {
+ if !sort.Float64sAreSorted(r.HistogramBuckets) {
+ errs = errs.Append(cfgerror.ErrBadOrder, "histogram_buckets")
+ }
+ }
+
+ return errs.AsError()
+}
+
// DefaultReconciliationConfig returns the default values for reconciliation configuration.
func DefaultReconciliationConfig() Reconciliation {
return Reconciliation{
diff --git a/internal/praefect/config/config_test.go b/internal/praefect/config/config_test.go
index 66f359e4c..7b6fa91b7 100644
--- a/internal/praefect/config/config_test.go
+++ b/internal/praefect/config/config_test.go
@@ -683,3 +683,43 @@ func TestBackgroundVerification_Validate(t *testing.T) {
})
}
}
+
+func TestReconciliation_Validate(t *testing.T) {
+ t.Parallel()
+ for _, tc := range []struct {
+ name string
+ reconciliation Reconciliation
+ expectedErr error
+ }{
+ {
+ name: "empty is valid",
+ reconciliation: Reconciliation{},
+ },
+ {
+ name: "valid",
+ reconciliation: Reconciliation{
+ SchedulingInterval: duration.Duration(1),
+ HistogramBuckets: []float64{-1, 0, 1},
+ },
+ },
+ {
+ name: "invalid",
+ reconciliation: Reconciliation{
+ SchedulingInterval: duration.Duration(-1),
+ HistogramBuckets: []float64{-1, 1, 0},
+ },
+ expectedErr: cfgerror.ValidationErrors{{
+ Key: []string{"scheduling_interval"},
+ Cause: fmt.Errorf("%w: -1ns", cfgerror.ErrIsNegative),
+ }, {
+ Key: []string{"histogram_buckets"},
+ Cause: cfgerror.ErrBadOrder,
+ }},
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ err := tc.reconciliation.Validate()
+ require.Equal(t, tc.expectedErr, err)
+ })
+ }
+}