Skip to content

Commit

Permalink
add certificate test and remove go impl for certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Apr 4, 2024
1 parent 1a41115 commit f6117cf
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 178 deletions.
10 changes: 1 addition & 9 deletions pkg/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func GetHealthCheckFunc(gvk schema.GroupVersionKind) func(obj *unstructured.Unst
case "argoproj.io":
switch gvk.Kind {
case "Workflow":
return getArgoWorkflowHealth
return GetArgoWorkflowHealth
case "Application":
return getArgoApplicationHealth
}
Expand All @@ -162,14 +162,6 @@ func GetHealthCheckFunc(gvk schema.GroupVersionKind) func(obj *unstructured.Unst
// return getAPIServiceHealth
// }

case "cert-manager.io":
switch gvk.Kind {
case CertificateKind:
return getCertificateHealth
case CertificateRequestKind:
return getCertificateRequestHealth
}

case "networking.k8s.io":
switch gvk.Kind {
case IngressKind:
Expand Down
2 changes: 1 addition & 1 deletion pkg/health/health_argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type argoWorkflow struct {
}
}

func getArgoWorkflowHealth(obj *unstructured.Unstructured) (*HealthStatus, error) {
func GetArgoWorkflowHealth(obj *unstructured.Unstructured) (*HealthStatus, error) {
var wf argoWorkflow
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &wf)
if err != nil {
Expand Down
101 changes: 0 additions & 101 deletions pkg/health/health_certmanager.go

This file was deleted.

136 changes: 71 additions & 65 deletions pkg/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,108 +2,114 @@
Package provides functionality that allows assessing the health state of a Kubernetes resource.
*/

package health
package health_test

import (
"os"
"testing"

"github.com/flanksource/is-healthy/pkg/health"
"github.com/flanksource/is-healthy/pkg/lua"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
)

func assertAppHealth(t *testing.T, yamlPath string, expectedStatus HealthStatusCode) {
func assertAppHealth(t *testing.T, yamlPath string, expectedStatus health.HealthStatusCode) {
health := getHealthStatus(yamlPath, t)
assert.NotNil(t, health)
assert.Equal(t, expectedStatus, health.Status)
}

func getHealthStatus(yamlPath string, t *testing.T) *HealthStatus {
func getHealthStatus(yamlPath string, t *testing.T) *health.HealthStatus {
yamlBytes, err := os.ReadFile(yamlPath)
require.NoError(t, err)
var obj unstructured.Unstructured
err = yaml.Unmarshal(yamlBytes, &obj)
require.NoError(t, err)
health, err := GetResourceHealth(&obj, nil)
health, err := health.GetResourceHealth(&obj, lua.ResourceHealthOverrides{})
require.NoError(t, err)
return health
}

func TestCertificate(t *testing.T) {
assertAppHealth(t, "./testdata/certificate-healthy.yaml", health.HealthStatusHealthy)
}

func TestDeploymentHealth(t *testing.T) {
assertAppHealth(t, "./testdata/nginx.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/deployment-progressing.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/deployment-suspended.yaml", HealthStatusSuspended)
assertAppHealth(t, "./testdata/deployment-degraded.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/nginx.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/deployment-progressing.yaml", health.HealthStatusProgressing)
assertAppHealth(t, "./testdata/deployment-suspended.yaml", health.HealthStatusSuspended)
assertAppHealth(t, "./testdata/deployment-degraded.yaml", health.HealthStatusDegraded)
}

func TestStatefulSetHealth(t *testing.T) {
assertAppHealth(t, "./testdata/statefulset.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/statefulset.yaml", health.HealthStatusHealthy)
}

func TestStatefulSetOnDeleteHealth(t *testing.T) {
assertAppHealth(t, "./testdata/statefulset-ondelete.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/statefulset-ondelete.yaml", health.HealthStatusHealthy)
}

func TestDaemonSetOnDeleteHealth(t *testing.T) {
assertAppHealth(t, "./testdata/daemonset-ondelete.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/daemonset-ondelete.yaml", health.HealthStatusHealthy)
}
func TestPVCHealth(t *testing.T) {
assertAppHealth(t, "./testdata/pvc-bound.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/pvc-pending.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/pvc-bound.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/pvc-pending.yaml", health.HealthStatusProgressing)
}

func TestServiceHealth(t *testing.T) {
assertAppHealth(t, "./testdata/svc-clusterip.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/svc-loadbalancer.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/svc-loadbalancer-unassigned.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/svc-loadbalancer-nonemptylist.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/svc-clusterip.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/svc-loadbalancer.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/svc-loadbalancer-unassigned.yaml", health.HealthStatusProgressing)
assertAppHealth(t, "./testdata/svc-loadbalancer-nonemptylist.yaml", health.HealthStatusHealthy)
}

func TestIngressHealth(t *testing.T) {
assertAppHealth(t, "./testdata/ingress.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/ingress-unassigned.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/ingress-nonemptylist.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/ingress.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/ingress-unassigned.yaml", health.HealthStatusProgressing)
assertAppHealth(t, "./testdata/ingress-nonemptylist.yaml", health.HealthStatusHealthy)
}

func TestCRD(t *testing.T) {
assert.Nil(t, getHealthStatus("./testdata/knative-service.yaml", t))
}

func TestJob(t *testing.T) {
assertAppHealth(t, "./testdata/job-running.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/job-failed.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/job-succeeded.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/job-suspended.yaml", HealthStatusSuspended)
assertAppHealth(t, "./testdata/job-running.yaml", health.HealthStatusProgressing)
assertAppHealth(t, "./testdata/job-failed.yaml", health.HealthStatusDegraded)
assertAppHealth(t, "./testdata/job-succeeded.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/job-suspended.yaml", health.HealthStatusSuspended)
}

func TestHPA(t *testing.T) {
assertAppHealth(t, "./testdata/hpa-v2-healthy.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v2-degraded.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/hpa-v2-progressing.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/hpa-v2beta2-healthy.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v2beta1-healthy-disabled.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v2beta1-healthy.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v1-degraded.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/hpa-v1-healthy.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v1-healthy-toofew.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v1-progressing.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/hpa-v1-progressing-with-no-annotations.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/hpa-v2-healthy.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v2-degraded.yaml", health.HealthStatusDegraded)
assertAppHealth(t, "./testdata/hpa-v2-progressing.yaml", health.HealthStatusProgressing)
assertAppHealth(t, "./testdata/hpa-v2beta2-healthy.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v2beta1-healthy-disabled.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v2beta1-healthy.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v1-degraded.yaml", health.HealthStatusDegraded)
assertAppHealth(t, "./testdata/hpa-v1-healthy.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v1-healthy-toofew.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/hpa-v1-progressing.yaml", health.HealthStatusProgressing)
assertAppHealth(t, "./testdata/hpa-v1-progressing-with-no-annotations.yaml", health.HealthStatusProgressing)
}

func TestPod(t *testing.T) {
assertAppHealth(t, "./testdata/pod-pending.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/pod-running-not-ready.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/pod-crashloop.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/pod-imagepullbackoff.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/pod-error.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/pod-running-restart-always.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/pod-running-restart-never.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/pod-running-restart-onfailure.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/pod-failed.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/pod-succeeded.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/pod-deletion.yaml", HealthStatusProgressing)
assertAppHealth(t, "./testdata/pod-pending.yaml", health.HealthStatusProgressing)
assertAppHealth(t, "./testdata/pod-running-not-ready.yaml", health.HealthStatusProgressing)
assertAppHealth(t, "./testdata/pod-crashloop.yaml", health.HealthStatusDegraded)
assertAppHealth(t, "./testdata/pod-imagepullbackoff.yaml", health.HealthStatusDegraded)
assertAppHealth(t, "./testdata/pod-error.yaml", health.HealthStatusDegraded)
assertAppHealth(t, "./testdata/pod-running-restart-always.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/pod-running-restart-never.yaml", health.HealthStatusProgressing)
assertAppHealth(t, "./testdata/pod-running-restart-onfailure.yaml", health.HealthStatusProgressing)
assertAppHealth(t, "./testdata/pod-failed.yaml", health.HealthStatusDegraded)
assertAppHealth(t, "./testdata/pod-succeeded.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/pod-deletion.yaml", health.HealthStatusProgressing)
}

func TestApplication(t *testing.T) {
Expand Down Expand Up @@ -131,10 +137,10 @@ func TestGetArgoWorkflowHealth(t *testing.T) {
},
}

health, err := getArgoWorkflowHealth(&sampleWorkflow)
argohealth, err := health.GetArgoWorkflowHealth(&sampleWorkflow)
require.NoError(t, err)
assert.Equal(t, HealthStatusProgressing, health.Status)
assert.Equal(t, "This node is running", health.Message)
assert.Equal(t, health.HealthStatusProgressing, argohealth.Status)
assert.Equal(t, "This node is running", argohealth.Message)

sampleWorkflow = unstructured.Unstructured{Object: map[string]interface{}{
"spec": map[string]interface{}{
Expand All @@ -148,10 +154,10 @@ func TestGetArgoWorkflowHealth(t *testing.T) {
},
}

health, err = getArgoWorkflowHealth(&sampleWorkflow)
argohealth, err = health.GetArgoWorkflowHealth(&sampleWorkflow)
require.NoError(t, err)
assert.Equal(t, HealthStatusHealthy, health.Status)
assert.Equal(t, "This node is has succeeded", health.Message)
assert.Equal(t, health.HealthStatusHealthy, argohealth.Status)
assert.Equal(t, "This node is has succeeded", argohealth.Message)

sampleWorkflow = unstructured.Unstructured{Object: map[string]interface{}{
"spec": map[string]interface{}{
Expand All @@ -161,28 +167,28 @@ func TestGetArgoWorkflowHealth(t *testing.T) {
},
}

health, err = getArgoWorkflowHealth(&sampleWorkflow)
argohealth, err = health.GetArgoWorkflowHealth(&sampleWorkflow)
require.NoError(t, err)
assert.Equal(t, HealthStatusProgressing, health.Status)
assert.Equal(t, "", health.Message)
assert.Equal(t, health.HealthStatusProgressing, argohealth.Status)
assert.Equal(t, "", argohealth.Message)

}

func TestArgoApplication(t *testing.T) {
assertAppHealth(t, "./testdata/argo-application-healthy.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/argo-application-missing.yaml", HealthStatusMissing)
assertAppHealth(t, "./testdata/argo-application-healthy.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/argo-application-missing.yaml", health.HealthStatusMissing)
}

func TestFluxResources(t *testing.T) {
assertAppHealth(t, "./testdata/flux-kustomization-healthy.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/flux-kustomization-unhealthy.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/flux-kustomization-healthy.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/flux-kustomization-unhealthy.yaml", health.HealthStatusDegraded)

assertAppHealth(t, "./testdata/flux-helmrelease-healthy.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/flux-helmrelease-unhealthy.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/flux-helmrelease-healthy.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/flux-helmrelease-unhealthy.yaml", health.HealthStatusDegraded)

assertAppHealth(t, "./testdata/flux-helmrepository-healthy.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/flux-helmrepository-unhealthy.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/flux-helmrepository-healthy.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/flux-helmrepository-unhealthy.yaml", health.HealthStatusDegraded)

assertAppHealth(t, "./testdata/flux-gitrepository-healthy.yaml", HealthStatusHealthy)
assertAppHealth(t, "./testdata/flux-gitrepository-unhealthy.yaml", HealthStatusDegraded)
assertAppHealth(t, "./testdata/flux-gitrepository-healthy.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/flux-gitrepository-unhealthy.yaml", health.HealthStatusDegraded)
}
Loading

0 comments on commit f6117cf

Please sign in to comment.