@@ -16,6 +16,7 @@ import (
1616 "k8s.io/apimachinery/pkg/runtime/schema"
1717 "sigs.k8s.io/yaml"
1818
19+ "github.com/argoproj/argo-cd/v3/common"
1920 "github.com/argoproj/argo-cd/v3/pkg/apis/application"
2021 appv1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
2122 "github.com/argoproj/argo-cd/v3/util/lua"
@@ -103,12 +104,103 @@ func TestSetApplicationHealth_ResourceHealthNotPersisted(t *testing.T) {
103104 assert .Nil (t , resourceStatuses [0 ].Health )
104105}
105106
107+ func TestSetApplicationHealth_NoResource (t * testing.T ) {
108+ resources := []managedResource {}
109+ resourceStatuses := initStatuses (resources )
110+
111+ healthStatus , err := setApplicationHealth (resources , resourceStatuses , lua.ResourceHealthOverrides {}, app , true )
112+ require .NoError (t , err )
113+ assert .Equal (t , health .HealthStatusHealthy , healthStatus )
114+ }
115+
116+ func TestSetApplicationHealth_OnlyHooks (t * testing.T ) {
117+ pod := resourceFromFile ("./testdata/pod-running-restart-always.yaml" )
118+ pod .SetAnnotations (map [string ]string {synccommon .AnnotationKeyHook : string (synccommon .HookTypeSync )})
119+
120+ resources := []managedResource {{
121+ Group : "" , Version : "v1" , Kind : "Pod" , Target : & pod , Live : & pod ,
122+ }}
123+ resourceStatuses := initStatuses (resources )
124+
125+ healthStatus , err := setApplicationHealth (resources , resourceStatuses , lua.ResourceHealthOverrides {}, app , true )
126+ require .NoError (t , err )
127+ assert .Equal (t , health .HealthStatusHealthy , healthStatus )
128+ }
129+
106130func TestSetApplicationHealth_MissingResource (t * testing.T ) {
107131 pod := resourceFromFile ("./testdata/pod-running-restart-always.yaml" )
132+ pod2 := pod .DeepCopy ()
133+ pod2 .SetName ("pod2" )
134+
135+ resources := []managedResource {
136+ {Group : "" , Version : "v1" , Kind : "Pod" , Target : & pod },
137+ {Group : "" , Version : "v1" , Kind : "Pod" , Target : pod2 , Live : pod2 },
138+ }
139+ resourceStatuses := initStatuses (resources )
140+
141+ healthStatus , err := setApplicationHealth (resources , resourceStatuses , lua.ResourceHealthOverrides {}, app , true )
142+ require .NoError (t , err )
143+ assert .Equal (t , health .HealthStatusHealthy , healthStatus )
144+ }
145+
146+ func TestSetApplicationHealth_MissingResource_WithIgnoreHealthcheck (t * testing.T ) {
147+ pod := resourceFromFile ("./testdata/pod-running-restart-always.yaml" )
148+ pod2 := pod .DeepCopy ()
149+ pod2 .SetName ("pod2" )
150+ pod2 .SetAnnotations (map [string ]string {common .AnnotationIgnoreHealthCheck : "true" })
151+
152+ resources := []managedResource {
153+ {Group : "" , Version : "v1" , Kind : "Pod" , Target : & pod },
154+ {Group : "" , Version : "v1" , Kind : "Pod" , Target : pod2 , Live : pod2 },
155+ }
156+ resourceStatuses := initStatuses (resources )
157+
158+ healthStatus , err := setApplicationHealth (resources , resourceStatuses , lua.ResourceHealthOverrides {}, app , true )
159+ require .NoError (t , err )
160+ assert .Equal (t , health .HealthStatusHealthy , healthStatus )
161+ }
162+
163+ func TestSetApplicationHealth_MissingResource_WithChildApp (t * testing.T ) {
164+ childApp := newAppLiveObj (health .HealthStatusUnknown )
165+ pod := resourceFromFile ("./testdata/pod-running-restart-always.yaml" )
166+ resources := []managedResource {
167+ {Group : application .Group , Version : "v1alpha1" , Kind : application .ApplicationKind , Target : childApp , Live : childApp },
168+ {Group : "" , Version : "v1" , Kind : "Pod" , Target : & pod },
169+ }
170+ resourceStatuses := initStatuses (resources )
171+
172+ healthStatus , err := setApplicationHealth (resources , resourceStatuses , lua.ResourceHealthOverrides {}, app , true )
173+ require .NoError (t , err )
174+ assert .Equal (t , health .HealthStatusHealthy , healthStatus )
175+ }
176+
177+ func TestSetApplicationHealth_AllMissingResources (t * testing.T ) {
178+ pod := resourceFromFile ("./testdata/pod-running-restart-always.yaml" )
179+ pod2 := pod .DeepCopy ()
180+ pod2 .SetName ("pod2" )
181+
182+ resources := []managedResource {
183+ {Group : "" , Version : "v1" , Kind : "Pod" , Target : & pod },
184+ {Group : "" , Version : "v1" , Kind : "Pod" , Target : pod2 },
185+ }
186+ resourceStatuses := initStatuses (resources )
187+
188+ healthStatus , err := setApplicationHealth (resources , resourceStatuses , lua.ResourceHealthOverrides {}, app , true )
189+ require .NoError (t , err )
190+ assert .Equal (t , health .HealthStatusMissing , healthStatus )
191+ }
192+
193+ func TestSetApplicationHealth_AllMissingResources_WithHooks (t * testing.T ) {
194+ pod := resourceFromFile ("./testdata/pod-running-restart-always.yaml" )
195+ pod2 := pod .DeepCopy ()
196+ pod2 .SetName ("pod2" )
197+ pod2 .SetAnnotations (map [string ]string {synccommon .AnnotationKeyHook : string (synccommon .HookTypeSync )})
108198
109199 resources := []managedResource {{
110200 Group : "" , Version : "v1" , Kind : "Pod" , Target : & pod ,
111- }, {}}
201+ }, {
202+ Group : "" , Version : "v1" , Kind : "Pod" , Target : pod2 , Live : pod2 ,
203+ }}
112204 resourceStatuses := initStatuses (resources )
113205
114206 healthStatus , err := setApplicationHealth (resources , resourceStatuses , lua.ResourceHealthOverrides {}, app , true )
@@ -149,32 +241,6 @@ func TestSetApplicationHealth_HealthImproves(t *testing.T) {
149241 }
150242}
151243
152- func TestSetApplicationHealth_MissingResourceNoBuiltHealthCheck (t * testing.T ) {
153- cm := resourceFromFile ("./testdata/configmap.yaml" )
154-
155- resources := []managedResource {{
156- Group : "" , Version : "v1" , Kind : "ConfigMap" , Target : & cm ,
157- }}
158- resourceStatuses := initStatuses (resources )
159-
160- t .Run ("NoOverride" , func (t * testing.T ) {
161- healthStatus , err := setApplicationHealth (resources , resourceStatuses , lua.ResourceHealthOverrides {}, app , true )
162- require .NoError (t , err )
163- assert .Equal (t , health .HealthStatusHealthy , healthStatus )
164- assert .Equal (t , health .HealthStatusMissing , resourceStatuses [0 ].Health .Status )
165- })
166-
167- t .Run ("HasOverride" , func (t * testing.T ) {
168- healthStatus , err := setApplicationHealth (resources , resourceStatuses , lua.ResourceHealthOverrides {
169- lua .GetConfigMapKey (schema.GroupVersionKind {Version : "v1" , Kind : "ConfigMap" }): appv1.ResourceOverride {
170- HealthLua : "some health check" ,
171- },
172- }, app , true )
173- require .NoError (t , err )
174- assert .Equal (t , health .HealthStatusMissing , healthStatus )
175- })
176- }
177-
178244func newAppLiveObj (status health.HealthStatusCode ) * unstructured.Unstructured {
179245 app := appv1.Application {
180246 ObjectMeta : metav1.ObjectMeta {
@@ -214,9 +280,9 @@ return hs`,
214280 }
215281
216282 t .Run ("ChildAppDegraded" , func (t * testing.T ) {
217- degradedApp := newAppLiveObj (health .HealthStatusDegraded )
283+ childApp := newAppLiveObj (health .HealthStatusDegraded )
218284 resources := []managedResource {{
219- Group : application .Group , Version : "v1alpha1" , Kind : application .ApplicationKind , Live : degradedApp ,
285+ Group : application .Group , Version : "v1alpha1" , Kind : application .ApplicationKind , Live : childApp ,
220286 }, {}}
221287 resourceStatuses := initStatuses (resources )
222288
@@ -226,9 +292,21 @@ return hs`,
226292 })
227293
228294 t .Run ("ChildAppMissing" , func (t * testing.T ) {
229- degradedApp := newAppLiveObj (health .HealthStatusMissing )
295+ childApp := newAppLiveObj (health .HealthStatusMissing )
296+ resources := []managedResource {{
297+ Group : application .Group , Version : "v1alpha1" , Kind : application .ApplicationKind , Live : childApp ,
298+ }, {}}
299+ resourceStatuses := initStatuses (resources )
300+
301+ healthStatus , err := setApplicationHealth (resources , resourceStatuses , overrides , app , true )
302+ require .NoError (t , err )
303+ assert .Equal (t , health .HealthStatusHealthy , healthStatus )
304+ })
305+
306+ t .Run ("ChildAppUnknown" , func (t * testing.T ) {
307+ childApp := newAppLiveObj (health .HealthStatusUnknown )
230308 resources := []managedResource {{
231- Group : application .Group , Version : "v1alpha1" , Kind : application .ApplicationKind , Live : degradedApp ,
309+ Group : application .Group , Version : "v1alpha1" , Kind : application .ApplicationKind , Live : childApp ,
232310 }, {}}
233311 resourceStatuses := initStatuses (resources )
234312
0 commit comments