-
Notifications
You must be signed in to change notification settings - Fork 51
/
helpers.go
235 lines (214 loc) · 6.88 KB
/
helpers.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
package testhelpers
import (
"io/ioutil"
"math/rand"
"path/filepath"
"testing"
"time"
schedulerapi "github.com/jenkins-x-plugins/jx-gitops/pkg/apis/scheduler/v1alpha1"
"github.com/jenkins-x/lighthouse-client/pkg/config/job"
"github.com/ghodss/yaml"
"github.com/jenkins-x-plugins/jx-gitops/pkg/pipelinescheduler"
"github.com/jenkins-x/lighthouse-client/pkg/config"
"github.com/jenkins-x/lighthouse-client/pkg/plugins"
"github.com/stretchr/testify/assert"
"github.com/pborman/uuid"
)
// CompleteScheduler returns a SchedulerSpec completely filled with dummy data
func CompleteScheduler() *schedulerapi.SchedulerSpec {
return &schedulerapi.SchedulerSpec{
Policy: pointerToGlobalProtectionPolicy(),
Merger: &schedulerapi.Merger{
ContextPolicy: &schedulerapi.ContextPolicy{
OptionalContexts: PointerToReplaceableSliceOfStrings(),
RequiredContexts: PointerToReplaceableSliceOfStrings(),
RequiredIfPresentContexts: PointerToReplaceableSliceOfStrings(),
},
MergeType: pointerToUUID(),
TargetURL: pointerToUUID(),
PRStatusBaseURL: pointerToUUID(),
BlockerLabel: pointerToUUID(),
SquashLabel: pointerToUUID(),
MaxGoroutines: pointerToRandomNumber(),
StatusUpdatePeriod: pointerToRandomDuration(),
SyncPeriod: pointerToRandomDuration(),
},
Presubmits: &schedulerapi.Presubmits{
Items: []*job.Presubmit{
{
Base: job.Base{
Name: "cheese",
},
},
},
},
Postsubmits: &schedulerapi.Postsubmits{
Items: []*job.Postsubmit{
{
Base: job.Base{
Name: "cheese",
},
},
},
},
Trigger: &schedulerapi.Trigger{
IgnoreOkToTest: pointerToTrue(),
JoinOrgURL: pointerToUUID(),
OnlyOrgMembers: pointerToTrue(),
TrustedOrg: pointerToUUID(),
},
SchedulerAgent: &schedulerapi.SchedulerAgent{
Agent: pointerToUUID(),
},
Approve: &schedulerapi.Approve{
RequireSelfApproval: pointerToTrue(),
LgtmActsAsApprove: pointerToTrue(),
IssueRequired: pointerToTrue(),
IgnoreReviewState: pointerToTrue(),
},
ExternalPlugins: &schedulerapi.ReplaceableSliceOfExternalPlugins{
Items: []*schedulerapi.ExternalPlugin{
{
Name: pointerToUUID(),
Events: PointerToReplaceableSliceOfStrings(),
Endpoint: pointerToUUID(),
},
},
},
LGTM: &schedulerapi.Lgtm{
StoreTreeHash: pointerToTrue(),
ReviewActsAsLgtm: pointerToTrue(),
StickyLgtmTeam: pointerToUUID(),
},
Plugins: PointerToReplaceableSliceOfStrings(),
}
}
func pointerToTrue() *bool {
b := true
return &b
}
func pointerToUUID() *string {
s := uuid.New()
return &s
}
func pointerToRandomNumber() *int {
i := rand.Int() // #nosec
return &i
}
func pointerToRandomDuration() *time.Duration {
i := rand.Int63()
duration := time.Duration(i)
return &duration
}
// PointerToReplaceableSliceOfStrings creaters a ReplaceableSliceOfStrings and returns its pointer
func PointerToReplaceableSliceOfStrings() *schedulerapi.ReplaceableSliceOfStrings {
return &schedulerapi.ReplaceableSliceOfStrings{
Items: []string{
uuid.New(),
},
}
}
func pointerToContextPolicy() *schedulerapi.ContextPolicy {
return &schedulerapi.ContextPolicy{
SkipUnknownContexts: pointerToTrue(),
FromBranchProtection: pointerToTrue(),
RequiredIfPresentContexts: PointerToReplaceableSliceOfStrings(),
RequiredContexts: PointerToReplaceableSliceOfStrings(),
OptionalContexts: PointerToReplaceableSliceOfStrings(),
}
}
func pointerToGlobalProtectionPolicy() *schedulerapi.GlobalProtectionPolicy {
return &schedulerapi.GlobalProtectionPolicy{
ProtectTested: pointerToTrue(),
ProtectionPolicy: pointerToProtectionPolicy(),
}
}
func pointerToProtectionPolicy() *schedulerapi.ProtectionPolicy {
return &schedulerapi.ProtectionPolicy{
Restrictions: &schedulerapi.Restrictions{
Users: PointerToReplaceableSliceOfStrings(),
Teams: PointerToReplaceableSliceOfStrings(),
},
Admins: pointerToTrue(),
RequiredPullRequestReviews: &schedulerapi.ReviewPolicy{
DismissalRestrictions: &schedulerapi.Restrictions{
Users: PointerToReplaceableSliceOfStrings(),
Teams: PointerToReplaceableSliceOfStrings(),
},
},
RequiredStatusChecks: &schedulerapi.BranchProtectionContextPolicy{
Strict: pointerToTrue(),
Contexts: PointerToReplaceableSliceOfStrings(),
},
Protect: pointerToTrue(),
}
}
// SchedulerFile contains a list of leaf files to build the scheduler from
type SchedulerFile struct {
// Filenames is the hierarchy with the leaf at the right
Filenames []string
Org string
Repo string
}
// BuildAndValidateProwConfig takes a list of schedulerFiles and builds them to a Prow config,
// and validates them against the expectedConfigFilename and expectedPluginsFilename that make up the prow config.
// Filepaths are relative to the baseDir
func BuildAndValidateProwConfig(t *testing.T, baseDir string, expectedConfigFilename string,
expectedPluginsFilename string, schedulerFiles []SchedulerFile) {
var expectedConfig config.Config
if expectedConfigFilename != "" {
cfgBytes, err := ioutil.ReadFile(filepath.Join(baseDir, expectedConfigFilename))
assert.NoError(t, err)
err = yaml.Unmarshal(cfgBytes, &expectedConfig)
assert.NoError(t, err)
}
var expectedPlugins plugins.Configuration
if expectedPluginsFilename != "" {
bytes, err := ioutil.ReadFile(filepath.Join(baseDir, expectedPluginsFilename))
assert.NoError(t, err)
err = yaml.Unmarshal(bytes, &expectedPlugins)
assert.NoError(t, err)
}
schedulerLeaves := make([]*pipelinescheduler.SchedulerLeaf, 0)
for _, sfs := range schedulerFiles {
schedulers := make([]*schedulerapi.SchedulerSpec, 0)
for _, f := range sfs.Filenames {
bytes, err := ioutil.ReadFile(filepath.Join(baseDir, f))
assert.NoError(t, err)
s := schedulerapi.SchedulerSpec{}
err = yaml.Unmarshal(bytes, &s)
assert.NoError(t, err)
schedulers = append(schedulers, &s)
}
s, err := pipelinescheduler.Build(schedulers)
assert.NoError(t, err)
schedulerLeaves = append(schedulerLeaves, &pipelinescheduler.SchedulerLeaf{
Repo: sfs.Repo,
Org: sfs.Org,
SchedulerSpec: s,
})
}
cfg, plugs, err := pipelinescheduler.BuildProwConfig(schedulerLeaves)
assert.NoError(t, err)
if expectedConfigFilename != "" {
expected, err := yaml.Marshal(&expectedConfig)
assert.NoError(t, err)
actual, err := yaml.Marshal(cfg)
assert.NoError(t, err)
assert.NotNil(t, expected)
if !assert.Equal(t, string(expected), string(actual)) {
t.Logf("config expected: %s\n", string(expected))
t.Logf("got: %s\n", string(actual))
}
}
if expectedPluginsFilename != "" {
expected, err := yaml.Marshal(&expectedPlugins)
assert.NoError(t, err)
actual, err := yaml.Marshal(plugs)
assert.NoError(t, err)
if !assert.Equal(t, string(expected), string(actual)) {
t.Logf("plugins expected: %s\n", string(expected))
t.Logf("got: %s\n", string(actual))
}
}
}