Repository: allegro/marathon-consul
Pull request #119 (2016-09-09 14:25:03)

Change structs to package private
`AppWrapper` and `eventHandler.New()` do not need to be public.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/7817156/badge)](https://coveralls.io/builds/7817156)

Coverage remained the same at 79.145% when pulling **c9228eefd88140338dd0a8a1dfd81b7c72362ad5 on reduce_scope** into **a6d4e3cb299b8cd9b9180dc229d9c2293ad14e03 on develop**.

dankraw: 👍 

Patch:
diff --git a/apps/app.go b/apps/app.go
index 5faa498..b512149 100644
--- a/apps/app.go
+++ b/apps/app.go
@@ -28,7 +28,7 @@ type PortDefinition struct {
 	Labels map[string]string `json:"labels"`
 }
 
-type AppWrapper struct {
+type appWrapper struct {
 	App App `json:"app"`
 }
 
@@ -107,7 +107,7 @@ func ParseApps(jsonBlob []byte) ([]*App, error) {
 }
 
 func ParseApp(jsonBlob []byte) (*App, error) {
-	wrapper := &AppWrapper{}
+	wrapper := &appWrapper{}
 	err := json.Unmarshal(jsonBlob, wrapper)
 
 	return &wrapper.App, err
@@ -182,24 +182,24 @@ func (app *App) labelsToName(labels map[string]string, nameSeparator string) str
 	return serviceName
 }
 
-type IndexedPortDefinition struct {
+type indexedPortDefinition struct {
 	Index  int
 	Port   int
 	Labels map[string]string
 }
 
-func (i *IndexedPortDefinition) toPort(task *Task) int {
+func (i *indexedPortDefinition) toPort(task *Task) int {
 	if i.Port == 0 {
 		return task.Ports[i.Index]
 	}
 	return i.Port
 }
 
-func (app *App) findConsulPortDefinitions() []IndexedPortDefinition {
-	var definitions []IndexedPortDefinition
+func (app *App) findConsulPortDefinitions() []indexedPortDefinition {
+	var definitions []indexedPortDefinition
 	for i, d := range app.PortDefinitions {
 		if _, ok := d.Labels[MarathonConsulLabel]; ok {
-			definitions = append(definitions, IndexedPortDefinition{
+			definitions = append(definitions, indexedPortDefinition{
 				Index:  i,
 				Port:   d.Port,
 				Labels: d.Labels,
diff --git a/consul/consul.go b/consul/consul.go
index bf9f6fa..26e5fa5 100644
--- a/consul/consul.go
+++ b/consul/consul.go
@@ -1,6 +1,7 @@
 package consul
 
 import (
+	"errors"
 	"fmt"
 	"net/url"
 	"strings"
@@ -52,7 +53,7 @@ func (c *Consul) getServicesUsingProviderWithRetriesOnAgentFailure(provide Servi
 			return services, nil
 		}
 	}
-	return nil, fmt.Errorf("An error occurred getting services from Consul. Giving up")
+	return nil, errors.New("An error occurred getting services from Consul. Giving up")
 }
 
 func (c *Consul) getServicesUsingAgent(name string, agent *consulapi.Client) ([]*service.Service, error) {
@@ -169,7 +170,7 @@ func (c *Consul) registerMultipleServices(services []*consulapi.AgentServiceRegi
 		}
 	}
 
-	return utils.MergeErrorsOrNil(registerErrors, fmt.Sprintf("registering services"))
+	return utils.MergeErrorsOrNil(registerErrors, fmt.Sprint("registering services"))
 }
 
 func (c *Consul) register(service *consulapi.AgentServiceRegistration) error {
diff --git a/events/events.go b/events/events.go
index 30f40fe..3b23cea 100644
--- a/events/events.go
+++ b/events/events.go
@@ -7,26 +7,22 @@ import (
 	"github.com/allegro/marathon-consul/apps"
 )
 
-var (
-	ErrNoEvent = errors.New("no event")
-)
-
 type Event interface {
 	Apps() []*apps.App
 	GetType() string
 }
 
-type BaseEvent struct {
+type baseEvent struct {
 	Type string `json:"eventType"`
 }
 
 func EventType(jsonBlob []byte) (string, error) {
-	event := BaseEvent{}
+	event := baseEvent{}
 	err := json.Unmarshal(jsonBlob, &event)
 	if err != nil {
 		return "", err
 	} else if event.Type == "" {
-		return "", ErrNoEvent
+		return "", errors.New("no event")
 	} else {
 		return event.Type, nil
 	}
diff --git a/utils/net.go b/utils/net.go
index e190e04..cc95a3b 100644
--- a/utils/net.go
+++ b/utils/net.go
@@ -1,7 +1,7 @@
 package utils
 
 import (
-	"fmt"
+	"errors"
 	"net"
 )
 
@@ -15,5 +15,5 @@ func HostToIPv4(host string) (net.IP, error) {
 			return IP, nil
 		}
 	}
-	return nil, fmt.Errorf("Could not resolve host to IPv4")
+	return nil, errors.New("Could not resolve host to IPv4")
 }
diff --git a/web/event_handler.go b/web/event_handler.go
index 2c6f1e5..4ae16e6 100644
--- a/web/event_handler.go
+++ b/web/event_handler.go
@@ -38,7 +38,7 @@ func newEventHandler(id int, serviceRegistry service.ServiceRegistry, marathon m
 	}
 }
 
-func (fh *eventHandler) Start() chan<- stopEvent {
+func (fh *eventHandler) start() chan<- stopEvent {
 	var e event
 	process := func() {
 		err := fh.handleEvent(e.eventType, e.body)
diff --git a/web/health_handler.go b/web/health_handler.go
index 9376b22..20ecdb8 100644
--- a/web/health_handler.go
+++ b/web/health_handler.go
@@ -5,6 +5,6 @@ import (
 	"net/http"
 )
 
-func HealthHandler(w http.ResponseWriter, r *http.Request) {
+func HealthHandler(w http.ResponseWriter, _ *http.Request) {
 	fmt.Fprintln(w, "OK")
 }
diff --git a/web/web.go b/web/web.go
index c58e5a1..0e9e5e0 100644
--- a/web/web.go
+++ b/web/web.go
@@ -16,7 +16,7 @@ func NewHandler(config Config, marathon marathon.Marathoner, serviceOperations s
 	eventQueue := make(chan event, config.QueueSize)
 	for i := 0; i < config.WorkersCount; i++ {
 		handler := newEventHandler(i, serviceOperations, marathon, eventQueue)
-		stopChannels[i] = handler.Start()
+		stopChannels[i] = handler.start()
 	}
 	return newWebHandler(eventQueue).Handle, stop(stopChannels)
 }


Test patch:
diff --git a/events/events_test.go b/events/events_test.go
index 8c15776..7c43856 100644
--- a/events/events_test.go
+++ b/events/events_test.go
@@ -18,7 +18,7 @@ func TestEventTypeInvalid(t *testing.T) {
 	t.Parallel()
 
 	out, err := EventType([]byte(`{}`))
-	assert.Equal(t, err, ErrNoEvent)
+	assert.Error(t, err)
 	assert.Equal(t, out, "")
 }
 
diff --git a/web/event_handler_test.go b/web/event_handler_test.go
index d48a3bb..f13e2df 100644
--- a/web/event_handler_test.go
+++ b/web/event_handler_test.go
@@ -23,7 +23,7 @@ func TestWebHandler_NotHandleUnknownEventType(t *testing.T) {
 	// given
 	queue := make(chan event)
 	handler := newWebHandler(queue)
-	stopChan := newEventHandler(0, nil, nil, queue).Start()
+	stopChan := newEventHandler(0, nil, nil, queue).start()
 	req, _ := http.NewRequest("POST", "/events", bytes.NewBuffer([]byte(`{"eventType":"test_event"}`)))
 
 	// when
@@ -121,7 +121,7 @@ func TestWebHandler_HandleAppInvalidBody(t *testing.T) {
 	// given
 	queue := make(chan event)
 	handler := newWebHandler(queue)
-	stopChan := newEventHandler(0, nil, nil, queue).Start()
+	stopChan := newEventHandler(0, nil, nil, queue).start()
 	body := `{"type":  "app_terminated_event", "appID": 123}`
 	req, _ := http.NewRequest("POST", "/events", bytes.NewBuffer([]byte(body)))
 
@@ -498,7 +498,7 @@ func TestWebHandler_NotHandleStatusEventWithInvalidBody(t *testing.T) {
 	// given
 	queue := make(chan event)
 	handler := newWebHandler(queue)
-	stopChan := newEventHandler(0, nil, nil, queue).Start()
+	stopChan := newEventHandler(0, nil, nil, queue).start()
 	body := `{
 	  "slaveId":"85e59460-a99e-4f16-b91f-145e0ea595bd-S0",
 	  "taskId":"python_simple.4a7e99d0-9cc1-11e5-b4d8-0a0027000004",
@@ -528,7 +528,7 @@ func TestWebHandler_NotHandleStatusEventAboutStartingTask(t *testing.T) {
 	// given
 	services := consul.NewConsulStub()
 	queue := make(chan event)
-	stopChan := newEventHandler(0, services, nil, queue).Start()
+	stopChan := newEventHandler(0, services, nil, queue).start()
 	handler := newWebHandler(queue)
 	ignoredTaskStatuses := []string{"TASK_STAGING", "TASK_STARTING", "TASK_RUNNING", "unknown"}
 	for _, taskStatus := range ignoredTaskStatuses {
@@ -571,7 +571,7 @@ func TestWebHandler_HandleStatusEventAboutDeadTask(t *testing.T) {
 			service.Register(&task, app)
 		}
 		queue := make(chan event)
-		stopChan := newEventHandler(0, service, nil, queue).Start()
+		stopChan := newEventHandler(0, service, nil, queue).start()
 		handler := newWebHandler(queue)
 		body := `{
 		  "slaveId":"85e59460-a99e-4f16-b91f-145e0ea595bd-S0",
@@ -614,7 +614,7 @@ func TestWebHandler_HandleEventAboutUnhealthyKilledTask(t *testing.T) {
 		service.Register(&task, app)
 	}
 	queue := make(chan event)
-	stopChan := newEventHandler(0, service, nil, queue).Start()
+	stopChan := newEventHandler(0, service, nil, queue).start()
 	handler := newWebHandler(queue)
 	body := `{
 	  "appId": "/test/app",
@@ -649,7 +649,7 @@ func TestWebHandler_NotHandleEventAboutUnhealthyKilledTaskWithInvalidBody(t *tes
 	// given
 	queue := make(chan event)
 	handler := newWebHandler(queue)
-	stopChan := newEventHandler(0, nil, nil, queue).Start()
+	stopChan := newEventHandler(0, nil, nil, queue).start()
 	body := `{
 	  "appId": "/test/app",
 	  "taskId": "test.app.1",
@@ -679,7 +679,7 @@ func TestWebHandler_HandleStatusEventAboutDeadTaskErrOnDeregistration(t *testing
 	service := consul.NewConsulStub()
 	service.FailDeregisterByTaskForID("task.1")
 	queue := make(chan event)
-	stopChan := newEventHandler(0, service, nil, queue).Start()
+	stopChan := newEventHandler(0, service, nil, queue).start()
 	handler := newWebHandler(queue)
 	body := `{
 	  "slaveId":"85e59460-a99e-4f16-b91f-145e0ea595bd-S0",
@@ -714,7 +714,7 @@ func TestWebHandler_NotHandleStatusEventAboutNonConsulAppsDeadTask(t *testing.T)
 	app := NonConsulApp("/test/app", 3)
 	service := consul.NewConsulStub()
 	queue := make(chan event)
-	stopChan := newEventHandler(0, service, nil, queue).Start()
+	stopChan := newEventHandler(0, service, nil, queue).start()
 	handler := newWebHandler(queue)
 	taskStatuses := []string{"TASK_FINISHED", "TASK_FAILED", "TASK_KILLED", "TASK_LOST"}
 	for _, taskStatus := range taskStatuses {
@@ -752,7 +752,7 @@ func TestWebHandler_NotHandleHealthStatusEventWhenAppHasNotConsulLabel(t *testin
 	app := NonConsulApp("/test/app", 3)
 	marathon := marathon.MarathonerStubForApps(app)
 	queue := make(chan event)
-	stopChan := newEventHandler(0, nil, marathon, queue).Start()
+	stopChan := newEventHandler(0, nil, marathon, queue).start()
 	handler := newWebHandler(queue)
 	body := healthStatusChangeEventForTask("test_app.1")
 	req, _ := http.NewRequest("POST", "/events", bytes.NewBuffer([]byte(body)))
@@ -822,7 +822,7 @@ func TestWebHandler_NotHandleHealthStatusEventForTaskWithNotAllHealthChecksPasse
 	app.Tasks[1].HealthCheckResults = []apps.HealthCheckResult{{Alive: true}, {Alive: false}}
 	marathon := marathon.MarathonerStubForApps(app)
 	queue := make(chan event)
-	stopChan := newEventHandler(0, nil, marathon, queue).Start()
+	stopChan := newEventHandler(0, nil, marathon, queue).start()
 	handler := newWebHandler(queue)
 	body := healthStatusChangeEventForTask("test_app.1")
 	req, _ := http.NewRequest("POST", "/events", bytes.NewBuffer([]byte(body)))
@@ -845,7 +845,7 @@ func TestWebHandler_NotHandleHealthStatusEventForTaskWithNoHealthCheck(t *testin
 	app.Tasks[0].HealthCheckResults = []apps.HealthCheckResult{}
 	marathon := marathon.MarathonerStubForApps(app)
 	queue := make(chan event)
-	stopChan := newEventHandler(0, nil, marathon, queue).Start()
+	stopChan := newEventHandler(0, nil, marathon, queue).start()
 	handler := newWebHandler(queue)
 	body := healthStatusChangeEventForTask("/test/app.0")
 	req, _ := http.NewRequest("POST", "/events", bytes.NewBuffer([]byte(body)))
@@ -865,7 +865,7 @@ func TestWebHandler_NotHandleHealthStatusEventWhenTaskIsNotAlive(t *testing.T) {
 
 	// given
 	queue := make(chan event)
-	stopChan := newEventHandler(0, nil, nil, queue).Start()
+	stopChan := newEventHandler(0, nil, nil, queue).start()
 	handler := newWebHandler(queue)
 	body := `{
 	  "appId":"/test/app",
@@ -891,7 +891,7 @@ func TestWebHandler_NotHandleHealthStatusEventWhenBodyIsInvalid(t *testing.T) {
 
 	// given
 	queue := make(chan event)
-	stopChan := newEventHandler(0, nil, nil, queue).Start()
+	stopChan := newEventHandler(0, nil, nil, queue).start()
 	handler := newWebHandler(queue)
 	body := `{
 	  "appId":"/test/app",
@@ -919,7 +919,7 @@ func TestWebHandler_HandleHealthStatusEventReturn202WhenMarathonReturnedError(t
 	app := ConsulApp("/test/app", 3)
 	marathon := marathon.MarathonerStubForApps(app)
 	queue := make(chan event)
-	stopChan := newEventHandler(0, nil, marathon, queue).Start()
+	stopChan := newEventHandler(0, nil, marathon, queue).start()
 	handler := newWebHandler(queue)
 	body := `{
 	  "appId":"unknown",
@@ -948,7 +948,7 @@ func TestWebHandler_HandleHealthStatusEventWhenTaskIsNotInMarathon(t *testing.T)
 	app := ConsulApp("/test/app", 3)
 	marathon := marathon.MarathonerStubForApps(app)
 	queue := make(chan event)
-	stopChan := newEventHandler(0, nil, marathon, queue).Start()
+	stopChan := newEventHandler(0, nil, marathon, queue).start()
 	handler := newWebHandler(queue)
 	body := healthStatusChangeEventForTask("unknown.1")
 	req, _ := http.NewRequest("POST", "/events", bytes.NewBuffer([]byte(body)))