This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from duck8823/feature/health_check
Add healthcheck
- Loading branch information
Showing
10 changed files
with
266 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package docker | ||
|
||
import ( | ||
"context" | ||
"github.com/duck8823/duci/infrastructure/docker" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type Service interface { | ||
Status() error | ||
} | ||
|
||
type serviceImpl struct { | ||
moby docker.Client | ||
} | ||
|
||
func New(moby docker.Client) Service { | ||
return &serviceImpl{moby} | ||
} | ||
|
||
func (s *serviceImpl) Status() error { | ||
if _, err := s.moby.Info(context.Background()); err != nil { | ||
return errors.Wrap(err, "Couldn't connect to Docker daemon.") | ||
} | ||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/duck8823/duci/application/service/docker" | ||
"net/http" | ||
) | ||
|
||
// HealthController is a handler of health check. | ||
type HealthController struct { | ||
Docker docker.Service | ||
} | ||
|
||
// ServeHTTP responses a server status | ||
func (c *HealthController) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if err := c.Docker.Status(); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package controller_test | ||
|
||
import ( | ||
"github.com/duck8823/duci/application/service/docker/mock_docker" | ||
"github.com/duck8823/duci/presentation/controller" | ||
"github.com/golang/mock/gomock" | ||
"github.com/pkg/errors" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestHealthCheckController_ServeHTTP(t *testing.T) { | ||
t.Run("without error", func(t *testing.T) { | ||
// given | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockService := mock_docker.NewMockService(ctrl) | ||
mockService.EXPECT(). | ||
Status(). | ||
Return(nil) | ||
|
||
handler := &controller.HealthController{Docker: mockService} | ||
|
||
// and | ||
s := httptest.NewServer(handler) | ||
defer s.Close() | ||
|
||
// and | ||
req := httptest.NewRequest("GET", "/health", nil) | ||
rec := httptest.NewRecorder() | ||
|
||
// when | ||
handler.ServeHTTP(rec, req) | ||
|
||
// then | ||
if rec.Code != 200 { | ||
t.Errorf("status code must be 200, but got %+v", rec.Code) | ||
} | ||
}) | ||
|
||
t.Run("with error", func(t *testing.T) { | ||
// given | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockService := mock_docker.NewMockService(ctrl) | ||
mockService.EXPECT(). | ||
Status(). | ||
Return(errors.New("test")) | ||
|
||
handler := &controller.HealthController{Docker: mockService} | ||
|
||
// and | ||
s := httptest.NewServer(handler) | ||
defer s.Close() | ||
|
||
// and | ||
req := httptest.NewRequest("GET", "/health", nil) | ||
rec := httptest.NewRecorder() | ||
|
||
// when | ||
handler.ServeHTTP(rec, req) | ||
|
||
// then | ||
if rec.Code != 500 { | ||
t.Errorf("status code must be 500, but got %+v", rec.Code) | ||
} | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters