2020-12-02 10:09:37 -05:00
|
|
|
package gitaly
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-28 08:09:06 -04:00
|
|
|
"os"
|
2020-12-02 10:09:37 -05:00
|
|
|
"testing"
|
|
|
|
|
2021-10-11 05:09:08 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-12-02 10:09:37 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/grpc/metadata"
|
2022-05-18 05:07:58 -04:00
|
|
|
|
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
|
2020-12-02 10:09:37 -05:00
|
|
|
)
|
|
|
|
|
2022-04-28 08:09:06 -04:00
|
|
|
func TestMain(m *testing.M) {
|
2021-10-11 05:09:08 -04:00
|
|
|
InitializeSidechannelRegistry(logrus.StandardLogger())
|
2022-04-28 08:09:06 -04:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
2021-10-11 05:09:08 -04:00
|
|
|
|
2022-04-28 08:09:06 -04:00
|
|
|
func TestNewSmartHTTPClient(t *testing.T) {
|
2022-05-18 05:07:58 -04:00
|
|
|
ctx, client, err := NewSmartHTTPClient(
|
|
|
|
context.Background(),
|
|
|
|
serverFixture(),
|
|
|
|
WithFeatures(features()),
|
|
|
|
WithUsername("gl_username"),
|
|
|
|
WithUserID("gl_id"),
|
|
|
|
)
|
2021-10-11 05:09:08 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
testOutgoingMetadata(t, ctx)
|
2022-05-18 05:07:58 -04:00
|
|
|
testOutgoingIDAndUsername(t, ctx)
|
2021-10-11 05:09:08 -04:00
|
|
|
require.NotNil(t, client.sidechannelRegistry)
|
2020-12-02 10:09:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewBlobClient(t *testing.T) {
|
2022-05-18 05:07:58 -04:00
|
|
|
ctx, _, err := NewBlobClient(
|
|
|
|
context.Background(),
|
|
|
|
serverFixture(),
|
|
|
|
WithFeatures(features()),
|
|
|
|
)
|
2020-12-02 10:09:37 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
testOutgoingMetadata(t, ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRepositoryClient(t *testing.T) {
|
2022-05-18 05:07:58 -04:00
|
|
|
ctx, _, err := NewRepositoryClient(
|
|
|
|
context.Background(),
|
|
|
|
serverFixture(),
|
|
|
|
WithFeatures(features()),
|
|
|
|
)
|
|
|
|
|
2020-12-02 10:09:37 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
testOutgoingMetadata(t, ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewNamespaceClient(t *testing.T) {
|
2022-05-18 05:07:58 -04:00
|
|
|
ctx, _, err := NewNamespaceClient(
|
|
|
|
context.Background(),
|
|
|
|
serverFixture(),
|
|
|
|
WithFeatures(features()),
|
|
|
|
)
|
2020-12-02 10:09:37 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
testOutgoingMetadata(t, ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDiffClient(t *testing.T) {
|
2022-05-18 05:07:58 -04:00
|
|
|
ctx, _, err := NewDiffClient(
|
|
|
|
context.Background(),
|
|
|
|
serverFixture(),
|
|
|
|
WithFeatures(features()),
|
|
|
|
)
|
2020-12-02 10:09:37 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
testOutgoingMetadata(t, ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testOutgoingMetadata(t *testing.T, ctx context.Context) {
|
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
require.True(t, ok, "get metadata from context")
|
|
|
|
|
|
|
|
for k, v := range allowedFeatures() {
|
|
|
|
actual := md[k]
|
|
|
|
require.Len(t, actual, 1, "expect one value for %v", k)
|
|
|
|
require.Equal(t, v, actual[0], "value for %v", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range badFeatureMetadata() {
|
|
|
|
require.Empty(t, md[k], "value for bad key %v", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 05:07:58 -04:00
|
|
|
func testOutgoingIDAndUsername(t *testing.T, ctx context.Context) {
|
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
require.True(t, ok, "get metadata from context")
|
|
|
|
|
|
|
|
require.Equal(t, md["user_id"], []string{"gl_id"})
|
|
|
|
require.Equal(t, md["username"], []string{"gl_username"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func features() map[string]string {
|
2020-12-02 10:09:37 -05:00
|
|
|
features := make(map[string]string)
|
|
|
|
for k, v := range allowedFeatures() {
|
|
|
|
features[k] = v
|
|
|
|
}
|
2022-05-18 05:07:58 -04:00
|
|
|
|
2020-12-02 10:09:37 -05:00
|
|
|
for k, v := range badFeatureMetadata() {
|
|
|
|
features[k] = v
|
|
|
|
}
|
|
|
|
|
2022-05-18 05:07:58 -04:00
|
|
|
return features
|
|
|
|
}
|
|
|
|
|
|
|
|
func serverFixture() api.GitalyServer {
|
|
|
|
return api.GitalyServer{Address: "tcp://localhost:123"}
|
2020-12-02 10:09:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func allowedFeatures() map[string]string {
|
|
|
|
return map[string]string{
|
|
|
|
"gitaly-feature-foo": "bar",
|
|
|
|
"gitaly-feature-qux": "baz",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func badFeatureMetadata() map[string]string {
|
|
|
|
return map[string]string{
|
|
|
|
"bad-metadata-1": "bad-value-1",
|
|
|
|
"bad-metadata-2": "bad-value-2",
|
|
|
|
}
|
|
|
|
}
|