Skip to content

Commit

Permalink
add tests for Remove and RemoveDeletedPipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
bidhan-a committed Jul 16, 2018
1 parent 51ede30 commit a19711a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func (ap *ActivePipelines) Contains(n string) bool {
// RemoveDeletedPipelines removes the pipelines whose names are NOT
// present in `existingPipelineNames` from the given ActivePipelines instance.
func (ap *ActivePipelines) RemoveDeletedPipelines(existingPipelineNames []string) {
var deletedPipelineIndices []int
var index int
for pipeline := range ap.Iter() {
found := false
Expand All @@ -178,10 +179,13 @@ func (ap *ActivePipelines) RemoveDeletedPipelines(existingPipelineNames []string
}
}
if !found {
ap.Remove(index)
deletedPipelineIndices = append(deletedPipelineIndices, index)
}
index++
}
for _, idx := range deletedPipelineIndices {
ap.Remove(idx)
}
}

// appendTypeToName appends the type to the output binary name.
Expand Down
77 changes: 77 additions & 0 deletions pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package pipeline

import (
"testing"
"time"

"github.com/gaia-pipeline/gaia"
)

func TestRemove(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.Append(p2)

ap.Remove(1)

count := 0
for pipeline := range ap.Iter() {
count++
if pipeline.Name == "Pipeline B" {
t.Fatalf("Pipeline B still exists. It should have been removed.")
}
}

if count != 1 {
t.Fatalf("Expected pipeline count to be %v. Got %v.", 1, count)
}
}

func TestRemoveDeletedPipelines(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.Append(p2)

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

// Let's assume Pipeline B was deleted.
existingPipelineNames := []string{"Pipeline A", "Pipeline C"}

ap.RemoveDeletedPipelines(existingPipelineNames)

for pipeline := range ap.Iter() {
if pipeline.Name == "Pipeline B" {
t.Fatalf("Pipeline B still exists. It should have been removed.")
}
}

}

0 comments on commit a19711a

Please sign in to comment.