diff --git a/http/health.go b/http/health.go index 8d4edd9673..a6a4740c68 100644 --- a/http/health.go +++ b/http/health.go @@ -3,11 +3,13 @@ package http import ( "fmt" "net/http" + + platform "github.com/influxdata/influxdb/v2" ) // HealthHandler returns the status of the process. func HealthHandler(w http.ResponseWriter, r *http.Request) { - msg := `{"name":"influxdb", "message":"ready for queries and writes", "status":"pass", "checks":[]}` + msg := fmt.Sprintf(`{"name":"influxdb", "message":"ready for queries and writes", "status":"pass", "checks":[], "version": %q}`, platform.GetBuildInfo().Version) w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusOK) fmt.Fprintln(w, msg) diff --git a/http/health_test.go b/http/health_test.go index e7fef30f00..fb012daf18 100644 --- a/http/health_test.go +++ b/http/health_test.go @@ -1,6 +1,7 @@ package http import ( + "encoding/json" "io/ioutil" "net/http" "net/http/httptest" @@ -11,7 +12,7 @@ func TestHealthHandler(t *testing.T) { type wants struct { statusCode int contentType string - body string + status string } tests := []struct { name string @@ -26,7 +27,7 @@ func TestHealthHandler(t *testing.T) { wants: wants{ statusCode: http.StatusOK, contentType: "application/json; charset=utf-8", - body: `{"name":"influxdb", "message":"ready for queries and writes", "status":"pass", "checks":[]}`, + status: "pass", }, }, } @@ -34,21 +35,34 @@ func TestHealthHandler(t *testing.T) { t.Run(tt.name, func(t *testing.T) { HealthHandler(tt.w, tt.r) res := tt.w.Result() - content := res.Header.Get("Content-Type") + contentType := res.Header.Get("Content-Type") body, _ := ioutil.ReadAll(res.Body) if res.StatusCode != tt.wants.statusCode { t.Errorf("%q. HealthHandler() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode) } - if tt.wants.contentType != "" && content != tt.wants.contentType { - t.Errorf("%q. HealthHandler() = %v, want %v", tt.name, content, tt.wants.contentType) + if tt.wants.contentType != "" && contentType != tt.wants.contentType { + t.Errorf("%q. HealthHandler() = %v, want %v", tt.name, contentType, tt.wants.contentType) } - if tt.wants.body != "" { - if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil { - t.Errorf("%q, HealthHandler(). error unmarshaling json %v", tt.name, err) - } else if !eq { - t.Errorf("%q. HealthHandler() = ***%s***", tt.name, diff) - } + var content map[string]interface{} + if err := json.Unmarshal(body, &content); err != nil { + t.Errorf("%q, HealthHandler(). error unmarshaling json %v", tt.name, err) + return + } + if _, found := content["name"]; !found { + t.Errorf("%q. HealthHandler() no name reported", tt.name) + } + if content["status"] != tt.wants.status { + t.Errorf("%q. HealthHandler() status= %v, want %v", tt.name, content["status"], tt.wants.status) + } + if _, found := content["message"]; !found { + t.Errorf("%q. HealthHandler() no message reported", tt.name) + } + if _, found := content["checks"]; !found { + t.Errorf("%q. HealthHandler() no checks reported", tt.name) + } + if _, found := content["version"]; !found { + t.Errorf("%q. HealthHandler() no version reported", tt.name) } }) } diff --git a/http/swagger.yml b/http/swagger.yml index 5c98e76c95..95c010d598 100644 --- a/http/swagger.yml +++ b/http/swagger.yml @@ -10893,6 +10893,8 @@ components: enum: - pass - fail + version: + type: string Labels: type: array items: