Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: more tests for prometheus.go (part 2) #95

Merged
merged 1 commit into from
Dec 14, 2024
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ test-count:
cd src && go test ./... -v | grep -c RUN

test-coverage:
cd src && go test ./... -cover

test-coverage-web:
cd src && go test ./... -coverprofile=cover.out && go tool cover -html=cover.out && rm cover.out

update:
Expand Down
4 changes: 2 additions & 2 deletions src/api/qbittorrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type Preferences struct {

type MainData struct {
CategoryMap map[string]Category `json:"categories"`
ServerState ServerState
Tags []string `json:"tags"`
ServerState ServerState `json:"server_state"`
Tags []string `json:"tags"`
}

type ServerState struct {
Expand Down
34 changes: 34 additions & 0 deletions src/app/app_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -96,3 +97,36 @@ func TestGetFeaturesEnabled(t *testing.T) {
})
}
}

func TestEnvSetToTrue(t *testing.T) {
tests := []struct {
input string
output bool
}{
{"true", true},
{"TRUE", true},
{"False", false},
{"false", false},
{"1", false},
{"0", false},
{"", false},
{"randomstring", false},
}

for _, test := range tests {
got := envSetToTrue(test.input)
if got != test.output {
t.Errorf("envSetToTrue(%q) = %v; want %v", test.input, got, test.output)
}
}
}

func TestGetPasswordMasked(t *testing.T) {
QBittorrent.Password = "mysecretpassword"
expected := strings.Repeat("*", len(QBittorrent.Password))
got := GetPasswordMasked()

if got != expected {
t.Errorf("GetPasswordMasked() = %q; want %q", got, expected)
}
}
3 changes: 2 additions & 1 deletion src/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ func Trackers(result []*API.Trackers, r *prometheus.Registry) {
metrics[QbittorrentTrackerSeeders].With(labels).Set((float64(tracker.NumSeeds)))
metrics[QbittorrentTrackerPeers].With(labels).Set((float64(tracker.NumPeers)))
metrics[QbittorrentTrackerStatus].With(labels).Set((float64(tracker.Status)))
metrics[QbittorrentTrackerTier].With(labels).Set((float64(tier)))

if app.Exporter.Feature.EnableHighCardinality {
qbittorrentTrackerInfoLabels := prometheus.Labels{
Expand All @@ -342,7 +343,7 @@ func MainData(result *API.MainData, r *prometheus.Registry) {
globalratio, err := strconv.ParseFloat((*result).ServerState.GlobalRatio, 64)

if err != nil {
logger.Log.Warn("error to convert ratio")
logger.Log.Warn(fmt.Sprintf("error to convert ratio \"%s\"", (*result).ServerState.GlobalRatio))
} else {
qbittorrentGlobalRatio := prometheus.NewGauge(prometheus.GaugeOpts{
Name: createMetricName(metricNameGlobal, "ratio"),
Expand Down
125 changes: 125 additions & 0 deletions src/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,131 @@ func TestTransfer(t *testing.T) {
testMetrics(expectedMetrics, registry, t)
}

func TestVersion(t *testing.T) {

expectedVersion := "v5.0.2"
version := []byte(expectedVersion)

registry := prometheus.NewRegistry()
Version(&version, registry)

metrics, err := registry.Gather()
if err != nil {
t.Fatalf("Failed to gather metrics: %v", err)
}

var found bool

expectedMetricName := "qbittorrent_app_version"
metricName := createMetricName(metricNameApp, "version")
for _, m := range metrics {
if m.GetName() == metricName {
found = true
if len(m.GetMetric()) == 0 {
t.Fatal("Expected metrics to have at least one entry")
}
if m.GetMetric()[0].GetGauge().GetValue() != 1.0 {
t.Errorf("Expected gauge value to be 1.0, got %v", m.GetMetric()[0].GetGauge().GetValue())
}
if len(m.GetMetric()[0].GetLabel()) == 0 || m.GetMetric()[0].GetLabel()[0].GetValue() != expectedVersion {
t.Errorf("Expected label value to be '%s', got %v", expectedVersion, m.GetMetric()[0].GetLabel())
}
break
}
}

if expectedMetricName != metricName {
t.Errorf("Error with metric name, expected %s got %s", expectedMetricName, metricName)
}

if !found {
t.Fatal("Expected qBittorrent version metric to be registered")
}
}

func TestTorrent(t *testing.T) {

mockInfo := &API.Info{
{
Name: "Torrent",
Hash: "hash",
Eta: 120,
Dlspeed: 500000,
Upspeed: 250000,
Progress: 50,
TimeActive: 3600,
NumSeeds: 10,
NumLeechs: 5,
Ratio: 1.5,
AmountLeft: 1000000000,
Size: 5000000000,
DownloadedSession: 250000000,
UploadedSession: 100000000,
Downloaded: 1000000000,
Uploaded: 500000000,
State: "stalledUP",
Tags: "tag1, tag2",
},
}

registry := prometheus.NewRegistry()

Torrent(mockInfo, registry)

expectedMetrics := map[string]float64{
"qbittorrent_torrent_eta": 120,
"qbittorrent_torrent_download_speed_bytes": 500000,
"qbittorrent_torrent_upload_speed_bytes": 250000,
"qbittorrent_torrent_progress": 50,
"qbittorrent_torrent_time_active": 3600,
"qbittorrent_torrent_seeders": 10,
"qbittorrent_torrent_leechers": 5,
"qbittorrent_torrent_ratio": 1.5,
"qbittorrent_torrent_amount_left_bytes": 1000000000,
"qbittorrent_torrent_size_bytes": 5000000000,
"qbittorrent_torrent_session_downloaded_bytes": 250000000,
"qbittorrent_torrent_session_uploaded_bytes": 100000000,
"qbittorrent_torrent_total_downloaded_bytes": 1000000000,
"qbittorrent_torrent_total_uploaded_bytes": 500000000,
"qbittorrent_torrent_states": 1,
"qbittorrent_global_torrents": 1,
}

testMetrics(expectedMetrics, registry, t)
}

func TestTrackers(t *testing.T) {

mockTrackers := []*API.Trackers{
{
{
URL: "http://tracker",
NumDownloaded: 100,
NumLeeches: 50,
NumSeeds: 10,
NumPeers: 60,
Status: 1,
Tier: []byte("1"),
Message: "Active",
},
},
}

registry := prometheus.NewRegistry()
Trackers(mockTrackers, registry)

expectedMetrics := map[string]float64{
"qbittorrent_tracker_downloaded": 100,
"qbittorrent_tracker_leeches": 50,
"qbittorrent_tracker_peers": 60,
"qbittorrent_tracker_seeders": 10,
"qbittorrent_tracker_status": 1,
"qbittorrent_tracker_tier": 1,
}

testMetrics(expectedMetrics, registry, t)
}

func testMetrics(expectedMetrics map[string]float64, registry *prometheus.Registry, t *testing.T) {

for name, expectedValue := range expectedMetrics {
Expand Down
Loading