Fixed detection of the etcd connection status 77/128477/1
authorLukasz Rajewski <lukasz.rajewski@orange.com>
Mon, 11 Apr 2022 18:36:00 +0000 (20:36 +0200)
committerLukasz Rajewski <lukasz.rajewski@orange.com>
Mon, 11 Apr 2022 18:36:23 +0000 (20:36 +0200)
Issue-ID: MULTICLOUD-1468
Signed-off-by: Lukasz Rajewski <lukasz.rajewski@orange.com>
Change-Id: If243e9a484afe5db7d9538a1875241fa425a2516

src/k8splugin/api/healthcheckhandler.go
src/k8splugin/api/healthcheckhandler_test.go
src/k8splugin/internal/db/etcd.go
src/k8splugin/internal/db/etcd_testing.go
src/k8splugin/internal/utils/utils.go

index 896c6df..2b338b9 100644 (file)
@@ -31,5 +31,10 @@ func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
                return
        }
 
+       err = db.Etcd.HealthCheck()
+       if err != nil {
+               http.Error(w, err.Error(), http.StatusInternalServerError)
+               return
+       }
        w.WriteHeader(http.StatusOK)
 }
index c6c07c1..cadcf76 100644 (file)
@@ -34,6 +34,9 @@ func TestHealthCheckHandler(t *testing.T) {
                db.DBconn = &db.MockDB{
                        Err: nil,
                }
+               db.Etcd = &db.MockEtcdClient{
+                       Err: nil,
+               }
                request := httptest.NewRequest("GET", "/v1/healthcheck", nil)
                resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil, nil, nil, nil, nil))
 
@@ -43,10 +46,29 @@ func TestHealthCheckHandler(t *testing.T) {
                }
        })
 
-       t.Run("FAILED HealthCheck", func(t *testing.T) {
+       t.Run("FAILED HealthCheck DB", func(t *testing.T) {
                db.DBconn = &db.MockDB{
                        Err: pkgerrors.New("Runtime Error in DB"),
                }
+               db.Etcd = &db.MockEtcdClient{
+                       Err: nil,
+               }
+               request := httptest.NewRequest("GET", "/v1/healthcheck", nil)
+               resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil, nil, nil, nil, nil))
+
+               //Check returned code
+               if resp.StatusCode != http.StatusInternalServerError {
+                       t.Fatalf("Expected %d; Got: %d", http.StatusInternalServerError, resp.StatusCode)
+               }
+       })
+
+       t.Run("FAILED HealthCheck Etcd", func(t *testing.T) {
+               db.DBconn = &db.MockDB{
+                       Err: nil,
+               }
+               db.Etcd = &db.MockEtcdClient{
+                       Err: pkgerrors.New("Runtime Error in Etcd"),
+               }
                request := httptest.NewRequest("GET", "/v1/healthcheck", nil)
                resp := executeRequest(request, NewRouter(nil, nil, nil, nil, nil, nil, nil, nil, nil))
 
index 5ce8135..a3a0935 100644 (file)
@@ -35,6 +35,7 @@ type EtcdConfig struct {
 
 // EtcdStore Interface needed for mocking
 type EtcdStore interface {
+       HealthCheck() error
        Get(key string) ([]byte, error)
        GetKeys(key string) ([]string, error)
        GetValues(key string) ([][]byte, error)
@@ -96,6 +97,20 @@ func newClient(store *clientv3.Client, c EtcdConfig) (EtcdClient, error) {
        }, nil
 }
 
+// HealthCheck verifies if the database is up and running
+func (e EtcdClient) HealthCheck() error {
+
+       if e.cli == nil {
+               return pkgerrors.Errorf("Etcd Client not initialized")
+       }
+       _, err := e.cli.Get(context.Background(), "HealthCheckKey")
+       if err != nil {
+               return pkgerrors.Errorf("Error getting etcd entry: %s", err.Error())
+       }
+
+       return nil
+}
+
 // Put values in Etcd DB
 func (e EtcdClient) Put(key, value string) error {
 
index 2f62d36..4022d30 100644 (file)
@@ -24,6 +24,10 @@ type MockEtcdClient struct {
        Err   error
 }
 
+func (c *MockEtcdClient) HealthCheck() error {
+       return c.Err
+}
+
 func (c *MockEtcdClient) Put(key, value string) error {
        if c.Items == nil {
                c.Items = make(map[string]string)
index 174f8e7..e8ce2f8 100644 (file)
@@ -76,6 +76,7 @@ func CheckDatabaseConnection() error {
 
        err = db.DBconn.HealthCheck()
        if err != nil {
+               log.Printf("MongoDB health problem: %s", err.Error())
                return pkgerrors.Cause(err)
        }
        // TODO Convert these to configuration files instead of environment variables.
@@ -88,6 +89,7 @@ func CheckDatabaseConnection() error {
        err = db.NewEtcdClient(nil, c)
        if err != nil {
                log.Printf("Etcd Client Initialization failed with error: %s", err.Error())
+               return pkgerrors.Cause(err)
        }
        return nil
 }