-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathruntime_test.go
More file actions
148 lines (124 loc) · 4.23 KB
/
Copy pathruntime_test.go
File metadata and controls
148 lines (124 loc) · 4.23 KB
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
package runtime_test
import (
"context"
"encoding/base64"
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/sst/sst/v3/pkg/runtime"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type mockRuntime struct {
matchFn func(string) bool
}
func (m *mockRuntime) Match(r string) bool {
return m.matchFn(r)
}
func (m *mockRuntime) Build(ctx context.Context, input *runtime.BuildInput) (*runtime.BuildOutput, error) {
return nil, nil
}
func (m *mockRuntime) Run(ctx context.Context, input *runtime.RunInput) (runtime.Worker, error) {
return nil, nil
}
func (m *mockRuntime) ShouldRebuild(functionID string, path string) bool {
return false
}
func (m *mockRuntime) ShouldRunEagerly() bool {
return true
}
func TestBuildInputOut(t *testing.T) {
cfgPath := filepath.Join("/project", "sst.config.ts")
workingDir := filepath.Join("/project", ".sst")
t.Run("dev mode", func(t *testing.T) {
input := &runtime.BuildInput{
CfgPath: cfgPath,
Dev: true,
FunctionID: "myFunc",
}
expected := filepath.Join(workingDir, "artifacts", "myFunc-dev")
assert.Equal(t, expected, input.Out())
})
t.Run("prod mode", func(t *testing.T) {
input := &runtime.BuildInput{
CfgPath: cfgPath,
Dev: false,
FunctionID: "myFunc",
}
expected := filepath.Join(workingDir, "artifacts", "myFunc-src")
assert.Equal(t, expected, input.Out())
})
}
func TestCollectionRuntime(t *testing.T) {
t.Run("matching runtime found", func(t *testing.T) {
mr := &mockRuntime{matchFn: func(r string) bool { return r == "nodejs" }}
c := runtime.NewCollection("cfg", mr)
rt, ok := c.Runtime("nodejs")
require.True(t, ok)
assert.Equal(t, mr, rt)
})
t.Run("no match", func(t *testing.T) {
mr := &mockRuntime{matchFn: func(r string) bool { return false }}
c := runtime.NewCollection("cfg", mr)
_, ok := c.Runtime("python")
assert.False(t, ok)
})
t.Run("empty collection", func(t *testing.T) {
c := runtime.NewCollection("cfg")
_, ok := c.Runtime("anything")
assert.False(t, ok)
})
}
func TestCollectionBuildEncryptedResourceFileWithBundle(t *testing.T) {
// A 32-byte AES-256 key (all zeroes is fine for testing purposes).
encryptionKey := base64.StdEncoding.EncodeToString(make([]byte, 32))
t.Run("writes per-function subdirectory when bundle is set", func(t *testing.T) {
bundleDir := t.TempDir()
mr := &mockRuntime{matchFn: func(r string) bool { return r == "nodejs" }}
c := runtime.NewCollection("cfg", mr)
input := &runtime.BuildInput{
FunctionID: "my-function",
Handler: "index.handler",
Bundle: bundleDir,
Runtime: "nodejs",
EncryptionKey: encryptionKey,
Links: map[string]json.RawMessage{},
}
_, err := c.Build(context.Background(), input)
require.NoError(t, err)
// Per-function file lives under .sst/<FunctionID>/ so concurrent
// Build calls sharing the same bundle directory don't race, and the
// uploaded zip for each function can exclude every sibling's subtree.
perFunctionPath := filepath.Join(bundleDir, ".sst", "my-function", "resource.enc")
_, err = os.Stat(perFunctionPath)
assert.NoError(t, err, "expected %s to exist", perFunctionPath)
// Default top-level filename is reserved for the non-bundle
// (per-function artifact directory) path.
defaultPath := filepath.Join(bundleDir, "resource.enc")
_, err = os.Stat(defaultPath)
assert.True(t, os.IsNotExist(err), "default resource.enc should not exist when bundle is set")
})
t.Run("distinct functions sharing a bundle write to distinct subdirs", func(t *testing.T) {
bundleDir := t.TempDir()
mr := &mockRuntime{matchFn: func(r string) bool { return r == "nodejs" }}
c := runtime.NewCollection("cfg", mr)
for _, id := range []string{"fn-a", "fn-b"} {
input := &runtime.BuildInput{
FunctionID: id,
Handler: "index.handler",
Bundle: bundleDir,
Runtime: "nodejs",
EncryptionKey: encryptionKey,
Links: map[string]json.RawMessage{},
}
_, err := c.Build(context.Background(), input)
require.NoError(t, err)
}
for _, id := range []string{"fn-a", "fn-b"} {
p := filepath.Join(bundleDir, ".sst", id, "resource.enc")
_, err := os.Stat(p)
assert.NoError(t, err, "expected %s to exist", p)
}
})
}