-
Notifications
You must be signed in to change notification settings - Fork 1
/
transformer.go
153 lines (133 loc) · 4.24 KB
/
transformer.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
141
142
143
144
145
146
147
148
149
150
151
152
153
package transformer
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/samber/lo"
dockertesthelper "github.com/rudderlabs/rudder-go-kit/testhelper/docker"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/rudderlabs/rudder-go-kit/httputil"
"github.com/rudderlabs/rudder-go-kit/testhelper/docker/resource"
)
type Resource struct {
TransformerURL string
Port string
}
type config struct {
repository string
tag string
exposedPorts []string
envs []string
extraHosts []string
}
func (c *config) setBackendConfigURL(url string) {
c.envs = append(
lo.Filter(c.envs, func(s string, _ int) bool {
return !strings.HasPrefix(s, "CONFIG_BACKEND_URL=")
}),
"CONFIG_BACKEND_URL="+url)
}
// WithUserTransformations will mock BE config to set transformation for given transformation versionID to transformation function map
//
// - events with transformationVersionID not present in map will not be transformed and transformer will return 404 for those requests
//
// - WithUserTransformations should not be used with WithConfigBackendURL option
//
// - only javascript transformation functions are supported
//
// e.g.
//
// WithUserTransformations(map[string]string{
// "transform-version-id-1": `export function transformEvent(event, metadata) {
// event.transformed=true
// return event;
// }`,
// })
func WithUserTransformations(transformations map[string]string, cleaner resource.Cleaner) func(*config) {
return func(conf *config) {
backendConfigSvc := newTestBackendConfigServer(transformations)
conf.setBackendConfigURL(dockertesthelper.ToInternalDockerHost(backendConfigSvc.URL))
conf.extraHosts = append(conf.extraHosts, "host.docker.internal:host-gateway")
cleaner.Cleanup(func() {
backendConfigSvc.Close()
})
}
}
// WithConnectionToHostEnabled lets transformer container connect with the host machine
// i.e. transformer container will be able to access localhost of the host machine
func WithConnectionToHostEnabled() func(*config) {
return func(conf *config) {
conf.extraHosts = append(conf.extraHosts, "host.docker.internal:host-gateway")
}
}
// WithConfigBackendURL lets transformer use custom backend config server for transformations
// WithConfigBackendURL should not be used with WithUserTransformations option
func WithConfigBackendURL(url string) func(*config) {
return func(conf *config) {
conf.setBackendConfigURL(dockertesthelper.ToInternalDockerHost(url))
}
}
func WithDockerImageTag(tag string) func(*config) {
return func(conf *config) {
conf.tag = tag
}
}
func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...func(conf *config)) (*Resource, error) {
// Set Rudder Transformer
// pulls an image first to make sure we don't have an old cached version locally,
// then it creates a container based on it and runs it
conf := &config{
repository: "rudderstack/rudder-transformer",
tag: "latest",
exposedPorts: []string{"9090"},
envs: []string{
"CONFIG_BACKEND_URL=https://api.rudderstack.com",
},
}
for _, opt := range opts {
opt(conf)
}
err := pool.Client.PullImage(docker.PullImageOptions{
Repository: conf.repository,
Tag: conf.tag,
}, docker.AuthConfiguration{})
if err != nil {
return nil, fmt.Errorf("failed to pull image: %w", err)
}
transformerContainer, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: conf.repository,
Tag: conf.tag,
ExposedPorts: conf.exposedPorts,
Env: conf.envs,
ExtraHosts: conf.extraHosts,
})
if err != nil {
return nil, err
}
d.Cleanup(func() {
if err := pool.Purge(transformerContainer); err != nil {
d.Log("Could not purge resource:", err)
}
})
transformerResource := &Resource{
TransformerURL: fmt.Sprintf("http://localhost:%s", transformerContainer.GetPort("9090/tcp")),
Port: transformerContainer.GetPort("9090/tcp"),
}
err = pool.Retry(func() (err error) {
resp, err := http.Get(transformerResource.TransformerURL + "/health")
if err != nil {
return err
}
defer func() { httputil.CloseResponse(resp) }()
if resp.StatusCode != http.StatusOK {
return errors.New(resp.Status)
}
return nil
})
if err != nil {
return nil, err
}
return transformerResource, nil
}