Skip to content

Commit

Permalink
chore: add service health tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Nov 28, 2024
1 parent 1e5cf60 commit cda0e78
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 20 deletions.
5 changes: 4 additions & 1 deletion pkg/health/health_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package health

import (
"fmt"
"time"

"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
Expand Down Expand Up @@ -30,8 +32,9 @@ func getCorev1ServiceHealth(service *corev1.Service) (*HealthStatus, error) {
health.Health = HealthHealthy
health.Ready = true
} else {
age := time.Since(service.CreationTimestamp.Time)
health.Status = HealthStatusCreating
health.Health = HealthUnknown
health.Health = lo.Ternary(age < time.Hour, HealthUnknown, HealthUnhealthy)
}
} else {
health.Ready = true
Expand Down
19 changes: 0 additions & 19 deletions pkg/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,25 +451,6 @@ func TestPVCHealth(t *testing.T) {
assertAppHealthMsg(t, "./testdata/pvc-pending.yaml", health.HealthStatusProgressing, health.HealthHealthy, false)
}

func TestServiceHealth(t *testing.T) {
assertAppHealthMsg(t, "./testdata/svc-clusterip.yaml", health.HealthStatusUnknown, health.HealthUnknown, true)
assertAppHealthMsg(t, "./testdata/svc-loadbalancer.yaml", health.HealthStatusRunning, health.HealthHealthy, true)
assertAppHealthMsg(
t,
"./testdata/svc-loadbalancer-unassigned.yaml",
health.HealthStatusCreating,
health.HealthUnknown,
false,
)
assertAppHealthMsg(
t,
"./testdata/svc-loadbalancer-nonemptylist.yaml",
health.HealthStatusRunning,
health.HealthHealthy,
true,
)
}

func TestIngressHealth(t *testing.T) {
assertAppHealthMsg(t, "./testdata/ingress.yaml", health.HealthStatusHealthy, health.HealthHealthy, true)
assertAppHealthMsg(t, "./testdata/ingress-unassigned.yaml", health.HealthStatusPending, health.HealthHealthy, false)
Expand Down
25 changes: 25 additions & 0 deletions pkg/health/testdata/Kubernetes/Service/svc-clusterip.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: v1
kind: Service
metadata:
annotations:
expected-ready: "true"
expected-health: unknown
creationTimestamp: 2018-10-27T06:36:27Z
name: argocd-metrics
namespace: argocd
resourceVersion: "1131"
selfLink: /api/v1/namespaces/argocd/services/argocd-metrics
uid: a1f65069-d9b2-11e8-b3c1-9ae2f452bd03
spec:
clusterIP: 10.96.199.2
ports:
- name: http
port: 8082
protocol: TCP
targetPort: 8082
selector:
app: argocd-server
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
38 changes: 38 additions & 0 deletions pkg/health/testdata/Kubernetes/Service/svc-loadbalancer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
apiVersion: v1
kind: Service
metadata:
annotations:
expected-status: Running
expected-health: healthy
expected-ready: "true"
service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout: "600"
creationTimestamp: 2018-06-05T23:34:58Z
labels:
app.kubernetes.io/instance: argocd-cdp
name: argocd-server
namespace: argocd
resourceVersion: "32559487"
selfLink: /api/v1/namespaces/argocd/services/argocd-server
uid: 0f5885a9-6919-11e8-ad29-020124679688
spec:
clusterIP: 100.69.46.185
externalTrafficPolicy: Cluster
ports:
- name: http
nodePort: 30354
port: 80
protocol: TCP
targetPort: 8080
- name: https
nodePort: 31866
port: 443
protocol: TCP
targetPort: 8080
selector:
app: argocd-server
sessionAffinity: None
type: LoadBalancer
status:
loadBalancer:
ingress:
- hostname: abc123.us-west-2.elb.amazonaws.com
29 changes: 29 additions & 0 deletions pkg/health/testdata/Kubernetes/Service/unhealthy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: v1
kind: Service
metadata:
annotations:
expected-status: Creating
expected-health: unhealthy
expected-ready: "false"
creationTimestamp: "@now-1h"
name: argo-artifacts
namespace: argo
resourceVersion: "346792"
selfLink: /api/v1/namespaces/argo/services/argo-artifacts
uid: 586f5e57-e160-11e8-b3c1-9ae2f452bd03
spec:
clusterIP: 10.105.70.181
externalTrafficPolicy: Cluster
ports:
- name: service
nodePort: 32667
port: 9000
protocol: TCP
targetPort: 9000
selector:
app: minio
release: argo-artifacts
sessionAffinity: None
type: LoadBalancer
status:
loadBalancer: {}
29 changes: 29 additions & 0 deletions pkg/health/testdata/Kubernetes/Service/unknown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: v1
kind: Service
metadata:
annotations:
expected-status: Creating
expected-health: unknown
expected-ready: "false"
creationTimestamp: "@now-5m"
name: argo-artifacts
namespace: argo
resourceVersion: "346792"
selfLink: /api/v1/namespaces/argo/services/argo-artifacts
uid: 586f5e57-e160-11e8-b3c1-9ae2f452bd03
spec:
clusterIP: 10.105.70.181
externalTrafficPolicy: Cluster
ports:
- name: service
nodePort: 32667
port: 9000
protocol: TCP
targetPort: 9000
selector:
app: minio
release: argo-artifacts
sessionAffinity: None
type: LoadBalancer
status:
loadBalancer: {}

0 comments on commit cda0e78

Please sign in to comment.