Skip to content

Commit caf8060

Browse files
committed
add pipeline tests
1 parent 3457391 commit caf8060

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

pipeline/pipeline_test.go

+140
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package pipeline
22

33
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
48
"testing"
59
"time"
610

@@ -25,6 +29,32 @@ func TestAppend(t *testing.T) {
2529

2630
}
2731

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+
2858
func TestRemove(t *testing.T) {
2959
ap := NewActivePipelines()
3060

@@ -115,6 +145,31 @@ func TestReplace(t *testing.T) {
115145
}
116146
}
117147

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+
118173
func TestIter(t *testing.T) {
119174
ap := NewActivePipelines()
120175

@@ -199,3 +254,88 @@ func TestRemoveDeletedPipelines(t *testing.T) {
199254
}
200255

201256
}
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

Comments
 (0)