Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created an error value, in case the for loop fails with index not found. #206

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion handlers/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ func PipelineDelete(c echo.Context) error {
}

// Remove from active pipelines
pipeline.GlobalActivePipelines.Remove(deletedPipelineIndex)
if err := pipeline.GlobalActivePipelines.Remove(deletedPipelineIndex); err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}

return c.String(http.StatusOK, "Pipeline has been deleted")
}
Expand Down
22 changes: 18 additions & 4 deletions workers/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,28 @@ func (ap *ActivePipelines) Append(p gaia.Pipeline) {
}

// Update updates a pipeline at the given index with the given pipeline.
func (ap *ActivePipelines) Update(index int, p gaia.Pipeline) {
func (ap *ActivePipelines) Update(index int, p gaia.Pipeline) error {
ap.Lock()
defer ap.Unlock()

if index >= len(ap.Pipelines) || index < 0 {
return fmt.Errorf("invalid index for len %d. index was: %d", len(ap.Pipelines), index)
}
ap.Pipelines[index] = p
return nil
}

// Remove removes a pipeline at the given index from ActivePipelines.
func (ap *ActivePipelines) Remove(index int) {
func (ap *ActivePipelines) Remove(index int) error {
ap.Lock()
defer ap.Unlock()

l := len(ap.Pipelines)
if index >= l || index+1 > l || index < 0 {
return fmt.Errorf("invalid index for len %d. index was: %d", len(ap.Pipelines), index)
}
ap.Pipelines = append(ap.Pipelines[:index], ap.Pipelines[index+1:]...)
return nil
}

// GetByName looks up the pipeline by the given name.
Expand Down Expand Up @@ -175,7 +184,9 @@ func (ap *ActivePipelines) Replace(p gaia.Pipeline) bool {
func (ap *ActivePipelines) ReplaceByName(n string, p gaia.Pipeline) bool {
for index, pipeline := range ap.GetAll() {
if pipeline.Name == n {
ap.Update(index, p)
// We can safely ignore the error here, since it wouldn't even
// come this far if it didn't find what to update.
_ = ap.Update(index, p)
return true
}
}
Expand Down Expand Up @@ -220,7 +231,10 @@ func (ap *ActivePipelines) RemoveDeletedPipelines(existingPipelineNames []string
}
}
for _, idx := range deletedPipelineIndices {
ap.Remove(idx)
if err := ap.Remove(idx); err != nil {
gaia.Cfg.Logger.Error("failed to remove pipeline with index", "index", idx, "error", err.Error())
break
}
}
}

Expand Down
60 changes: 58 additions & 2 deletions workers/pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func TestUpdate(t *testing.T) {
Created: time.Now(),
}

ap.Update(0, p2)
err := ap.Update(0, p2)
if err != nil {
t.Fatal(err)
}

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

Expand All @@ -54,6 +57,28 @@ func TestUpdate(t *testing.T) {

}

func TestUpdateIndexOutOfBounds(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(),
}

err := ap.Update(1, p2)
if err == nil {
t.Fatal("expected error to occur since we are out of bounds")
}
}

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

Expand All @@ -71,7 +96,10 @@ func TestRemove(t *testing.T) {
}
ap.Append(p2)

ap.Remove(1)
err := ap.Remove(1)
if err != nil {
t.Fatal(err)
}

count := 0
for _, pipeline := range ap.GetAll() {
Expand All @@ -86,6 +114,34 @@ func TestRemove(t *testing.T) {
}
}

func TestRemoveInvalidIndex(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)

err := ap.Remove(2)
if err == nil {
t.Fatal("expected error when accessing something outside the length ")
}

err = ap.Remove(3)
if err == nil {
t.Fatal("expected error when accessing something outside the length ")
}
}

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

Expand Down