-
Notifications
You must be signed in to change notification settings - Fork 4
/
metadata.go
320 lines (277 loc) · 7.22 KB
/
metadata.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
package metadatamock
import (
"context"
"errors"
"github.com/Interhyp/metadata-service/acorns/errors/concurrencyerror"
"github.com/Interhyp/metadata-service/acorns/errors/nochangeserror"
"github.com/Interhyp/metadata-service/acorns/errors/unavailableerror"
"github.com/Interhyp/metadata-service/acorns/repository"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"io/ioutil"
"os"
"time"
)
import _ "github.com/go-git/go-git/v5"
type Impl struct {
Fs billy.Filesystem
Now func() time.Time
FilesWritten map[string]bool
FilesCommitted map[string]bool
Pushed bool
SimulateRemoteFailure bool
SimulateConcurrencyFailure bool
SimulateUnchangedFailure bool
}
const ownerInfo = `contact: somebody@some-organisation.com
productOwner: kschlangenheldt
defaultJiraProject: ISSUE
`
const service = `quicklinks:
- title: Swagger UI
url: /swagger-ui/index.html
repositories:
- some-service-backend/helm-deployment
- some-service-backend/implementation
alertTarget: https://webhook.com/9asdflk29d4m39g
developmentOnly: false
requiredScans:
- SAST
- SCA
`
const deployment = `mainline: main
url: ssh://git@bitbucket.some-organisation.com:7999/PROJECT/some-service-backend-deployment.git
deployment:
kubernetes:
instances:
- namespace: project
environment: prod
cluster: openshift
- namespace: project
environment: dev
cluster: openshift
- namespace: project
environment: test
cluster: openshift
- namespace: project
environment: livetest
cluster: openshift
generator: third-party-software
configuration:
accessKeys:
- key: DEPLOYMENT
permission: REPO_READ
webhooks:
pipelineTrigger: true
commitMessageType: DEFAULT
requireIssue: true
approvers:
testing:
- some-user
`
const deployment2 = `mainline: main
url: ssh://git@bitbucket.some-organisation.com:7999/PROJECT/new-service-deployment.git
generator: third-party-software
`
const implementation = `mainline: master
url: ssh://git@bitbucket.some-organisation.com:7999/PROJECT/some-service-backend.git
generator: java-spring-cloud
`
const implementation2 = `mainline: master
url: ssh://git@bitbucket.some-organisation.com:7999/PROJECT/new-service.git
generator: java-spring-cloud
`
const chart = `url: ssh://git@bitbucket.some-organisation.com:7999/helm/karma-wrapper.git
mainline: master
unittest: false
`
const (
origCommitHash = "6c8ac2c35791edf9979623c717a243fc53400000"
newCommitHash = "6c8ac2c35791edf9979623c717a2430000000000"
origCommitMessage = "ISSUE-0000: original"
newCommitMessage = "ISSUE-2345: new"
)
func (r *Impl) commitHash(filename string) string {
_, found := r.FilesCommitted[filename]
if found {
return newCommitHash
} else {
return origCommitHash
}
}
func (r *Impl) commitMessage(filename string) string {
_, found := r.FilesCommitted[filename]
if found {
return newCommitMessage
} else {
return origCommitMessage
}
}
func (r *Impl) writeFile(filename string, contents string) error {
f, err := r.Fs.Create(filename)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write([]byte(contents))
return err
}
func (r *Impl) Clone(ctx context.Context) error {
r.Fs = memfs.New()
err := r.Fs.MkdirAll("owners/some-owner/services", 0755)
if err != nil {
return err
}
err = r.Fs.MkdirAll("owners/some-owner/repositories", 0755)
if err != nil {
return err
}
err = r.Fs.MkdirAll("owners/deleteme/services", 0755)
if err != nil {
return err
}
err = r.Fs.MkdirAll("owners/deleteme/repositories", 0755)
if err != nil {
return err
}
err = r.writeFile("owners/some-owner/owner.info.yaml", ownerInfo)
if err != nil {
return err
}
err = r.writeFile("owners/some-owner/services/some-service-backend.yaml", service)
if err != nil {
return err
}
err = r.writeFile("owners/some-owner/repositories/some-service-backend.helm-deployment.yaml", deployment)
if err != nil {
return err
}
err = r.writeFile("owners/some-owner/repositories/some-service-backend.implementation.yaml", implementation)
if err != nil {
return err
}
err = r.writeFile("owners/some-owner/repositories/new-service.helm-deployment.yaml", deployment2)
if err != nil {
return err
}
err = r.writeFile("owners/some-owner/repositories/new-service.implementation.yaml", implementation2)
if err != nil {
return err
}
err = r.writeFile("owners/some-owner/repositories/karma-wrapper.helm-chart.yaml", chart)
if err != nil {
return err
}
err = r.writeFile("owners/deleteme/owner.info.yaml", ownerInfo)
if err != nil {
return err
}
r.FilesCommitted = make(map[string]bool)
r.FilesWritten = make(map[string]bool)
r.SimulateRemoteFailure = false
r.SimulateConcurrencyFailure = false
r.SimulateUnchangedFailure = false
r.Pushed = false
return nil
}
func (r *Impl) Pull(ctx context.Context) error {
return nil
}
func (r *Impl) Commit(ctx context.Context, message string) (repository.CommitInfo, error) {
commitInfo := repository.CommitInfo{
CommitHash: "",
TimeStamp: r.Now(),
Message: "",
}
if r.SimulateUnchangedFailure {
return commitInfo, nochangeserror.New(ctx)
}
r.FilesCommitted = r.FilesWritten
commitInfo.CommitHash = newCommitHash
commitInfo.Message = message
return commitInfo, nil
}
func (r *Impl) Push(ctx context.Context) error {
if r.SimulateRemoteFailure {
return unavailableerror.New(ctx, "metadata")
}
if r.SimulateConcurrencyFailure {
return concurrencyerror.New(ctx, "cannot push")
}
r.Pushed = true
return nil
}
func (r *Impl) Discard(ctx context.Context) {
}
func (r *Impl) LastUpdated() time.Time {
return r.Now()
}
func (r *Impl) NewPulledCommits() []repository.CommitInfo {
return make([]repository.CommitInfo, 0)
}
func (r *Impl) IsCommitKnown(hash string) bool {
return false
}
func (r *Impl) Stat(filename string) (os.FileInfo, error) {
return r.Fs.Stat(filename)
}
func (r *Impl) ReadDir(path string) ([]os.FileInfo, error) {
return r.Fs.ReadDir(path)
}
func (r *Impl) ReadFile(filename string) ([]byte, repository.CommitInfo, error) {
commitInfo := repository.CommitInfo{
CommitHash: "",
TimeStamp: r.Now(),
Message: "",
}
fileHandle, err := r.Fs.Open(filename)
if err != nil {
return nil, commitInfo, err
}
defer fileHandle.Close()
data, err := ioutil.ReadAll(fileHandle)
if err != nil {
return nil, commitInfo, err
}
commitInfo.CommitHash = r.commitHash(filename)
commitInfo.Message = r.commitMessage(filename)
return data, commitInfo, nil
}
func (r *Impl) WriteFile(filename string, contents []byte) error {
fileHandle, err := r.Fs.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer fileHandle.Close()
_, err = fileHandle.Write(contents)
if err != nil {
return err
}
r.FilesWritten[filename] = true
return nil
}
func (r *Impl) DeleteFile(filename string) error {
err := r.Fs.Remove(filename)
if err != nil {
return err
}
r.FilesWritten[filename] = true
return nil
}
func (r *Impl) MkdirAll(path string) error {
return r.Fs.MkdirAll(path, 0755)
}
// reset for the next test
func (r *Impl) Reset() {
_ = r.Clone(context.TODO())
}
func (r *Impl) ReadContents(filename string) string {
by, _, err := r.ReadFile(filename)
if errors.Is(os.ErrNotExist, err) {
return "<notfound>"
}
if by == nil {
return "<nil>"
}
return string(by)
}