diff --git a/internal/gitaly/service/repository/info_attributes_test.go b/internal/gitaly/service/repository/info_attributes_test.go index bafb9f6c9f0b5edc40f28a55fd2a42cd83104eab..984906469be782f28b5414f9fc761f8bb3f8b48d 100644 --- a/internal/gitaly/service/repository/info_attributes_test.go +++ b/internal/gitaly/service/repository/info_attributes_test.go @@ -5,71 +5,52 @@ import ( "io/ioutil" "os" "path/filepath" - "testing" - "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/streamio" ) -func TestGetInfoAttributesExisting(t *testing.T) { - locator := config.NewLocator(config.Config) - serverSocketPath, stop := runRepoServer(t, locator) - defer stop() +func (suite *RepositoryServiceTestSuite) TestGetInfoAttributesExisting() { + require := suite.Require() - client, conn := newRepositoryClient(t, serverSocketPath) - defer conn.Close() - - testRepo, repoPath, cleanupFn := testhelper.NewTestRepo(t) - defer cleanupFn() - - infoPath := filepath.Join(repoPath, "info") + infoPath := filepath.Join(suite.repositoryPath, "info") os.MkdirAll(infoPath, 0755) buffSize := streamio.WriteBufferSize + 1 data := bytes.Repeat([]byte("*.pbxproj binary\n"), buffSize) attrsPath := filepath.Join(infoPath, "attributes") err := ioutil.WriteFile(attrsPath, data, 0644) - require.NoError(t, err) + require.NoError(err) - request := &gitalypb.GetInfoAttributesRequest{Repository: testRepo} + request := &gitalypb.GetInfoAttributesRequest{Repository: suite.repository} testCtx, cancelCtx := testhelper.Context() defer cancelCtx() - stream, err := client.GetInfoAttributes(testCtx, request) - require.NoError(t, err) + stream, err := suite.client.GetInfoAttributes(testCtx, request) + require.NoError(err) receivedData, err := ioutil.ReadAll(streamio.NewReader(func() ([]byte, error) { response, err := stream.Recv() return response.GetAttributes(), err })) - require.NoError(t, err) - require.Equal(t, data, receivedData) + require.NoError(err) + require.Equal(data, receivedData) } -func TestGetInfoAttributesNonExisting(t *testing.T) { - locator := config.NewLocator(config.Config) - serverSocketPath, stop := runRepoServer(t, locator) - defer stop() - - client, conn := newRepositoryClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := testhelper.NewTestRepo(t) - defer cleanupFn() +func (suite *RepositoryServiceTestSuite) TestGetInfoAttributesNonExisting() { + require := suite.Require() - request := &gitalypb.GetInfoAttributesRequest{Repository: testRepo} + request := &gitalypb.GetInfoAttributesRequest{Repository: suite.repository} testCtx, cancelCtx := testhelper.Context() defer cancelCtx() - response, err := client.GetInfoAttributes(testCtx, request) - require.NoError(t, err) + response, err := suite.client.GetInfoAttributes(testCtx, request) + require.NoError(err) message, err := response.Recv() - require.NoError(t, err) + require.NoError(err) - require.Empty(t, message.GetAttributes()) + require.Empty(message.GetAttributes()) } diff --git a/internal/gitaly/service/repository/suite_test.go b/internal/gitaly/service/repository/suite_test.go new file mode 100644 index 0000000000000000000000000000000000000000..0805b9d8f64176865c9b1a449dbe7c177fd2d9ca --- /dev/null +++ b/internal/gitaly/service/repository/suite_test.go @@ -0,0 +1,57 @@ +package repository + +import ( + "testing" + + "github.com/stretchr/testify/suite" + "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" + "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" + "google.golang.org/grpc" +) + +type RepositoryServiceTestSuite struct { + // This include a T() function to access *testing.T + suite.Suite + + repository *gitalypb.Repository + repositoryPath string + repoCleanupFn func() + + srv *testhelper.TestServer + + client gitalypb.RepositoryServiceClient + conn *grpc.ClientConn +} + +// Setup the repository service server and testing repository +// before each test +func (suite *RepositoryServiceTestSuite) SetupTest() { + testRepo, repoPath, cleanupFn := testhelper.NewTestRepo(suite.T()) + + suite.repository = testRepo + suite.repositoryPath = repoPath + suite.repoCleanupFn = cleanupFn + + suite.srv = buildTestingServer(suite.T(), config.Config, config.NewLocator(config.Config)) + + req := suite.Require() + req.NoError(suite.srv.Start()) + + cl, conn := newRepositoryClient(suite.T(), "unix://"+suite.srv.Socket()) + suite.client = cl + suite.conn = conn +} + +func (suite *RepositoryServiceTestSuite) TearDownTest() { + suite.conn.Close() + suite.srv.Stop() + + suite.repoCleanupFn() +} + +// In order for 'go test' to run this suite, we need to create +// a normal test function and pass our suite to suite.Run +func TestRepositoryService(t *testing.T) { + suite.Run(t, new(RepositoryServiceTestSuite)) +} diff --git a/internal/gitaly/service/repository/testhelper_test.go b/internal/gitaly/service/repository/testhelper_test.go index 9095225ff9d9288de5de742a0b4ad78f6fa23937..6b91fe4790ca0ea0ed2a46605378c4ae84932bcc 100644 --- a/internal/gitaly/service/repository/testhelper_test.go +++ b/internal/gitaly/service/repository/testhelper_test.go @@ -96,7 +96,7 @@ func newSecureRepoClient(t *testing.T, serverSocketPath string, pool *x509.CertP var NewSecureRepoClient = newSecureRepoClient -func runRepoServerWithConfig(t *testing.T, cfg config.Cfg, locator storage.Locator, opts ...testhelper.TestServerOpt) (string, func()) { +func buildTestingServer(t *testing.T, cfg config.Cfg, locator storage.Locator, opts ...testhelper.TestServerOpt) *testhelper.TestServer { streamInt := []grpc.StreamServerInterceptor{ mcache.StreamInvalidator(dcache.LeaseKeyer{}, protoregistry.GitalyProtoPreregistered), } @@ -109,6 +109,11 @@ func runRepoServerWithConfig(t *testing.T, cfg config.Cfg, locator storage.Locat gitalypb.RegisterRepositoryServiceServer(srv.GrpcServer(), NewServer(cfg, RubyServer, locator, config.Config.GitalyInternalSocketPath())) reflection.Register(srv.GrpcServer()) + return srv +} + +func runRepoServerWithConfig(t *testing.T, cfg config.Cfg, locator storage.Locator, opts ...testhelper.TestServerOpt) (string, func()) { + srv := buildTestingServer(t, cfg, locator, opts...) require.NoError(t, srv.Start()) return "unix://" + srv.Socket(), srv.Stop