github.com/argoproj/argo-cd@v1.8.7/util/health/health_test.go (about)

     1  package health
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/argoproj/gitops-engine/pkg/health"
     8  	synccommon "github.com/argoproj/gitops-engine/pkg/sync/common"
     9  	"github.com/argoproj/gitops-engine/pkg/utils/kube"
    10  	"github.com/ghodss/yaml"
    11  	"github.com/stretchr/testify/assert"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    14  
    15  	appv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    16  )
    17  
    18  func TestSetApplicationHealth(t *testing.T) {
    19  	yamlBytes, err := ioutil.ReadFile("./testdata/job-failed.yaml")
    20  	assert.Nil(t, err)
    21  	var failedJob unstructured.Unstructured
    22  	err = yaml.Unmarshal(yamlBytes, &failedJob)
    23  	assert.Nil(t, err)
    24  
    25  	yamlBytes, err = ioutil.ReadFile("./testdata/pod-running-restart-always.yaml")
    26  	assert.Nil(t, err)
    27  	var runningPod unstructured.Unstructured
    28  	err = yaml.Unmarshal(yamlBytes, &runningPod)
    29  	assert.Nil(t, err)
    30  
    31  	resources := []*appv1.ResourceStatus{
    32  		&appv1.ResourceStatus{
    33  			Group:   "",
    34  			Version: "v1",
    35  			Kind:    "Pod",
    36  			Name:    runningPod.GetName(),
    37  		},
    38  		&appv1.ResourceStatus{
    39  			Group:   "batch",
    40  			Version: "v1",
    41  			Kind:    "Job",
    42  			Name:    failedJob.GetName(),
    43  		},
    44  	}
    45  	liveObjs := []*unstructured.Unstructured{
    46  		&runningPod,
    47  		&failedJob,
    48  	}
    49  	healthStatus, err := SetApplicationHealth(resources, liveObjs, nil, noFilter)
    50  	assert.NoError(t, err)
    51  	assert.Equal(t, health.HealthStatusDegraded, healthStatus.Status)
    52  
    53  	// now mark the job as a hook and retry. it should ignore the hook and consider the app healthy
    54  	failedJob.SetAnnotations(map[string]string{synccommon.AnnotationKeyHook: "PreSync"})
    55  	healthStatus, err = SetApplicationHealth(resources, liveObjs, nil, noFilter)
    56  	assert.NoError(t, err)
    57  	assert.Equal(t, health.HealthStatusHealthy, healthStatus.Status)
    58  }
    59  
    60  func TestAppOfAppsHealth(t *testing.T) {
    61  	newAppLiveObj := func(name string, status health.HealthStatusCode) (*unstructured.Unstructured, *appv1.ResourceStatus) {
    62  		app := appv1.Application{
    63  			ObjectMeta: metav1.ObjectMeta{
    64  				Name: "foo",
    65  			},
    66  			TypeMeta: metav1.TypeMeta{
    67  				APIVersion: "argoproj.io/v1alpha1",
    68  				Kind:       "Application",
    69  			},
    70  			Status: appv1.ApplicationStatus{
    71  				Health: appv1.HealthStatus{
    72  					Status: status,
    73  				},
    74  			},
    75  		}
    76  		resStatus := &appv1.ResourceStatus{
    77  			Group:   "argoproj.io",
    78  			Version: "v1alpha1",
    79  			Kind:    "Application",
    80  			Name:    name,
    81  		}
    82  		return kube.MustToUnstructured(&app), resStatus
    83  	}
    84  
    85  	missingApp, missingStatus := newAppLiveObj("foo", health.HealthStatusMissing)
    86  	unknownApp, unknownStatus := newAppLiveObj("fooz", health.HealthStatusUnknown)
    87  	healthyApp, healthyStatus := newAppLiveObj("bar", health.HealthStatusHealthy)
    88  	degradedApp, degradedStatus := newAppLiveObj("baz", health.HealthStatusDegraded)
    89  
    90  	// verify missing child app does not affect app health
    91  	{
    92  		missingAndHealthyStatuses := []*appv1.ResourceStatus{missingStatus, healthyStatus}
    93  		missingAndHealthyLiveObjects := []*unstructured.Unstructured{missingApp, healthyApp}
    94  		healthStatus, err := SetApplicationHealth(missingAndHealthyStatuses, missingAndHealthyLiveObjects, nil, noFilter)
    95  		assert.NoError(t, err)
    96  		assert.Equal(t, health.HealthStatusHealthy, healthStatus.Status)
    97  	}
    98  
    99  	// verify unknown child app does not affect app health
   100  	{
   101  		unknownAndHealthyStatuses := []*appv1.ResourceStatus{unknownStatus, healthyStatus}
   102  		unknownAndHealthyLiveObjects := []*unstructured.Unstructured{unknownApp, healthyApp}
   103  		healthStatus, err := SetApplicationHealth(unknownAndHealthyStatuses, unknownAndHealthyLiveObjects, nil, noFilter)
   104  		assert.NoError(t, err)
   105  		assert.Equal(t, health.HealthStatusHealthy, healthStatus.Status)
   106  	}
   107  
   108  	// verify degraded does affect
   109  	{
   110  		degradedAndHealthyStatuses := []*appv1.ResourceStatus{degradedStatus, healthyStatus}
   111  		degradedAndHealthyLiveObjects := []*unstructured.Unstructured{degradedApp, healthyApp}
   112  		healthStatus, err := SetApplicationHealth(degradedAndHealthyStatuses, degradedAndHealthyLiveObjects, nil, noFilter)
   113  		assert.NoError(t, err)
   114  		assert.Equal(t, health.HealthStatusHealthy, healthStatus.Status)
   115  	}
   116  
   117  }
   118  
   119  func noFilter(obj *unstructured.Unstructured) bool {
   120  	return true
   121  }