func TestSyncApps(t *testing.T) { t.Parallel() kv := mocks.NewKVer() deleteMe := &api.KVPair{Key: "marathon/deleteMe", Value: []byte("app")} deleteMeTask := &api.KVPair{Key: "marathon/deleteMe/tasks/test", Value: []byte("task")} testAppKV := &api.KVPair{Key: "marathon/testApp", Value: []byte("app")} testAppKVTask := &api.KVPair{Key: "marathon/testApp/tasks/test", Value: []byte("task")} kv.Put(deleteMe) kv.Put(deleteMeTask) kv.Put(testAppKV) kv.Put(testAppKVTask) // test! consul := Consul{kv, appPrefix} err := consul.SyncApps([]*apps.App{testApp}) assert.Nil(t, err) // testApp should have been updated newTestApp, _, err := kv.Get(testAppKV.Key) assert.Nil(t, err) assert.NotEqual(t, testAppKV, newTestApp) // deleteMe should have been deleted newDeleteMe, _, err := kv.Get(deleteMe.Key) assert.Nil(t, err) assert.Nil(t, newDeleteMe) // deleteMeTask should have been deleted newDeleteMeTask, _, err := kv.Get(deleteMeTask.Key) assert.Nil(t, err) assert.Nil(t, newDeleteMeTask) }
func TestSyncTasks(t *testing.T) { t.Parallel() kv := mocks.NewKVer() testAppKV := &api.KVPair{Key: "marathon/testApp", Value: []byte("app")} deleteTaskKV := &api.KVPair{Key: "marathon/testApp/tasks/delete", Value: []byte("task")} kv.Put(testAppKV) kv.Put(deleteTaskKV) tasks := []*tasks.Task{testTask} // test! consul := Consul{kv, appPrefix} err := consul.SyncTasks(testApp.ID, tasks) assert.Nil(t, err) // deleteTaskKV should not be present newDeleteTaskKV, _, err := kv.Get(deleteTaskKV.Key) assert.Nil(t, err) assert.Nil(t, newDeleteTaskKV) // a new task (with ID "new" should have been added) newTask, _, err := kv.Get("marathon/testApp/tasks/testTask") if assert.NotNil(t, newTask) { assert.True(t, bytes.Equal(newTask.Value, testTask.KV().Value)) } }
func TestForwardHandlerHandleTerminationEvent(t *testing.T) { t.Parallel() // create a handler kv := mocks.NewKVer() consul := consul.NewConsul(kv, "") handler := ForwardHandler{consul} err := consul.UpdateApp(testApp) assert.Nil(t, err) body, err := json.Marshal(events.AppTerminatedEvent{ Type: "app_terminated_event", AppID: testApp.ID, }) assert.Nil(t, err) // test! recorder := httptest.NewRecorder() handler.HandleTerminationEvent(recorder, body) assert.Equal(t, 200, recorder.Code) assert.Equal(t, "OK\n", recorder.Body.String()) result, _, err := kv.Get(testApp.Key()) assert.Nil(t, err) assert.Nil(t, result) }
func TestForwardHandlerHandleTerminationEvent(t *testing.T) { t.Parallel() // create a handler kv := mocks.NewKVer() consul := consul.NewConsul(kv, "") handler := ForwardHandler{consul} err := consul.UpdateApp(testApp) assert.Nil(t, err) body, err := json.Marshal(events.AppTerminatedEvent{ Type: "app_terminated_event", AppID: testApp.ID, }) assert.Nil(t, err) // test! err = handler.HandleTerminationEvent(body) assert.Nil(t, err) result, _, err := kv.Get(testApp.Key()) assert.Nil(t, err) assert.Nil(t, result) }
func TestForwardHandlerHandleStatusEvent(t *testing.T) { t.Parallel() // create a handler kv := mocks.NewKVer() consul := consul.NewConsul(kv, "") handler := ForwardHandler{consul} // deletes for _, status := range []string{"TASK_FINISHED", "TASK_FAILED", "TASK_KILLED", "TASK_LOST"} { tempBody := tempTaskBody(status) err := consul.UpdateTask(testTask) assert.Nil(t, err) // test recorder := httptest.NewRecorder() handler.HandleStatusEvent(recorder, tempBody) assert.Equal(t, 200, recorder.Code) assert.Equal(t, "OK\n", recorder.Body.String()) // assert result, _, err := kv.Get(testTask.Key()) assert.Nil(t, err) assert.Nil(t, result) } // puts for _, status := range []string{"TASK_STAGING", "TASK_STARTING", "TASK_RUNNING"} { tempBody := tempTaskBody(status) tempTask, _ := tasks.ParseTask(tempBody) // test recorder := httptest.NewRecorder() handler.HandleStatusEvent(recorder, tempBody) assert.Equal(t, 200, recorder.Code) assert.Equal(t, "OK\n", recorder.Body.String()) // assert result, _, err := kv.Get(tempTask.Key()) assert.Nil(t, err) assert.Equal(t, result, tempTask.KV()) // cleanup _, err = kv.Delete(testTask.Key()) assert.Nil(t, err) } // bad status tempBody := tempTaskBody("TASK_BATMAN") recorder := httptest.NewRecorder() handler.HandleStatusEvent(recorder, tempBody) assert.Equal(t, 500, recorder.Code) assert.Equal(t, "unknown task status\n", recorder.Body.String()) }
func TestCreateTask(t *testing.T) { t.Parallel() kv := mocks.NewKVer() // test! consul := Consul{kv, appPrefix} err := consul.UpdateTask(testTask) assert.Nil(t, err) // testTask should have been updated newTaskKV, _, err := kv.Get("marathon/testApp/tasks/testTask") assert.Nil(t, err) assert.NotNil(t, newTaskKV) }
func TestDeleteApp(t *testing.T) { t.Parallel() kv := mocks.NewKVer() oldAppKV := testApp.KV() oldAppKV.Key = WithPrefix(appPrefix, oldAppKV.Key) kv.Put(oldAppKV) // test! consul := Consul{kv, appPrefix} err := consul.DeleteApp(testApp) assert.Nil(t, err) // testApp should have been deleted newAppKV, _, err := kv.Get(oldAppKV.Key) assert.Nil(t, err) assert.Nil(t, newAppKV) }
func TestUpdateApp(t *testing.T) { t.Parallel() kv := mocks.NewKVer() oldAppKV := &api.KVPair{Key: "marathon/testApp", Value: []byte("app")} kv.Put(oldAppKV) // test! consul := Consul{kv, appPrefix} err := consul.UpdateApp(testApp) assert.Nil(t, err) // testApp should have been updated newAppKV, _, err := kv.Get(oldAppKV.Key) assert.Nil(t, err) assert.NotEqual(t, oldAppKV, newAppKV) }
func TestForwardHandlerHandleAppEvent(t *testing.T) { t.Parallel() // create a handler kv := mocks.NewKVer() consul := consul.NewConsul(kv, "") handler := ForwardHandler{consul} body, err := json.Marshal(events.APIPostEvent{"api_post_event", testApp}) assert.Nil(t, err) // test! err = handler.HandleAppEvent(body) assert.Nil(t, err) result, _, err := kv.Get(testApp.Key()) assert.Nil(t, err) assert.Equal(t, result, testAppKV) }
func TestDeleteTask(t *testing.T) { t.Parallel() kv := mocks.NewKVer() oldTaskKV := &api.KVPair{ Key: "marathon/testApp/tasks/testTask", Value: []byte(""), } kv.Put(oldTaskKV) // test! consul := Consul{kv, appPrefix} err := consul.DeleteTask(testTask) assert.Nil(t, err) // testTask should have been deleted newTaskKV, _, err := kv.Get(oldTaskKV.Key) assert.Nil(t, err) assert.Nil(t, newTaskKV) }
func TestForwardHandlerHandleAppEvent(t *testing.T) { t.Parallel() // create a handler kv := mocks.NewKVer() consul := consul.NewConsul(kv, "") handler := ForwardHandler{consul} body, err := json.Marshal(events.APIPostEvent{"api_post_event", testApp}) assert.Nil(t, err) // test! recorder := httptest.NewRecorder() handler.HandleAppEvent(recorder, body) assert.Equal(t, 200, recorder.Code) assert.Equal(t, "OK\n", recorder.Body.String()) result, _, err := kv.Get(testApp.Key()) assert.Nil(t, err) assert.Equal(t, result, testAppKV) }