debian-mirror-gitlab/workhorse/internal/gitaly/gitaly_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
2.3 KiB
Go
Raw Normal View History

2021-02-22 17:27:13 +05:30
package gitaly
import (
"context"
2022-07-16 23:28:13 +05:30
"os"
2021-02-22 17:27:13 +05:30
"testing"
2021-11-18 22:05:49 +05:30
"github.com/sirupsen/logrus"
2021-02-22 17:27:13 +05:30
"github.com/stretchr/testify/require"
"google.golang.org/grpc/metadata"
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
)
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
func TestMain(m *testing.M) {
2021-11-18 22:05:49 +05:30
InitializeSidechannelRegistry(logrus.StandardLogger())
2022-07-16 23:28:13 +05:30
os.Exit(m.Run())
}
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
func TestNewSmartHTTPClient(t *testing.T) {
ctx, client, err := NewSmartHTTPClient(
context.Background(),
serverFixture(),
)
2021-11-18 22:05:49 +05:30
require.NoError(t, err)
testOutgoingMetadata(t, ctx)
require.NotNil(t, client.sidechannelRegistry)
2021-02-22 17:27:13 +05:30
}
func TestNewBlobClient(t *testing.T) {
2022-07-16 23:28:13 +05:30
ctx, _, err := NewBlobClient(
context.Background(),
serverFixture(),
)
2021-02-22 17:27:13 +05:30
require.NoError(t, err)
testOutgoingMetadata(t, ctx)
}
func TestNewRepositoryClient(t *testing.T) {
2022-07-16 23:28:13 +05:30
ctx, _, err := NewRepositoryClient(
context.Background(),
serverFixture(),
)
2021-02-22 17:27:13 +05:30
require.NoError(t, err)
testOutgoingMetadata(t, ctx)
}
func TestNewNamespaceClient(t *testing.T) {
2022-07-16 23:28:13 +05:30
ctx, _, err := NewNamespaceClient(
context.Background(),
serverFixture(),
)
2021-02-22 17:27:13 +05:30
require.NoError(t, err)
testOutgoingMetadata(t, ctx)
}
func TestNewDiffClient(t *testing.T) {
2022-07-16 23:28:13 +05:30
ctx, _, err := NewDiffClient(
context.Background(),
serverFixture(),
)
2021-02-22 17:27:13 +05:30
require.NoError(t, err)
testOutgoingMetadata(t, ctx)
}
func testOutgoingMetadata(t *testing.T, ctx context.Context) {
2023-03-04 22:38:38 +05:30
t.Helper()
2021-02-22 17:27:13 +05:30
md, ok := metadata.FromOutgoingContext(ctx)
require.True(t, ok, "get metadata from context")
2023-03-04 22:38:38 +05:30
require.Equal(t, metadata.MD{"username": {"janedoe"}}, md)
2022-07-16 23:28:13 +05:30
}
func serverFixture() api.GitalyServer {
2023-03-04 22:38:38 +05:30
return api.GitalyServer{
Address: "tcp://localhost:123",
CallMetadata: map[string]string{"username": "janedoe"},
2021-02-22 17:27:13 +05:30
}
}
2023-03-04 22:38:38 +05:30
func TestWithOutgoingMetadata(t *testing.T) {
ctx := withOutgoingMetadata(context.Background(), api.GitalyServer{
CallMetadata: map[string]string{
"gitaly-feature-abc": "true",
"gitaly-featuregarbage": "blocked",
"bad-header": "blocked",
"user_id": "234",
"username": "janedoe",
"remote_ip": "1.2.3.4",
},
})
md, ok := metadata.FromOutgoingContext(ctx)
require.True(t, ok)
require.Equal(t, metadata.MD{
"gitaly-feature-abc": {"true"},
"user_id": {"234"},
"username": {"janedoe"},
"remote_ip": {"1.2.3.4"},
}, md)
2021-02-22 17:27:13 +05:30
}