Skip to content

Commit

Permalink
🔥 Removing unused code/variables
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosroman committed Nov 23, 2018
1 parent 80898dc commit 4e8b5d0
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 64 deletions.
7 changes: 3 additions & 4 deletions cachingController.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ func newMeasuredService(service service) measuredService {
}
}

func (c *healthCheckController) collectChecksFromCachesFor(categories map[string]category) ([]fthealth.CheckResult, map[string][]fthealth.CheckResult, error) {
func (c *healthCheckController) collectChecksFromCachesFor(categories map[string]category) ([]fthealth.CheckResult, error) {
var checkResults []fthealth.CheckResult
categorisedResults := make(map[string][]fthealth.CheckResult)
serviceNames := getServiceNamesFromCategories(categories)
services := c.healthCheckService.getServicesMapByNames(serviceNames)
servicesThatAreNotInCache := make(map[string]service)
Expand All @@ -45,12 +44,12 @@ func (c *healthCheckController) collectChecksFromCachesFor(categories map[string
if len(servicesThatAreNotInCache) != 0 {
notCachedChecks, err := c.runServiceChecksByServiceNames(servicesThatAreNotInCache, categories)
if err != nil {
return nil, nil, err
return nil, err
}
checkResults = append(checkResults, notCachedChecks...)
}

return checkResults, categorisedResults, nil
return checkResults, nil
}

func (c *healthCheckController) updateCachedHealth(services map[string]service, categories map[string]category) {
Expand Down
27 changes: 13 additions & 14 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ type healthCheckController struct {
}

type controller interface {
buildServicesHealthResult([]string, bool) (fthealth.HealthResult, map[string]category, map[string]category, error)
buildServicesHealthResult([]string, bool) (fthealth.HealthResult, map[string]category, error)
runServiceChecksByServiceNames(map[string]service, map[string]category) ([]fthealth.CheckResult, error)
runServiceChecksFor(map[string]category) ([]fthealth.CheckResult, map[string][]fthealth.CheckResult, error)
runServiceChecksFor(map[string]category) ([]fthealth.CheckResult, error)
buildPodsHealthResult(string) (fthealth.HealthResult, error)
runPodChecksFor(string) ([]fthealth.CheckResult, error)
collectChecksFromCachesFor(map[string]category) ([]fthealth.CheckResult, map[string][]fthealth.CheckResult, error)
collectChecksFromCachesFor(map[string]category) ([]fthealth.CheckResult, error)
updateCachedHealth(map[string]service, map[string]category)
scheduleCheck(measuredService, time.Duration, *time.Timer)
getIndividualPodHealth(string) ([]byte, string, error)
Expand Down Expand Up @@ -85,24 +85,24 @@ func (c *healthCheckController) addAck(serviceName string, ackMessage string) er
return nil
}

func (c *healthCheckController) buildServicesHealthResult(providedCategories []string, useCache bool) (fthealth.HealthResult, map[string]category, map[string]category, error) {
func (c *healthCheckController) buildServicesHealthResult(providedCategories []string, useCache bool) (fthealth.HealthResult, map[string]category, error) {
var checkResults []fthealth.CheckResult
desc := "Health of the whole cluster of the moment served without cache."
availableCategories, err := c.healthCheckService.getCategories()
if err != nil {
return fthealth.HealthResult{}, nil, nil, fmt.Errorf("cannot build health check result for services: %v", err.Error())
return fthealth.HealthResult{}, nil, fmt.Errorf("cannot build health check result for services: %v", err.Error())
}

matchingCategories := getMatchingCategories(providedCategories, availableCategories)

if useCache {
desc = "Health of the whole cluster served from cache."
checkResults, _, err = c.collectChecksFromCachesFor(matchingCategories)
checkResults, err = c.collectChecksFromCachesFor(matchingCategories)
} else {
checkResults, _, err = c.runServiceChecksFor(matchingCategories)
checkResults, err = c.runServiceChecksFor(matchingCategories)
}
if err != nil {
return fthealth.HealthResult{}, nil, nil, fmt.Errorf("cannot build health check result for services: %v", err.Error())
return fthealth.HealthResult{}, nil, fmt.Errorf("cannot build health check result for services: %v", err.Error())
}

c.disableStickyFailingCategories(matchingCategories, checkResults)
Expand All @@ -120,7 +120,7 @@ func (c *healthCheckController) buildServicesHealthResult(providedCategories []s

sort.Sort(byNameComparator(health.Checks))

return health, matchingCategories, nil, nil
return health, matchingCategories, nil
}

func (c *healthCheckController) runServiceChecksByServiceNames(services map[string]service, categories map[string]category) ([]fthealth.CheckResult, error) {
Expand Down Expand Up @@ -171,16 +171,15 @@ func (c *healthCheckController) runServiceChecksByServiceNames(services map[stri
return healthChecks, nil
}

func (c *healthCheckController) runServiceChecksFor(categories map[string]category) ([]fthealth.CheckResult, map[string][]fthealth.CheckResult, error) {
categorisedResults := make(map[string][]fthealth.CheckResult)
func (c *healthCheckController) runServiceChecksFor(categories map[string]category) (healthChecks []fthealth.CheckResult, err error) {
serviceNames := getServiceNamesFromCategories(categories)
services := c.healthCheckService.getServicesMapByNames(serviceNames)
healthChecks, err := c.runServiceChecksByServiceNames(services, categories)
healthChecks, err = c.runServiceChecksByServiceNames(services, categories)
if err != nil {
return nil, nil, err
return nil, err
}

return healthChecks, categorisedResults, nil
return healthChecks, err
}

func (c *healthCheckController) disableStickyFailingCategories(categories map[string]category, healthChecks []fthealth.CheckResult) {
Expand Down
103 changes: 67 additions & 36 deletions controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
podWithCriticalSeverity = "podWithCriticalSeverity"
failingPod = "failingPod"
podWithBrokenService = "podWithBrokenService"
podWithBadAddress = "podWithBadAddress"
nonExistingCategoryName = "nonExistingCategoryName"
validCat = "validCat"
validService = "validService"
Expand All @@ -46,7 +47,9 @@ var defaultPods = []pod{
}

type MockService struct {
httpClient *http.Client
httpClient *http.Client
getServiceByNameErr error
getDeploymentsErr error
}

func (m *MockService) getCategories() (map[string]category, error) {
Expand Down Expand Up @@ -79,7 +82,7 @@ func (m *MockService) getDeployments() (map[string]deployment, error) {
"test-service-name-2": {
desiredReplicas: 2,
},
}, nil
}, m.getDeploymentsErr
}

func (m *MockService) isServicePresent(serviceName string) bool {
Expand All @@ -94,7 +97,7 @@ func (m *MockService) getServiceByName(serviceName string) (service, error) {
name: "test-service-name",
ack: "test ack",
isResilient: strings.HasPrefix(serviceName, "resilient"),
}, nil
}, m.getServiceByNameErr
}

func (m *MockService) getServicesMapByNames(serviceNames []string) map[string]service {
Expand Down Expand Up @@ -158,6 +161,13 @@ func (m *MockService) getPodByName(podName string) (pod, error) {
serviceName: nonExistingServiceName,
}, nil
}
case podWithBadAddress:
{
return pod{
name: "test-pod-name-8425234-9hdfg ",
ip: "[fe80::1%en0]",
}, nil
}
default:
return pod{
name: "test-pod-name-8425234-9hdfg ",
Expand Down Expand Up @@ -212,9 +222,9 @@ func (m *MockService) getHTTPClient() *http.Client {
return m.httpClient
}

func initializeMockController(env string, httpClient *http.Client) *healthCheckController {
func initializeMockController(env string, httpClient *http.Client) (hcc *healthCheckController, service *MockService) {
measuredServices := make(map[string]measuredService)
service := new(MockService)
service = new(MockService)
service.httpClient = httpClient
stickyCategoriesFailedServices := make(map[string]int)

Expand All @@ -223,121 +233,142 @@ func initializeMockController(env string, httpClient *http.Client) *healthCheckC
environment: env,
measuredServices: measuredServices,
stickyCategoriesFailedServices: stickyCategoriesFailedServices,
}
}, service
}

func TestAddAckNilError(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
err := controller.addAck("abc", "abc")
assert.Nil(t, err)
}

func TestAddAckInvalidServiceName(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
err := controller.addAck(nonExistingServiceName, "abc")
assert.NotNil(t, err)
}

func TestAddAckInvalidServiceNameWillAckingError(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
err := controller.addAck(serviceNameForAckErr, "abc")
assert.NotNil(t, err)
}

func TestRemoveAckNonExistingServiceErr(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
err := controller.removeAck(nonExistingServiceName)
assert.NotNil(t, err)
}

func TestRemoveAckServiceErr(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
err := controller.removeAck(serviceNameForAckErr)
assert.NotNil(t, err)
}

func TestRemoveAckHappyFlow(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
err := controller.removeAck(validService)
assert.Nil(t, err)
}

func TestBuildServicesHealthResult(t *testing.T) {
controller := initializeMockController("test", nil)
_, _, _, err := controller.buildServicesHealthResult([]string{"abc"}, false)
controller, _ := initializeMockController("test", nil)
_, _, err := controller.buildServicesHealthResult([]string{"abc"}, false)
assert.Nil(t, err)
}

func TestBuildServicesHealthResult_RunServiceChecksForFails(t *testing.T) {
controller, m := initializeMockController("test", nil)
m.getDeploymentsErr = errors.New("someerror")
_, _, err := controller.buildServicesHealthResult([]string{"abc"}, false)
assert.Error(t, err)
}

func TestBuildServicesHealthResultFromCache(t *testing.T) {
controller := initializeMockController("test", nil)
_, _, _, err := controller.buildServicesHealthResult([]string{"abc"}, true)
controller, _ := initializeMockController("test", nil)
_, _, err := controller.buildServicesHealthResult([]string{"abc"}, true)
assert.Nil(t, err)
}

func TestGetIndividualPodHealthHappyFlowWithGetServiceByNameErr(t *testing.T) {
httpClient := initializeMockHTTPClient(http.StatusOK, "")
controller, m := initializeMockController("test", httpClient)
m.getServiceByNameErr = errors.New("error is ignored")
_, _, err := controller.getIndividualPodHealth("testPod")
assert.NoError(t, err)
}

func TestGetIndividualPodHealthHappyFlow(t *testing.T) {
httpClient := initializeMockHTTPClient(http.StatusOK, "")
controller := initializeMockController("test", httpClient)
controller, _ := initializeMockController("test", httpClient)
_, _, err := controller.getIndividualPodHealth("testPod")
assert.Nil(t, err)
}

func TestGetIndividualPodHealthNonExistingPod(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
_, _, err := controller.getIndividualPodHealth(nonExistingPodName)
assert.NotNil(t, err)
}

func TestGetIndividualPodHealthBadUrl(t *testing.T) {
controller, _ := initializeMockController("test", nil)
_, _, err := controller.getIndividualPodHealth(podWithBadAddress)
assert.NotNil(t, err)
}

func TestGetIndividualPodHealthFailingService(t *testing.T) {
httpClient := initializeMockHTTPClient(http.StatusOK, "")
controller := initializeMockController("test", httpClient)
controller, _ := initializeMockController("test", httpClient)
_, _, err := controller.getIndividualPodHealth(podWithBrokenService)
assert.Nil(t, err)
}

func TestBuildPodsHealthResultHappyFlow(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
_, err := controller.buildPodsHealthResult("testPod")
assert.Nil(t, err)
}

func TestBuildPodsHealthResultInvalidPodName(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
_, err := controller.buildPodsHealthResult(invalidNameForService)
assert.NotNil(t, err)
}

func TestBuildPodsHealthResultInvalidServiceName(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
_, err := controller.buildPodsHealthResult(nonExistingServiceName)
assert.NotNil(t, err)
}

func TestGetSeverityForPodInvalidPodName(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
severity := controller.getSeverityForPod(nonExistingPodName, 8080)
assert.Equal(t, defaultSeverity, severity)
}

func TestComputeSeverityByPods(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
severity := controller.computeSeverityByPods([]pod{{name: nonExistingPodName}}, 8080)
assert.Equal(t, defaultSeverity, severity)
}

func TestComputeSeverityForPodWithCriticalSeverity(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
severity := controller.computeSeverityByPods([]pod{{name: failingPod}, {name: podWithCriticalSeverity}}, 8080)
assert.Equal(t, uint8(1), severity)
}

func TestGetSeverityForServiceInvalidServiceName(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
severity := controller.getSeverityForService(invalidNameForService, 8080)
assert.Equal(t, defaultSeverity, severity)
}

func TestGetSeverityForResilientService(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)

var testCases = []struct {
serviceName string
Expand Down Expand Up @@ -383,19 +414,19 @@ func TestGetSeverityForResilientService(t *testing.T) {
}

func TestGetSeverityForNonResilientService(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
severity := controller.getSeverityForService(invalidNameForService, 8080)
assert.Equal(t, defaultSeverity, severity)
}

func TestUpdateStickyCategoryInvalidCategoryName(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
err := controller.updateStickyCategory(nonExistingCategoryName, false)
assert.NotNil(t, err)
}

func TestUpdateStickyCategoryHappyFlow(t *testing.T) {
controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
err := controller.updateStickyCategory(validCat, false)
assert.Nil(t, err)
}
Expand Down Expand Up @@ -445,8 +476,8 @@ func TestRunServiceChecksForStickyCategory(t *testing.T) {
isEnabled: true,
}

controller := initializeMockController("test", nil)
hc, categories, _, _ := controller.buildServicesHealthResult([]string{"test", "publishing"}, false)
controller, _ := initializeMockController("test", nil)
hc, categories, _ := controller.buildServicesHealthResult([]string{"test", "publishing"}, false)

assert.NotNil(t, hc)
assert.False(t, categories["test"].isEnabled)
Expand All @@ -469,8 +500,8 @@ func TestRunServiceChecksForStickyCategoryUpdateError(t *testing.T) {
isEnabled: true,
}

controller := initializeMockController("test", nil)
hc, categories, _, _ := controller.buildServicesHealthResult([]string{"test", "publishing", nonExistingCategoryName}, true)
controller, _ := initializeMockController("test", nil)
hc, categories, _ := controller.buildServicesHealthResult([]string{"test", "publishing", nonExistingCategoryName}, true)

assert.NotNil(t, hc)
assert.False(t, hc.Ok)
Expand Down Expand Up @@ -502,7 +533,7 @@ func TestDisableStickyFailingCategoriesThresholdNotReached(t *testing.T) {
},
}

controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
controller.disableStickyFailingCategories(categories, healthchecks)
assert.True(t, categories["test"].isEnabled)
}
Expand Down Expand Up @@ -540,7 +571,7 @@ func TestDisableStickyFailingCategoriesThresholdReached(t *testing.T) {
},
}

controller := initializeMockController("test", nil)
controller, _ := initializeMockController("test", nil)
controller.disableStickyFailingCategories(categories, healthchecks)
controller.disableStickyFailingCategories(categories, healthchecks)
controller.disableStickyFailingCategories(categories, healthchecks)
Expand Down

0 comments on commit 4e8b5d0

Please sign in to comment.