-
Notifications
You must be signed in to change notification settings - Fork 324
/
s3_test.go
140 lines (119 loc) · 3.47 KB
/
s3_test.go
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
//go:build s3
// +build s3
package main
import (
"context"
"net"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/aerokube/selenoid/event"
"github.com/aerokube/selenoid/session"
"github.com/aerokube/selenoid/upload"
assert "github.com/stretchr/testify/require"
)
var (
s3Srv *httptest.Server
)
func init() {
s3Srv = httptest.NewServer(s3Mux())
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}
http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
if strings.Contains(addr, "s3-mock.example.com") {
addr = s3Srv.Listener.Addr().String()
}
return dialer.DialContext(ctx, network, addr)
}
}
func s3Mux() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", func(_ http.ResponseWriter, _ *http.Request) {})
return mux
}
var testSession = &session.Session{
Quota: "some-user",
Caps: session.Caps{
Name: "internet explorer",
Version: "11",
Platform: "WINDOWS",
},
}
func TestS3Uploader(t *testing.T) {
uploader := &upload.S3Uploader{
Endpoint: "http://s3-mock.example.com",
Region: "us-west-1",
AccessKey: "some-access-key",
SecretKey: "some-secret-key",
BucketName: "test-bucket",
KeyPattern: "$fileName",
ReducedRedundancy: true,
}
uploader.Init()
f, _ := os.CreateTemp("", "some-file")
input := event.CreatedFile{
Event: event.Event{
RequestId: 4342,
SessionId: "some-session-id",
Session: testSession,
},
Name: f.Name(),
Type: "log",
}
uploaded, err := uploader.Upload(input)
assert.NoError(t, err)
assert.True(t, uploaded)
}
func TestGetKey(t *testing.T) {
const testPattern = "$quota/$sessionId_$browserName_$browserVersion_$platformName/$fileType$fileExtension"
input := event.CreatedFile{
Event: event.Event{
SessionId: "some-Session-id",
Session: testSession,
RequestId: 12345,
},
Name: "/path/to/Some-File.txt",
Type: "log",
}
key := upload.GetS3Key(testPattern, input)
assert.Equal(t, key, "some-user/some-Session-id_internet-explorer_11_windows/log.txt")
input.Session.Caps.Name = ""
input.Session.Caps.DeviceName = "internet explorer"
key = upload.GetS3Key(testPattern, input)
assert.Equal(t, key, "some-user/some-Session-id_internet-explorer_11_windows/log.txt")
input.Session.Caps.S3KeyPattern = "$quota/$fileType$fileExtension"
key = upload.GetS3Key(testPattern, input)
assert.Equal(t, key, "some-user/log.txt")
input.Session.Caps.S3KeyPattern = "$fileName"
key = upload.GetS3Key(testPattern, input)
assert.Equal(t, key, "Some-File.txt")
}
func TestFileMatches(t *testing.T) {
matches, err := upload.FileMatches("", "", "any-file-name")
assert.NoError(t, err)
assert.True(t, matches)
matches, err = upload.FileMatches("[", "", "/path/to/file.mp4")
assert.Error(t, err)
assert.False(t, matches)
matches, err = upload.FileMatches("", "[", "/path/to/file.mp4")
assert.Error(t, err)
assert.False(t, matches)
matches, err = upload.FileMatches("*.mp4", "", "/path/to/file.mp4")
assert.NoError(t, err)
assert.True(t, matches)
matches, err = upload.FileMatches("*.mp4", "", "/path/to/file.log")
assert.NoError(t, err)
assert.False(t, matches)
matches, err = upload.FileMatches("*.mp4", "", "/path/to/file.log")
assert.NoError(t, err)
assert.False(t, matches)
matches, err = upload.FileMatches("", "*.log", "/path/to/file.log")
assert.NoError(t, err)
assert.False(t, matches)
}