Merged in feature/expose-version (pull request #186)
expose version to api * expose version
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"queryorchestration/internal/cognitoauth"
|
||||
"queryorchestration/internal/document/batch"
|
||||
documentupload "queryorchestration/internal/document/upload"
|
||||
"queryorchestration/internal/serviceconfig/build"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
|
||||
"queryorchestration/internal/server"
|
||||
@@ -277,9 +278,14 @@ func New(ctx context.Context, cfg Config) (*Server, error) {
|
||||
e.GET("/metrics", echoprometheus.NewHandler())
|
||||
}
|
||||
|
||||
// Health endpoint
|
||||
// Health endpoint with version information
|
||||
e.GET("/health", func(c echo.Context) error {
|
||||
return c.NoContent(http.StatusOK)
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"status": "healthy",
|
||||
"version": build.GetVersion(),
|
||||
"buildTime": build.GetBuildTime(),
|
||||
"commit": build.GetGitCommit(),
|
||||
})
|
||||
})
|
||||
|
||||
e.Use(getSlogCustomEchoMiddleware(cfg.GetLogger()))
|
||||
|
||||
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"queryorchestration/internal/backgroundtask"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/build"
|
||||
"queryorchestration/internal/serviceconfig/logger"
|
||||
"queryorchestration/internal/test"
|
||||
|
||||
@@ -192,9 +194,14 @@ func TestHealthEndpoint(t *testing.T) {
|
||||
// Create a new Echo instance
|
||||
e := echo.New()
|
||||
|
||||
// Register health endpoint
|
||||
// Register health endpoint with version information
|
||||
e.GET("/health", func(c echo.Context) error {
|
||||
return c.NoContent(http.StatusOK)
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"status": "healthy",
|
||||
"version": build.GetVersion(),
|
||||
"buildTime": build.GetBuildTime(),
|
||||
"commit": build.GetGitCommit(),
|
||||
})
|
||||
})
|
||||
|
||||
// Create a request to the health endpoint
|
||||
@@ -207,8 +214,63 @@ func TestHealthEndpoint(t *testing.T) {
|
||||
// Assert the response status code
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
// Assert the response body is empty
|
||||
assert.Empty(t, rec.Body.String())
|
||||
// Parse the JSON response
|
||||
var response map[string]interface{}
|
||||
err := json.Unmarshal(rec.Body.Bytes(), &response)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Assert the response contains expected fields
|
||||
assert.Equal(t, "healthy", response["status"])
|
||||
assert.NotEmpty(t, response["version"])
|
||||
assert.NotEmpty(t, response["buildTime"])
|
||||
assert.NotEmpty(t, response["commit"])
|
||||
}
|
||||
|
||||
func TestHealthEndpointWithVersionInfo(t *testing.T) {
|
||||
// Create a new Echo instance
|
||||
e := echo.New()
|
||||
|
||||
// Register health endpoint with version information (matching actual implementation)
|
||||
e.GET("/health", func(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"status": "healthy",
|
||||
"version": build.GetVersion(),
|
||||
"buildTime": build.GetBuildTime(),
|
||||
"commit": build.GetGitCommit(),
|
||||
})
|
||||
})
|
||||
|
||||
// Create a request to the health endpoint
|
||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
// Serve the request
|
||||
e.ServeHTTP(rec, req)
|
||||
|
||||
// Assert the response status code
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
// Check that Content-Type starts with application/json (may or may not include charset)
|
||||
contentType := rec.Header().Get("Content-Type")
|
||||
assert.True(t, strings.HasPrefix(contentType, "application/json"), "Content-Type should be application/json, got: %s", contentType)
|
||||
|
||||
// Parse the JSON response
|
||||
var response map[string]interface{}
|
||||
err := json.Unmarshal(rec.Body.Bytes(), &response)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify all expected fields are present
|
||||
assert.Contains(t, response, "status", "Response should contain status field")
|
||||
assert.Contains(t, response, "version", "Response should contain version field")
|
||||
assert.Contains(t, response, "buildTime", "Response should contain buildTime field")
|
||||
assert.Contains(t, response, "commit", "Response should contain commit field")
|
||||
|
||||
// Verify status is healthy
|
||||
assert.Equal(t, "healthy", response["status"], "Status should be healthy")
|
||||
|
||||
// Verify version fields match the build package values
|
||||
assert.Equal(t, build.GetVersion(), response["version"], "Version should match build.GetVersion()")
|
||||
assert.Equal(t, build.GetBuildTime(), response["buildTime"], "BuildTime should match build.GetBuildTime()")
|
||||
assert.Equal(t, build.GetGitCommit(), response["commit"], "Commit should match build.GetGitCommit()")
|
||||
}
|
||||
|
||||
func TestGetSlogCustomEchoMiddleware(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user