-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add unit tests - small code improvements
- Loading branch information
Showing
31 changed files
with
1,589 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package config | ||
|
||
import ( | ||
"heartbeats/pkg/heartbeat" | ||
"heartbeats/pkg/history" | ||
"heartbeats/pkg/notify" | ||
"heartbeats/pkg/notify/notifier" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Sample configuration YAML for testing. | ||
const sampleConfig = ` | ||
version: "1.0.0" | ||
verbose: true | ||
path: "./config.yaml" | ||
server: | ||
siteRoot: "http://localhost:8080" | ||
listenAddress: "localhost:8080" | ||
cache: | ||
maxSize: 100 | ||
reduce: 10 | ||
notifications: | ||
slack: | ||
type: "slack" | ||
slack_config: | ||
channel: "general" | ||
heartbeats: | ||
heartbeat1: | ||
name: "heartbeat1" | ||
interval: "1m" | ||
grace: "1m" | ||
notifications: | ||
- slack | ||
` | ||
|
||
func writeSampleConfig(t *testing.T, content string) string { | ||
file, err := os.CreateTemp("", "config*.yaml") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp file: %v", err) | ||
} | ||
defer file.Close() | ||
|
||
if _, err := file.WriteString(content); err != nil { | ||
t.Fatalf("Failed to write to temp file: %v", err) | ||
} | ||
|
||
return file.Name() | ||
} | ||
|
||
func TestConfig_Read(t *testing.T) { | ||
App.NotificationStore = notify.NewStore() | ||
HistoryStore = history.NewStore() | ||
|
||
tempFile := writeSampleConfig(t, sampleConfig) | ||
defer os.Remove(tempFile) | ||
|
||
App.Path = tempFile | ||
|
||
err := App.Read() | ||
assert.NoError(t, err, "Expected no error when reading the config file") | ||
|
||
notification := App.NotificationStore.Get("slack") | ||
assert.NotNil(t, notification, "Expected slack notification to be present") | ||
assert.Equal(t, `{{ if eq .Status "ok" }}good{{ else }}danger{{ end }}`, notification.SlackConfig.ColorTemplate) | ||
|
||
heartbeat := App.HeartbeatStore.Get("heartbeat1") | ||
assert.NotNil(t, heartbeat, "Expected heartbeat1 to be present") | ||
assert.Equal(t, "heartbeat1", heartbeat.Name) | ||
} | ||
|
||
func TestProcessNotifications(t *testing.T) { | ||
App.NotificationStore = notify.NewStore() | ||
HistoryStore = history.NewStore() | ||
|
||
var rawConfig map[string]interface{} | ||
err := yaml.Unmarshal([]byte(sampleConfig), &rawConfig) | ||
assert.NoError(t, err) | ||
err = App.processNotifications(rawConfig["notifications"]) | ||
assert.NoError(t, err, "Expected no error when processing notifications") | ||
|
||
notification := App.NotificationStore.Get("slack") | ||
assert.NotNil(t, notification, "Expected slack notification to be present") | ||
assert.Equal(t, "slack", notification.Type) | ||
assert.Equal(t, `{{ if eq .Status "ok" }}good{{ else }}danger{{ end }}`, notification.SlackConfig.ColorTemplate) | ||
} | ||
|
||
func TestProcessHeartbeats(t *testing.T) { | ||
App.HeartbeatStore = heartbeat.NewStore() | ||
HistoryStore = history.NewStore() | ||
|
||
var rawConfig map[string]interface{} | ||
err := yaml.Unmarshal([]byte(sampleConfig), &rawConfig) | ||
assert.NoError(t, err) | ||
|
||
err = App.processHeartbeats(rawConfig["heartbeats"]) | ||
assert.NoError(t, err, "Expected no error when processing heartbeats") | ||
|
||
heartbeat := App.HeartbeatStore.Get("heartbeat1") | ||
assert.NotNil(t, heartbeat, "Expected heartbeat1 to be present") | ||
assert.Equal(t, "heartbeat1", heartbeat.Name) | ||
} | ||
|
||
func TestUpdateSlackNotification(t *testing.T) { | ||
notification := ¬ify.Notification{ | ||
Type: "slack", | ||
SlackConfig: ¬ifier.SlackConfig{ | ||
Channel: "general", | ||
}, | ||
} | ||
|
||
err := App.updateSlackNotification("slack", notification) | ||
assert.NoError(t, err, "Expected no error when updating slack notification") | ||
assert.Equal(t, `{{ if eq .Status "ok" }}good{{ else }}danger{{ end }}`, notification.SlackConfig.ColorTemplate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package flags | ||
|
||
import ( | ||
"heartbeats/pkg/config" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/spf13/pflag" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func resetFlags() { | ||
pflag.CommandLine = pflag.NewFlagSet("heartbeats", pflag.ExitOnError) | ||
} | ||
|
||
func TestParseFlags(t *testing.T) { | ||
resetFlags() | ||
|
||
output := &strings.Builder{} | ||
args := []string{"cmd", "-c", "config.yaml", "-l", "127.0.0.1:9090", "-s", "http://example.com", "-m", "200", "-r", "20", "-v"} | ||
|
||
result := ParseFlags(args, output) | ||
assert.NoError(t, result.Err) | ||
assert.Equal(t, result.ShowVersion, false) | ||
assert.Equal(t, result.ShowHelp, false) | ||
|
||
assert.Equal(t, "config.yaml", config.App.Path) | ||
assert.Equal(t, "127.0.0.1:9090", config.App.Server.ListenAddress) | ||
assert.Equal(t, "http://example.com", config.App.Server.SiteRoot) | ||
assert.Equal(t, 200, config.App.Cache.MaxSize) | ||
assert.Equal(t, 20, config.App.Cache.Reduce) | ||
assert.True(t, config.App.Verbose) | ||
} | ||
|
||
func TestShowVersionFlag(t *testing.T) { | ||
resetFlags() | ||
|
||
output := &strings.Builder{} | ||
args := []string{"cmd", "--version"} | ||
result := ParseFlags(args, output) | ||
assert.NoError(t, result.Err) | ||
assert.Equal(t, result.ShowVersion, true) | ||
assert.Equal(t, result.ShowHelp, false) | ||
} | ||
|
||
func TestShowHelpFlag(t *testing.T) { | ||
resetFlags() | ||
|
||
output := &strings.Builder{} | ||
args := []string{"cmd", "--help"} | ||
|
||
result := ParseFlags(args, output) | ||
assert.NoError(t, result.Err) | ||
assert.Equal(t, result.ShowVersion, false) | ||
assert.Equal(t, result.ShowHelp, true) | ||
} | ||
|
||
func TestProcessEnvVariables(t *testing.T) { | ||
resetFlags() | ||
|
||
os.Setenv("HEARTBEATS_CONFIG", "env_config.yaml") | ||
os.Setenv("HEARTBEATS_LISTEN_ADDRESS", "0.0.0.0:8080") | ||
os.Setenv("HEARTBEATS_SITE_ROOT", "http://env.com") | ||
os.Setenv("HEARTBEATS_MAX_SIZE", "300") | ||
os.Setenv("HEARTBEATS_REDUCE", "30") | ||
os.Setenv("HEARTBEATS_VERBOSE", "true") | ||
|
||
pflag.StringVarP(&config.App.Path, "config", "c", "./deploy/config.yaml", "Path to the configuration file") | ||
pflag.StringVarP(&config.App.Server.ListenAddress, "listen-address", "l", "localhost:8080", "Address to listen on") | ||
pflag.StringVarP(&config.App.Server.SiteRoot, "site-root", "s", "", "Site root for the heartbeat service (default \"http://<listenAddress>\")") | ||
pflag.IntVarP(&config.App.Cache.MaxSize, "max-size", "m", 100, "Maximum size of the cache") | ||
pflag.IntVarP(&config.App.Cache.Reduce, "reduce", "r", 10, "Amount to reduce when max size is exceeded") | ||
pflag.BoolVarP(&config.App.Verbose, "verbose", "v", false, "Enable verbose logging") | ||
|
||
pflag.Parse() | ||
|
||
processEnvVariables() | ||
|
||
assert.Equal(t, "env_config.yaml", config.App.Path) | ||
assert.Equal(t, "0.0.0.0:8080", config.App.Server.ListenAddress) | ||
assert.Equal(t, "http://env.com", config.App.Server.SiteRoot) | ||
assert.Equal(t, 300, config.App.Cache.MaxSize) | ||
assert.Equal(t, 30, config.App.Cache.Reduce) | ||
assert.True(t, config.App.Verbose) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package handlers | ||
|
||
import ( | ||
"heartbeats/pkg/logger" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHealthz(t *testing.T) { | ||
log := logger.NewLogger(true) | ||
handler := Healthz(log) | ||
|
||
req := httptest.NewRequest("GET", "/healthz", nil) | ||
rec := httptest.NewRecorder() | ||
|
||
handler.ServeHTTP(rec, req) | ||
|
||
assert.Equal(t, http.StatusOK, rec.Code, "Expected status code 200") | ||
assert.Equal(t, "ok", rec.Body.String(), "Expected response body 'ok'") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.