Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ linters:
- legacy
- std-error-handling
rules:
- linters:
- revive
text: "var-naming: avoid meaningless package names"
path: internal/util/
- linters:
- bodyclose
- dogsled
Expand Down
16 changes: 13 additions & 3 deletions internal/model/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewUptimeCheck(ingressName string, annotations map[string]string) (*UptimeC
ID: id,
Name: name,
URL: url,
Tags: stringToSlice(annotations[AnnotationTags]),
Tags: parseTags(annotations[AnnotationTags]),
Interval: interval,
RequestHeaders: kvStringToMap(annotations[AnnotationRequestHeaders]),
StringContains: annotations[AnnotationStringContains],
Expand Down Expand Up @@ -99,14 +99,24 @@ func kvStringToMap(s string) map[string]string {
return result
}

func stringToSlice(s string) []string {
func parseTags(s string) []string {
if s == "" {
return nil
}

seen := make(map[string]struct{})
var result []string
splits := strings.Split(s, ",")
for _, part := range splits {
result = append(result, strings.TrimSpace(part))
value := strings.TrimSpace(part)
if value == "" {
continue
}
if _, ok := seen[value]; ok {
continue
}
seen[value] = struct{}{}
result = append(result, value)
}
return result
}
40 changes: 33 additions & 7 deletions internal/model/check_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"slices"
"testing"
)

Expand All @@ -9,6 +10,7 @@ func TestNewUptimeCheck(t *testing.T) {
name string
ingressName string
annotations map[string]string
wantTags []string
wantErr bool
}{
{
Expand All @@ -23,7 +25,8 @@ func TestNewUptimeCheck(t *testing.T) {
"uptime.pdok.nl/response-check-for-string-contains": "test string",
"uptime.pdok.nl/response-check-for-string-not-contains": "",
},
wantErr: false,
wantTags: []string{"tag1", "tag2", TagManagedBy},
wantErr: false,
},
{
name: "Missing ID annotation",
Expand All @@ -36,7 +39,8 @@ func TestNewUptimeCheck(t *testing.T) {
"uptime.pdok.nl/response-check-for-string-contains": "test string",
"uptime.pdok.nl/response-check-for-string-not-contains": "",
},
wantErr: true,
wantTags: nil,
wantErr: true,
},
{
name: "Missing Name annotation",
Expand All @@ -49,7 +53,8 @@ func TestNewUptimeCheck(t *testing.T) {
"uptime.pdok.nl/response-check-for-string-contains": "test string",
"uptime.pdok.nl/response-check-for-string-not-contains": "",
},
wantErr: true,
wantTags: nil,
wantErr: true,
},
{
name: "Missing URL annotation",
Expand All @@ -62,7 +67,8 @@ func TestNewUptimeCheck(t *testing.T) {
"uptime.pdok.nl/response-check-for-string-contains": "test string",
"uptime.pdok.nl/response-check-for-string-not-contains": "",
},
wantErr: true,
wantTags: nil,
wantErr: true,
},
{
name: "Missing tags annotation",
Expand All @@ -75,7 +81,8 @@ func TestNewUptimeCheck(t *testing.T) {
"uptime.pdok.nl/response-check-for-string-contains": "test string",
"uptime.pdok.nl/response-check-for-string-not-contains": "",
},
wantErr: false,
wantTags: []string{TagManagedBy},
wantErr: false,
},
{
name: "Missing request-headers annotation",
Expand All @@ -88,15 +95,34 @@ func TestNewUptimeCheck(t *testing.T) {
"uptime.pdok.nl/response-check-for-string-contains": "test string",
"uptime.pdok.nl/response-check-for-string-not-contains": "",
},
wantErr: false,
wantTags: []string{"tag1", "tag2", TagManagedBy},
wantErr: false,
},
{
name: "Duplicate tags",
ingressName: "test-ingress",
annotations: map[string]string{
"uptime.pdok.nl/id": "1234567890",
"uptime.pdok.nl/name": "Test Check",
"uptime.pdok.nl/url": "https://pdok.example",
"uptime.pdok.nl/tags": "tag1, tag2, tag1, tag3, tag2, , tag3",
},
wantTags: []string{"tag1", "tag2", "tag3", TagManagedBy},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewUptimeCheck(tt.ingressName, tt.annotations)
check, err := NewUptimeCheck(tt.ingressName, tt.annotations)
if (err != nil) != tt.wantErr {
t.Errorf("NewUptimeCheck() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr || tt.wantTags == nil {
return
}
if !slices.Equal(check.Tags, tt.wantTags) {
t.Errorf("NewUptimeCheck().Tags = %v, want %v", check.Tags, tt.wantTags)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/service/providers/betterstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (h Client) execRequest(req *http.Request, expectedStatus int) (*http.Respon
if resp.StatusCode != expectedStatus {
defer resp.Body.Close()
result, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("got status %d, expected %d. Body: %b", resp.StatusCode, expectedStatus, result)
return nil, fmt.Errorf("got status %d, expected %d. Body: %s", resp.StatusCode, expectedStatus, result)
}
return resp, nil // caller should close resp.Body!
}
Expand Down
2 changes: 1 addition & 1 deletion internal/util/flag.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util //nolint:revive
package util

import (
"strings"
Expand Down
Loading