forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtaBuild_test.go
403 lines (283 loc) · 14 KB
/
mtaBuild_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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package cmd
import (
"errors"
"net/http"
"os"
"path/filepath"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/ghodss/yaml"
"github.com/stretchr/testify/assert"
)
type mtaBuildTestUtilsBundle struct {
*mock.ExecMockRunner
*mock.FilesMock
projectSettingsFile string
globalSettingsFile string
registryUsedInSetNpmRegistries string
}
func (m *mtaBuildTestUtilsBundle) SetNpmRegistries(defaultNpmRegistry string) error {
m.registryUsedInSetNpmRegistries = defaultNpmRegistry
return nil
}
func (m *mtaBuildTestUtilsBundle) InstallAllDependencies(defaultNpmRegistry string) error {
return errors.New("Test should not install dependencies.") //TODO implement test
}
func (m *mtaBuildTestUtilsBundle) DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile string) error {
m.projectSettingsFile = projectSettingsFile
m.globalSettingsFile = globalSettingsFile
return nil
}
func (m *mtaBuildTestUtilsBundle) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
return errors.New("Test should not download files.")
}
func newMtaBuildTestUtilsBundle() *mtaBuildTestUtilsBundle {
utilsBundle := mtaBuildTestUtilsBundle{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
return &utilsBundle
}
func TestMtaBuild(t *testing.T) {
cpe := mtaBuildCommonPipelineEnvironment{}
t.Run("Application name not set", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
options := mtaBuildOptions{}
err := runMtaBuild(options, &cpe, utilsMock)
assert.NotNil(t, err)
assert.Equal(t, "'mta.yaml' not found in project sources and 'applicationName' not provided as parameter - cannot generate 'mta.yaml' file", err.Error())
})
t.Run("Provide default npm registry", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", DefaultNpmRegistry: "https://example.org/npm", MtarName: "myName", Source: "./", Target: "./"}
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
err := runMtaBuild(options, &cpe, utilsMock)
assert.Nil(t, err)
assert.Equal(t, "https://example.org/npm", utilsMock.registryUsedInSetNpmRegistries)
})
t.Run("Package json does not exist", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
options := mtaBuildOptions{ApplicationName: "myApp"}
err := runMtaBuild(options, &cpe, utilsMock)
assert.NotNil(t, err)
assert.Equal(t, "package.json file does not exist", err.Error())
})
t.Run("Write yaml file", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", MtarName: "myName", Source: "./", Target: "./"}
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
err := runMtaBuild(options, &cpe, utilsMock)
assert.Nil(t, err)
type MtaResult struct {
Version string
ID string `yaml:"ID,omitempty"`
Parameters map[string]string
Modules []struct {
Name string
Type string
Parameters map[string]interface{}
}
}
assert.True(t, utilsMock.HasWrittenFile("mta.yaml"))
var result MtaResult
mtaContent, _ := utilsMock.FileRead("mta.yaml")
yaml.Unmarshal(mtaContent, &result)
assert.Equal(t, "myName", result.ID)
assert.Equal(t, "1.2.3", result.Version)
assert.Equal(t, "myApp", result.Modules[0].Name)
assert.Regexp(t, "^1\\.2\\.3-[\\d]{14}$", result.Modules[0].Parameters["version"])
assert.Equal(t, "myApp", result.Modules[0].Parameters["name"])
})
t.Run("Dont write mta yaml file when already present no timestamp placeholder", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
options := mtaBuildOptions{ApplicationName: "myApp"}
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
utilsMock.AddFile("mta.yaml", []byte("already there"))
_ = runMtaBuild(options, &cpe, utilsMock)
assert.False(t, utilsMock.HasWrittenFile("mta.yaml"))
})
t.Run("Write mta yaml file when already present with timestamp placeholder", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
options := mtaBuildOptions{ApplicationName: "myApp"}
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
utilsMock.AddFile("mta.yaml", []byte("already there with-${timestamp}"))
_ = runMtaBuild(options, &cpe, utilsMock)
assert.True(t, utilsMock.HasWrittenFile("mta.yaml"))
})
t.Run("Mta build mbt toolset", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
cpe.mtarFilePath = ""
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", MtarName: "myName.mtar", Source: "./", Target: "./"}
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
err := runMtaBuild(options, &cpe, utilsMock)
assert.Nil(t, err)
if assert.Len(t, utilsMock.Calls, 1) {
assert.Equal(t, "mbt", utilsMock.Calls[0].Exec)
assert.Equal(t, []string{"build", "--mtar", "myName.mtar", "--platform", "CF", "--source", filepath.FromSlash("./"), "--target", filepath.FromSlash(_ignoreError(os.Getwd()))}, utilsMock.Calls[0].Params)
}
assert.Equal(t, "myName.mtar", cpe.mtarFilePath)
})
t.Run("Source and target related tests", func(t *testing.T) {
t.Run("Mta build mbt toolset with custom source and target paths", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
cpe.mtarFilePath = ""
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", MtarName: "myName.mtar", Source: "mySourcePath/", Target: "myTargetPath/"}
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
err := runMtaBuild(options, &cpe, utilsMock)
assert.Nil(t, err)
if assert.Len(t, utilsMock.Calls, 1) {
assert.Equal(t, "mbt", utilsMock.Calls[0].Exec)
assert.Equal(t, []string{"build", "--mtar", "myName.mtar", "--platform", "CF",
"--source", filepath.FromSlash("mySourcePath/"),
"--target", filepath.Join(_ignoreError(os.Getwd()), filepath.FromSlash("mySourcePath/myTargetPath/"))},
utilsMock.Calls[0].Params)
}
assert.Equal(t, "mySourcePath/myTargetPath/myName.mtar", cpe.mtarFilePath)
assert.Equal(t, "mySourcePath/mta.yaml", cpe.custom.mtaBuildToolDesc)
})
})
t.Run("M2Path related tests", func(t *testing.T) {
t.Run("Mta build mbt toolset with m2Path", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.CurrentDir = "root_folder/workspace"
cpe.mtarFilePath = ""
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", MtarName: "myName.mtar", Source: "./", Target: "./", M2Path: ".pipeline/local_repo"}
utilsMock.AddFile("mta.yaml", []byte("ID: \"myNameFromMtar\""))
err := runMtaBuild(options, &cpe, utilsMock)
assert.Nil(t, err)
assert.Contains(t, utilsMock.Env, filepath.FromSlash("MAVEN_OPTS=-Dmaven.repo.local=/root_folder/workspace/.pipeline/local_repo"))
})
})
t.Run("Settings file releatd tests", func(t *testing.T) {
t.Run("Copy global settings file", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("ID: \"myNameFromMtar\""))
options := mtaBuildOptions{ApplicationName: "myApp", GlobalSettingsFile: "/opt/maven/settings.xml", Platform: "CF", MtarName: "myName", Source: "./", Target: "./"}
err := runMtaBuild(options, &cpe, utilsMock)
assert.Nil(t, err)
assert.Equal(t, "/opt/maven/settings.xml", utilsMock.globalSettingsFile)
assert.Equal(t, "", utilsMock.projectSettingsFile)
})
t.Run("Copy project settings file", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("ID: \"myNameFromMtar\""))
options := mtaBuildOptions{ApplicationName: "myApp", ProjectSettingsFile: "/my/project/settings.xml", Platform: "CF", MtarName: "myName", Source: "./", Target: "./"}
err := runMtaBuild(options, &cpe, utilsMock)
assert.Nil(t, err)
assert.Equal(t, "/my/project/settings.xml", utilsMock.projectSettingsFile)
assert.Equal(t, "", utilsMock.globalSettingsFile)
})
})
t.Run("publish related tests", func(t *testing.T) {
t.Run("error when no repository url", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("ID: \"myNameFromMtar\""))
options := mtaBuildOptions{ApplicationName: "myApp", GlobalSettingsFile: "/opt/maven/settings.xml", Platform: "CF", MtarName: "myName", Source: "./", Target: "./", Publish: true}
err := runMtaBuild(options, &cpe, utilsMock)
assert.Equal(t, "mtaDeploymentRepositoryUser, mtaDeploymentRepositoryPassword and mtaDeploymentRepositoryURL not found , must be present", err.Error())
})
t.Run("error when no mtar group", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("ID: \"myNameFromMtar\""))
options := mtaBuildOptions{ApplicationName: "myApp", GlobalSettingsFile: "/opt/maven/settings.xml", Platform: "CF", MtarName: "myName", Source: "./", Target: "./", Publish: true,
MtaDeploymentRepositoryURL: "dummy", MtaDeploymentRepositoryPassword: "dummy", MtaDeploymentRepositoryUser: "dummy"}
err := runMtaBuild(options, &cpe, utilsMock)
assert.Equal(t, "mtarGroup, version not found and must be present", err.Error())
})
})
}
func TestMtaBuildSourceDir(t *testing.T) {
t.Run("getSourcePath", func(t *testing.T) {
t.Parallel()
t.Run("getPath dir unset", func(t *testing.T) {
options := mtaBuildOptions{Source: "", Target: ""}
assert.Equal(t, filepath.FromSlash("./"), getSourcePath(options))
assert.Equal(t, filepath.FromSlash("./"), getTargetPath(options))
})
t.Run("getPath source set", func(t *testing.T) {
options := mtaBuildOptions{Source: "spath", Target: ""}
assert.Equal(t, filepath.FromSlash("spath"), getSourcePath(options))
assert.Equal(t, filepath.FromSlash("./"), getTargetPath(options))
})
t.Run("getPath target set", func(t *testing.T) {
options := mtaBuildOptions{Source: "", Target: "tpath"}
assert.Equal(t, filepath.FromSlash("./"), getSourcePath(options))
assert.Equal(t, filepath.FromSlash("tpath"), getTargetPath(options))
})
t.Run("getPath dir set to relative path", func(t *testing.T) {
options := mtaBuildOptions{Source: "spath", Target: "tpath"}
assert.Equal(t, filepath.FromSlash("spath"), getSourcePath(options))
assert.Equal(t, filepath.FromSlash("tpath"), getTargetPath(options))
})
t.Run("getPath dir ends with seperator", func(t *testing.T) {
options := mtaBuildOptions{Source: "spath/", Target: "tpath/"}
assert.Equal(t, filepath.FromSlash("spath/"), getSourcePath(options))
assert.Equal(t, filepath.FromSlash("tpath/"), getTargetPath(options))
})
t.Run("getPath dir set to absolute path", func(t *testing.T) {
sourcePath := filepath.Join(_ignoreError(os.Getwd()), "spath")
targetPath := filepath.Join(_ignoreError(os.Getwd()), "tpath")
options := mtaBuildOptions{Source: sourcePath, Target: targetPath}
assert.Equal(t, filepath.FromSlash(sourcePath), getSourcePath(options))
assert.Equal(t, filepath.FromSlash(targetPath), getTargetPath(options))
})
})
t.Run("find build tool descriptor from configuration", func(t *testing.T) {
t.Parallel()
cpe := mtaBuildCommonPipelineEnvironment{}
t.Run("default mta.yaml", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("already there"))
_ = runMtaBuild(mtaBuildOptions{ApplicationName: "myApp"}, &cpe, utilsMock)
assert.False(t, utilsMock.HasWrittenFile("mta.yaml"))
})
t.Run("create mta.yaml from config.source", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
_ = runMtaBuild(mtaBuildOptions{ApplicationName: "myApp", Source: "create"}, &cpe, utilsMock)
assert.True(t, utilsMock.HasWrittenFile("create/mta.yaml"))
})
t.Run("read yaml from config.source", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("path/mta.yaml", []byte("already there"))
_ = runMtaBuild(mtaBuildOptions{ApplicationName: "myApp", Source: "path"}, &cpe, utilsMock)
assert.False(t, utilsMock.HasWrittenFile("path/mta.yaml"))
})
})
}
func TestMtaBuildMtar(t *testing.T) {
t.Run("getMtarName", func(t *testing.T) {
t.Parallel()
t.Run("mtar name from yaml", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("ID: \"nameFromMtar\""))
assert.Equal(t, filepath.FromSlash("nameFromMtar.mtar"), _ignoreError(getMtarName(mtaBuildOptions{MtarName: ""}, "mta.yaml", utilsMock)))
})
t.Run("mtar name from config", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("ID: \"nameFromMtar\""))
assert.Equal(t, filepath.FromSlash("nameFromConfig.mtar"), _ignoreError(getMtarName(mtaBuildOptions{MtarName: "nameFromConfig.mtar"}, "mta.yaml", utilsMock)))
})
})
t.Run("getMtarFilePath", func(t *testing.T) {
t.Parallel()
t.Run("plain mtar name", func(t *testing.T) {
assert.Equal(t, "mta.mtar", getMtarFilePath(mtaBuildOptions{Source: "", Target: ""}, "mta.mtar"))
})
t.Run("plain mtar name from default", func(t *testing.T) {
assert.Equal(t, "mta.mtar", getMtarFilePath(mtaBuildOptions{Source: "./", Target: "./"}, "mta.mtar"))
})
t.Run("source path", func(t *testing.T) {
assert.Equal(t, filepath.FromSlash("source/mta.mtar"), getMtarFilePath(mtaBuildOptions{Source: "source", Target: ""}, "mta.mtar"))
})
t.Run("target path", func(t *testing.T) {
assert.Equal(t, filepath.FromSlash("target/mta.mtar"), getMtarFilePath(mtaBuildOptions{Source: "", Target: "target"}, "mta.mtar"))
})
t.Run("source and target path", func(t *testing.T) {
assert.Equal(t, filepath.FromSlash("source/target/mta.mtar"), getMtarFilePath(mtaBuildOptions{Source: "source", Target: "target"}, "mta.mtar"))
})
})
}
func _ignoreError(s string, e error) string {
return s
}