mirror of
https://github.com/TwiN/gatus.git
synced 2025-06-21 10:18:23 +02:00
test: Fix flaky maintenance test
This commit is contained in:
parent
35a3238bc9
commit
d5fe682f9a
@ -179,14 +179,14 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
scenarios := []struct {
|
scenarios := []struct {
|
||||||
name string
|
name string
|
||||||
cfg *Config
|
cfg *Config
|
||||||
expected bool
|
expectedUnderMaintenance bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "disabled",
|
name: "disabled",
|
||||||
cfg: &Config{
|
cfg: &Config{
|
||||||
Enabled: &no,
|
Enabled: &no,
|
||||||
},
|
},
|
||||||
expected: false,
|
expectedUnderMaintenance: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "under-maintenance-explicitly-enabled",
|
name: "under-maintenance-explicitly-enabled",
|
||||||
@ -195,7 +195,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Start: fmt.Sprintf("%02d:00", now.Hour()),
|
Start: fmt.Sprintf("%02d:00", now.Hour()),
|
||||||
Duration: 2 * time.Hour,
|
Duration: 2 * time.Hour,
|
||||||
},
|
},
|
||||||
expected: true,
|
expectedUnderMaintenance: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "under-maintenance-starting-now-for-2h",
|
name: "under-maintenance-starting-now-for-2h",
|
||||||
@ -203,7 +203,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Start: fmt.Sprintf("%02d:00", now.Hour()),
|
Start: fmt.Sprintf("%02d:00", now.Hour()),
|
||||||
Duration: 2 * time.Hour,
|
Duration: 2 * time.Hour,
|
||||||
},
|
},
|
||||||
expected: true,
|
expectedUnderMaintenance: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "under-maintenance-starting-now-for-8h",
|
name: "under-maintenance-starting-now-for-8h",
|
||||||
@ -211,7 +211,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Start: fmt.Sprintf("%02d:00", now.Hour()),
|
Start: fmt.Sprintf("%02d:00", now.Hour()),
|
||||||
Duration: 8 * time.Hour,
|
Duration: 8 * time.Hour,
|
||||||
},
|
},
|
||||||
expected: true,
|
expectedUnderMaintenance: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "under-maintenance-starting-now-for-8h-explicit-days",
|
name: "under-maintenance-starting-now-for-8h-explicit-days",
|
||||||
@ -220,7 +220,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Duration: 8 * time.Hour,
|
Duration: 8 * time.Hour,
|
||||||
Every: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"},
|
Every: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"},
|
||||||
},
|
},
|
||||||
expected: true,
|
expectedUnderMaintenance: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "under-maintenance-starting-now-for-23h-explicit-days",
|
name: "under-maintenance-starting-now-for-23h-explicit-days",
|
||||||
@ -229,7 +229,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Duration: 23 * time.Hour,
|
Duration: 23 * time.Hour,
|
||||||
Every: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"},
|
Every: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"},
|
||||||
},
|
},
|
||||||
expected: true,
|
expectedUnderMaintenance: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "under-maintenance-starting-4h-ago-for-8h",
|
name: "under-maintenance-starting-4h-ago-for-8h",
|
||||||
@ -237,7 +237,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-4)),
|
Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-4)),
|
||||||
Duration: 8 * time.Hour,
|
Duration: 8 * time.Hour,
|
||||||
},
|
},
|
||||||
expected: true,
|
expectedUnderMaintenance: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "under-maintenance-starting-22h-ago-for-23h",
|
name: "under-maintenance-starting-22h-ago-for-23h",
|
||||||
@ -245,7 +245,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-22)),
|
Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-22)),
|
||||||
Duration: 23 * time.Hour,
|
Duration: 23 * time.Hour,
|
||||||
},
|
},
|
||||||
expected: true,
|
expectedUnderMaintenance: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "under-maintenance-starting-22h-ago-for-24h",
|
name: "under-maintenance-starting-22h-ago-for-24h",
|
||||||
@ -253,16 +253,16 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-22)),
|
Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-22)),
|
||||||
Duration: 24 * time.Hour,
|
Duration: 24 * time.Hour,
|
||||||
},
|
},
|
||||||
expected: true,
|
expectedUnderMaintenance: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "under-maintenance-amsterdam-timezone-starting-now-for-2h",
|
name: "under-maintenance-amsterdam-timezone-starting-now-for-2h",
|
||||||
cfg: &Config{
|
cfg: &Config{
|
||||||
Start: fmt.Sprintf("%02d:00", now.Hour()),
|
Start: fmt.Sprintf("%02d:00", inTimezone(now, "Europe/Amsterdam", t).Hour()),
|
||||||
Duration: 2 * time.Hour,
|
Duration: 2 * time.Hour,
|
||||||
Timezone: "Europe/Amsterdam",
|
Timezone: "Europe/Amsterdam",
|
||||||
},
|
},
|
||||||
expected: true,
|
expectedUnderMaintenance: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "under-maintenance-perth-timezone-starting-now-for-2h",
|
name: "under-maintenance-perth-timezone-starting-now-for-2h",
|
||||||
@ -271,7 +271,17 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Duration: 2 * time.Hour,
|
Duration: 2 * time.Hour,
|
||||||
Timezone: "Australia/Perth",
|
Timezone: "Australia/Perth",
|
||||||
},
|
},
|
||||||
expected: true,
|
expectedUnderMaintenance: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "not-under-maintenance-los-angeles-timezone-starting-now-for-2h-today",
|
||||||
|
cfg: &Config{
|
||||||
|
Start: fmt.Sprintf("%02d:00", inTimezone(now, "America/Los_Angeles", t).Hour()),
|
||||||
|
Duration: 2 * time.Hour,
|
||||||
|
Timezone: "America/Los_Angeles",
|
||||||
|
Every: []string{now.Weekday().String()},
|
||||||
|
},
|
||||||
|
expectedUnderMaintenance: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "under-maintenance-utc-timezone-starting-now-for-2h",
|
name: "under-maintenance-utc-timezone-starting-now-for-2h",
|
||||||
@ -280,7 +290,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Duration: 2 * time.Hour,
|
Duration: 2 * time.Hour,
|
||||||
Timezone: "UTC",
|
Timezone: "UTC",
|
||||||
},
|
},
|
||||||
expected: true,
|
expectedUnderMaintenance: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "not-under-maintenance-starting-4h-ago-for-3h",
|
name: "not-under-maintenance-starting-4h-ago-for-3h",
|
||||||
@ -288,7 +298,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-4)),
|
Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-4)),
|
||||||
Duration: 3 * time.Hour,
|
Duration: 3 * time.Hour,
|
||||||
},
|
},
|
||||||
expected: false,
|
expectedUnderMaintenance: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "not-under-maintenance-starting-5h-ago-for-1h",
|
name: "not-under-maintenance-starting-5h-ago-for-1h",
|
||||||
@ -296,7 +306,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-5)),
|
Start: fmt.Sprintf("%02d:00", normalizeHour(now.Hour()-5)),
|
||||||
Duration: time.Hour,
|
Duration: time.Hour,
|
||||||
},
|
},
|
||||||
expected: false,
|
expectedUnderMaintenance: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "not-under-maintenance-today",
|
name: "not-under-maintenance-today",
|
||||||
@ -305,7 +315,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Duration: time.Hour,
|
Duration: time.Hour,
|
||||||
Every: []string{now.Add(48 * time.Hour).Weekday().String()},
|
Every: []string{now.Add(48 * time.Hour).Weekday().String()},
|
||||||
},
|
},
|
||||||
expected: false,
|
expectedUnderMaintenance: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "not-under-maintenance-today-with-24h-duration",
|
name: "not-under-maintenance-today-with-24h-duration",
|
||||||
@ -314,17 +324,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
Duration: 24 * time.Hour,
|
Duration: 24 * time.Hour,
|
||||||
Every: []string{now.Add(48 * time.Hour).Weekday().String()},
|
Every: []string{now.Add(48 * time.Hour).Weekday().String()},
|
||||||
},
|
},
|
||||||
expected: false,
|
expectedUnderMaintenance: false,
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "not-under-maintenance-los-angeles-timezone-starting-now-for-2h-today",
|
|
||||||
cfg: &Config{
|
|
||||||
Start: fmt.Sprintf("%02d:00", now.Hour()),
|
|
||||||
Duration: 2 * time.Hour,
|
|
||||||
Timezone: "America/Los_Angeles",
|
|
||||||
Every: []string{now.Weekday().String()},
|
|
||||||
},
|
|
||||||
expected: false,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, scenario := range scenarios {
|
for _, scenario := range scenarios {
|
||||||
@ -335,8 +335,8 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
|||||||
t.Fatal("validation shouldn't have returned an error, got", err)
|
t.Fatal("validation shouldn't have returned an error, got", err)
|
||||||
}
|
}
|
||||||
isUnderMaintenance := scenario.cfg.IsUnderMaintenance()
|
isUnderMaintenance := scenario.cfg.IsUnderMaintenance()
|
||||||
if isUnderMaintenance != scenario.expected {
|
if isUnderMaintenance != scenario.expectedUnderMaintenance {
|
||||||
t.Errorf("expected %v, got %v", scenario.expected, isUnderMaintenance)
|
t.Errorf("expectedUnderMaintenance %v, got %v", scenario.expectedUnderMaintenance, isUnderMaintenance)
|
||||||
t.Logf("start=%v; duration=%v; now=%v", scenario.cfg.Start, scenario.cfg.Duration, time.Now().UTC())
|
t.Logf("start=%v; duration=%v; now=%v", scenario.cfg.Start, scenario.cfg.Duration, time.Now().UTC())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -352,7 +352,6 @@ func normalizeHour(hour int) int {
|
|||||||
|
|
||||||
func inTimezone(passedTime time.Time, timezone string, t *testing.T) time.Time {
|
func inTimezone(passedTime time.Time, timezone string, t *testing.T) time.Time {
|
||||||
timezoneLocation, err := time.LoadLocation(timezone)
|
timezoneLocation, err := time.LoadLocation(timezone)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("timezone %s did not load", timezone)
|
t.Fatalf("timezone %s did not load", timezone)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user