1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package repository
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/internal/tempdir"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/streamio"
"google.golang.org/grpc/codes"
)
func TestSuccessfulCreateBundleRequest(t *testing.T) {
cfg, repo, repoPath, client := setupRepositoryService(t)
ctx, cancel := testhelper.Context()
defer cancel()
// Create a work tree with a HEAD pointing to a commit that is missing. CreateBundle should
// clean this up before creating the bundle.
sha := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("branch"))
require.NoError(t, os.MkdirAll(filepath.Join(repoPath, "gitlab-worktree"), 0755))
testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "worktree", "add", "gitlab-worktree/worktree1", sha.String())
require.NoError(t, os.Chtimes(filepath.Join(repoPath, "gitlab-worktree", "worktree1"), time.Now().Add(-7*time.Hour), time.Now().Add(-7*time.Hour)))
testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "branch", "-D", "branch")
require.NoError(t, os.Remove(filepath.Join(repoPath, "objects", sha.String()[0:2], sha.String()[2:])))
request := &gitalypb.CreateBundleRequest{Repository: repo}
c, err := client.CreateBundle(ctx, request)
require.NoError(t, err)
reader := streamio.NewReader(func() ([]byte, error) {
response, err := c.Recv()
return response.GetData(), err
})
dstDir, err := tempdir.New(ctx, repo, config.NewLocator(cfg))
require.NoError(t, err)
dstFile, err := ioutil.TempFile(dstDir, "")
require.NoError(t, err)
defer dstFile.Close()
defer os.RemoveAll(dstFile.Name())
_, err = io.Copy(dstFile, reader)
require.NoError(t, err)
output := testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "bundle", "verify", dstFile.Name())
// Extra sanity; running verify should fail on bad bundles
require.Contains(t, string(output), "The bundle records a complete history")
}
func TestFailedCreateBundleRequestDueToValidations(t *testing.T) {
_, client := setupRepositoryServiceWithoutRepo(t)
testCases := []struct {
desc string
request *gitalypb.CreateBundleRequest
code codes.Code
}{
{
desc: "empty repository",
request: &gitalypb.CreateBundleRequest{},
code: codes.InvalidArgument,
},
}
for _, testCase := range testCases {
t.Run(testCase.desc, func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
stream, err := client.CreateBundle(ctx, testCase.request)
require.NoError(t, err)
_, err = stream.Recv()
require.NotEqual(t, io.EOF, err)
testhelper.RequireGrpcError(t, err, testCase.code)
})
}
}
|