all: add test
This commit is contained in:
parent
c1cb8df2b0
commit
8915818e81
|
@ -9,8 +9,10 @@ import (
|
|||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
@ -559,16 +561,28 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
|
|||
err = setupOpts(opts)
|
||||
fatalOnError(err)
|
||||
|
||||
execPath, err := os.Executable()
|
||||
fatalOnError(errors.Annotate(err, "getting executable path: %w"))
|
||||
|
||||
u := &url.URL{
|
||||
Scheme: "https",
|
||||
// TODO(a.garipov): Make configurable.
|
||||
Host: "static.adtidy.org",
|
||||
Path: path.Join("adguardhome", version.Channel(), "version.json"),
|
||||
}
|
||||
|
||||
upd := updater.NewUpdater(&updater.Config{
|
||||
Client: config.Filtering.HTTPClient,
|
||||
Version: version.Version(),
|
||||
Channel: version.Channel(),
|
||||
GOARCH: runtime.GOARCH,
|
||||
GOOS: runtime.GOOS,
|
||||
GOARM: version.GOARM(),
|
||||
GOMIPS: version.GOMIPS(),
|
||||
WorkDir: Context.workDir,
|
||||
ConfName: config.getConfigFilename(),
|
||||
Client: config.Filtering.HTTPClient,
|
||||
Version: version.Version(),
|
||||
Channel: version.Channel(),
|
||||
GOARCH: runtime.GOARCH,
|
||||
GOOS: runtime.GOOS,
|
||||
GOARM: version.GOARM(),
|
||||
GOMIPS: version.GOMIPS(),
|
||||
WorkDir: Context.workDir,
|
||||
ConfName: config.getConfigFilename(),
|
||||
ExecPath: execPath,
|
||||
VersionCheckURL: u.String(),
|
||||
})
|
||||
|
||||
// TODO(e.burkov): This could be made earlier, probably as the option's
|
||||
|
|
|
@ -8,10 +8,8 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
@ -36,6 +34,7 @@ type Updater struct {
|
|||
|
||||
workDir string
|
||||
confName string
|
||||
execPath string
|
||||
versionCheckURL string
|
||||
|
||||
// mu protects all fields below.
|
||||
|
@ -74,18 +73,19 @@ type Config struct {
|
|||
// ConfName is the name of the current configuration file. Typically,
|
||||
// "AdGuardHome.yaml".
|
||||
ConfName string
|
||||
|
||||
// WorkDir is the working directory that is used for temporary files.
|
||||
WorkDir string
|
||||
|
||||
// ExecPath is path to the executable file.
|
||||
ExecPath string
|
||||
|
||||
// VersionCheckURL is url to the latest version announcement.
|
||||
VersionCheckURL string
|
||||
}
|
||||
|
||||
// NewUpdater creates a new Updater.
|
||||
func NewUpdater(conf *Config) *Updater {
|
||||
u := &url.URL{
|
||||
Scheme: "https",
|
||||
// TODO(a.garipov): Make configurable.
|
||||
Host: "static.adtidy.org",
|
||||
Path: path.Join("adguardhome", conf.Channel, "version.json"),
|
||||
}
|
||||
return &Updater{
|
||||
client: conf.Client,
|
||||
|
||||
|
@ -98,7 +98,8 @@ func NewUpdater(conf *Config) *Updater {
|
|||
|
||||
confName: conf.ConfName,
|
||||
workDir: conf.WorkDir,
|
||||
versionCheckURL: u.String(),
|
||||
execPath: conf.ExecPath,
|
||||
versionCheckURL: conf.VersionCheckURL,
|
||||
|
||||
mu: &sync.RWMutex{},
|
||||
}
|
||||
|
@ -119,12 +120,7 @@ func (u *Updater) Update(firstRun bool) (err error) {
|
|||
}
|
||||
}()
|
||||
|
||||
execPath, err := os.Executable()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting executable path: %w", err)
|
||||
}
|
||||
|
||||
err = u.prepare(execPath)
|
||||
err = u.prepare()
|
||||
if err != nil {
|
||||
return fmt.Errorf("preparing: %w", err)
|
||||
}
|
||||
|
@ -178,7 +174,7 @@ func (u *Updater) VersionCheckURL() (vcu string) {
|
|||
}
|
||||
|
||||
// prepare fills all necessary fields in Updater object.
|
||||
func (u *Updater) prepare(exePath string) (err error) {
|
||||
func (u *Updater) prepare() (err error) {
|
||||
u.updateDir = filepath.Join(u.workDir, fmt.Sprintf("agh-update-%s", u.newVersion))
|
||||
|
||||
_, pkgNameOnly := filepath.Split(u.packageURL)
|
||||
|
@ -194,7 +190,7 @@ func (u *Updater) prepare(exePath string) (err error) {
|
|||
updateExeName = "AdGuardHome.exe"
|
||||
}
|
||||
|
||||
u.backupExeName = filepath.Join(u.backupDir, filepath.Base(exePath))
|
||||
u.backupExeName = filepath.Join(u.backupDir, filepath.Base(u.execPath))
|
||||
u.updateExeName = filepath.Join(u.updateDir, updateExeName)
|
||||
|
||||
log.Debug(
|
||||
|
@ -204,7 +200,7 @@ func (u *Updater) prepare(exePath string) (err error) {
|
|||
u.packageURL,
|
||||
)
|
||||
|
||||
u.currentExeName = exePath
|
||||
u.currentExeName = u.execPath
|
||||
_, err = os.Stat(u.currentExeName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("checking %q: %w", u.currentExeName, err)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package updater
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
|
@ -23,15 +24,17 @@ func TestMain(m *testing.M) {
|
|||
}
|
||||
|
||||
func startHTTPServer(t *testing.T, data []byte) (c *http.Client, u *url.URL) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
t.Helper()
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write(data)
|
||||
}))
|
||||
t.Cleanup(ts.Close)
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
u, err := url.Parse(ts.URL)
|
||||
u, err := url.Parse(srv.URL)
|
||||
require.NoError(t, err)
|
||||
|
||||
return ts.Client(), u
|
||||
return srv.Client(), u
|
||||
}
|
||||
|
||||
func TestUpdateGetVersion(t *testing.T) {
|
||||
|
@ -65,18 +68,17 @@ func TestUpdateGetVersion(t *testing.T) {
|
|||
}`
|
||||
|
||||
fakeClient, fakeURL := startHTTPServer(t, []byte(jsonData))
|
||||
fakeURL.Path = path.Join("adguardhome", version.ChannelBeta, "version.json")
|
||||
fakeURL = fakeURL.JoinPath("adguardhome", version.ChannelBeta, "version.json")
|
||||
|
||||
u := NewUpdater(&Config{
|
||||
Client: fakeClient,
|
||||
Version: "v0.103.0-beta.1",
|
||||
Channel: version.ChannelBeta,
|
||||
GOARCH: "arm",
|
||||
GOOS: "linux",
|
||||
Client: fakeClient,
|
||||
Version: "v0.103.0-beta.1",
|
||||
Channel: version.ChannelBeta,
|
||||
GOARCH: "arm",
|
||||
GOOS: "linux",
|
||||
VersionCheckURL: fakeURL.String(),
|
||||
})
|
||||
|
||||
u.versionCheckURL = fakeURL.String()
|
||||
|
||||
info, err := u.VersionInfo(false)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
@ -108,19 +110,20 @@ func TestUpdate(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
fakeClient, fakeURL := startHTTPServer(t, pkgData)
|
||||
fakeURL.Path = "AdGuardHome.tar.gz"
|
||||
fakeURL = fakeURL.JoinPath("AdGuardHome.tar.gz")
|
||||
|
||||
u := NewUpdater(&Config{
|
||||
Client: fakeClient,
|
||||
Version: "v0.103.0",
|
||||
Client: fakeClient,
|
||||
Version: "v0.103.0",
|
||||
ExecPath: exePath,
|
||||
WorkDir: wd,
|
||||
ConfName: yamlPath,
|
||||
})
|
||||
|
||||
u.workDir = wd
|
||||
u.confName = yamlPath
|
||||
u.newVersion = "v0.103.1"
|
||||
u.packageURL = fakeURL.String()
|
||||
|
||||
require.NoError(t, u.prepare(exePath))
|
||||
require.NoError(t, u.prepare())
|
||||
require.NoError(t, u.downloadPackageFile())
|
||||
require.NoError(t, u.unpack())
|
||||
// require.NoError(t, u.check())
|
||||
|
@ -183,17 +186,18 @@ func TestUpdateWindows(t *testing.T) {
|
|||
fakeURL.Path = "AdGuardHome.zip"
|
||||
|
||||
u := NewUpdater(&Config{
|
||||
Client: fakeClient,
|
||||
GOOS: "windows",
|
||||
Version: "v0.103.0",
|
||||
Client: fakeClient,
|
||||
GOOS: "windows",
|
||||
Version: "v0.103.0",
|
||||
ExecPath: exePath,
|
||||
WorkDir: wd,
|
||||
ConfName: yamlPath,
|
||||
})
|
||||
|
||||
u.workDir = wd
|
||||
u.confName = yamlPath
|
||||
u.newVersion = "v0.103.1"
|
||||
u.packageURL = fakeURL.String()
|
||||
|
||||
require.NoError(t, u.prepare(exePath))
|
||||
require.NoError(t, u.prepare())
|
||||
require.NoError(t, u.downloadPackageFile())
|
||||
require.NoError(t, u.unpack())
|
||||
// assert.Nil(t, u.check())
|
||||
|
@ -245,19 +249,18 @@ func TestUpdater_VersionInto_ARM(t *testing.T) {
|
|||
}`
|
||||
|
||||
fakeClient, fakeURL := startHTTPServer(t, []byte(jsonData))
|
||||
fakeURL.Path = path.Join("adguardhome", version.ChannelBeta, "version.json")
|
||||
fakeURL = fakeURL.JoinPath("adguardhome", version.ChannelBeta, "version.json")
|
||||
|
||||
u := NewUpdater(&Config{
|
||||
Client: fakeClient,
|
||||
Version: "v0.103.0-beta.1",
|
||||
Channel: version.ChannelBeta,
|
||||
GOARCH: "arm",
|
||||
GOOS: "linux",
|
||||
GOARM: "7",
|
||||
Client: fakeClient,
|
||||
Version: "v0.103.0-beta.1",
|
||||
Channel: version.ChannelBeta,
|
||||
GOARCH: "arm",
|
||||
GOOS: "linux",
|
||||
GOARM: "7",
|
||||
VersionCheckURL: fakeURL.String(),
|
||||
})
|
||||
|
||||
u.versionCheckURL = fakeURL.String()
|
||||
|
||||
info, err := u.VersionInfo(false)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
@ -277,19 +280,18 @@ func TestUpdater_VersionInto_MIPS(t *testing.T) {
|
|||
}`
|
||||
|
||||
fakeClient, fakeURL := startHTTPServer(t, []byte(jsonData))
|
||||
fakeURL.Path = path.Join("adguardhome", version.ChannelBeta, "version.json")
|
||||
fakeURL = fakeURL.JoinPath("adguardhome", version.ChannelBeta, "version.json")
|
||||
|
||||
u := NewUpdater(&Config{
|
||||
Client: fakeClient,
|
||||
Version: "v0.103.0-beta.1",
|
||||
Channel: version.ChannelBeta,
|
||||
GOARCH: "mips",
|
||||
GOOS: "linux",
|
||||
GOMIPS: "softfloat",
|
||||
Client: fakeClient,
|
||||
Version: "v0.103.0-beta.1",
|
||||
Channel: version.ChannelBeta,
|
||||
GOARCH: "mips",
|
||||
GOOS: "linux",
|
||||
GOMIPS: "softfloat",
|
||||
VersionCheckURL: fakeURL.String(),
|
||||
})
|
||||
|
||||
u.versionCheckURL = fakeURL.String()
|
||||
|
||||
info, err := u.VersionInfo(false)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
@ -298,3 +300,99 @@ func TestUpdater_VersionInto_MIPS(t *testing.T) {
|
|||
assert.Equal(t, "https://github.com/AdguardTeam/AdGuardHome/internal/releases", info.AnnouncementURL)
|
||||
assert.Equal(t, aghalg.NBTrue, info.CanAutoUpdate)
|
||||
}
|
||||
|
||||
func TestUpdater_Update(t *testing.T) {
|
||||
const jsonData = `{
|
||||
"version": "v0.103.0-beta.2",
|
||||
"announcement": "AdGuard Home v0.103.0-beta.2 is now available!",
|
||||
"announcement_url": "https://github.com/AdguardTeam/AdGuardHome/internal/releases",
|
||||
"selfupdate_min_version": "v0.0",
|
||||
"download_linux_amd64": "%s"
|
||||
}`
|
||||
|
||||
const packagePath = "/AdGuardHome.tar.gz"
|
||||
|
||||
wd := t.TempDir()
|
||||
|
||||
exePath := filepath.Join(wd, "AdGuardHome")
|
||||
yamlPath := filepath.Join(wd, "AdGuardHome.yaml")
|
||||
readmePath := filepath.Join(wd, "README.md")
|
||||
licensePath := filepath.Join(wd, "LICENSE.txt")
|
||||
|
||||
require.NoError(t, os.WriteFile(exePath, []byte("AdGuardHome"), 0o755))
|
||||
require.NoError(t, os.WriteFile(yamlPath, []byte("AdGuardHome.yaml"), 0o644))
|
||||
require.NoError(t, os.WriteFile(readmePath, []byte("README.md"), 0o644))
|
||||
require.NoError(t, os.WriteFile(licensePath, []byte("LICENSE.txt"), 0o644))
|
||||
|
||||
pkgData, err := os.ReadFile("testdata/AdGuardHome.tar.gz")
|
||||
require.NoError(t, err)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc(packagePath, func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write(pkgData)
|
||||
})
|
||||
|
||||
versionPath := path.Join("/adguardhome", version.ChannelBeta, "version.json")
|
||||
mux.HandleFunc(versionPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
var u string
|
||||
u, err = url.JoinPath("http://", r.Host, packagePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, _ = fmt.Fprintf(w, jsonData, u)
|
||||
})
|
||||
|
||||
srv := httptest.NewServer(mux)
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
versionCheckURL, err := url.JoinPath(srv.URL, versionPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
u := NewUpdater(&Config{
|
||||
Client: srv.Client(),
|
||||
GOARCH: "amd64",
|
||||
GOOS: "linux",
|
||||
Version: "v0.103.0",
|
||||
ConfName: yamlPath,
|
||||
WorkDir: wd,
|
||||
ExecPath: exePath,
|
||||
VersionCheckURL: versionCheckURL,
|
||||
})
|
||||
|
||||
_, err = u.VersionInfo(false)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = u.Update(true)
|
||||
require.NoError(t, err)
|
||||
|
||||
// check backup files
|
||||
d, err := os.ReadFile(filepath.Join(wd, "agh-backup", "LICENSE.txt"))
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "LICENSE.txt", string(d))
|
||||
|
||||
d, err = os.ReadFile(filepath.Join(wd, "agh-backup", "README.md"))
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "README.md", string(d))
|
||||
|
||||
// check updated files
|
||||
d, err = os.ReadFile(exePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "1", string(d))
|
||||
|
||||
d, err = os.ReadFile(readmePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "2", string(d))
|
||||
|
||||
d, err = os.ReadFile(licensePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "3", string(d))
|
||||
|
||||
d, err = os.ReadFile(yamlPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "AdGuardHome.yaml", string(d))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue