-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathenv_setup_helpers.go
More file actions
107 lines (83 loc) · 2.66 KB
/
env_setup_helpers.go
File metadata and controls
107 lines (83 loc) · 2.66 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package testdata
import (
"bytes"
"fmt"
"net"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
)
func CreateTestSocketPair(t *testing.T) (fd [2]int) {
domain, socketType := syscall.AF_UNIX, syscall.SOCK_DGRAM
fds, err := syscall.Socketpair(domain, socketType, 0)
if err != nil {
t.Error("Could not create socketpair for testing: ", err)
}
return fds
}
func CreateTestLogFile(t *testing.T) *os.File {
file, err := os.CreateTemp(os.TempDir(), "rapid-unit-tests")
assert.NoError(t, err, "error opening tmp log file for test")
return file
}
type TestSocketsRapid struct {
CtrlFd int
CnslFd int
}
type TestSocketsSlicer struct {
CtrlSock net.Conn
CnslSock net.Conn
CtrlFd int
CnslFd int
}
func SetupTestSockets(t *testing.T) (TestSocketsRapid, TestSocketsSlicer) {
ctrlFds := CreateTestSocketPair(t)
testCtrlFd := os.NewFile(uintptr(ctrlFds[0]), "ctrlParent")
cnslFds := CreateTestSocketPair(t)
testCnslFd := os.NewFile(uintptr(cnslFds[0]), "cnslParent")
ctrlSock, err := net.FileConn(testCtrlFd)
assert.NoError(t, err, "failed to setup test socket")
cnslSock, err := net.FileConn(testCnslFd)
assert.NoError(t, err, "failed to setup test socket")
rapidSockets := TestSocketsRapid{ctrlFds[1], cnslFds[1]}
slicerSockets := TestSocketsSlicer{ctrlSock, cnslSock, ctrlFds[0], cnslFds[0]}
return rapidSockets, slicerSockets
}
func SetupTestXRayUDPSocket(t *testing.T) net.PacketConn {
pc, err := net.ListenPacket("udp", "localhost:0")
assert.NoError(t, err, "failed to create udp listener for testing")
return pc
}
func setTestDependenciesBinPath(t *testing.T) {
var testDepsPath bytes.Buffer
brazilPathCmd := exec.Command("brazil-path", "testrun.runtimefarm")
brazilPathCmd.Stdout = &testDepsPath
err := brazilPathCmd.Run()
if err != nil {
assert.Fail(t, "Could not run brazil-path to setup $PATH for test runtime")
}
testDepsBinPath := fmt.Sprintf("%s/bin", testDepsPath.String())
err = os.Setenv("PATH", fmt.Sprintf("%s:%s", testDepsBinPath, os.Getenv("PATH")))
if err != nil {
assert.Fail(t, "Could not run brazil-path to setup $PATH for test runtime")
}
return
}
func SetupTestRuntime(t *testing.T, bootstrapScriptName string) (string, string) {
_, b, _, _ := runtime.Caller(0)
base := filepath.Dir(b)
resourcesDir := path.Join(base, "../testdata")
bootstrap := path.Join(resourcesDir, bootstrapScriptName)
taskRoot := filepath.Dir(bootstrap)
setTestDependenciesBinPath(t)
err := os.Setenv("LAMBDA_TASK_ROOT", taskRoot)
assert.NoError(t, err)
return bootstrap, taskRoot
}