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>2020-09-30 14:17:23 +0300
committerPavlo Strokov <pstrokov@gitlab.com>2020-09-30 14:36:19 +0300
commit2ec2156a9d8e345d42d1bf093144a68735cc08ac (patch)
tree7d38a2125a2a7011ebe9a923f3507f4c5f1b534d
parentbe4a37406e949983d483a4aabb7af0c0768e60af (diff)
Fix injection of gitaly servers info
InjectGitalyServers used `NewOutgoingContext` that completely overrides all outgoing metadata values, so no values except 'gitaly-servers' were passed with outgoing requests. Fixed by replacing `NewOutgoingContext` with `AppendToOutgoingContext` that appends passed values to existing metadata or crete it if not present. Closes: https://gitlab.com/gitlab-org/gitaly/-/issues/3158
-rw-r--r--changelogs/unreleased/ps-fix-inject-gitalies.yml5
-rw-r--r--internal/helper/storage.go2
-rw-r--r--internal/helper/storage_test.go41
3 files changed, 47 insertions, 1 deletions
diff --git a/changelogs/unreleased/ps-fix-inject-gitalies.yml b/changelogs/unreleased/ps-fix-inject-gitalies.yml
new file mode 100644
index 000000000..0bdd05b5a
--- /dev/null
+++ b/changelogs/unreleased/ps-fix-inject-gitalies.yml
@@ -0,0 +1,5 @@
+---
+title: Fix injection of gitaly servers info
+merge_request: 2615
+author:
+type: fixed
diff --git a/internal/helper/storage.go b/internal/helper/storage.go
index 523ca7ed6..0a465756f 100644
--- a/internal/helper/storage.go
+++ b/internal/helper/storage.go
@@ -74,5 +74,5 @@ func InjectGitalyServers(ctx context.Context, name, address, token string) (cont
return nil, err
}
- return metadata.NewOutgoingContext(ctx, metadata.Pairs("gitaly-servers", base64.StdEncoding.EncodeToString(gitalyServersJSON))), nil
+ return metadata.AppendToOutgoingContext(ctx, "gitaly-servers", base64.StdEncoding.EncodeToString(gitalyServersJSON)), nil
}
diff --git a/internal/helper/storage_test.go b/internal/helper/storage_test.go
index d0f229e0e..1e50e04cd 100644
--- a/internal/helper/storage_test.go
+++ b/internal/helper/storage_test.go
@@ -1,7 +1,10 @@
package helper
import (
+ "context"
"encoding/base64"
+ "encoding/json"
+ "strings"
"testing"
"github.com/stretchr/testify/require"
@@ -56,3 +59,41 @@ func TestExtractGitalyServers(t *testing.T) {
})
}
}
+
+func TestInjectGitalyServers(t *testing.T) {
+ check := func(t *testing.T, ctx context.Context) {
+ t.Helper()
+
+ newCtx, err := InjectGitalyServers(ctx, "gitaly-1", "1.1.1.1", "secret")
+ require.NoError(t, err)
+
+ md, found := metadata.FromOutgoingContext(newCtx)
+ require.True(t, found)
+
+ gs, found := md["gitaly-servers"]
+ require.True(t, found)
+
+ require.Len(t, gs, 1)
+
+ var servers map[string]interface{}
+ require.NoError(t, json.NewDecoder(base64.NewDecoder(base64.StdEncoding, strings.NewReader(gs[0]))).Decode(&servers), "received %s", gs[0])
+ require.EqualValues(t, map[string]interface{}{"gitaly-1": map[string]interface{}{"address": "1.1.1.1", "token": "secret"}}, servers)
+ }
+
+ t.Run("brand new context", func(t *testing.T) {
+ ctx := context.Background()
+
+ check(t, ctx)
+ })
+
+ t.Run("context with existing outgoing metadata should not be re-written", func(t *testing.T) {
+ existing := metadata.New(map[string]string{"foo": "bar"})
+
+ ctx := metadata.NewOutgoingContext(context.Background(), existing)
+ check(t, ctx)
+
+ md, found := metadata.FromOutgoingContext(ctx)
+ require.True(t, found)
+ require.Equal(t, []string{"bar"}, md["foo"])
+ })
+}