forked from sequinstream/sequin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyaml_controller_test.exs
More file actions
379 lines (337 loc) · 11.4 KB
/
yaml_controller_test.exs
File metadata and controls
379 lines (337 loc) · 11.4 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
defmodule SequinWeb.YamlControllerTest do
use SequinWeb.ConnCase, async: true
alias Sequin.Databases.PostgresDatabase
alias Sequin.Databases.Sequence
alias Sequin.Test.UnboxedRepo
alias Sequin.TestSupport.Models.Character
alias Sequin.TestSupport.ReplicationSlots
@moduletag :unboxed
@publication "characters_publication"
def replication_slot, do: ReplicationSlots.slot_name(__MODULE__)
setup :authenticated_conn
setup do
Application.put_env(:sequin, :self_hosted, true)
# Fast-forward the replication slot to the current WAL position
:ok = ReplicationSlots.reset_slot(UnboxedRepo, replication_slot())
:ok
end
describe "plan/2" do
test "returns planned resources for valid yaml", %{conn: conn} do
yaml = """
users:
- email: "admin@sequinstream.com"
password: "sequinpassword!"
databases:
- name: "test-db"
username: "postgres"
password: "postgres"
hostname: "localhost"
port: 5432
database: "sequin_test"
slot_name: "#{replication_slot()}"
publication_name: "#{@publication}"
pool_size: 10
tables:
- table_name: "Characters"
table_schema: "public"
"""
conn = post(conn, ~p"/api/config/plan", %{yaml: yaml})
assert %{
"changes" => [
%{
"action" => "create",
"resource_type" => "user",
"new" => %{
"email" => "admin@sequinstream.com",
"id" => user_id
},
"old" => nil
},
%{
"action" => "create",
"resource_type" => "database",
"new" => %{
"database" => "sequin_test",
"hostname" => "localhost",
"ipv6" => false,
"name" => "test-db",
"password" => "********",
"pool_size" => 10,
"port" => 5432,
"ssl" => false,
"use_local_tunnel" => false,
"username" => "postgres",
"id" => postgres_database_id
},
"old" => nil
},
%{
"action" => "update",
"new" => %{
"id" => account_id,
"name" => account_name
},
"old" => %{
"id" => account_id,
"name" => account_name
},
"resource_type" => "account"
}
]
} = json_response(conn, 200)
assert Sequin.String.is_uuid?(account_id)
assert Sequin.String.is_uuid?(user_id)
assert Sequin.String.is_uuid?(postgres_database_id)
end
test "returns error for invalid yaml", %{conn: conn} do
yaml = """
databases:
- name: "test-db"
"""
conn = post(conn, ~p"/api/config/plan", %{yaml: yaml})
assert json_response(conn, 400) == %{
"summary" => "Error creating database 'test-db': - database: can't be blank"
}
end
test "successfully plans configuration with wider set of fields", %{conn: conn} do
yaml = """
change_retentions:
- name: test_retention
filters: []
destination_database: sequin_test
source_database: sequin_test
actions:
- insert
- update
- delete
source_table_name: #{Character.table_name()}
source_table_schema: public
destination_table_name: sequin_events
destination_table_schema: public
databases:
- name: sequin_test
port: 5432
ssl: false
ipv6: false
hostname: localhost
pool_size: 10
username: postgres
password: '********'
database: sequin_test
slot_name: sequin_slot
use_local_tunnel: false
publication_name: characters_publication
http_endpoints:
- name: test_http_endpoint
url: http://localhost:4000/something
headers: {}
sinks:
- name: accounts_sink
status: active
table: public.#{Character.table_name()}
filters: []
destination:
port: 4222
type: nats
host: localhost
tls: false
database: sequin_test
transform: record-transform
active_backfill:
batch_size: 1
load_shedding_policy: pause_on_full
max_retry_count:
timestamp_format: iso8601
actions:
- insert
- update
- delete
group_column_names:
- id
transforms:
- name: record-transform
type: path
path: record
description: Extracts just the record from the Sequin message shape.
"""
conn = post(conn, ~p"/api/config/plan", %{yaml: yaml})
response = json_response(conn, 200)
assert %{"changes" => changes} = response
assert is_list(changes)
end
end
describe "apply/2" do
test "successfully applies valid yaml configuration", %{conn: conn} do
yaml = """
users:
- email: "admin@sequinstream.com"
password: "sequinpassword!"
databases:
- name: "test-db"
username: "postgres"
password: "postgres"
hostname: "localhost"
port: 5432
database: "sequin_test"
slot_name: "#{replication_slot()}"
publication_name: "#{@publication}"
pool_size: 10
change_retentions:
- name: "characters"
source_database: "test-db"
source_table_schema: "public"
source_table_name: "Characters"
destination_database: "test-db"
destination_table_schema: "public"
destination_table_name: "Characters"
"""
conn = post(conn, ~p"/api/config/apply", %{yaml: yaml})
assert %{
"resources" => [
%{
"id" => account_id,
"name" => account_name,
"inserted_at" => _,
"updated_at" => _
},
%{
"auth_provider" => "identity",
"email" => "admin@sequinstream.com",
"id" => user_id,
"auth_provider_id" => nil,
"inserted_at" => _,
"name" => nil,
"updated_at" => _
},
%{
"database" => "sequin_test",
"hostname" => "localhost",
"id" => database_id,
"name" => "test-db",
"ipv6" => false,
"password" => "postgres",
"pool_size" => 10,
"port" => 5432,
"ssl" => false,
"use_local_tunnel" => false,
"username" => "postgres"
},
%{
"destination_database_id" => database_id,
"destination_oid" => _,
"id" => wal_pipeline_id,
"name" => "characters",
"seq" => _,
"source_tables" => [
%{
"actions" => ["insert", "update", "delete"],
"column_filters" => [],
"group_column_attnums" => nil,
"oid" => _,
"schema_name" => nil,
"table_name" => nil
}
],
"status" => "active"
}
]
} = json_response(conn, 200)
assert is_binary(account_name)
assert Sequin.String.is_uuid?(account_id)
assert Sequin.String.is_uuid?(user_id)
assert Sequin.String.is_uuid?(database_id)
assert Sequin.String.is_uuid?(wal_pipeline_id)
end
test "returns error for invalid yaml", %{conn: conn} do
yaml = """
databases:
- name: "test-db"
"""
conn = post(conn, ~p"/api/config/apply", %{yaml: yaml})
assert json_response(conn, 400) == %{
"summary" => "Error creating database 'test-db': - database: can't be blank"
}
end
end
describe "export/2" do
test "returns yaml representation of existing resources", %{conn: conn} do
# First apply some configuration
yaml = """
users:
- email: "admin@sequinstream.com"
password: "sequinpassword!"
databases:
- name: "test-db"
username: "postgres"
password: "postgres"
hostname: "localhost"
port: 5432
database: "sequin_test"
slot_name: "#{replication_slot()}"
publication_name: "#{@publication}"
pool_size: 10
tables:
- table_name: "Characters"
table_schema: "public"
http_endpoints:
- name: "sequin-playground-webhook"
url: "https://example.com/webhook"
sinks:
- name: "sequin-playground-webhook"
database: "test-db"
table: "Characters"
destination:
type: "webhook"
http_endpoint: "sequin-playground-webhook"
"""
# Apply the configuration first
assert conn |> post(~p"/api/config/apply", %{yaml: yaml}) |> json_response(200)
# Now test the export endpoint
conn = get(conn, ~p"/api/config/export")
assert %{"yaml" => exported_yaml} = json_response(conn, 200)
[_database] = Repo.all(PostgresDatabase)
[sequence] = Repo.all(Sequence)
assert sequence.name == "test-db.public.Characters"
# Parse the exported YAML to verify its structure
parsed_yaml = YamlElixir.read_from_string!(exported_yaml)
assert get_in(parsed_yaml, ["databases", Access.at(0)]) == %{
"database" => "sequin_test",
"hostname" => "localhost",
"name" => "test-db",
"password" => "********",
"pool_size" => 10,
"port" => 5432,
"publication_name" => @publication,
"slot_name" => replication_slot(),
"ssl" => false,
"ipv6" => false,
"use_local_tunnel" => false,
"username" => "postgres"
}
assert %{
"name" => "sequin-playground-webhook",
"url" => "https://example.com/webhook"
} = get_in(parsed_yaml, ["http_endpoints", Access.at(0)])
assert %{
"name" => "sequin-playground-webhook",
"database" => "test-db",
"table" => "public.Characters",
"destination" => %{
"type" => "webhook",
"http_endpoint" => "sequin-playground-webhook"
}
} = get_in(parsed_yaml, ["sinks", Access.at(0)])
end
end
describe "apply_from_yml!/1" do
test "returns error for invalid yaml", %{conn: conn} do
yaml = """
---
- -
databases:
- name: "test-db"
"""
assert conn |> post(~p"/api/config/apply", %{yaml: yaml}) |> json_response(400)
end
end
end