-
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.
- Loading branch information
Showing
8 changed files
with
400 additions
and
15 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,152 @@ | ||
package app | ||
|
||
import ( | ||
"bytes" | ||
"log/slog" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"qbit-exp/logger" | ||
) | ||
|
||
var buff = &bytes.Buffer{} | ||
|
||
func init() { | ||
logger.Log = &logger.Logger{Logger: slog.New(slog.NewTextHandler(buff, &slog.HandlerOptions{}))} | ||
} | ||
|
||
func TestGetEnvReturnsEnvValue(t *testing.T) { | ||
envVar := "EXPORTER_PORT" | ||
expectedValue := "9090" | ||
os.Setenv(envVar, expectedValue) | ||
defer os.Unsetenv(envVar) | ||
value := getEnv(defaultPort) | ||
|
||
if value != expectedValue { | ||
t.Errorf("Expected %s, got %s", expectedValue, value) | ||
} | ||
} | ||
|
||
func TestGetEnvReturnsDefaultWhenEnvNotSet(t *testing.T) { | ||
envVar := "EXPORTER_PORT" | ||
expectedValue := strconv.Itoa(DEFAULT_PORT) | ||
os.Unsetenv(envVar) | ||
value := getEnv(defaultPort) | ||
|
||
if value != expectedValue { | ||
t.Errorf("Expected default %s, got %s", expectedValue, value) | ||
} | ||
} | ||
|
||
func TestGetEnvLogsWarningIfHelpMessagePresent(t *testing.T) { | ||
envVar := "QBITTORRENT_USERNAME" | ||
os.Unsetenv(envVar) | ||
expectedLogMessage := defaultUsername.Help | ||
getEnv(defaultUsername) | ||
|
||
if !strings.Contains(buff.String(), expectedLogMessage) { | ||
t.Errorf("Expected log message to contain '%s', got '%s'", expectedLogMessage, buff.String()) | ||
} | ||
} | ||
|
||
func TestGetEnvWithDifferentDefaults(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
env Env | ||
expectedValue string | ||
}{ | ||
{"DefaultLogLevel", defaultLogLevel, "INFO"}, | ||
{"DefaultPort", defaultPort, strconv.Itoa(DEFAULT_PORT)}, | ||
{"DefaultTimeout", defaultTimeout, strconv.Itoa(DEFAULT_TIMEOUT)}, | ||
{"DefaultUsername", defaultUsername, "admin"}, | ||
{"DefaultPassword", defaultPassword, "adminadmin"}, | ||
{"DefaultBaseUrl", defaultBaseUrl, "http://localhost:8080"}, | ||
{"DefaultDisableTracker", defaultDisableTracker, "true"}, | ||
{"DefaultHighCardinality", defaultHighCardinality, "false"}, | ||
{"DefaultLabelWithHash", defaultLabelWithHash, "false"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
os.Unsetenv(tt.env.Key) | ||
value := getEnv(tt.env) | ||
if value != tt.expectedValue { | ||
t.Errorf("Expected %s, got %s", tt.expectedValue, value) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetEnvReturnsBooleanValues(t *testing.T) { | ||
tests := []struct { | ||
envVar Env | ||
setValue string | ||
expectVal string | ||
}{ | ||
{defaultDisableTracker, "false", "false"}, | ||
{defaultHighCardinality, "true", "true"}, | ||
{defaultLabelWithHash, "true", "true"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.envVar.Key, func(t *testing.T) { | ||
cleanup := setAndClearEnv(tt.envVar.Key, tt.setValue) | ||
defer cleanup() | ||
|
||
value := getEnv(tt.envVar) | ||
if value != tt.expectVal { | ||
t.Errorf("Expected %s, got %s", tt.expectVal, value) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetEnvHandlesEmptyEnvVarGracefully(t *testing.T) { | ||
// Arrange | ||
envVar := "QBITTORRENT_USERNAME" | ||
os.Setenv(envVar, "") | ||
defer os.Unsetenv(envVar) | ||
expectedValue := defaultUsername.DefaultValue | ||
|
||
// Act | ||
value := getEnv(defaultUsername) | ||
|
||
// Assert | ||
if value != expectedValue { | ||
t.Errorf("Expected %s, got %s", expectedValue, value) | ||
} | ||
} | ||
|
||
func TestGetEnvLogsWarningsCorrectly(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
env Env | ||
expectedLog string | ||
}{ | ||
{"UsernameWarning", defaultUsername, "Qbittorrent username is not set. Using default username"}, | ||
{"PasswordWarning", defaultPassword, "Qbittorrent password is not set. Using default password"}, | ||
{"BaseUrlWarning", defaultBaseUrl, "Qbittorrent base_url is not set. Using default base_url"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
os.Unsetenv(tt.env.Key) | ||
|
||
getEnv(tt.env) | ||
|
||
if !strings.Contains(buff.String(), tt.expectedLog) { | ||
t.Errorf("Expected log message to contain '%s', got '%s'", tt.expectedLog, buff.String()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func setAndClearEnv(key, value string) func() { | ||
os.Setenv(key, value) | ||
return func() { | ||
os.Unsetenv(key) | ||
} | ||
} |
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,84 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"qbit-exp/logger" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var buff = &bytes.Buffer{} | ||
|
||
func init() { | ||
logger.Log = &logger.Logger{Logger: slog.New(slog.NewTextHandler(buff, &slog.HandlerOptions{}))} | ||
} | ||
|
||
func TestMetricsFailureResponse(t *testing.T) { | ||
|
||
req, err := http.NewRequest("GET", "/metrics", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
rec := httptest.NewRecorder() | ||
|
||
metrics(rec, req, func(registry *prometheus.Registry) error { | ||
return fmt.Errorf("mock error") | ||
}) | ||
|
||
if status := rec.Code; status != http.StatusServiceUnavailable { | ||
t.Errorf("expected status code 503, got %d", status) | ||
} | ||
} | ||
|
||
func TestMetricsReturnMetric(t *testing.T) { | ||
|
||
buff.Reset() | ||
opts := &slog.HandlerOptions{ | ||
Level: slog.Level(logger.Trace), | ||
} | ||
|
||
logger.Log = &logger.Logger{Logger: slog.New(slog.NewTextHandler(buff, opts))} | ||
|
||
req, err := http.NewRequest("GET", "/metrics", nil) | ||
req.RemoteAddr = "127.0.0.1:80" | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
rec := httptest.NewRecorder() | ||
|
||
metrics(rec, req, func(registry *prometheus.Registry) error { | ||
|
||
qbittorrent_app_version := prometheus.NewGauge(prometheus.GaugeOpts{ | ||
Name: "qbittorrent_app_version", | ||
Help: "The current qBittorrent version", | ||
ConstLabels: map[string]string{ | ||
"version": string("1.0"), | ||
}, | ||
}) | ||
registry.MustRegister(qbittorrent_app_version) | ||
qbittorrent_app_version.Set(1) | ||
return nil | ||
}) | ||
|
||
if status := rec.Code; status != http.StatusOK { | ||
t.Errorf("expected status code 200, got %d", status) | ||
} | ||
|
||
expectedBody := "# HELP qbittorrent_app_version The current qBittorrent version\n# TYPE qbittorrent_app_version gauge\nqbittorrent_app_version{version=\"1.0\"} 1\n" | ||
|
||
if rec.Body.String() != expectedBody { | ||
t.Errorf("expected \n%s, got \n%s", expectedBody, rec.Body.String()) | ||
} | ||
|
||
traceMessage := "New request from" | ||
if !strings.Contains(buff.String(), traceMessage) { | ||
t.Errorf("expected %s, got %s", traceMessage, buff.String()) | ||
} | ||
} |
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.