Skip to content

Commit

Permalink
add pipeline tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bidhan-a committed Jul 24, 2018
1 parent 3457391 commit caf8060
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package pipeline

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

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

}

func TestUpdate(t *testing.T) {
ap := NewActivePipelines()

p1 := gaia.Pipeline{
Name: "Pipeline A",
Type: gaia.PTypeGolang,
Created: time.Now(),
}
ap.Append(p1)

p2 := gaia.Pipeline{
Name: "Pipeline B",
Type: gaia.PTypeGolang,
Created: time.Now(),
}

ap.Update(0, p2)

ret := ap.GetByName("Pipeline B")

if p2.Name != ret.Name {
t.Fatalf("Pipeline should have been updated.")
}

}

func TestRemove(t *testing.T) {
ap := NewActivePipelines()

Expand Down Expand Up @@ -115,6 +145,31 @@ func TestReplace(t *testing.T) {
}
}

func TestReplaceByName(t *testing.T) {
ap := NewActivePipelines()

p1 := gaia.Pipeline{
Name: "Pipeline A",
Type: gaia.PTypeGolang,
Created: time.Now(),
}
ap.Append(p1)

p2 := gaia.Pipeline{
Name: "Pipeline B",
Type: gaia.PTypeGolang,
Created: time.Now(),
}

ap.ReplaceByName("Pipeline A", p2)

ret := ap.GetByName("Pipeline B")

if p2.Name != ret.Name {
t.Fatalf("Pipeline should have been updated.")
}
}

func TestIter(t *testing.T) {
ap := NewActivePipelines()

Expand Down Expand Up @@ -199,3 +254,88 @@ func TestRemoveDeletedPipelines(t *testing.T) {
}

}

func TestRenameBinary(t *testing.T) {
tmp := os.TempDir()
gaia.Cfg = new(gaia.Config)
gaia.Cfg.PipelinePath = tmp

p := gaia.Pipeline{
Name: "Pipeline A",
Type: gaia.PTypeGolang,
Created: time.Now(),
}

newName := "Pipeline B"

src := filepath.Join(tmp, appendTypeToName(p.Name, p.Type))
dst := filepath.Join(tmp, appendTypeToName(newName, p.Type))
f, _ := os.Create(src)
defer f.Close()
defer os.Remove(src)
defer os.Remove(dst)

ioutil.WriteFile(src, []byte("testcontent"), 0666)

err := RenameBinary(p, newName)
if err != nil {
t.Fatal("an error occured while renaming the binary: ", err)
}

content, err := ioutil.ReadFile(dst)
if err != nil {
t.Fatal("an error occured while reading destination file: ", err)
}
if string(content) != "testcontent" {
t.Fatal("file content does not equal src content. was: ", string(content))
}

}

func TestDeleteBinary(t *testing.T) {
tmp := os.TempDir()
gaia.Cfg = new(gaia.Config)
gaia.Cfg.PipelinePath = tmp

p := gaia.Pipeline{
Name: "Pipeline A",
Type: gaia.PTypeGolang,
Created: time.Now(),
}

src := filepath.Join(tmp, appendTypeToName(p.Name, p.Type))
f, _ := os.Create(src)
defer f.Close()
defer os.Remove(src)

ioutil.WriteFile(src, []byte("testcontent"), 0666)

err := DeleteBinary(p)
if err != nil {
t.Fatal("an error occured while deleting the binary: ", err)
}

_, err = os.Stat(src)
if !os.IsNotExist(err) {
t.Fatal("the binary file still exists. It should have been deleted")
}
}

func TestGetExecPath(t *testing.T) {
tmp := os.TempDir()
gaia.Cfg = new(gaia.Config)
gaia.Cfg.PipelinePath = tmp

p := gaia.Pipeline{
Name: "Pipeline A",
Type: gaia.PTypeGolang,
Created: time.Now(),
}

expectedPath := fmt.Sprintf("%s%s_%s", tmp, p.Name, p.Type)
execPath := GetExecPath(p)

if execPath != expectedPath {
t.Fatalf("expected execpath to be %s. got %s", expectedPath, execPath)
}
}

0 comments on commit caf8060

Please sign in to comment.