Skip to content

Commit

Permalink
Change FindTaskByID
Browse files Browse the repository at this point in the history
Change FindTaskByID so that Marathon-Consul can be used with marathon version > 1.9, this change will fix issue: allegro#296

Refactor test so the output resembles marathon v1.9
Also added 3 tests to test the find tasks method.

This change should be backwards compatible
  • Loading branch information
Jurrian Fahner committed Nov 14, 2019
1 parent 775145b commit fc6ad3c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type TasksResponse struct {

func FindTaskByID(id TaskID, tasks []Task) (Task, bool) {
for _, task := range tasks {
if task.ID == id {
if strings.HasPrefix(task.ID.String(), id.String()) && len(id.String) > 36 {
return task, true
}
}
Expand Down
40 changes: 38 additions & 2 deletions apps/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ func TestParseTasks(t *testing.T) {

expectedTasks := []Task{
{
ID: "test.47de43bd-1a81-11e5-bdb6-e6cb6734eaf8",
ID: "test.47de43bd-1a81-11e5-bdb6-e6cb6734eaf8._app.1",
Timestamp: time.Timestamp{},
AppID: "/test",
Host: "192.168.2.114",
Ports: []int{31315},
HealthCheckResults: []HealthCheckResult{{Alive: true}},
},
{
ID: "test.4453212c-1a81-11e5-bdb6-e6cb6734eaf8",
ID: "test.4453212c-1a81-11e5-bdb6-e6cb6734eaf8._app.1",
AppID: "/test",
Host: "192.168.2.114",
Ports: []int{31797},
Expand Down Expand Up @@ -137,3 +137,39 @@ func TestId_AppIdForInvalidIdShouldPanic(t *testing.T) {
assert.Nil(t, a)
})
}

func TestFindTaskByIdNotExactMatch(t *testing.T) { // Marathon version 1.9 doesn't match the event app id with the current app id, because it has not the suffix "._app.1"
t.Parallel()

tasksBlob, _ := ioutil.ReadFile("testdata/tasks.json")
tasks, err := ParseTasks(tasksBlob)
assert.Nil(t, err)

task := TaskID("test.4453212c-1a81-11e5-bdb6-e6cb6734eaf8")
_, found := FindTaskByID(task, tasks)
assert.True(t, found)
}

func TestFindTaskByIdNotFound(t *testing.T) {
t.Parallel()

tasksBlob, _ := ioutil.ReadFile("testdata/tasks.json")
tasks, err := ParseTasks(tasksBlob)
assert.Nil(t, err)

task := TaskID("this-task-doesnt-exist")
_, found := FindTaskByID(task, tasks)
assert.False(t, found)
}

func TestFindTaskByIdExactMatch(t *testing.T) { // When marathon app id in events is the same as the app id in the task, which is also currently the case.
t.Parallel()

tasksBlob, _ := ioutil.ReadFile("testdata/tasks.json")
tasks, err := ParseTasks(tasksBlob)
assert.Nil(t, err)

task := TaskID("test.47de43bd-1a81-11e5-bdb6-e6cb6734eaf8._app.1")
_, found := FindTaskByID(task, tasks)
assert.True(t, found)
}

0 comments on commit fc6ad3c

Please sign in to comment.