-
Notifications
You must be signed in to change notification settings - Fork 20
/
substation_test.go
237 lines (199 loc) · 6.05 KB
/
substation_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package substation_test
import (
"context"
"encoding/json"
"fmt"
"testing"
"github.com/brexhq/substation/v2"
"github.com/brexhq/substation/v2/config"
"github.com/brexhq/substation/v2/message"
"github.com/brexhq/substation/v2/transform"
)
func ExampleSubstation() {
// Substation applications rely on a context for cancellation and timeouts.
ctx := context.Background()
// Define a configuration. For native Substation applications, this is managed by Jsonnet.
//
// This example copies an object's value and prints the data to stdout.
conf := []byte(`
{
"transforms":[
{"type":"object_copy","settings":{"object":{"source_key":"a","target_key":"c"}}},
{"type":"send_stdout"}
]
}
`)
cfg := substation.Config{}
if err := json.Unmarshal(conf, &cfg); err != nil {
// Handle error.
panic(err)
}
// Create a new Substation instance.
sub, err := substation.New(ctx, cfg)
if err != nil {
// Handle error.
panic(err)
}
// Print the Substation configuration.
fmt.Println(sub)
// Substation instances process data defined as a Message. Messages can be processed
// individually or in groups. This example processes multiple messages as a group.
msg := []*message.Message{
// The first message is a data message. Only data messages are transformed.
message.New().SetData([]byte(`{"a":"b"}`)),
// The second message is a ctrl message. ctrl messages flush the pipeline.
message.New().AsControl(),
}
// Transform the group of messages. In this example, results are not used.
if _, err := sub.Transform(ctx, msg...); err != nil {
// Handle error.
panic(err)
}
// Output:
// {"transforms":[{"type":"object_copy","settings":{"object":{"source_key":"a","target_key":"c"}}},{"type":"send_stdout","settings":null}]}
// {"a":"b","c":"b"}
}
// Custom applications should embed the Substation configuration and
// add additional configuration options.
type customConfig struct {
substation.Config
Auth struct {
Username string `json:"username"`
// Please don't store passwords in configuration files, this is only an example!
Password string `json:"password"`
} `json:"auth"`
}
// String returns an example string representation of the custom configuration.
func (c customConfig) String() string {
return fmt.Sprintf("%s:%s", c.Auth.Username, c.Auth.Password)
}
func Example_substationCustomConfig() {
// Substation applications rely on a context for cancellation and timeouts.
ctx := context.Background()
// Define and load the custom configuration. This config includes a username
// and password for authentication.
conf := []byte(`
{
"transforms":[
{"type":"object_copy","settings":{"object":{"source_key":"a","target_key":"c"}}},
{"type":"send_stdout"}
],
"auth":{
"username":"foo",
"password":"bar"
}
}
`)
cfg := customConfig{}
if err := json.Unmarshal(conf, &cfg); err != nil {
// Handle error.
panic(err)
}
// Create a new Substation instance from the embedded configuration.
sub, err := substation.New(ctx, cfg.Config)
if err != nil {
// Handle error.
panic(err)
}
// Print the Substation configuration.
fmt.Println(sub)
// Print the custom configuration.
fmt.Println(cfg)
// Output:
// {"transforms":[{"type":"object_copy","settings":{"object":{"source_key":"a","target_key":"c"}}},{"type":"send_stdout","settings":null}]}
// foo:bar
}
func Example_substationCustomTransforms() {
// Substation applications rely on a context for cancellation and timeouts.
ctx := context.Background()
// Define and load the configuration. This config includes a transform that
// is not part of the standard Substation package.
conf := []byte(`
{
"transforms":[
{"type":"utility_duplicate"},
{"type":"send_stdout"}
]
}
`)
cfg := substation.Config{}
if err := json.Unmarshal(conf, &cfg); err != nil {
// Handle error.
panic(err)
}
// Create a new Substation instance with a custom transform factory for loading
// the custom transform.
sub, err := substation.New(ctx, cfg, substation.WithTransformFactory(customFactory))
if err != nil {
// Handle error.
panic(err)
}
msg := []*message.Message{
message.New().SetData([]byte(`{"a":"b"}`)),
message.New().AsControl(),
}
// Transform the group of messages. In this example, results are not used.
if _, err := sub.Transform(ctx, msg...); err != nil {
// Handle error.
panic(err)
}
// Output:
// {"a":"b"}
// {"a":"b"}
}
// customFactory is used in the custom transform example to load the custom transform.
func customFactory(ctx context.Context, cfg config.Config) (transform.Transformer, error) {
switch cfg.Type {
// Usually a custom transform requires configuration, but this
// is a toy example. Customizable transforms should have a new
// function that returns a new instance of the configured transform.
case "utility_duplicate":
return &utilityDuplicate{Count: 1}, nil
}
return transform.New(ctx, cfg)
}
// Duplicates a message.
type utilityDuplicate struct {
// Count is the number of times to duplicate the message.
Count int `json:"count"`
}
func (t *utilityDuplicate) Transform(ctx context.Context, msg *message.Message) ([]*message.Message, error) {
// Always return control messages.
if msg.HasFlag(message.IsControl) {
return []*message.Message{msg}, nil
}
output := []*message.Message{msg}
for i := 0; i < t.Count; i++ {
output = append(output, msg)
}
return output, nil
}
func FuzzTestSubstation(f *testing.F) {
testcases := [][]byte{
[]byte(`{"transforms":[{"type":"utility_duplicate"}]}`),
[]byte(`{"transforms":[{"type":"utility_duplicate", "count":2}]}`),
[]byte(`{"transforms":[{"type":"unknown_type"}]}`),
[]byte(`{"transforms":[{"type":"utility_duplicate", "count":"invalid"}]}`),
[]byte(``),
}
for _, tc := range testcases {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
var cfg substation.Config
err := json.Unmarshal(data, &cfg)
if err != nil {
return
}
sub, err := substation.New(ctx, cfg)
if err != nil {
return
}
msg := message.New().SetData(data)
_, err = sub.Transform(ctx, msg)
if err != nil {
return
}
})
}