-
Notifications
You must be signed in to change notification settings - Fork 2
/
github-payload_test.go
94 lines (76 loc) · 2.37 KB
/
github-payload_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
package main
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPushEventIsMatch(t *testing.T) {
e := PushEvent{}
e.Repository.Name = "cd-core"
e.Repository.Owner.Name = "Pica9"
e.Ref = "refs/heads/master"
c := Criteria{
Event: "push",
}
assert.Equal(t, e.IsMatch(c), true, "Expected a match")
c.Owner = "Pica9"
assert.Equal(t, e.IsMatch(c), true, "Expected a match")
c.Repository = "cd-core"
assert.Equal(t, e.IsMatch(c), true, "Expected a match")
c.PushParams.Branch = "master"
assert.Equal(t, e.IsMatch(c), true, "Expected a match")
c.PushParams.Branch = "testing"
assert.Equal(t, e.IsMatch(c), false, "Did not expect a match")
c.PushParams.Branch = "mast*"
assert.Equal(t, e.IsMatch(c), true, "Expected a match")
c.Event = "release"
assert.Equal(t, e.IsMatch(c), false, "Did not expect a match")
}
func TestPushEventIsMatchReleaseBranch(t *testing.T) {
e := PushEvent{}
e.Repository.Name = "cd-core"
e.Repository.Owner.Name = "Pica9"
e.Ref = "refs/heads/release-4.16.0"
c := Criteria{
Event: "push",
Owner: "Pica9",
Repository: "cd-core",
}
c.PushParams.Branch = "release-4.17.0"
assert.Equal(t, e.IsMatch(c), false, "Did not expect a match")
e.Ref = "refs/heads/release-4.17.0"
assert.Equal(t, e.IsMatch(c), true, "Expected a match")
}
func TestParseConfigAndMatch(t *testing.T) {
jsonReader := bytes.NewBufferString(`
{
"port": 8000,
"rules": [
{
"command": "echo test",
"criteria": [
{
"event": "push",
"owner": "Pica9",
"repository": "cd-core",
"push_params": {
"branch": "release-4.17.0"
}
}
]
}
]
}`)
config := getConfigFromReader(jsonReader)
assert.Equal(t, 1, len(config.Rules), "Rules not loaded properly")
assert.Equal(t, "echo test", config.Rules[0].Command, "Command not parsed correctly")
assert.Equal(t, 1, len(config.Rules[0].Criteria), "Criteria not parsed correctly")
assert.Equal(t, "release-4.17.0", config.Rules[0].Criteria[0].PushParams.Branch, "Branch not parsed correctly")
e := PushEvent{}
e.Repository.Name = "cd-core"
e.Repository.Owner.Name = "Pica9"
e.Ref = "refs/heads/release-4.16.0"
assert.Equal(t, e.IsMatch(config.Rules[0].Criteria[0]), false, "Did not expect a match")
e.Ref = "refs/heads/release-4.17.0"
assert.Equal(t, e.IsMatch(config.Rules[0].Criteria[0]), true, "Expected a match")
}