2023-01-27 13:37:20 -08:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-11-13 07:32:37 -08:00
|
|
|
|
|
|
|
package health
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2024-06-25 22:02:38 -07:00
|
|
|
"slices"
|
2024-11-20 11:46:14 +01:00
|
|
|
"strconv"
|
2022-11-13 07:32:37 -08:00
|
|
|
"testing"
|
health: begin work to use structured health warnings instead of strings, pipe changes into ipn.Notify (#12406)
Updates tailscale/tailscale#4136
This PR is the first round of work to move from encoding health warnings as strings and use structured data instead. The current health package revolves around the idea of Subsystems. Each subsystem can have (or not have) a Go error associated with it. The overall health of the backend is given by the concatenation of all these errors.
This PR polishes the concept of Warnable introduced by @bradfitz a few weeks ago. Each Warnable is a component of the backend (for instance, things like 'dns' or 'magicsock' are Warnables). Each Warnable has a unique identifying code. A Warnable is an entity we can warn the user about, by setting (or unsetting) a WarningState for it. Warnables have:
- an identifying Code, so that the GUI can track them as their WarningStates come and go
- a Title, which the GUIs can use to tell the user what component of the backend is broken
- a Text, which is a function that is called with a set of Args to generate a more detailed error message to explain the unhappy state
Additionally, this PR also begins to send Warnables and their WarningStates through LocalAPI to the clients, using ipn.Notify messages. An ipn.Notify is only issued when a warning is added or removed from the Tracker.
In a next PR, we'll get rid of subsystems entirely, and we'll start using structured warnings for all errors affecting the backend functionality.
Signed-off-by: Andrea Gottardo <andrea@gottardo.me>
2024-06-14 11:53:56 -07:00
|
|
|
"time"
|
2024-06-27 09:36:29 -07:00
|
|
|
|
|
|
|
"tailscale.com/tailcfg"
|
2025-01-27 14:21:25 +00:00
|
|
|
"tailscale.com/tstest"
|
2024-06-27 09:36:29 -07:00
|
|
|
"tailscale.com/types/opt"
|
2024-11-20 11:46:14 +01:00
|
|
|
"tailscale.com/util/usermetric"
|
2024-12-05 15:45:48 -08:00
|
|
|
"tailscale.com/version"
|
2022-11-13 07:32:37 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAppendWarnableDebugFlags(t *testing.T) {
|
2024-04-25 13:24:49 -07:00
|
|
|
var tr Tracker
|
2022-11-13 07:32:37 -08:00
|
|
|
|
2024-04-16 13:15:13 -07:00
|
|
|
for i := range 10 {
|
health: begin work to use structured health warnings instead of strings, pipe changes into ipn.Notify (#12406)
Updates tailscale/tailscale#4136
This PR is the first round of work to move from encoding health warnings as strings and use structured data instead. The current health package revolves around the idea of Subsystems. Each subsystem can have (or not have) a Go error associated with it. The overall health of the backend is given by the concatenation of all these errors.
This PR polishes the concept of Warnable introduced by @bradfitz a few weeks ago. Each Warnable is a component of the backend (for instance, things like 'dns' or 'magicsock' are Warnables). Each Warnable has a unique identifying code. A Warnable is an entity we can warn the user about, by setting (or unsetting) a WarningState for it. Warnables have:
- an identifying Code, so that the GUI can track them as their WarningStates come and go
- a Title, which the GUIs can use to tell the user what component of the backend is broken
- a Text, which is a function that is called with a set of Args to generate a more detailed error message to explain the unhappy state
Additionally, this PR also begins to send Warnables and their WarningStates through LocalAPI to the clients, using ipn.Notify messages. An ipn.Notify is only issued when a warning is added or removed from the Tracker.
In a next PR, we'll get rid of subsystems entirely, and we'll start using structured warnings for all errors affecting the backend functionality.
Signed-off-by: Andrea Gottardo <andrea@gottardo.me>
2024-06-14 11:53:56 -07:00
|
|
|
w := Register(&Warnable{
|
|
|
|
Code: WarnableCode(fmt.Sprintf("warnable-code-%d", i)),
|
|
|
|
MapDebugFlag: fmt.Sprint(i),
|
|
|
|
})
|
|
|
|
defer unregister(w)
|
2022-11-13 07:32:37 -08:00
|
|
|
if i%2 == 0 {
|
health: begin work to use structured health warnings instead of strings, pipe changes into ipn.Notify (#12406)
Updates tailscale/tailscale#4136
This PR is the first round of work to move from encoding health warnings as strings and use structured data instead. The current health package revolves around the idea of Subsystems. Each subsystem can have (or not have) a Go error associated with it. The overall health of the backend is given by the concatenation of all these errors.
This PR polishes the concept of Warnable introduced by @bradfitz a few weeks ago. Each Warnable is a component of the backend (for instance, things like 'dns' or 'magicsock' are Warnables). Each Warnable has a unique identifying code. A Warnable is an entity we can warn the user about, by setting (or unsetting) a WarningState for it. Warnables have:
- an identifying Code, so that the GUI can track them as their WarningStates come and go
- a Title, which the GUIs can use to tell the user what component of the backend is broken
- a Text, which is a function that is called with a set of Args to generate a more detailed error message to explain the unhappy state
Additionally, this PR also begins to send Warnables and their WarningStates through LocalAPI to the clients, using ipn.Notify messages. An ipn.Notify is only issued when a warning is added or removed from the Tracker.
In a next PR, we'll get rid of subsystems entirely, and we'll start using structured warnings for all errors affecting the backend functionality.
Signed-off-by: Andrea Gottardo <andrea@gottardo.me>
2024-06-14 11:53:56 -07:00
|
|
|
tr.SetUnhealthy(w, Args{"test-arg": fmt.Sprint(i)})
|
2022-11-13 07:32:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
want := []string{"z", "y", "0", "2", "4", "6", "8"}
|
|
|
|
|
|
|
|
var got []string
|
2024-04-16 13:15:13 -07:00
|
|
|
for range 20 {
|
2022-11-13 07:32:37 -08:00
|
|
|
got = append(got[:0], "z", "y")
|
2024-04-25 13:24:49 -07:00
|
|
|
got = tr.AppendWarnableDebugFlags(got)
|
2022-11-13 07:32:37 -08:00
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Fatalf("AppendWarnableDebugFlags = %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-25 20:26:49 -07:00
|
|
|
|
|
|
|
// Test that all exported methods on *Tracker don't panic with a nil receiver.
|
|
|
|
func TestNilMethodsDontCrash(t *testing.T) {
|
|
|
|
var nilt *Tracker
|
|
|
|
rv := reflect.ValueOf(nilt)
|
|
|
|
for i := 0; i < rv.NumMethod(); i++ {
|
|
|
|
mt := rv.Type().Method(i)
|
|
|
|
t.Logf("calling Tracker.%s ...", mt.Name)
|
|
|
|
var args []reflect.Value
|
|
|
|
for j := 0; j < mt.Type.NumIn(); j++ {
|
|
|
|
if j == 0 && mt.Type.In(j) == reflect.TypeFor[*Tracker]() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
args = append(args, reflect.Zero(mt.Type.In(j)))
|
|
|
|
}
|
|
|
|
rv.Method(i).Call(args)
|
|
|
|
}
|
|
|
|
}
|
health: begin work to use structured health warnings instead of strings, pipe changes into ipn.Notify (#12406)
Updates tailscale/tailscale#4136
This PR is the first round of work to move from encoding health warnings as strings and use structured data instead. The current health package revolves around the idea of Subsystems. Each subsystem can have (or not have) a Go error associated with it. The overall health of the backend is given by the concatenation of all these errors.
This PR polishes the concept of Warnable introduced by @bradfitz a few weeks ago. Each Warnable is a component of the backend (for instance, things like 'dns' or 'magicsock' are Warnables). Each Warnable has a unique identifying code. A Warnable is an entity we can warn the user about, by setting (or unsetting) a WarningState for it. Warnables have:
- an identifying Code, so that the GUI can track them as their WarningStates come and go
- a Title, which the GUIs can use to tell the user what component of the backend is broken
- a Text, which is a function that is called with a set of Args to generate a more detailed error message to explain the unhappy state
Additionally, this PR also begins to send Warnables and their WarningStates through LocalAPI to the clients, using ipn.Notify messages. An ipn.Notify is only issued when a warning is added or removed from the Tracker.
In a next PR, we'll get rid of subsystems entirely, and we'll start using structured warnings for all errors affecting the backend functionality.
Signed-off-by: Andrea Gottardo <andrea@gottardo.me>
2024-06-14 11:53:56 -07:00
|
|
|
|
|
|
|
func TestSetUnhealthyWithDuplicateThenHealthyAgain(t *testing.T) {
|
|
|
|
ht := Tracker{}
|
|
|
|
if len(ht.Strings()) != 0 {
|
|
|
|
t.Fatalf("before first insertion, len(newTracker.Strings) = %d; want = 0", len(ht.Strings()))
|
|
|
|
}
|
|
|
|
|
|
|
|
ht.SetUnhealthy(testWarnable, Args{ArgError: "Hello world 1"})
|
|
|
|
want := []string{"Hello world 1"}
|
|
|
|
if !reflect.DeepEqual(ht.Strings(), want) {
|
|
|
|
t.Fatalf("after calling SetUnhealthy, newTracker.Strings() = %v; want = %v", ht.Strings(), want)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding a second warning state with the same WarningCode overwrites the existing warning state,
|
|
|
|
// the count shouldn't have changed.
|
|
|
|
ht.SetUnhealthy(testWarnable, Args{ArgError: "Hello world 2"})
|
|
|
|
want = []string{"Hello world 2"}
|
|
|
|
if !reflect.DeepEqual(ht.Strings(), want) {
|
|
|
|
t.Fatalf("after insertion of same WarningCode, newTracker.Strings() = %v; want = %v", ht.Strings(), want)
|
|
|
|
}
|
|
|
|
|
|
|
|
ht.SetHealthy(testWarnable)
|
|
|
|
want = []string{}
|
|
|
|
if !reflect.DeepEqual(ht.Strings(), want) {
|
|
|
|
t.Fatalf("after setting the healthy, newTracker.Strings() = %v; want = %v", ht.Strings(), want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveAllWarnings(t *testing.T) {
|
|
|
|
ht := Tracker{}
|
|
|
|
if len(ht.Strings()) != 0 {
|
|
|
|
t.Fatalf("before first insertion, len(newTracker.Strings) = %d; want = 0", len(ht.Strings()))
|
|
|
|
}
|
|
|
|
|
|
|
|
ht.SetUnhealthy(testWarnable, Args{"Text": "Hello world 1"})
|
|
|
|
if len(ht.Strings()) != 1 {
|
|
|
|
t.Fatalf("after first insertion, len(newTracker.Strings) = %d; want = %d", len(ht.Strings()), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
ht.SetHealthy(testWarnable)
|
|
|
|
if len(ht.Strings()) != 0 {
|
|
|
|
t.Fatalf("after RemoveAll, len(newTracker.Strings) = %d; want = 0", len(ht.Strings()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestWatcher tests that a registered watcher function gets called with the correct
|
|
|
|
// Warnable and non-nil/nil UnhealthyState upon setting a Warnable to unhealthy/healthy.
|
|
|
|
func TestWatcher(t *testing.T) {
|
|
|
|
ht := Tracker{}
|
|
|
|
wantText := "Hello world"
|
|
|
|
becameUnhealthy := make(chan struct{})
|
|
|
|
becameHealthy := make(chan struct{})
|
|
|
|
|
|
|
|
watcherFunc := func(w *Warnable, us *UnhealthyState) {
|
|
|
|
if w != testWarnable {
|
|
|
|
t.Fatalf("watcherFunc was called, but with an unexpected Warnable: %v, want: %v", w, testWarnable)
|
|
|
|
}
|
|
|
|
|
|
|
|
if us != nil {
|
|
|
|
if us.Text != wantText {
|
|
|
|
t.Fatalf("unexpected us.Text: %s, want: %s", us.Text, wantText)
|
|
|
|
}
|
|
|
|
if us.Args[ArgError] != wantText {
|
|
|
|
t.Fatalf("unexpected us.Args[ArgError]: %s, want: %s", us.Args[ArgError], wantText)
|
|
|
|
}
|
|
|
|
becameUnhealthy <- struct{}{}
|
|
|
|
} else {
|
|
|
|
becameHealthy <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unregisterFunc := ht.RegisterWatcher(watcherFunc)
|
|
|
|
if len(ht.watchers) != 1 {
|
|
|
|
t.Fatalf("after RegisterWatcher, len(newTracker.watchers) = %d; want = 1", len(ht.watchers))
|
|
|
|
}
|
|
|
|
ht.SetUnhealthy(testWarnable, Args{ArgError: wantText})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-becameUnhealthy:
|
|
|
|
// Test passed because the watcher got notified of an unhealthy state
|
|
|
|
case <-becameHealthy:
|
|
|
|
// Test failed because the watcher got of a healthy state instead of an unhealthy one
|
|
|
|
t.Fatalf("watcherFunc was called with a healthy state")
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatalf("watcherFunc didn't get called upon calling SetUnhealthy")
|
|
|
|
}
|
|
|
|
|
|
|
|
ht.SetHealthy(testWarnable)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-becameUnhealthy:
|
|
|
|
// Test failed because the watcher got of an unhealthy state instead of a healthy one
|
|
|
|
t.Fatalf("watcherFunc was called with an unhealthy state")
|
|
|
|
case <-becameHealthy:
|
|
|
|
// Test passed because the watcher got notified of a healthy state
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatalf("watcherFunc didn't get called upon calling SetUnhealthy")
|
|
|
|
}
|
|
|
|
|
|
|
|
unregisterFunc()
|
|
|
|
if len(ht.watchers) != 0 {
|
|
|
|
t.Fatalf("after unregisterFunc, len(newTracker.watchers) = %d; want = 0", len(ht.watchers))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-11 11:51:47 -07:00
|
|
|
// TestWatcherWithTimeToVisible tests that a registered watcher function gets called with the correct
|
|
|
|
// Warnable and non-nil/nil UnhealthyState upon setting a Warnable to unhealthy/healthy, but the Warnable
|
|
|
|
// has a TimeToVisible set, which means that a watcher should only be notified of an unhealthy state after
|
|
|
|
// the TimeToVisible duration has passed.
|
|
|
|
func TestSetUnhealthyWithTimeToVisible(t *testing.T) {
|
|
|
|
ht := Tracker{}
|
|
|
|
mw := Register(&Warnable{
|
|
|
|
Code: "test-warnable-3-secs-to-visible",
|
|
|
|
Title: "Test Warnable with 3 seconds to visible",
|
|
|
|
Text: StaticMessage("Hello world"),
|
|
|
|
TimeToVisible: 2 * time.Second,
|
|
|
|
ImpactsConnectivity: true,
|
|
|
|
})
|
|
|
|
defer unregister(mw)
|
|
|
|
|
|
|
|
becameUnhealthy := make(chan struct{})
|
|
|
|
becameHealthy := make(chan struct{})
|
|
|
|
|
|
|
|
watchFunc := func(w *Warnable, us *UnhealthyState) {
|
|
|
|
if w != mw {
|
|
|
|
t.Fatalf("watcherFunc was called, but with an unexpected Warnable: %v, want: %v", w, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
if us != nil {
|
|
|
|
becameUnhealthy <- struct{}{}
|
|
|
|
} else {
|
|
|
|
becameHealthy <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ht.RegisterWatcher(watchFunc)
|
|
|
|
ht.SetUnhealthy(mw, Args{ArgError: "Hello world"})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-becameUnhealthy:
|
|
|
|
// Test failed because the watcher got notified of an unhealthy state
|
|
|
|
t.Fatalf("watcherFunc was called with an unhealthy state")
|
|
|
|
case <-becameHealthy:
|
|
|
|
// Test failed because the watcher got of a healthy state
|
|
|
|
t.Fatalf("watcherFunc was called with a healthy state")
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
// As expected, watcherFunc still had not been called after 1 second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
health: begin work to use structured health warnings instead of strings, pipe changes into ipn.Notify (#12406)
Updates tailscale/tailscale#4136
This PR is the first round of work to move from encoding health warnings as strings and use structured data instead. The current health package revolves around the idea of Subsystems. Each subsystem can have (or not have) a Go error associated with it. The overall health of the backend is given by the concatenation of all these errors.
This PR polishes the concept of Warnable introduced by @bradfitz a few weeks ago. Each Warnable is a component of the backend (for instance, things like 'dns' or 'magicsock' are Warnables). Each Warnable has a unique identifying code. A Warnable is an entity we can warn the user about, by setting (or unsetting) a WarningState for it. Warnables have:
- an identifying Code, so that the GUI can track them as their WarningStates come and go
- a Title, which the GUIs can use to tell the user what component of the backend is broken
- a Text, which is a function that is called with a set of Args to generate a more detailed error message to explain the unhappy state
Additionally, this PR also begins to send Warnables and their WarningStates through LocalAPI to the clients, using ipn.Notify messages. An ipn.Notify is only issued when a warning is added or removed from the Tracker.
In a next PR, we'll get rid of subsystems entirely, and we'll start using structured warnings for all errors affecting the backend functionality.
Signed-off-by: Andrea Gottardo <andrea@gottardo.me>
2024-06-14 11:53:56 -07:00
|
|
|
func TestRegisterWarnablePanicsWithDuplicate(t *testing.T) {
|
|
|
|
w := &Warnable{
|
|
|
|
Code: "test-warnable-1",
|
|
|
|
}
|
|
|
|
|
|
|
|
Register(w)
|
|
|
|
defer unregister(w)
|
|
|
|
if registeredWarnables[w.Code] != w {
|
|
|
|
t.Fatalf("after Register, registeredWarnables[%s] = %v; want = %v", w.Code, registeredWarnables[w.Code], w)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Fatalf("Registering the same Warnable twice didn't panic")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
Register(w)
|
|
|
|
}
|
2024-06-18 13:34:55 -07:00
|
|
|
|
|
|
|
// TestCheckDependsOnAppearsInUnhealthyState asserts that the DependsOn field in the UnhealthyState
|
|
|
|
// is populated with the WarnableCode(s) of the Warnable(s) that a warning depends on.
|
|
|
|
func TestCheckDependsOnAppearsInUnhealthyState(t *testing.T) {
|
|
|
|
ht := Tracker{}
|
|
|
|
w1 := Register(&Warnable{
|
|
|
|
Code: "w1",
|
|
|
|
Text: StaticMessage("W1 Text"),
|
|
|
|
DependsOn: []*Warnable{},
|
|
|
|
})
|
|
|
|
defer unregister(w1)
|
|
|
|
w2 := Register(&Warnable{
|
|
|
|
Code: "w2",
|
|
|
|
Text: StaticMessage("W2 Text"),
|
|
|
|
DependsOn: []*Warnable{w1},
|
|
|
|
})
|
|
|
|
defer unregister(w2)
|
|
|
|
|
|
|
|
ht.SetUnhealthy(w1, Args{ArgError: "w1 is unhealthy"})
|
|
|
|
us1, ok := ht.CurrentState().Warnings[w1.Code]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Expected an UnhealthyState for w1, got nothing")
|
|
|
|
}
|
2024-06-25 22:02:38 -07:00
|
|
|
wantDependsOn := []WarnableCode{warmingUpWarnable.Code}
|
|
|
|
if !reflect.DeepEqual(us1.DependsOn, wantDependsOn) {
|
|
|
|
t.Fatalf("Expected DependsOn = %v in the unhealthy state, got: %v", wantDependsOn, us1.DependsOn)
|
2024-06-18 13:34:55 -07:00
|
|
|
}
|
|
|
|
ht.SetUnhealthy(w2, Args{ArgError: "w2 is also unhealthy now"})
|
|
|
|
us2, ok := ht.CurrentState().Warnings[w2.Code]
|
2025-01-27 13:37:49 +00:00
|
|
|
if ok {
|
|
|
|
t.Fatalf("Saw w2 being unhealthy but it shouldn't be, as it depends on unhealthy w1")
|
|
|
|
}
|
|
|
|
ht.SetHealthy(w1)
|
|
|
|
us2, ok = ht.CurrentState().Warnings[w2.Code]
|
2024-06-18 13:34:55 -07:00
|
|
|
if !ok {
|
2025-01-27 13:37:49 +00:00
|
|
|
t.Fatalf("w2 wasn't unhealthy; want it to be unhealthy now that w1 is back healthy")
|
2024-06-18 13:34:55 -07:00
|
|
|
}
|
2025-01-27 13:37:49 +00:00
|
|
|
|
2024-06-25 22:02:38 -07:00
|
|
|
wantDependsOn = slices.Concat([]WarnableCode{w1.Code}, wantDependsOn)
|
|
|
|
if !reflect.DeepEqual(us2.DependsOn, wantDependsOn) {
|
|
|
|
t.Fatalf("Expected DependsOn = %v in the unhealthy state, got: %v", wantDependsOn, us2.DependsOn)
|
2024-06-18 13:34:55 -07:00
|
|
|
}
|
|
|
|
}
|
2024-06-27 09:36:29 -07:00
|
|
|
|
|
|
|
func TestShowUpdateWarnable(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
check bool
|
|
|
|
apply opt.Bool
|
|
|
|
cv *tailcfg.ClientVersion
|
|
|
|
wantWarnable *Warnable
|
|
|
|
wantShow bool
|
|
|
|
}{
|
|
|
|
{
|
2024-11-20 11:46:14 +01:00
|
|
|
desc: "nil ClientVersion",
|
2024-06-27 09:36:29 -07:00
|
|
|
check: true,
|
|
|
|
cv: nil,
|
|
|
|
wantWarnable: nil,
|
|
|
|
wantShow: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "RunningLatest",
|
|
|
|
check: true,
|
|
|
|
cv: &tailcfg.ClientVersion{RunningLatest: true},
|
|
|
|
wantWarnable: nil,
|
|
|
|
wantShow: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no LatestVersion",
|
|
|
|
check: true,
|
|
|
|
cv: &tailcfg.ClientVersion{RunningLatest: false, LatestVersion: ""},
|
|
|
|
wantWarnable: nil,
|
|
|
|
wantShow: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "show regular update",
|
|
|
|
check: true,
|
|
|
|
cv: &tailcfg.ClientVersion{RunningLatest: false, LatestVersion: "1.2.3"},
|
|
|
|
wantWarnable: updateAvailableWarnable,
|
|
|
|
wantShow: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "show security update",
|
|
|
|
check: true,
|
|
|
|
cv: &tailcfg.ClientVersion{RunningLatest: false, LatestVersion: "1.2.3", UrgentSecurityUpdate: true},
|
|
|
|
wantWarnable: securityUpdateAvailableWarnable,
|
|
|
|
wantShow: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "update check disabled",
|
|
|
|
check: false,
|
|
|
|
cv: &tailcfg.ClientVersion{RunningLatest: false, LatestVersion: "1.2.3"},
|
|
|
|
wantWarnable: nil,
|
|
|
|
wantShow: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "hide update with auto-updates",
|
|
|
|
check: true,
|
|
|
|
apply: opt.NewBool(true),
|
|
|
|
cv: &tailcfg.ClientVersion{RunningLatest: false, LatestVersion: "1.2.3"},
|
|
|
|
wantWarnable: nil,
|
|
|
|
wantShow: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "show security update with auto-updates",
|
|
|
|
check: true,
|
|
|
|
apply: opt.NewBool(true),
|
|
|
|
cv: &tailcfg.ClientVersion{RunningLatest: false, LatestVersion: "1.2.3", UrgentSecurityUpdate: true},
|
|
|
|
wantWarnable: securityUpdateAvailableWarnable,
|
|
|
|
wantShow: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
tr := &Tracker{
|
|
|
|
checkForUpdates: tt.check,
|
|
|
|
applyUpdates: tt.apply,
|
|
|
|
latestVersion: tt.cv,
|
|
|
|
}
|
|
|
|
gotWarnable, gotShow := tr.showUpdateWarnable()
|
|
|
|
if gotWarnable != tt.wantWarnable {
|
|
|
|
t.Errorf("got warnable: %v, want: %v", gotWarnable, tt.wantWarnable)
|
|
|
|
}
|
|
|
|
if gotShow != tt.wantShow {
|
|
|
|
t.Errorf("got show: %v, want: %v", gotShow, tt.wantShow)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-11-20 11:46:14 +01:00
|
|
|
|
|
|
|
func TestHealthMetric(t *testing.T) {
|
2024-12-05 15:45:48 -08:00
|
|
|
unstableBuildWarning := 0
|
|
|
|
if version.IsUnstableBuild() {
|
|
|
|
unstableBuildWarning = 1
|
|
|
|
}
|
|
|
|
|
2024-11-20 11:46:14 +01:00
|
|
|
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:
|
2024-12-05 15:45:48 -08:00
|
|
|
// - is-using-unstable-version (except on the release branch)
|
2024-11-20 11:46:14 +01:00
|
|
|
// - wantrunning-false
|
|
|
|
{
|
|
|
|
desc: "base-warnings",
|
|
|
|
check: true,
|
|
|
|
cv: nil,
|
2024-12-05 15:45:48 -08:00
|
|
|
wantMetricCount: unstableBuildWarning + 1,
|
2024-11-20 11:46:14 +01:00
|
|
|
},
|
|
|
|
// with: update-available
|
|
|
|
{
|
|
|
|
desc: "update-warning",
|
|
|
|
check: true,
|
|
|
|
cv: &tailcfg.ClientVersion{RunningLatest: false, LatestVersion: "1.2.3"},
|
2024-12-05 15:45:48 -08:00
|
|
|
wantMetricCount: unstableBuildWarning + 2,
|
2024-11-20 11:46:14 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2025-01-27 14:21:25 +00:00
|
|
|
|
|
|
|
// TestNoDERPHomeWarnable checks that we don't
|
|
|
|
// complain about no DERP home if we're not in a
|
|
|
|
// map poll.
|
|
|
|
func TestNoDERPHomeWarnable(t *testing.T) {
|
|
|
|
t.Skip("TODO: fix https://github.com/tailscale/tailscale/issues/14798 to make this test not deadlock")
|
|
|
|
clock := tstest.NewClock(tstest.ClockOpts{
|
|
|
|
Start: time.Unix(123, 0),
|
|
|
|
FollowRealTime: false,
|
|
|
|
})
|
|
|
|
ht := &Tracker{
|
|
|
|
testClock: clock,
|
|
|
|
}
|
|
|
|
ht.SetIPNState("NeedsLogin", true)
|
|
|
|
|
|
|
|
// Advance 30 seconds to get past the "recentlyLoggedIn" check.
|
|
|
|
clock.Advance(30 * time.Second)
|
|
|
|
ht.updateBuiltinWarnablesLocked()
|
|
|
|
|
|
|
|
// Advance to get past the the TimeToVisible delay.
|
|
|
|
clock.Advance(noDERPHomeWarnable.TimeToVisible * 2)
|
|
|
|
|
|
|
|
ht.updateBuiltinWarnablesLocked()
|
|
|
|
if ws, ok := ht.CurrentState().Warnings[noDERPHomeWarnable.Code]; ok {
|
|
|
|
t.Fatalf("got unexpected noDERPHomeWarnable warnable: %v", ws)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestNoDERPHomeWarnableManual is like TestNoDERPHomeWarnable
|
|
|
|
// but doesn't use tstest.Clock so avoids the deadlock
|
|
|
|
// I hit: https://github.com/tailscale/tailscale/issues/14798
|
|
|
|
func TestNoDERPHomeWarnableManual(t *testing.T) {
|
|
|
|
ht := &Tracker{}
|
|
|
|
ht.SetIPNState("NeedsLogin", true)
|
|
|
|
|
|
|
|
// Avoid wantRunning:
|
|
|
|
ht.ipnWantRunningLastTrue = ht.ipnWantRunningLastTrue.Add(-10 * time.Second)
|
|
|
|
ht.updateBuiltinWarnablesLocked()
|
|
|
|
|
|
|
|
ws, ok := ht.warnableVal[noDERPHomeWarnable]
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("got unexpected noDERPHomeWarnable warnable: %v", ws)
|
|
|
|
}
|
|
|
|
}
|