Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service/pkg/storage/suite_test.go
2501 views
1
// Copyright (c) 2022 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 storage_test
6
7
import (
8
"context"
9
"testing"
10
11
"github.com/gitpod-io/gitpod/content-service/pkg/storage"
12
)
13
14
type TestablePresignedAccess interface {
15
storage.PresignedAccess
16
17
ForTestCreateObj(ctx context.Context, bucket, path, content string) error
18
ForTestReset(ctx context.Context) error
19
}
20
21
func SuiteTestPresignedAccess(t *testing.T, ps TestablePresignedAccess) {
22
tests := []struct {
23
Name string
24
Test func(t *testing.T, ps TestablePresignedAccess)
25
}{
26
{
27
Name: "happy path",
28
Test: testPresignedHappyPath,
29
},
30
}
31
for _, test := range tests {
32
t.Run(test.Name, func(t *testing.T) {
33
test.Test(t, ps)
34
})
35
}
36
}
37
38
func testPresignedHappyPath(t *testing.T, ps TestablePresignedAccess) {
39
ctx := context.Background()
40
failOnErr(t, ps.ForTestReset(ctx))
41
42
const (
43
bucket = "test-bucket"
44
path = "foo/bar.txt"
45
)
46
47
failOnErr(t, ps.ForTestCreateObj(ctx, bucket, path, "hello world"))
48
49
tests := []struct {
50
Name string
51
Test func(t *testing.T, os TestablePresignedAccess)
52
}{
53
{
54
Name: "ObjectExists",
55
Test: func(t *testing.T, os TestablePresignedAccess) {
56
exists, err := ps.ObjectExists(ctx, bucket, path)
57
failOnErr(t, err)
58
if !exists {
59
t.Errorf("expected %s/%s to exist - it did not", bucket, path)
60
}
61
},
62
},
63
{
64
Name: "ObjectHash",
65
Test: func(t *testing.T, os TestablePresignedAccess) {
66
67
hash, err := ps.ObjectHash(ctx, bucket, path)
68
failOnErr(t, err)
69
if hash == "" {
70
t.Errorf("expected non-empty object hash")
71
}
72
},
73
},
74
{
75
Name: "DiskUsage",
76
Test: func(t *testing.T, os TestablePresignedAccess) {
77
size, err := ps.DiskUsage(ctx, bucket, path)
78
failOnErr(t, err)
79
if size == 0 {
80
t.Errorf("expected non-zero disk usage")
81
}
82
},
83
},
84
{
85
Name: "SignDownload",
86
Test: func(t *testing.T, os TestablePresignedAccess) {
87
nfo, err := ps.SignDownload(ctx, bucket, path, &storage.SignedURLOptions{})
88
failOnErr(t, err)
89
switch {
90
case nfo == nil:
91
t.Errorf("expected non-nil download info")
92
case nfo.URL == "":
93
t.Errorf("expected non-empty download URL")
94
case nfo.Size == 0:
95
t.Errorf("expected non-zero object size")
96
}
97
},
98
},
99
{
100
Name: "SignUpload",
101
Test: func(t *testing.T, os TestablePresignedAccess) {
102
nfo, err := ps.SignUpload(ctx, bucket, path, &storage.SignedURLOptions{})
103
failOnErr(t, err)
104
switch {
105
case nfo == nil:
106
t.Errorf("expected non-nil download info")
107
case nfo.URL == "":
108
t.Errorf("expected non-empty download URL")
109
}
110
},
111
},
112
}
113
114
for _, test := range tests {
115
t.Run(test.Name, func(t *testing.T) {
116
test.Test(t, ps)
117
})
118
}
119
}
120
121
func failOnErr(t *testing.T, err error) {
122
if err != nil {
123
t.Fatal(err)
124
}
125
}
126
127