-
Notifications
You must be signed in to change notification settings - Fork 2
/
workflow_test.go
218 lines (186 loc) · 6.09 KB
/
workflow_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
package kubeci
import (
"regexp"
"strconv"
"strings"
"testing"
"github.com/google/go-github/v45/github"
)
var rfc1035Re = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`)
var workflowRegexp = regexp.MustCompile(`^(([A-Za-z0-9][-A-Za-z0-9.]*)?[A-Za-z0-9])?$`)
func TestWFName(t *testing.T) {
var tests = []struct {
org string
repo string
entrypoint string
ref string
expected string
}{
{"QubitProducts", "kube-ci", "", "master", "kube-ci.master"},
{"qubitdigital", "atom-api", "", "ERBC-96-post-release-stripping-of-locale-null-from-the-db", "atom-api.erbc-96-post-release-stripping"},
{"QubitProducts", "kube-ci", "deploy", "master", "kube-ci.deploy.master"},
{"QubitProducts", "kube-ci", "deploy:migrations", "master", "kube-ci.deploy-migratio.master"},
{"myorg", "myrepo", "", "user-project-1234-some-really-long-branch-namme-from-some-dirt-bag-issue-tracking-system", "myrepo.user-project-1234-some-really-lon"},
{"QubitProdcts", "quite-long-repo-name", "", "user-project-1234-some-really-long-branch-namme-from-some-dirt-bag-issue-tracking-system", "quite-long-repo-name.user-project-1234-s"},
{"QubitProdcts", "quite-long-repo-name", "deploy:migrations", "user-project-1234-some-really-long-branch-namme-from-some-dirt-bag-issue-tracking-system", "quite-long-repo.deploy-migratio.user-pro"},
}
for i, tt := range tests {
tt := tt
t.Run(strconv.Itoa(i), func(t *testing.T) {
actual := wfName(tt.org, tt.repo, tt.entrypoint, tt.ref)
t.Logf("name: %v", actual)
if len(actual) > 50 {
t.Errorf("workflow name %s is too long (should be <= 50)", actual)
}
parts := strings.Split(actual, ".")
for _, p := range parts {
if !rfc1035Re.MatchString(p) {
t.Errorf("%s in %s to match rfc1035", p, actual)
}
}
if !workflowRegexp.MatchString(actual) {
t.Errorf("%s does not match workflowRegexp", actual)
}
parts = parts[0 : len(parts)-1]
hash := parts[len(parts)-1]
if hash == "" {
t.Errorf("workflow name %s did not include a hash", actual)
}
actual = strings.Join(parts, ".")
if actual != tt.expected {
t.Errorf("expected %s, actual %s", tt.expected, actual)
}
})
}
}
func FuzzWFName(f *testing.F) {
//
f.Add("myorg", "myrepo", "", "master")
f.Add("myorg", "myrepo", "deploy", "master")
f.Fuzz(func(t *testing.T, org, repo, entrypoint, ref string) {
actual := wfName(org, repo, entrypoint, ref)
if len(actual) > 50 {
t.Errorf("workflow name %s is too long (should be <= 50)", actual)
}
parts := strings.Split(actual, ".")
for _, p := range parts {
if !rfc1035Re.MatchString(p) {
t.Errorf("%s in %s to match rfc1035", p, actual)
}
}
if !workflowRegexp.MatchString(actual) {
t.Errorf("%s does not match workflowRegexp", actual)
}
})
}
func TestPolicy(t *testing.T) {
for _, st := range []struct {
testName string
config Config
repo *github.Repository
headbranch string
title string
prs []*github.PullRequest
exp *policyRejection
}{
{
testName: "default behaviour (non-PR)",
config: Config{},
repo: &github.Repository{},
headbranch: "master",
prs: nil,
exp: nil,
},
{
testName: "unmatched build branch",
config: Config{
buildBranches: regexp.MustCompile("^main$"),
},
repo: &github.Repository{},
headbranch: "master",
prs: nil,
exp: &policyRejection{message: "checks are not automatically run for base branches that do not match `^main$`, you can run manually using `/kube-ci run`"},
},
{
testName: "one non-draft PR",
repo: &github.Repository{},
headbranch: "master",
prs: []*github.PullRequest{
{
Head: &github.PullRequestBranch{Repo: &github.Repository{URL: github.String("http://localhost/blah")}},
Base: &github.PullRequestBranch{Repo: &github.Repository{URL: github.String("http://localhost/blah")}},
Draft: github.Bool(false),
},
},
exp: nil,
}, {
testName: "one non-draft PR, unmatched branch",
config: Config{
buildBranches: regexp.MustCompile("^main$"),
},
repo: &github.Repository{},
headbranch: "master",
prs: []*github.PullRequest{
{
Head: &github.PullRequestBranch{Repo: &github.Repository{URL: github.String("http://localhost/blah")}},
Base: &github.PullRequestBranch{Repo: &github.Repository{URL: github.String("http://localhost/blah")}},
Draft: github.Bool(false),
},
},
exp: &policyRejection{message: "checks are not automatically run for base branches that do not match `^main$`, you can run manually using `/kube-ci run`"},
},
{
testName: "one draft PR",
config: Config{BuildDraftPRs: true},
repo: &github.Repository{},
headbranch: "master",
prs: []*github.PullRequest{
{
Head: &github.PullRequestBranch{Repo: &github.Repository{URL: github.String("http://localhost/blah")}},
Base: &github.PullRequestBranch{Repo: &github.Repository{URL: github.String("http://localhost/blah")}},
Draft: github.Bool(true),
},
},
exp: nil,
},
{
testName: "one draft PR",
config: Config{BuildDraftPRs: false},
repo: &github.Repository{},
headbranch: "master",
prs: []*github.PullRequest{
{
Head: &github.PullRequestBranch{Repo: &github.Repository{URL: github.String("http://localhost/blah")}},
Base: &github.PullRequestBranch{Repo: &github.Repository{URL: github.String("http://localhost/blah")}},
Draft: github.Bool(true),
},
},
exp: &policyRejection{message: "auto checks Draft PRs are disabled, you can run manually using `/kube-ci run`"},
},
} {
st := st
ws := workflowSyncer{config: st.config}
t.Run(st.testName, func(t *testing.T) {
err := ws.policy(
st.repo,
st.headbranch,
st.title,
st.prs,
)
if err != nil {
if st.exp == nil {
t.Fatalf("unexpected policy rejection, got %v", err)
return
}
err.log = ""
if *err != *st.exp {
t.Fatalf("expected policy to evaluate to %v, got %v", st.exp, err)
}
return
}
if st.exp != nil {
t.Fatalf("did not get expected policy rejection %v", st.exp)
}
})
}
}