-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
135 lines (113 loc) · 3.4 KB
/
integration_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
package main_test
import (
"errors"
"fmt"
"github.com/cadence-workflow/starlark-worker/cadstar"
"github.com/cadence-workflow/starlark-worker/ext"
"github.com/cadence-workflow/starlark-worker/plugin"
"github.com/stretchr/testify/suite"
"go.starlark.net/starlark"
"go.uber.org/cadence"
"io/fs"
"log"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
)
type Suite struct {
suite.Suite
cadstar.StarTestSuite
httpHandler ext.HTTPTestHandler
server *httptest.Server
env *cadstar.StarTestEnvironment
}
func TestIT(t *testing.T) { suite.Run(t, new(Suite)) }
func (r *Suite) SetupSuite() {
r.httpHandler = ext.NewHTTPTestHandler(r.T())
r.server = httptest.NewServer(r.httpHandler)
}
func (r *Suite) SetupTest() {
r.env = r.NewEnvironment(r.T(), &cadstar.StarTestEnvironmentParams{
RootDirectory: ".",
Plugins: plugin.Registry,
})
}
func (r *Suite) TearDownTest() {
r.env.AssertExpectations(r.T())
}
func (r *Suite) TearDownSuite() {
r.server.Close()
}
func (r *Suite) TestAll() {
var testFiles []string
err := filepath.WalkDir("testdata", func(entryPath string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if !entry.IsDir() && strings.HasSuffix(entry.Name(), "_test.star") {
testFiles = append(testFiles, entryPath)
}
return nil
})
require := r.Require()
require.NoError(err)
require.True(len(testFiles) > 0, "no test files found")
for _, file := range testFiles {
r.runTestFile(file)
}
}
func (r *Suite) TestAtExit() {
// clean up test server resources if any
resources := r.httpHandler.GetResources()
for k := range resources {
delete(resources, k)
}
// run the test
r.runTestFunction("testdata/atexit_test.star", "injected_error_test", func() {
err := r.env.GetResult(nil)
require := r.Require()
require.Error(err)
var cadenceErr *cadence.CustomError
require.True(errors.As(err, &cadenceErr))
var details map[string]any
require.NoError(cadenceErr.Details(&details))
require.NotNil(details["error"])
require.IsType("", details["error"])
require.True(strings.Contains(details["error"].(string), "injected error"), "Unexpected error details:\n%s", details)
})
// make sure the test run did not leak any resources on the test server
r.Require().Equal(0, len(resources), "Test server contains leaked resources:\n%v", resources)
}
func (r *Suite) runTestFile(filePath string) {
testFunctions, err := r.env.GetTestFunctions(filePath)
require := r.Require()
require.NoError(err)
require.True(len(testFunctions) > 0, "no test functions found in %s", filePath)
for _, fn := range testFunctions {
r.runTestFunction(filePath, fn, func() {
var res any
if err := r.env.GetResult(&res); err != nil {
details := err.Error()
var customErr *cadence.CustomError
if errors.As(err, &customErr) && customErr.HasDetails() {
var d []byte
r.Require().NoError(customErr.Details(&d))
details = fmt.Sprintf("%s: %s", customErr.Reason(), d)
}
r.Require().Fail(details)
}
})
}
}
func (r *Suite) runTestFunction(filePath string, fn string, assert func()) {
r.Run(fmt.Sprintf("%s//%s", filePath, fn), func() {
r.SetupTest()
defer r.TearDownTest()
environ := starlark.NewDict(1)
r.Require().NoError(environ.SetKey(starlark.String("TEST_SERVER_URL"), starlark.String(r.server.URL)))
log.Printf("[t] environ: %s", environ.String())
r.env.ExecuteFunction(filePath, fn, nil, nil, environ)
assert()
})
}