From 78d1b044e924f8b8b36f6357475dac3b4bf90c3c Mon Sep 17 00:00:00 2001 From: josebarn Date: Wed, 16 Aug 2017 09:08:13 -0300 Subject: [PATCH] adding lifecycle hook for test exit --- api/api.go | 4 +++- api/lifecycle.go | 26 ++++++++++++++++++++++++++ script/container-integ-test.sh | 15 +++++++++------ 3 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 api/lifecycle.go diff --git a/api/api.go b/api/api.go index 76d16ef..123c5dd 100644 --- a/api/api.go +++ b/api/api.go @@ -28,7 +28,9 @@ func addMiddleware(mux *chi.Mux) { func addRoutes(mux *chi.Mux) { // Add a simple resource - mux.Mount("/api/id", AppResource{}.Routes()) + mux.Mount("/api/id", AppResource{}.Routes()) + // lifecycle management hooks + mux.Mount("/.service", AppLifecycleResource{}.Routes()) // Live a healthy life! mux.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("yolo")) diff --git a/api/lifecycle.go b/api/lifecycle.go new file mode 100644 index 0000000..20d7c2b --- /dev/null +++ b/api/lifecycle.go @@ -0,0 +1,26 @@ +package api + +import ( + "github.com/go-chi/chi" + "github.com/go-chi/render" + "net/http" + "os" +) + +type AppLifecycleResource struct{} + +// Routes creates a REST router for the todos resource +func (self AppLifecycleResource) Routes() chi.Router { + r := chi.NewRouter() + + r.Route("/lifecycle", func(r chi.Router) { + r.Post("/stop", self.stop) + }) + + return r +} + +func (self AppLifecycleResource) stop(w http.ResponseWriter, r *http.Request) { + render.Status(r, http.StatusOK) + os.Exit(0) +} diff --git a/script/container-integ-test.sh b/script/container-integ-test.sh index ce2d7b6..42831c0 100755 --- a/script/container-integ-test.sh +++ b/script/container-integ-test.sh @@ -1,13 +1,16 @@ -sleep 5 - +# Check if it is alive ping -c 5 target -curl -X POST target:8080/api/id/1 - +EXIT_CODE=0 +# RUN THE TEST if curl -X POST target:8080/api/id/1 | grep -q '{"op":"POST","id":1}'; then echo "Tests passed!" - exit 0 else echo "Tests failed!" - exit 1 + let EXIT_CODE=1 fi +# GOOD API DESIGN OF A SERVICE INCLUDES A LIFECYCLE MGMT INTERFACE +# WHICH WE CAN LEVERAGE FOR TESTING... INVOKE IT TO STOP THE SERVICE +curl -X POST target:8080/.service/lifecycle/stop +sleep 1 +exit $EXIT_CODE