1
1
package pipeline
2
2
3
3
import (
4
+ "fmt"
5
+ "io/ioutil"
6
+ "os"
7
+ "path/filepath"
4
8
"testing"
5
9
"time"
6
10
@@ -25,6 +29,32 @@ func TestAppend(t *testing.T) {
25
29
26
30
}
27
31
32
+ func TestUpdate (t * testing.T ) {
33
+ ap := NewActivePipelines ()
34
+
35
+ p1 := gaia.Pipeline {
36
+ Name : "Pipeline A" ,
37
+ Type : gaia .PTypeGolang ,
38
+ Created : time .Now (),
39
+ }
40
+ ap .Append (p1 )
41
+
42
+ p2 := gaia.Pipeline {
43
+ Name : "Pipeline B" ,
44
+ Type : gaia .PTypeGolang ,
45
+ Created : time .Now (),
46
+ }
47
+
48
+ ap .Update (0 , p2 )
49
+
50
+ ret := ap .GetByName ("Pipeline B" )
51
+
52
+ if p2 .Name != ret .Name {
53
+ t .Fatalf ("Pipeline should have been updated." )
54
+ }
55
+
56
+ }
57
+
28
58
func TestRemove (t * testing.T ) {
29
59
ap := NewActivePipelines ()
30
60
@@ -115,6 +145,31 @@ func TestReplace(t *testing.T) {
115
145
}
116
146
}
117
147
148
+ func TestReplaceByName (t * testing.T ) {
149
+ ap := NewActivePipelines ()
150
+
151
+ p1 := gaia.Pipeline {
152
+ Name : "Pipeline A" ,
153
+ Type : gaia .PTypeGolang ,
154
+ Created : time .Now (),
155
+ }
156
+ ap .Append (p1 )
157
+
158
+ p2 := gaia.Pipeline {
159
+ Name : "Pipeline B" ,
160
+ Type : gaia .PTypeGolang ,
161
+ Created : time .Now (),
162
+ }
163
+
164
+ ap .ReplaceByName ("Pipeline A" , p2 )
165
+
166
+ ret := ap .GetByName ("Pipeline B" )
167
+
168
+ if p2 .Name != ret .Name {
169
+ t .Fatalf ("Pipeline should have been updated." )
170
+ }
171
+ }
172
+
118
173
func TestIter (t * testing.T ) {
119
174
ap := NewActivePipelines ()
120
175
@@ -199,3 +254,88 @@ func TestRemoveDeletedPipelines(t *testing.T) {
199
254
}
200
255
201
256
}
257
+
258
+ func TestRenameBinary (t * testing.T ) {
259
+ tmp := os .TempDir ()
260
+ gaia .Cfg = new (gaia.Config )
261
+ gaia .Cfg .PipelinePath = tmp
262
+
263
+ p := gaia.Pipeline {
264
+ Name : "Pipeline A" ,
265
+ Type : gaia .PTypeGolang ,
266
+ Created : time .Now (),
267
+ }
268
+
269
+ newName := "Pipeline B"
270
+
271
+ src := filepath .Join (tmp , appendTypeToName (p .Name , p .Type ))
272
+ dst := filepath .Join (tmp , appendTypeToName (newName , p .Type ))
273
+ f , _ := os .Create (src )
274
+ defer f .Close ()
275
+ defer os .Remove (src )
276
+ defer os .Remove (dst )
277
+
278
+ ioutil .WriteFile (src , []byte ("testcontent" ), 0666 )
279
+
280
+ err := RenameBinary (p , newName )
281
+ if err != nil {
282
+ t .Fatal ("an error occured while renaming the binary: " , err )
283
+ }
284
+
285
+ content , err := ioutil .ReadFile (dst )
286
+ if err != nil {
287
+ t .Fatal ("an error occured while reading destination file: " , err )
288
+ }
289
+ if string (content ) != "testcontent" {
290
+ t .Fatal ("file content does not equal src content. was: " , string (content ))
291
+ }
292
+
293
+ }
294
+
295
+ func TestDeleteBinary (t * testing.T ) {
296
+ tmp := os .TempDir ()
297
+ gaia .Cfg = new (gaia.Config )
298
+ gaia .Cfg .PipelinePath = tmp
299
+
300
+ p := gaia.Pipeline {
301
+ Name : "Pipeline A" ,
302
+ Type : gaia .PTypeGolang ,
303
+ Created : time .Now (),
304
+ }
305
+
306
+ src := filepath .Join (tmp , appendTypeToName (p .Name , p .Type ))
307
+ f , _ := os .Create (src )
308
+ defer f .Close ()
309
+ defer os .Remove (src )
310
+
311
+ ioutil .WriteFile (src , []byte ("testcontent" ), 0666 )
312
+
313
+ err := DeleteBinary (p )
314
+ if err != nil {
315
+ t .Fatal ("an error occured while deleting the binary: " , err )
316
+ }
317
+
318
+ _ , err = os .Stat (src )
319
+ if ! os .IsNotExist (err ) {
320
+ t .Fatal ("the binary file still exists. It should have been deleted" )
321
+ }
322
+ }
323
+
324
+ func TestGetExecPath (t * testing.T ) {
325
+ tmp := os .TempDir ()
326
+ gaia .Cfg = new (gaia.Config )
327
+ gaia .Cfg .PipelinePath = tmp
328
+
329
+ p := gaia.Pipeline {
330
+ Name : "Pipeline A" ,
331
+ Type : gaia .PTypeGolang ,
332
+ Created : time .Now (),
333
+ }
334
+
335
+ expectedPath := fmt .Sprintf ("%s%s_%s" , tmp , p .Name , p .Type )
336
+ execPath := GetExecPath (p )
337
+
338
+ if execPath != expectedPath {
339
+ t .Fatalf ("expected execpath to be %s. got %s" , expectedPath , execPath )
340
+ }
341
+ }
0 commit comments