Welcome to mirror list, hosted at ThFree Co, Russian Federation.

find_file_test.go « wiki « service « gitaly « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: effa48ab4d724ff3bb7d9d2dbe2b92fa75d95d7f (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package wiki

import (
	"fmt"
	"io"
	"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/gitaly/rubyserver"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper"
	"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
	"google.golang.org/grpc/codes"
)

func testSuccessfulWikiFindFileRequest(t *testing.T, cfg config.Cfg, rubySrv *rubyserver.Server) {
	_, wikiRepoPath, cleanupFunc := setupWikiRepo(t, cfg)
	defer cleanupFunc()

	client := setupWikiService(t, cfg, rubySrv)

	committerName := "Scrooge McDuck"
	committerEmail := "scrooge@mcduck.com"
	sandboxWikiPath := filepath.Join(cfg.Storages[0].Path, "find-file-sandbox")

	testhelper.MustRunCommand(t, nil, "git", "clone", wikiRepoPath, sandboxWikiPath)
	defer os.RemoveAll(sandboxWikiPath)

	sandboxWiki := &gitalypb.Repository{
		StorageName:  "default",
		RelativePath: "find-file-sandbox/.git",
	}

	content, err := ioutil.ReadFile("testdata/clouds.png")
	require.NoError(t, err)

	err = ioutil.WriteFile(filepath.Join(sandboxWikiPath, "cloúds.png"), content, 0644)
	require.NoError(t, err)

	err = ioutil.WriteFile(filepath.Join(sandboxWikiPath, "no_content.png"), nil, 0644)
	require.NoError(t, err)

	// Sandbox wiki is empty, so we create a commit to be used later
	testhelper.MustRunCommand(t, nil, "git", "-C", sandboxWikiPath,
		"-c", fmt.Sprintf("user.name=%s", committerName),
		"-c", fmt.Sprintf("user.email=%s", committerEmail),
		"commit", "--allow-empty", "-m", "Adding an empty commit")
	oldHeadID := testhelper.MustRunCommand(t, nil, "git", "-C", sandboxWikiPath, "show", "--format=format:%H", "--no-patch", "HEAD")

	testhelper.MustRunCommand(t, nil, "git", "-C", sandboxWikiPath, "add", ".")
	testhelper.MustRunCommand(t, nil, "git", "-C", sandboxWikiPath,
		"-c", fmt.Sprintf("user.name=%s", committerName),
		"-c", fmt.Sprintf("user.email=%s", committerEmail),
		"commit", "-m", "Adding an image")

	newHeadID := testhelper.MustRunCommand(t, nil, "git", "-C", sandboxWikiPath, "show", "--format=format:%H", "--no-patch", "HEAD")

	response := &gitalypb.WikiFindFileResponse{
		Name:     []byte("cloúds.png"),
		MimeType: "image/png",
		Path:     []byte("cloúds.png"),
	}

	testCases := []struct {
		desc            string
		request         *gitalypb.WikiFindFileRequest
		response        *gitalypb.WikiFindFileResponse
		expectedContent []byte
	}{
		{
			desc: "name only",
			request: &gitalypb.WikiFindFileRequest{
				Repository: sandboxWiki,
				Name:       []byte("cloúds.png"),
			},
			response:        response,
			expectedContent: content,
		},
		{
			desc: "name + revision that includes the file",
			request: &gitalypb.WikiFindFileRequest{
				Repository: sandboxWiki,
				Name:       []byte("cloúds.png"),
				Revision:   newHeadID,
			},
			response:        response,
			expectedContent: content,
		},
		{
			desc: "name + revision that does not include the file",
			request: &gitalypb.WikiFindFileRequest{
				Repository: sandboxWiki,
				Name:       []byte("cloúds.png"),
				Revision:   oldHeadID,
			},
			response:        &gitalypb.WikiFindFileResponse{},
			expectedContent: content,
		},
		{
			desc: "non-existent name",
			request: &gitalypb.WikiFindFileRequest{
				Repository: sandboxWiki,
				Name:       []byte("moar-clouds.png"),
			},
			response:        &gitalypb.WikiFindFileResponse{},
			expectedContent: content,
		},
		{
			desc: "file with no content",
			request: &gitalypb.WikiFindFileRequest{
				Repository: sandboxWiki,
				Name:       []byte("no_content.png"),
			},
			response: &gitalypb.WikiFindFileResponse{
				Name:     []byte("no_content.png"),
				MimeType: "image/png",
				Path:     []byte("no_content.png"),
			},
			expectedContent: nil,
		},
	}

	for _, testCase := range testCases {
		t.Run(testCase.desc, func(t *testing.T) {
			ctx, cancel := testhelper.Context()
			defer cancel()

			c, err := client.WikiFindFile(ctx, testCase.request)
			require.NoError(t, err)

			expectedResponse := testCase.response
			receivedResponse := readFullResponseFromWikiFindFileClient(t, c)

			// require.Equal doesn't display a proper diff when either expected/actual has a field
			// with large data (RawData in our case), so we compare file attributes and content separately.
			receivedContent := receivedResponse.GetRawData()
			if receivedResponse != nil {
				receivedResponse.RawData = nil
			}

			testhelper.ProtoEqual(t, expectedResponse, receivedResponse)
			if len(expectedResponse.Name) > 0 {
				require.Equal(t, testCase.expectedContent, receivedContent, "mismatched content")
			}
		})
	}
}

func testFailedWikiFindFileDueToValidation(t *testing.T, cfg config.Cfg, rubySrv *rubyserver.Server) {
	wikiRepo, _, cleanupFunc := setupWikiRepo(t, cfg)
	defer cleanupFunc()

	client := setupWikiService(t, cfg, rubySrv)

	testCases := []struct {
		desc     string
		name     string
		revision string
		code     codes.Code
	}{
		{
			desc:     "empty file path",
			name:     "",
			revision: "master",
			code:     codes.InvalidArgument,
		},
		{
			desc:     "invalid revision",
			name:     "image.jpg",
			revision: "deadfacedeadfacedeadfacedeadfacedeadface",
			code:     codes.Unknown,
		},
		{
			desc:     "dangerously invalid revision",
			name:     "image.jpg",
			revision: "--output=/meow",
			code:     codes.InvalidArgument,
		},
	}

	for _, testCase := range testCases {
		t.Run(testCase.desc, func(t *testing.T) {
			ctx, cancel := testhelper.Context()
			defer cancel()

			request := &gitalypb.WikiFindFileRequest{
				Repository: wikiRepo,
				Name:       []byte(testCase.name),
				Revision:   []byte(testCase.revision),
			}

			c, err := client.WikiFindFile(ctx, request)
			require.NoError(t, err)

			err = drainWikiFindFileResponse(c)
			testhelper.RequireGrpcError(t, err, testCase.code)
		})
	}
}

func drainWikiFindFileResponse(c gitalypb.WikiService_WikiFindFileClient) error {
	for {
		_, err := c.Recv()
		if err != nil {
			return err
		}
	}
}

func readFullResponseFromWikiFindFileClient(t *testing.T, c gitalypb.WikiService_WikiFindFileClient) (fullResponse *gitalypb.WikiFindFileResponse) {
	t.Helper()

	for {
		resp, err := c.Recv()
		if err == io.EOF {
			break
		} else if err != nil {
			t.Fatal(err)
		}

		if fullResponse == nil {
			fullResponse = resp
		} else {
			fullResponse.RawData = append(fullResponse.RawData, resp.GetRawData()...)
		}
	}

	return fullResponse
}