diff --git a/pkg/api/version.go b/pkg/api/version.go new file mode 100644 index 0000000..c9596bf --- /dev/null +++ b/pkg/api/version.go @@ -0,0 +1,15 @@ +package api + +import ( + "net/http" + + "github.com/stefanprodan/k8s-podinfo/pkg/version" +) + +func (s *Server) versionHandler(w http.ResponseWriter, r *http.Request) { + result := map[string]string{ + "version": version.VERSION, + "commit": version.REVISION, + } + s.JSONResponse(w, r, result) +} diff --git a/pkg/api/version_test.go b/pkg/api/version_test.go new file mode 100644 index 0000000..e7ee256 --- /dev/null +++ b/pkg/api/version_test.go @@ -0,0 +1,36 @@ +package api + +import ( + "fmt" + "net/http" + "net/http/httptest" + "regexp" + "testing" +) + +func TestVersionHandler(t *testing.T) { + req, err := http.NewRequest("GET", "/version", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + srv := NewMockServer() + handler := http.HandlerFunc(srv.versionHandler) + + handler.ServeHTTP(rr, req) + + // Check the status code is what we expect. + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", + status, http.StatusOK) + } + + // Check the response body is what we expect. + expected := "unknown" + r := regexp.MustCompile(fmt.Sprintf("(?m:%s)", expected)) + if !r.MatchString(rr.Body.String()) { + t.Fatalf("handler returned unexpected body:\ngot \n%v \nwant \n%s", + rr.Body.String(), expected) + } +}