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))
}
})
- 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))
// 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)
}, 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 {
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.
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
}