Skip to content

Commit

Permalink
Fixes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarcic committed Oct 7, 2017
1 parent 944386f commit b134d8f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ EXPOSE 8080

CMD ["docker-flow-swarm-listener"]

HEALTHCHECK --interval=5s --start-period=3s --timeout=5s CMD wget -qO- "http://localhost:8080/v1/docker-flow-swarm-listener/ping"

COPY --from=build /src/docker-flow-swarm-listener /usr/local/bin/docker-flow-swarm-listener
RUN chmod +x /usr/local/bin/docker-flow-swarm-listener
32 changes: 24 additions & 8 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,25 @@ type Serve struct {
Notification service.Sender
}

//Response message
type Response struct {
Status string
}

// NewServe returns a new instance of the `Serve`
func NewServe(service service.Servicer, notification service.Sender) *Serve {
return &Serve{
Service: service,
Notification: notification,
}
}

// Run executes a server
func (m *Serve) Run() error {
mux := http.NewServeMux()
mux.HandleFunc("/v1/docker-flow-swarm-listener/notify-services", m.NotifyServices)
mux.HandleFunc("/v1/docker-flow-swarm-listener/get-services", m.GetServices)
mux.HandleFunc("/v1/docker-flow-swarm-listener/ping", m.PingHandler)
mux.Handle("/metrics", prometheus.Handler())
if err := httpListenAndServe(":8080", mux); err != nil {
return err
Expand All @@ -35,8 +49,10 @@ func (m *Serve) Run() error {
func (m *Serve) NotifyServices(w http.ResponseWriter, req *http.Request) {
services, _ := m.Service.GetServices()
go m.Notification.ServicesCreate(services, 10, 5)
// TODO: Add response message
js, _ := json.Marshal(Response{Status: "OK"})
httpWriterSetContentType(w, "application/json")
w.WriteHeader(http.StatusOK)
w.Write(js)
}

// GetServices retrieves all services with the `com.df.notify` label set to `true`
Expand All @@ -54,10 +70,10 @@ func (m *Serve) GetServices(w http.ResponseWriter, req *http.Request) {
httpWriterSetContentType(w, "application/json")
}

// NewServe returns a new instance of the `Serve`
func NewServe(service service.Servicer, notification service.Sender) *Serve {
return &Serve{
Service: service,
Notification: notification,
}
}
// PingHandler is used for health checks
func (m *Serve) PingHandler(w http.ResponseWriter, req *http.Request) {
js, _ := json.Marshal(Response{Status: "OK"})
httpWriterSetContentType(w, "application/json")
w.WriteHeader(http.StatusOK)
w.Write(js)
}
35 changes: 34 additions & 1 deletion serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ func (s *ServerTestSuite) Test_Run_ReturnsError_WhenHTTPListenAndServeFails() {

// NotifyServices

func (s *ServerTestSuite) Test_NotifyServices_ReturnsStatus200() {
servicerMock := getServicerMock("")
notifMock := NotificationMock{
ServicesCreateMock: func(services *[]service.SwarmService, retries, interval int) error {
return nil
},
}
rw := getResponseWriterMock()
req, _ := http.NewRequest("GET", "/v1/docker-flow-swarm-listener/notify-services", nil)
expected, _ := json.Marshal(Response{Status: "OK"})

srv := NewServe(servicerMock, notifMock)
srv.NotifyServices(rw, req)

rw.AssertCalled(s.T(), "WriteHeader", 200)
rw.AssertCalled(s.T(), "Write", []byte(expected))
}

func (s *ServerTestSuite) Test_NotifyServices_SetsContentTypeToJSON() {
var actual string
httpWriterSetContentType = func(w http.ResponseWriter, value string) {
Expand Down Expand Up @@ -109,7 +127,6 @@ func (s *ServerTestSuite) Test_NotifyServices_InvokesServicesCreate() {
// GetServices

func (s *ServerTestSuite) Test_GetServices_ReturnsServices() {

servicerMock := getServicerMock("GetServicesParameters")
mapParam := []map[string]string{
{"serviceName": "demo",
Expand All @@ -129,9 +146,25 @@ func (s *ServerTestSuite) Test_GetServices_ReturnsServices() {
rsp := []map[string]string{}
json.Unmarshal(value, &rsp)
s.Equal(&mapParam, &rsp)
}

// PingHandler

func (s *ServerTestSuite) Test_PingHandler_ReturnsStatus200() {
servicerMock := getServicerMock("")
notifMock := NotificationMock{}
rw := getResponseWriterMock()
req, _ := http.NewRequest("GET", "/v1/docker-flow-swarm-listener/ping", nil)
expected, _ := json.Marshal(Response{Status: "OK"})

srv := NewServe(servicerMock, notifMock)
srv.PingHandler(rw, req)

rw.AssertCalled(s.T(), "WriteHeader", 200)
rw.AssertCalled(s.T(), "Write", []byte(expected))
}


// NewServe

func (s *ServerTestSuite) Test_NewServe_SetsService() {
Expand Down

0 comments on commit b134d8f

Please sign in to comment.