diff --git a/pipeline/pipeline.go b/pipeline/pipeline.go index 9a7952be..65e6ebb8 100644 --- a/pipeline/pipeline.go +++ b/pipeline/pipeline.go @@ -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 @@ -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. diff --git a/pipeline/pipeline_test.go b/pipeline/pipeline_test.go new file mode 100644 index 00000000..0972858e --- /dev/null +++ b/pipeline/pipeline_test.go @@ -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.") + } + } + +}