Skip to content

Commit

Permalink
Pullrequest comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cedi committed Sep 19, 2023
1 parent bfa00a3 commit 55b0b7e
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 44 deletions.
54 changes: 10 additions & 44 deletions internal/cmd/loadbalancer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,55 +178,21 @@ func loadBalancerHealth(l *hcloud.LoadBalancer) string {
unknownCount := 0

for _, lbTarget := range l.Targets {
switch loadBalancerTargetHealth(&lbTarget) {
case string(hcloud.LoadBalancerTargetHealthStatusStatusHealthy):
healthyCount++
for _, svcHealth := range lbTarget.HealthStatus {
switch svcHealth.Status {
case hcloud.LoadBalancerTargetHealthStatusStatusHealthy:
healthyCount++

case string(hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy):
unhealthyCount++
case hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy:
unhealthyCount++

case "mixed":
return "mixed"

default:
unknownCount++
}
}

switch len(l.Targets) {
case healthyCount:
return string(hcloud.LoadBalancerTargetHealthStatusStatusHealthy)

case unhealthyCount:
return string(hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy)

case unknownCount:
return string(hcloud.LoadBalancerTargetHealthStatusStatusUnknown)

default:
return "mixed"
}
}

func loadBalancerTargetHealth(t *hcloud.LoadBalancerTarget) string {
healthyCount := 0
unhealthyCount := 0
unknownCount := 0

for _, targetHealth := range t.HealthStatus {
switch targetHealth.Status {
case hcloud.LoadBalancerTargetHealthStatusStatusHealthy:
healthyCount++

case hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy:
unhealthyCount++

default:
unknownCount++
default:
unknownCount++
}
}
}

switch len(t.HealthStatus) {
switch len(l.Targets) * len(l.Services) {
case healthyCount:
return string(hcloud.LoadBalancerTargetHealthStatusStatusHealthy)

Expand Down
202 changes: 202 additions & 0 deletions internal/cmd/loadbalancer/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package loadbalancer

import (
"testing"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/stretchr/testify/assert"
)

func TestLoadBalancerHealth(t *testing.T) {
tests := []struct {
name string
lb *hcloud.LoadBalancer
expected string
}{
{
name: "healthy",
lb: &hcloud.LoadBalancer{
Name: "foobar",
Services: make([]hcloud.LoadBalancerService, 1),
Targets: []hcloud.LoadBalancerTarget{
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusHealthy,
},
},
},
},
},
expected: string(hcloud.LoadBalancerTargetHealthStatusStatusHealthy),
},
{
name: "unhealthy",
lb: &hcloud.LoadBalancer{
Name: "foobar",
Services: make([]hcloud.LoadBalancerService, 1),
Targets: []hcloud.LoadBalancerTarget{
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy,
},
},
},
},
},
expected: string(hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy),
},
{
name: "unknown",
lb: &hcloud.LoadBalancer{
Name: "foobar",
Services: make([]hcloud.LoadBalancerService, 1),
Targets: []hcloud.LoadBalancerTarget{
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnknown,
},
},
},
},
},
expected: string(hcloud.LoadBalancerTargetHealthStatusStatusUnknown),
},
{
name: "mixed",
lb: &hcloud.LoadBalancer{
Name: "foobar",
Services: make([]hcloud.LoadBalancerService, 1),
Targets: []hcloud.LoadBalancerTarget{
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusHealthy,
},
},
},
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy,
},
},
},
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnknown,
},
},
},
},
},
expected: "mixed",
},
{
name: "mixed_many_services_grouped_by_target",
lb: &hcloud.LoadBalancer{
Name: "foobar",
Services: make([]hcloud.LoadBalancerService, 3),
Targets: []hcloud.LoadBalancerTarget{
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusHealthy,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusHealthy,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusHealthy,
},
},
},
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy,
},
},
},
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnknown,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnknown,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnknown,
},
},
},
},
},
expected: "mixed",
},
{
name: "mixed_many_services_mixed",
lb: &hcloud.LoadBalancer{
Name: "foobar",
Services: make([]hcloud.LoadBalancerService, 3),
Targets: []hcloud.LoadBalancerTarget{
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusHealthy,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnknown,
},
},
},
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusHealthy,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnknown,
},
},
},
{
HealthStatus: []hcloud.LoadBalancerTargetHealthStatus{
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusHealthy,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnhealthy,
},
{
Status: hcloud.LoadBalancerTargetHealthStatusStatusUnknown,
},
},
},
},
},
expected: "mixed",
},
}

for _, test := range tests {
res := loadBalancerHealth(test.lb)
assert.Equal(t, test.expected, res, test.name)
}
}

0 comments on commit 55b0b7e

Please sign in to comment.