forked from smartcontractkit/chainlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs_commands_test.go
457 lines (385 loc) · 13 KB
/
jobs_commands_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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package cmd_test
import (
"bytes"
_ "embed"
"flag"
"fmt"
"testing"
"time"
"github.com/google/uuid"
"github.com/hashicorp/consul/sdk/freeport"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/cli"
commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink/v2/core/cmd"
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
func TestJobPresenter_RenderTable(t *testing.T) {
t.Parallel()
var (
id = "1"
name = "Job 1"
jobSpecType = "fluxmonitor"
schemaVersion = uint32(1)
maxTaskDuration = models.Interval(1 * time.Second)
createdAt = time.Now()
updatedAt = time.Now().Add(time.Second)
buffer = bytes.NewBufferString("")
r = cmd.RendererTable{Writer: buffer}
)
p := cmd.JobPresenter{
JobResource: presenters.JobResource{
JAID: presenters.NewJAID(id),
Name: name,
Type: presenters.JobSpecType(jobSpecType),
SchemaVersion: schemaVersion,
MaxTaskDuration: maxTaskDuration,
DirectRequestSpec: nil,
FluxMonitorSpec: &presenters.FluxMonitorSpec{
CreatedAt: createdAt,
UpdatedAt: updatedAt,
},
OffChainReportingSpec: nil,
KeeperSpec: nil,
PipelineSpec: presenters.PipelineSpec{
ID: 1,
DotDAGSource: "ds1 [type=http method=GET url=\"example.com\" allowunrestrictednetworkaccess=\"true\"];\n ds1_parse [type=jsonparse path=\"USD\"];\n ds1_multiply [type=multiply times=100];\n ds1 -\u003e ds1_parse -\u003e ds1_multiply;\n",
},
},
}
// Render a single resource
require.NoError(t, p.RenderTable(r))
output := buffer.String()
assert.Contains(t, output, id)
assert.Contains(t, output, name)
assert.Contains(t, output, jobSpecType)
assert.Contains(t, output, "ds1 http")
assert.Contains(t, output, "ds1_parse jsonparse")
assert.Contains(t, output, "ds1_multiply multiply")
assert.Contains(t, output, createdAt.Format(time.RFC3339))
// Render many resources
buffer.Reset()
ps := cmd.JobPresenters{p}
require.NoError(t, ps.RenderTable(r))
output = buffer.String()
assert.Contains(t, output, id)
assert.Contains(t, output, name)
assert.Contains(t, output, jobSpecType)
assert.Contains(t, output, "ds1 http")
assert.Contains(t, output, "ds1_parse jsonparse")
assert.Contains(t, output, "ds1_multiply multiply")
assert.Contains(t, output, createdAt.Format(time.RFC3339))
}
func TestJobRenderer_GetTasks(t *testing.T) {
t.Parallel()
r := &cmd.JobPresenter{}
t.Run("gets the tasks from the DAG in reverse order", func(t *testing.T) {
r.PipelineSpec = presenters.PipelineSpec{
DotDAGSource: "ds1 [type=http method=GET url=\"example.com\" allowunrestrictednetworkaccess=\"true\"];\n ds1_parse [type=jsonparse path=\"USD\"];\n ds1_multiply [type=multiply times=100];\n ds1 -\u003e ds1_parse -\u003e ds1_multiply;\n",
}
tasks, err := r.GetTasks()
assert.NoError(t, err)
assert.Equal(t, []string{
"ds1 http",
"ds1_parse jsonparse",
"ds1_multiply multiply",
}, tasks)
})
t.Run("parse error", func(t *testing.T) {
r.PipelineSpec = presenters.PipelineSpec{
DotDAGSource: "invalid dot",
}
tasks, err := r.GetTasks()
assert.Error(t, err)
assert.Nil(t, tasks)
})
}
func TestJob_FriendlyTasks(t *testing.T) {
t.Parallel()
r := &cmd.JobPresenter{}
t.Run("gets the tasks in a printable format", func(t *testing.T) {
r.PipelineSpec = presenters.PipelineSpec{
DotDAGSource: " ds1 [type=http method=GET url=\"example.com\" allowunrestrictednetworkaccess=\"true\"];\n ds1_parse [type=jsonparse path=\"USD\"];\n ds1_multiply [type=multiply times=100];\n ds1 -\u003e ds1_parse -\u003e ds1_multiply;\n",
}
assert.Equal(t, []string{
"ds1 http",
"ds1_parse jsonparse",
"ds1_multiply multiply",
}, r.FriendlyTasks())
})
t.Run("parse error", func(t *testing.T) {
r.PipelineSpec = presenters.PipelineSpec{
DotDAGSource: "invalid dot",
}
assert.Equal(t, []string{"error parsing DAG"}, r.FriendlyTasks())
})
}
func TestJob_FriendlyCreatedAt(t *testing.T) {
t.Parallel()
now := time.Now()
testCases := []struct {
name string
job *cmd.JobPresenter
result string
}{
{
"gets the direct request spec created at timestamp",
&cmd.JobPresenter{
JobResource: presenters.JobResource{
Type: presenters.DirectRequestJobSpec,
DirectRequestSpec: &presenters.DirectRequestSpec{
CreatedAt: now,
},
},
},
now.Format(time.RFC3339),
},
{
"gets the flux monitor spec created at timestamp",
&cmd.JobPresenter{
JobResource: presenters.JobResource{
Type: presenters.FluxMonitorJobSpec,
FluxMonitorSpec: &presenters.FluxMonitorSpec{
CreatedAt: now,
},
},
},
now.Format(time.RFC3339),
},
{
"gets the cron spec created at timestamp",
&cmd.JobPresenter{
JobResource: presenters.JobResource{
Type: presenters.CronJobSpec,
CronSpec: &presenters.CronSpec{
CreatedAt: now,
},
},
},
now.Format(time.RFC3339),
},
{
"gets the vrf spec created at timestamp",
&cmd.JobPresenter{
JobResource: presenters.JobResource{
Type: presenters.VRFJobSpec,
VRFSpec: &presenters.VRFSpec{
CreatedAt: now,
},
},
},
now.Format(time.RFC3339),
},
{
"gets the off chain reporting spec created at timestamp",
&cmd.JobPresenter{
JobResource: presenters.JobResource{
Type: presenters.OffChainReportingJobSpec,
OffChainReportingSpec: &presenters.OffChainReportingSpec{
CreatedAt: now,
},
},
},
now.Format(time.RFC3339),
},
{
"gets the blockhash store spec created at timestamp",
&cmd.JobPresenter{
JobResource: presenters.JobResource{
Type: presenters.BlockhashStoreJobSpec,
BlockhashStoreSpec: &presenters.BlockhashStoreSpec{
CreatedAt: now,
},
},
},
now.Format(time.RFC3339),
},
{
"gets the blockheaderfeeder spec created at timestamp",
&cmd.JobPresenter{
JobResource: presenters.JobResource{
Type: presenters.BlockHeaderFeederJobSpec,
BlockHeaderFeederSpec: &presenters.BlockHeaderFeederSpec{
CreatedAt: now,
},
},
},
now.Format(time.RFC3339),
},
{
"invalid type",
&cmd.JobPresenter{
JobResource: presenters.JobResource{
Type: "invalid type",
},
},
"unknown",
},
{
"no spec exists",
&cmd.JobPresenter{
JobResource: presenters.JobResource{
Type: presenters.DirectRequestJobSpec,
},
},
"N/A",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.result, tc.job.FriendlyCreatedAt())
})
}
}
func TestJob_ToRows(t *testing.T) {
t.Parallel()
now := time.Now()
job := &cmd.JobPresenter{
JAID: cmd.NewJAID("1"),
JobResource: presenters.JobResource{
Name: "Test Job",
Type: presenters.DirectRequestJobSpec,
DirectRequestSpec: &presenters.DirectRequestSpec{
CreatedAt: now,
},
PipelineSpec: presenters.PipelineSpec{
DotDAGSource: " ds1 [type=http method=GET url=\"example.com\" allowunrestrictednetworkaccess=\"true\"];\n ds1_parse [type=jsonparse path=\"USD\"];\n ds1_multiply [type=multiply times=100];\n ds1 -\u003e ds1_parse -\u003e ds1_multiply;\n",
},
},
}
assert.Equal(t, [][]string{
{"1", "Test Job", "directrequest", "ds1 http", now.Format(time.RFC3339)},
{"1", "Test Job", "directrequest", "ds1_parse jsonparse", now.Format(time.RFC3339)},
{"1", "Test Job", "directrequest", "ds1_multiply multiply", now.Format(time.RFC3339)},
}, job.ToRows())
// Produce a single row even if there is not DAG
job.PipelineSpec.DotDAGSource = ""
assert.Equal(t, [][]string{
{"1", "Test Job", "directrequest", "", now.Format(time.RFC3339)},
}, job.ToRows())
}
//go:embed direct-request-spec-template.yml
var directRequestSpecTemplate string
func getDirectRequestSpec() string {
return fmt.Sprintf(directRequestSpecTemplate, uuid.New(), uuid.New())
}
func TestShell_ListFindJobs(t *testing.T) {
t.Parallel()
app := startNewApplicationV2(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].Enabled = ptr(true)
})
client, r := app.NewShellAndRenderer()
// Create the job
fs := flag.NewFlagSet("", flag.ExitOnError)
flagSetApplyFromAction(client.CreateJob, fs, "")
require.NoError(t, fs.Parse([]string{getDirectRequestSpec()}))
err := client.CreateJob(cli.NewContext(nil, fs, nil))
require.NoError(t, err)
require.Len(t, r.Renders, 1)
createOutput, ok := r.Renders[0].(*cmd.JobPresenter)
require.True(t, ok, "Expected Renders[0] to be *cmd.JobPresenter, got %T", r.Renders[0])
require.Nil(t, client.ListJobs(cltest.EmptyCLIContext()))
jobs := *r.Renders[1].(*cmd.JobPresenters)
require.Equal(t, 1, len(jobs))
assert.Equal(t, createOutput.ID, jobs[0].ID)
}
func TestShell_ShowJob(t *testing.T) {
t.Parallel()
app := startNewApplicationV2(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].Enabled = ptr(true)
})
client, r := app.NewShellAndRenderer()
// Create the job
fs := flag.NewFlagSet("", flag.ExitOnError)
flagSetApplyFromAction(client.CreateJob, fs, "")
require.NoError(t, fs.Parse([]string{getDirectRequestSpec()}))
err := client.CreateJob(cli.NewContext(nil, fs, nil))
require.NoError(t, err)
require.Len(t, r.Renders, 1)
createOutput, ok := r.Renders[0].(*cmd.JobPresenter)
require.True(t, ok, "Expected Renders[0] to be *cmd.JobPresenter, got %T", r.Renders[0])
set := flag.NewFlagSet("test", 0)
err = set.Parse([]string{createOutput.ID})
require.NoError(t, err)
c := cli.NewContext(nil, set, nil)
require.NoError(t, client.ShowJob(c))
job := *r.Renders[0].(*cmd.JobPresenter)
assert.Equal(t, createOutput.ID, job.ID)
}
//go:embed ocr-bootstrap-spec.yml
var ocrBootstrapSpec string
func TestShell_CreateJobV2(t *testing.T) {
t.Parallel()
app := startNewApplicationV2(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.Database.Listener.FallbackPollInterval = commonconfig.MustNewDuration(100 * time.Millisecond)
c.OCR.Enabled = ptr(true)
c.P2P.V2.Enabled = ptr(true)
c.P2P.V2.ListenAddresses = &[]string{fmt.Sprintf("127.0.0.1:%d", freeport.GetOne(t))}
c.P2P.PeerID = &cltest.DefaultP2PPeerID
c.EVM[0].Enabled = ptr(true)
c.EVM[0].NonceAutoSync = ptr(false)
c.EVM[0].BalanceMonitor.Enabled = ptr(false)
c.EVM[0].GasEstimator.Mode = ptr("FixedPrice")
}, func(opts *startOptions) {
opts.FlagsAndDeps = append(opts.FlagsAndDeps, cltest.DefaultP2PKey)
})
client, r := app.NewShellAndRenderer()
requireJobsCount(t, app.JobORM(), 0)
fs := flag.NewFlagSet("", flag.ExitOnError)
flagSetApplyFromAction(client.CreateJob, fs, "")
nameAndExternalJobID := uuid.New()
spec := fmt.Sprintf(ocrBootstrapSpec, nameAndExternalJobID, nameAndExternalJobID)
require.NoError(t, fs.Parse([]string{spec}))
err := client.CreateJob(cli.NewContext(nil, fs, nil))
require.NoError(t, err)
requireJobsCount(t, app.JobORM(), 1)
output := *r.Renders[0].(*cmd.JobPresenter)
assert.Equal(t, presenters.JobSpecType("offchainreporting"), output.Type)
assert.Equal(t, uint32(1), output.SchemaVersion)
assert.Equal(t, "0x27548a32b9aD5D64c5945EaE9Da5337bc3169D15", output.OffChainReportingSpec.ContractAddress.String())
}
func TestShell_DeleteJob(t *testing.T) {
t.Parallel()
app := startNewApplicationV2(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.Database.Listener.FallbackPollInterval = commonconfig.MustNewDuration(100 * time.Millisecond)
c.EVM[0].Enabled = ptr(true)
c.EVM[0].NonceAutoSync = ptr(false)
c.EVM[0].BalanceMonitor.Enabled = ptr(false)
c.EVM[0].GasEstimator.Mode = ptr("FixedPrice")
})
client, r := app.NewShellAndRenderer()
// Create the job
fs := flag.NewFlagSet("", flag.ExitOnError)
flagSetApplyFromAction(client.CreateJob, fs, "")
require.NoError(t, fs.Parse([]string{getDirectRequestSpec()}))
err := client.CreateJob(cli.NewContext(nil, fs, nil))
require.NoError(t, err)
require.NotEmpty(t, r.Renders)
output := *r.Renders[0].(*cmd.JobPresenter)
requireJobsCount(t, app.JobORM(), 1)
jobs, _, err := app.JobORM().FindJobs(0, 1000)
require.NoError(t, err)
jobID := jobs[0].ID
cltest.AwaitJobActive(t, app.JobSpawner(), jobID, 3*time.Second)
// Must supply job id
set := flag.NewFlagSet("test", 0)
flagSetApplyFromAction(client.DeleteJob, set, "")
c := cli.NewContext(nil, set, nil)
require.Equal(t, "must pass the job id to be archived", client.DeleteJob(c).Error())
set = flag.NewFlagSet("test", 0)
flagSetApplyFromAction(client.DeleteJob, set, "")
require.NoError(t, set.Parse([]string{output.ID}))
c = cli.NewContext(nil, set, nil)
require.NoError(t, client.DeleteJob(c))
requireJobsCount(t, app.JobORM(), 0)
}
func requireJobsCount(t *testing.T, orm job.ORM, expected int) {
jobs, _, err := orm.FindJobs(0, 1000)
require.NoError(t, err)
require.Len(t, jobs, expected)
}