health: move health metrics test to health_test

Updates #13420

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby
2024-11-20 11:46:14 +01:00
committed by Kristoffer Dalby
parent bac3af06f5
commit 41e56cedf8
3 changed files with 50 additions and 33 deletions

View File

@@ -7,11 +7,13 @@ import (
"fmt"
"reflect"
"slices"
"strconv"
"testing"
"time"
"tailscale.com/tailcfg"
"tailscale.com/types/opt"
"tailscale.com/util/usermetric"
)
func TestAppendWarnableDebugFlags(t *testing.T) {
@@ -273,7 +275,7 @@ func TestShowUpdateWarnable(t *testing.T) {
wantShow bool
}{
{
desc: "nil CientVersion",
desc: "nil ClientVersion",
check: true,
cv: nil,
wantWarnable: nil,
@@ -348,3 +350,47 @@ func TestShowUpdateWarnable(t *testing.T) {
})
}
}
func TestHealthMetric(t *testing.T) {
tests := []struct {
desc string
check bool
apply opt.Bool
cv *tailcfg.ClientVersion
wantMetricCount int
}{
// When running in dev, and not initialising the client, there will be two warnings
// by default:
// - is-using-unstable-version
// - wantrunning-false
{
desc: "base-warnings",
check: true,
cv: nil,
wantMetricCount: 2,
},
// with: update-available
{
desc: "update-warning",
check: true,
cv: &tailcfg.ClientVersion{RunningLatest: false, LatestVersion: "1.2.3"},
wantMetricCount: 3,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
tr := &Tracker{
checkForUpdates: tt.check,
applyUpdates: tt.apply,
latestVersion: tt.cv,
}
tr.SetMetricsRegistry(&usermetric.Registry{})
if val := tr.metricHealthMessage.Get(metricHealthMessageLabel{Type: MetricLabelWarning}).String(); val != strconv.Itoa(tt.wantMetricCount) {
t.Fatalf("metric value: %q, want: %q", val, strconv.Itoa(tt.wantMetricCount))
}
for _, w := range tr.CurrentState().Warnings {
t.Logf("warning: %v", w)
}
})
}
}