Files
netbird/version/update_test.go
Maycon Santos 5343bee7b2 [management] check and log on new management version (#4029)
This PR enhances the version checker to send a custom User-Agent header when polling for updates, and configures both the management CLI and client UI to use distinct agents. 

- NewUpdate now takes an `httpAgent` string to set the User-Agent header.
- `fetchVersion` builds a custom HTTP request (instead of `http.Get`) and sets the User-Agent.
- Management CLI and client UI now pass `"nb/management"` and `"nb/client-ui"` respectively to NewUpdate.
- Tests updated to supply an `httpAgent` constant.
- Logs if there is a new version available for management
2025-06-22 16:44:33 +02:00

104 lines
1.7 KiB
Go

package version
import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
)
const httpAgent = "pkg/test"
func TestNewUpdate(t *testing.T) {
version = "1.0.0"
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "10.0.0")
}))
defer svr.Close()
versionURL = svr.URL
wg := &sync.WaitGroup{}
wg.Add(1)
onUpdate := false
u := NewUpdate(httpAgent)
defer u.StopWatch()
u.SetOnUpdateListener(func() {
onUpdate = true
wg.Done()
})
waitTimeout(wg)
if onUpdate != true {
t.Errorf("update not found")
}
}
func TestDoNotUpdate(t *testing.T) {
version = "11.0.0"
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "10.0.0")
}))
defer svr.Close()
versionURL = svr.URL
wg := &sync.WaitGroup{}
wg.Add(1)
onUpdate := false
u := NewUpdate(httpAgent)
defer u.StopWatch()
u.SetOnUpdateListener(func() {
onUpdate = true
wg.Done()
})
waitTimeout(wg)
if onUpdate == true {
t.Errorf("invalid update")
}
}
func TestDaemonUpdate(t *testing.T) {
version = "11.0.0"
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "11.0.0")
}))
defer svr.Close()
versionURL = svr.URL
wg := &sync.WaitGroup{}
wg.Add(1)
onUpdate := false
u := NewUpdate(httpAgent)
defer u.StopWatch()
u.SetOnUpdateListener(func() {
onUpdate = true
wg.Done()
})
u.SetDaemonVersion("10.0.0")
waitTimeout(wg)
if onUpdate != true {
t.Errorf("invalid daemon version check")
}
}
func waitTimeout(wg *sync.WaitGroup) {
c := make(chan struct{})
go func() {
wg.Wait()
close(c)
}()
select {
case <-c:
return
case <-time.After(time.Second):
return
}
}