-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathvalidators_test.go
122 lines (109 loc) · 3.76 KB
/
validators_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
package services_test
import (
"encoding/json"
"errors"
"fmt"
"testing"
"time"
"net/url"
"github.com/smartcontractkit/chainlink/internal/cltest"
"github.com/smartcontractkit/chainlink/services"
"github.com/smartcontractkit/chainlink/store/models"
"github.com/smartcontractkit/chainlink/utils"
"github.com/stretchr/testify/assert"
)
func TestValidateJob(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []byte
want error
}{
{"base case", cltest.LoadJSON("../internal/fixtures/web/hello_world_job.json"), nil},
{"error in job", cltest.LoadJSON("../internal/fixtures/web/invalid_endat_job.json"),
errors.New(`job validation: startat cannot be before endat`)},
{"error in runat initr", cltest.LoadJSON("../internal/fixtures/web/run_at_wo_time_job.json"),
errors.New(`job validation: initiator validation: runat must have a time`)},
{"error in task", cltest.LoadJSON("../internal/fixtures/web/nonexistent_task_job.json"),
errors.New(`job validation: task validation: idonotexist is not a supported adapter type`)},
}
store, cleanup := cltest.NewStore()
defer cleanup()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var j models.JobSpec
assert.NoError(t, json.Unmarshal(test.input, &j))
result := services.ValidateJob(j, store)
assert.Equal(t, test.want, result)
})
}
}
func TestValidateAdapter(t *testing.T) {
t.Parallel()
store, cleanup := cltest.NewStore()
defer cleanup()
tt := models.BridgeType{}
tt.Name = models.MustNewTaskType("solargridreporting")
u, err := url.Parse("https://denergy.eth")
assert.NoError(t, err)
tt.URL = models.WebURL{URL: u}
assert.NoError(t, store.Save(&tt))
tests := []struct {
description string
name string
want error
}{
{"existing external adapter", "solargridreporting",
errors.New("adapter validation: adapter solargridreporting exists")},
{"existing core adapter", "ethtx",
errors.New("adapter validation: adapter ethtx exists")},
{"no adapter name", "",
errors.New("adapter validation: no name specified")},
{"invalid adapter name", "invalid/adapter",
errors.New("adapter validation error: Task Type validation: name invalid/adapter contains invalid characters")},
{"new external adapter", "gdaxprice", nil},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
bt := &models.BridgeType{Name: models.TaskType(test.name)}
result := services.ValidateAdapter(bt, store)
assert.Equal(t, test.want, result)
})
}
}
func TestValidateInitiator(t *testing.T) {
t.Parallel()
startAt := time.Now()
endAt := startAt.Add(time.Second)
job := cltest.NewJob()
job.StartAt = cltest.NullableTime(startAt)
job.EndAt = cltest.NullableTime(endAt)
tests := []struct {
name string
input string
wantError bool
}{
{"web", `{"type":"web"}`, false},
{"ethlog", `{"type":"ethlog"}`, false},
{"runlog", `{"type":"runlog"}`, false},
{"runat", fmt.Sprintf(`{"type":"runat","time":"%v"}`, utils.ISO8601UTC(startAt)), false},
{"runat w/o time", `{"type":"runat"}`, true},
{"runat w time before start at", fmt.Sprintf(`{"type":"runat","time":"%v"}`, startAt.Add(-1*time.Second).Unix()), true},
{"runat w time after end at", fmt.Sprintf(`{"type":"runat","time":"%v"}`, endAt.Add(time.Second).Unix()), true},
{"cron", `{"type":"cron","schedule":"* * * * * *"}`, false},
{"cron w/o schedule", `{"type":"cron"}`, true},
{"non-existent initiator", `{"type":"doesntExist"}`, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var initr models.Initiator
assert.NoError(t, json.Unmarshal([]byte(test.input), &initr))
result := services.ValidateInitiator(initr, job)
if test.wantError {
assert.Error(t, result)
} else {
assert.NoError(t, result)
}
})
}
}