Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/component_health_test.go
4093 views
1
package component_test
2
3
import (
4
"testing"
5
"time"
6
7
"github.com/grafana/agent/component"
8
"github.com/stretchr/testify/require"
9
)
10
11
func TestMergeHealth(t *testing.T) {
12
var (
13
jan1 = time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC)
14
jan2 = time.Date(2023, time.January, 2, 0, 0, 0, 0, time.UTC)
15
)
16
17
_ = jan2
18
19
tt := []struct {
20
name string
21
healths []component.Health
22
expectIndex int
23
}{
24
{
25
name: "returns first health",
26
healths: []component.Health{{
27
Health: component.HealthTypeHealthy,
28
UpdateTime: jan1,
29
}},
30
expectIndex: 0,
31
},
32
{
33
name: "exited > unhealthy",
34
healths: []component.Health{{
35
Health: component.HealthTypeUnhealthy,
36
UpdateTime: jan1,
37
}, {
38
Health: component.HealthTypeExited,
39
UpdateTime: jan1,
40
}},
41
expectIndex: 1,
42
},
43
{
44
name: "unhealthy > healthy",
45
healths: []component.Health{{
46
Health: component.HealthTypeHealthy,
47
UpdateTime: jan1,
48
}, {
49
Health: component.HealthTypeUnhealthy,
50
UpdateTime: jan1,
51
}},
52
expectIndex: 1,
53
},
54
{
55
name: "unknown > healthy",
56
healths: []component.Health{{
57
Health: component.HealthTypeHealthy,
58
UpdateTime: jan1,
59
}, {
60
Health: component.HealthTypeUnknown,
61
UpdateTime: jan1,
62
}},
63
expectIndex: 1,
64
},
65
{
66
name: "newer timestamp",
67
healths: []component.Health{{
68
Health: component.HealthTypeUnhealthy,
69
UpdateTime: jan1,
70
}, {
71
Health: component.HealthTypeUnhealthy,
72
UpdateTime: jan2,
73
}},
74
expectIndex: 1,
75
},
76
{
77
name: "use first found of matching health type and time",
78
healths: []component.Health{{
79
Health: component.HealthTypeHealthy,
80
UpdateTime: jan2,
81
}, {
82
Health: component.HealthTypeUnhealthy,
83
UpdateTime: jan2,
84
}, {
85
Health: component.HealthTypeUnhealthy,
86
UpdateTime: jan2,
87
}},
88
expectIndex: 1,
89
},
90
}
91
92
for _, tc := range tt {
93
t.Run(tc.name, func(t *testing.T) {
94
if len(tc.healths) == 0 {
95
panic("Must have at least one health in test case")
96
}
97
98
expect := tc.healths[tc.expectIndex]
99
actual := component.LeastHealthy(tc.healths[0], tc.healths[1:]...)
100
101
require.Equal(t, expect, actual)
102
})
103
}
104
}
105
106