1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
package internal
import (
"os"
"testing"
"time"
)
func TestIsSuppressed_EmptyPath(t *testing.T) {
// Empty file path should not suppress
if isSuppressed("", 86400) {
t.Error("Expected empty path to not suppress")
}
}
func TestIsSuppressed_NonExistentFile(t *testing.T) {
// Non-existent file should not suppress
if isSuppressed("/nonexistent/path/to/file", 86400) {
t.Error("Expected non-existent file to not suppress")
}
}
func TestIsSuppressed_RecentFile(t *testing.T) {
// Create a temporary file
tmpFile, err := os.CreateTemp("", "suppress_test")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
tmpFile.Close()
// Recent file should suppress
if !isSuppressed(tmpFile.Name(), 86400) {
t.Error("Expected recent file to suppress")
}
}
func TestIsSuppressed_OldFile(t *testing.T) {
// Create a temporary file
tmpFile, err := os.CreateTemp("", "suppress_test")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
tmpFile.Close()
// Set the file's modification time to 2 hours ago
oldTime := time.Now().Add(-2 * time.Hour)
if err := os.Chtimes(tmpFile.Name(), oldTime, oldTime); err != nil {
t.Fatalf("Failed to change file time: %v", err)
}
// File older than maxAgeS (1 hour = 3600s) should not suppress
if isSuppressed(tmpFile.Name(), 3600) {
t.Error("Expected old file to not suppress")
}
// File within maxAgeS (3 hours = 10800s) should suppress
if !isSuppressed(tmpFile.Name(), 10800) {
t.Error("Expected file within max age to suppress")
}
}
func TestIsCheckSuppressed_PrometheusCheck(t *testing.T) {
// Create a temporary file for Prometheus suppression
tmpFile, err := os.CreateTemp("", "prometheus_suppress_test")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
tmpFile.Close()
conf := config{
PrometheusOnlyIfNotExists: tmpFile.Name(),
PrometheusOnlyIfNotExistsMaxS: 86400,
Checks: make(map[string]check),
}
// Prometheus check should be suppressed when file exists
if !isCheckSuppressed("Prometheus: TestAlert", conf) {
t.Error("Expected Prometheus check to be suppressed")
}
// Non-Prometheus check should not be affected by Prometheus suppression
conf.Checks["Regular Check"] = check{}
if isCheckSuppressed("Regular Check", conf) {
t.Error("Expected regular check to not be suppressed by Prometheus config")
}
}
func TestIsCheckSuppressed_RegularCheck(t *testing.T) {
// Create a temporary file for check suppression
tmpFile, err := os.CreateTemp("", "check_suppress_test")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
tmpFile.Close()
conf := config{
PrometheusOnlyIfNotExistsMaxS: 86400,
Checks: map[string]check{
"Suppressed Check": {
OnlyIfNotExists: tmpFile.Name(),
OnlyIfNotExistsMaxS: 86400,
},
"Normal Check": {},
},
}
// Check with suppression file should be suppressed
if !isCheckSuppressed("Suppressed Check", conf) {
t.Error("Expected check with suppression file to be suppressed")
}
// Check without suppression file should not be suppressed
if isCheckSuppressed("Normal Check", conf) {
t.Error("Expected check without suppression file to not be suppressed")
}
// Unknown check (not in config) should not be suppressed
if isCheckSuppressed("Unknown Check", conf) {
t.Error("Expected unknown check to not be suppressed")
}
}
func TestIsCheckSuppressed_UsesGlobalDefaultMaxAge(t *testing.T) {
// Create a temporary file
tmpFile, err := os.CreateTemp("", "suppress_test")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
tmpFile.Close()
// Set the file's modification time to 2 hours ago
oldTime := time.Now().Add(-2 * time.Hour)
if err := os.Chtimes(tmpFile.Name(), oldTime, oldTime); err != nil {
t.Fatalf("Failed to change file time: %v", err)
}
// Config with short global max age (1 hour)
conf := config{
PrometheusOnlyIfNotExistsMaxS: 3600,
Checks: map[string]check{
"Test Check": {
OnlyIfNotExists: tmpFile.Name(),
OnlyIfNotExistsMaxS: 0, // Use global default
},
},
}
// Should NOT be suppressed because file is older than global default (1 hour)
if isCheckSuppressed("Test Check", conf) {
t.Error("Expected check to not be suppressed when file is older than global max age")
}
// Config with longer global max age (3 hours)
conf.PrometheusOnlyIfNotExistsMaxS = 10800
if !isCheckSuppressed("Test Check", conf) {
t.Error("Expected check to be suppressed when file is within global max age")
}
}
|