netbird/version/update_test.go
Fabio Fantoni c99ae6f009
fix some typo spotted with codespell (#1278)
Fixed spelling typos on logs, comments and command help text
2023-11-01 17:11:16 +01:00

102 lines
1.7 KiB
Go

package version
import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
)
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()
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()
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()
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
}
}