diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2023-02-14 03:19:58 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2023-04-13 09:15:42 +0300 |
commit | 3e401d35ec33907ee3fc75ac42920382e30bc938 (patch) | |
tree | 4267e5722d01c1d4759cc975a8a5b06651e52f3a | |
parent | f8bbd0c9ffa5dfe2ade88356d1394d8d87d684a1 (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.go | 10 | ||||
-rw-r--r-- | internal/praefect/config/node_test.go | 34 |
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) + }) + } +} |