VOL-2021 - default health check to true

also add nil check in case k8s closes before received is defined

Change-Id: Ic60d4ad005e79ff758a3fe09c0886a8398c2401b
diff --git a/common/probe/probe.go b/common/probe/probe.go
index 984036c..fdb07e8 100644
--- a/common/probe/probe.go
+++ b/common/probe/probe.go
@@ -116,6 +116,18 @@
 			log.Debugw("probe-service-registered", log.Fields{"service-name": name})
 		}
 	}
+
+	if p.readyFunc != nil {
+		p.isReady = p.readyFunc(p.status)
+	} else {
+		p.isReady = defaultReadyFunc(p.status)
+	}
+
+	if p.healthFunc != nil {
+		p.isHealthy = p.healthFunc(p.status)
+	} else {
+		p.isHealthy = defaultHealthFunc(p.status)
+	}
 }
 
 // UpdateStatus utility function to send a service update to the probe
diff --git a/common/probe/probe_test.go b/common/probe/probe_test.go
index 18c4835..a7edc7f 100644
--- a/common/probe/probe_test.go
+++ b/common/probe/probe_test.go
@@ -202,7 +202,7 @@
 	assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for no services")
 }
 
-func TestReadzWithServices(t *testing.T) {
+func TestReadzWithServicesWithTrue(t *testing.T) {
 	p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
 	p.RegisterService("one", "two")
 
@@ -210,7 +210,18 @@
 	w := httptest.NewRecorder()
 	p.readzFunc(w, req)
 	resp := w.Result()
-	assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for no services")
+	assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code for registered only services")
+}
+
+func TestReadzWithServicesWithDefault(t *testing.T) {
+	p := &Probe{}
+	p.RegisterService("one", "two")
+
+	req := httptest.NewRequest("GET", "http://example.com/readz", nil)
+	w := httptest.NewRecorder()
+	p.readzFunc(w, req)
+	resp := w.Result()
+	assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for registered only services")
 }
 
 func TestReadzNpServicesDefault(t *testing.T) {
@@ -258,7 +269,7 @@
 	assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for no services")
 }
 
-func TestHealthzWithServices(t *testing.T) {
+func TestHealthzWithServicesWithTrue(t *testing.T) {
 	p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
 	p.RegisterService("one", "two")
 
@@ -266,7 +277,18 @@
 	w := httptest.NewRecorder()
 	p.healthzFunc(w, req)
 	resp := w.Result()
-	assert.Equal(t, http.StatusTeapot, resp.StatusCode, "invalid status code for no services")
+	assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code for registered only services")
+}
+
+func TestHealthzWithServicesWithDefault(t *testing.T) {
+	p := &Probe{}
+	p.RegisterService("one", "two")
+
+	req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
+	w := httptest.NewRecorder()
+	p.healthzFunc(w, req)
+	resp := w.Result()
+	assert.Equal(t, http.StatusOK, resp.StatusCode, "invalid status code for registered only services")
 }
 
 func TestHealthzNoServicesDefault(t *testing.T) {
diff --git a/ro_core/core/core.go b/ro_core/core/core.go
index cd27a42..8b0317b 100644
--- a/ro_core/core/core.go
+++ b/ro_core/core/core.go
@@ -86,11 +86,19 @@
 
 func (core *Core) Stop(ctx context.Context) {
 	log.Info("stopping-adaptercore")
-	core.exitChannel <- 1
+	if core.exitChannel != nil {
+		core.exitChannel <- 1
+	}
 	// Stop all the started services
-	core.grpcServer.Stop()
-	core.logicalDeviceMgr.stop(ctx)
-	core.deviceMgr.stop(ctx)
+	if core.grpcServer != nil {
+		core.grpcServer.Stop()
+	}
+	if core.logicalDeviceMgr != nil {
+		core.logicalDeviceMgr.stop(ctx)
+	}
+	if core.deviceMgr != nil {
+		core.deviceMgr.stop(ctx)
+	}
 	log.Info("adaptercore-stopped")
 }
 
diff --git a/rw_core/core/core.go b/rw_core/core/core.go
index e74e869..f22f8b8 100644
--- a/rw_core/core/core.go
+++ b/rw_core/core/core.go
@@ -136,12 +136,22 @@
 
 func (core *Core) Stop(ctx context.Context) {
 	log.Info("stopping-adaptercore")
-	core.exitChannel <- 1
+	if core.exitChannel != nil {
+		core.exitChannel <- 1
+	}
 	// Stop all the started services
-	core.grpcServer.Stop()
-	core.logicalDeviceMgr.stop(ctx)
-	core.deviceMgr.stop(ctx)
-	core.kmp.Stop()
+	if core.grpcServer != nil {
+		core.grpcServer.Stop()
+	}
+	if core.logicalDeviceMgr != nil {
+		core.logicalDeviceMgr.stop(ctx)
+	}
+	if core.deviceMgr != nil {
+		core.deviceMgr.stop(ctx)
+	}
+	if core.kmp != nil {
+		core.kmp.Stop()
+	}
 	log.Info("adaptercore-stopped")
 }