mirror of
https://github.com/netbirdio/netbird.git
synced 2025-08-16 10:08:12 +02:00
Feature/update check (#1232)
Periodically fetch the latest available version, and the UI will shows a new menu for the download link. It checks both the daemon version and the UI version.
This commit is contained in:
101
version/update_test.go
Normal file
101
version/update_test.go
Normal file
@ -0,0 +1,101 @@
|
||||
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 dameon 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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user