Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service/pkg/storage/storage_test.go
2501 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 storage
6
7
import (
8
"errors"
9
"testing"
10
11
"golang.org/x/xerrors"
12
)
13
14
func TestBlobObjectName(t *testing.T) {
15
tests := []struct {
16
Name string
17
Input string
18
ExpectedBlobName string
19
ExpectedError error
20
}{
21
{
22
Name: "simple name",
23
Input: "my-simple-name",
24
ExpectedBlobName: "blobs/my-simple-name",
25
},
26
{
27
Name: "name with slash",
28
Input: "my-object-name/with-slash",
29
ExpectedBlobName: "blobs/my-object-name/with-slash",
30
},
31
{
32
Name: "name with whitespace",
33
Input: "name with whitespace",
34
ExpectedError: invalidNameError("name with whitespace"),
35
},
36
{
37
Name: "name with invalid char",
38
Input: "ä-is-invalid",
39
ExpectedError: invalidNameError("ä-is-invalid"),
40
},
41
}
42
43
for _, test := range tests {
44
t.Run(test.Name, func(t *testing.T) {
45
actualBlobName, err := blobObjectName(test.Input)
46
if actualBlobName != test.ExpectedBlobName {
47
t.Fatalf("unexpected object name: is '%s' but expected '%s'", actualBlobName, test.ExpectedBlobName)
48
}
49
if !equivalentError(err, test.ExpectedError) {
50
t.Fatalf("unexpected error: is '%v' but expected '%v'", err, test.ExpectedError)
51
}
52
})
53
}
54
}
55
56
func invalidNameError(name string) error {
57
return xerrors.Errorf(`blob name '%s' needs to match regex '^[a-zA-Z0-9._\-\/]+$'`, name)
58
}
59
60
func equivalentError(e1 error, e2 error) bool {
61
if e1 == e2 {
62
return true
63
}
64
if errors.Is(e1, e2) {
65
return true
66
}
67
if e1 == nil || e2 == nil {
68
return false
69
}
70
if e1.Error() == e2.Error() {
71
return true
72
}
73
return false
74
}
75
76