mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-07 16:54:16 +01:00
6d4240a5ae
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.
102 lines
1.7 KiB
Go
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 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
|
|
}
|
|
}
|