Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service/pkg/initializer/download_test.go
2499 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package initializer
6
7
import (
8
"bytes"
9
"context"
10
"io"
11
"net/http"
12
"os"
13
"testing"
14
15
"github.com/gitpod-io/gitpod/content-service/api"
16
"github.com/opencontainers/go-digest"
17
)
18
19
// RoundTripFunc .
20
type RoundTripFunc func(req *http.Request) *http.Response
21
22
// RoundTrip .
23
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
24
return f(req), nil
25
}
26
27
func TestFileDownloadInitializer(t *testing.T) {
28
const defaultContent = "hello world"
29
30
type serverSideFile struct {
31
Path string
32
Content string
33
}
34
tests := []struct {
35
Name string
36
Files []fileInfo
37
ServerSide []serverSideFile
38
ExpectedError string
39
}{
40
{
41
Name: "happy path",
42
Files: []fileInfo{
43
{
44
URL: "/file1",
45
Path: "/level/file1",
46
Digest: digest.FromString(defaultContent),
47
},
48
// duplication is intentional
49
{
50
URL: "/file1",
51
Path: "/level/file1",
52
Digest: digest.FromString(defaultContent),
53
},
54
{
55
URL: "/file2",
56
Path: "/level/file2",
57
Digest: digest.FromString(defaultContent),
58
},
59
},
60
ServerSide: []serverSideFile{
61
{Path: "/file1", Content: defaultContent},
62
{Path: "/file2", Content: defaultContent},
63
},
64
},
65
{
66
Name: "digest mismatch",
67
Files: []fileInfo{
68
{
69
URL: "/file1",
70
Path: "/level/file1",
71
Digest: digest.FromString(defaultContent + "foobar"),
72
},
73
},
74
ServerSide: []serverSideFile{
75
{Path: "/file1", Content: defaultContent},
76
},
77
ExpectedError: "digest mismatch",
78
},
79
{
80
Name: "file not found",
81
Files: []fileInfo{
82
{
83
URL: "/file1",
84
Path: "/level/file1",
85
Digest: digest.FromString(defaultContent + "foobar"),
86
},
87
},
88
ExpectedError: "non-OK download response: Not Found",
89
},
90
}
91
92
for _, test := range tests {
93
t.Run(test.Name, func(t *testing.T) {
94
tmpdir, err := os.MkdirTemp("", "TestFileDownloadInitializer*")
95
if err != nil {
96
t.Fatal("cannot create tempdir", err)
97
}
98
defer os.RemoveAll(tmpdir)
99
100
client := &http.Client{
101
Transport: RoundTripFunc(func(req *http.Request) *http.Response {
102
for _, f := range test.ServerSide {
103
if f.Path != req.URL.Path {
104
continue
105
}
106
107
return &http.Response{
108
StatusCode: http.StatusOK,
109
Body: io.NopCloser(bytes.NewReader([]byte(f.Content))),
110
Header: make(http.Header),
111
}
112
}
113
114
return &http.Response{
115
Status: http.StatusText(http.StatusNotFound),
116
StatusCode: http.StatusNotFound,
117
Header: make(http.Header),
118
}
119
}),
120
}
121
122
req := &api.FileDownloadInitializer{}
123
for _, f := range test.Files {
124
req.Files = append(req.Files, &api.FileDownloadInitializer_FileInfo{
125
Url: "http://foobar" + f.URL,
126
FilePath: f.Path,
127
Digest: string(f.Digest),
128
})
129
}
130
131
initializer, err := newFileDownloadInitializer(tmpdir, req)
132
if err != nil {
133
t.Fatal(err)
134
}
135
initializer.HTTPClient = client
136
initializer.RetryTimeout = 0
137
138
src, _, err := initializer.Run(context.Background(), nil)
139
if err == nil && src != api.WorkspaceInitFromOther {
140
t.Errorf("initializer returned wrong content init source. expected %v, got %v", api.WorkspaceInitFromOther, src)
141
}
142
if err != nil && err.Error() != test.ExpectedError {
143
t.Fatalf("unexpected error: %v", err)
144
}
145
})
146
}
147
}
148
149