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 03:19:58 +0300
committerPavlo Strokov <pstrokov@gitlab.com>2023-04-13 09:15:42 +0300
commit3e401d35ec33907ee3fc75ac42920382e30bc938 (patch)
tree4267e5722d01c1d4759cc975a8a5b06651e52f3a
parentf8bbd0c9ffa5dfe2ade88356d1394d8d87d684a1 (diff)
Praefect: Node.Validate method
The new 'Validate' method validates values of the 'Node' type. It will be used in the later changes.
-rw-r--r--internal/praefect/config/node.go10
-rw-r--r--internal/praefect/config/node_test.go34
2 files changed, 44 insertions, 0 deletions
diff --git a/internal/praefect/config/node.go b/internal/praefect/config/node.go
index 47bd576ca..c10b31bd0 100644
--- a/internal/praefect/config/node.go
+++ b/internal/praefect/config/node.go
@@ -3,6 +3,8 @@ package config
import (
"encoding/json"
"fmt"
+
+ "gitlab.com/gitlab-org/gitaly/v15/internal/errors/cfgerror"
)
// Node describes an address that serves a storage
@@ -24,3 +26,11 @@ func (n Node) MarshalJSON() ([]byte, error) {
func (n Node) String() string {
return fmt.Sprintf("storage_name: %s, address: %s", n.Storage, n.Address)
}
+
+// Validate runs validation on all fields and compose all found errors.
+func (n Node) Validate() error {
+ return cfgerror.New().
+ Append(cfgerror.NotBlank(n.Storage), "storage").
+ Append(cfgerror.NotBlank(n.Address), "address").
+ AsError()
+}
diff --git a/internal/praefect/config/node_test.go b/internal/praefect/config/node_test.go
index 56bac085c..c2d63e34d 100644
--- a/internal/praefect/config/node_test.go
+++ b/internal/praefect/config/node_test.go
@@ -7,6 +7,7 @@ import (
"testing"
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitaly/v15/internal/errors/cfgerror"
)
func TestNode_MarshalJSON(t *testing.T) {
@@ -21,3 +22,36 @@ func TestNode_MarshalJSON(t *testing.T) {
require.NoError(t, err)
require.JSONEq(t, `{"storage":"storage","address":"address"}`, string(b))
}
+
+func TestNode_Validate(t *testing.T) {
+ t.Parallel()
+ for _, tc := range []struct {
+ name string
+ node Node
+ expectedErr error
+ }{
+ {
+ name: "valid",
+ node: Node{Storage: "storage", Address: "address"},
+ },
+ {
+ name: "invalid",
+ node: Node{Storage: "", Address: " \n \t"},
+ expectedErr: cfgerror.ValidationErrors{
+ {
+ Key: []string{"storage"},
+ Cause: cfgerror.ErrBlankOrEmpty,
+ },
+ {
+ Key: []string{"address"},
+ Cause: cfgerror.ErrBlankOrEmpty,
+ },
+ },
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ err := tc.node.Validate()
+ require.Equal(t, tc.expectedErr, err)
+ })
+ }
+}