AdGuardHome/internal/updater/updater_test.go

332 lines
11 KiB
Go
Raw Normal View History

package updater
import (
"net"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"testing"
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
"github.com/AdguardTeam/AdGuardHome/internal/version"
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TODO(a.garipov): Rewrite these tests.
func TestMain(m *testing.M) {
testutil.DiscardLogOutput(m)
}
func startHTTPServer(data string) (l net.Listener, portStr string) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(data))
})
listener, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
go func() { _ = http.Serve(listener, mux) }()
return listener, strconv.FormatUint(uint64(listener.Addr().(*net.TCPAddr).Port), 10)
}
func TestUpdateGetVersion(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_windows_amd64": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_windows_amd64.zip",
"download_windows_386": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_windows_386.zip",
"download_darwin_amd64": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_darwin_amd64.zip",
"download_darwin_386": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_darwin_386.zip",
"download_linux_amd64": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_amd64.tar.gz",
"download_linux_386": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_386.tar.gz",
"download_linux_arm": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz",
"download_linux_armv5": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_armv5.tar.gz",
"download_linux_armv6": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz",
"download_linux_armv7": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_armv7.tar.gz",
"download_linux_arm64": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_arm64.tar.gz",
"download_linux_mips": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_mips_softfloat.tar.gz",
"download_linux_mipsle": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_mipsle_softfloat.tar.gz",
"download_linux_mips64": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_mips64_softfloat.tar.gz",
"download_linux_mips64le": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_mips64le_softfloat.tar.gz",
"download_freebsd_386": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_freebsd_386.tar.gz",
"download_freebsd_amd64": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_freebsd_amd64.tar.gz",
"download_freebsd_arm": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_freebsd_armv6.tar.gz",
"download_freebsd_armv5": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_freebsd_armv5.tar.gz",
"download_freebsd_armv6": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_freebsd_armv6.tar.gz",
"download_freebsd_armv7": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_freebsd_armv7.tar.gz",
"download_freebsd_arm64": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_freebsd_arm64.tar.gz"
}`
l, lport := startHTTPServer(jsonData)
testutil.CleanupAndRequireSuccess(t, l.Close)
u := NewUpdater(&Config{
Client: &http.Client{},
Version: "v0.103.0-beta.1",
Channel: version.ChannelBeta,
GOARCH: "arm",
GOOS: "linux",
})
fakeURL := &url.URL{
Scheme: "http",
Host: net.JoinHostPort("127.0.0.1", lport),
Path: path.Join("adguardhome", version.ChannelBeta, "version.json"),
}
u.versionCheckURL = fakeURL.String()
info, err := u.VersionInfo(false)
require.NoError(t, err)
assert.Equal(t, "v0.103.0-beta.2", info.NewVersion)
assert.Equal(t, "AdGuard Home v0.103.0-beta.2 is now available!", info.Announcement)
assert.Equal(t, "https://github.com/AdguardTeam/AdGuardHome/internal/releases", info.AnnouncementURL)
assert.Equal(t, aghalg.NBTrue, info.CanAutoUpdate)
// check cached
_, err = u.VersionInfo(false)
require.NoError(t, err)
}
func TestUpdate(t *testing.T) {
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))
// start server for returning package file
pkgData, err := os.ReadFile("testdata/AdGuardHome.tar.gz")
require.NoError(t, err)
l, lport := startHTTPServer(string(pkgData))
testutil.CleanupAndRequireSuccess(t, l.Close)
u := NewUpdater(&Config{
Client: &http.Client{},
Version: "v0.103.0",
})
fakeURL := &url.URL{
Scheme: "http",
Host: net.JoinHostPort("127.0.0.1", lport),
Path: "AdGuardHome.tar.gz",
}
u.workDir = wd
u.confName = yamlPath
u.newVersion = "v0.103.1"
u.packageURL = fakeURL.String()
require.NoError(t, u.prepare(exePath))
Pull request: 5191-update-flag Merge in DNS/adguard-home from 5191-update-flag to master Updates #5191. Updates #4223. Squashed commit of the following: commit fbace4942844dc67f2467479385e06843c3abb6a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Jan 9 12:05:16 2023 +0400 all: imp code, docs commit 8237dceb771ba95f545f79565d76cbb4ebd0d805 Merge: ca9518f2 bbdcc673 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Dec 30 14:45:55 2022 +0400 Merge branch 'master' into 5191-update-flag commit ca9518f20e5643572adf9734b93a5436ba30c865 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 20:36:33 2022 +0400 home: imp code commit 1dc6c7c3480df3df4a5f3f923f1feab7761a7945 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 18:26:08 2022 +0400 all: imp code, docs commit 7bbe893e98063b956482fd6f1c6be95a4f1956cf Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 03:44:48 2022 +0400 home: restart service on update commit e0d3c287a7e1c05b1e397f4727c447a1fcd9f7f6 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 03:22:49 2022 +0400 all: update on first run commit 0aa4e78f03bf3819425accb468ce59e747506ef3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 02:47:30 2022 +0400 all: move some code to init less commit 68aebfa050b9965afef26653e9b699ff4aaf5b8b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 00:36:00 2022 +0400 WIP commit 2c7fb97d701ac158613c5a3a4d4d35c5b79b3d59 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Dec 28 14:15:59 2022 +0400 home: imp logs commit 4b06d089da835d6d187803bbb5ca1caf9973e2d3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Dec 27 19:21:17 2022 +0400 all: fix update flag
2023-01-09 10:38:31 +00:00
require.NoError(t, u.downloadPackageFile())
require.NoError(t, u.unpack())
// require.NoError(t, u.check())
Pull request: 5191-update-flag Merge in DNS/adguard-home from 5191-update-flag to master Updates #5191. Updates #4223. Squashed commit of the following: commit fbace4942844dc67f2467479385e06843c3abb6a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Jan 9 12:05:16 2023 +0400 all: imp code, docs commit 8237dceb771ba95f545f79565d76cbb4ebd0d805 Merge: ca9518f2 bbdcc673 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Dec 30 14:45:55 2022 +0400 Merge branch 'master' into 5191-update-flag commit ca9518f20e5643572adf9734b93a5436ba30c865 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 20:36:33 2022 +0400 home: imp code commit 1dc6c7c3480df3df4a5f3f923f1feab7761a7945 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 18:26:08 2022 +0400 all: imp code, docs commit 7bbe893e98063b956482fd6f1c6be95a4f1956cf Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 03:44:48 2022 +0400 home: restart service on update commit e0d3c287a7e1c05b1e397f4727c447a1fcd9f7f6 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 03:22:49 2022 +0400 all: update on first run commit 0aa4e78f03bf3819425accb468ce59e747506ef3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 02:47:30 2022 +0400 all: move some code to init less commit 68aebfa050b9965afef26653e9b699ff4aaf5b8b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 00:36:00 2022 +0400 WIP commit 2c7fb97d701ac158613c5a3a4d4d35c5b79b3d59 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Dec 28 14:15:59 2022 +0400 home: imp logs commit 4b06d089da835d6d187803bbb5ca1caf9973e2d3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Dec 27 19:21:17 2022 +0400 all: fix update flag
2023-01-09 10:38:31 +00:00
require.NoError(t, u.backup(false))
require.NoError(t, u.replace())
u.clean()
// check backup files
d, err := os.ReadFile(filepath.Join(wd, "agh-backup", "AdGuardHome.yaml"))
require.NoError(t, err)
assert.Equal(t, "AdGuardHome.yaml", string(d))
d, err = os.ReadFile(filepath.Join(wd, "agh-backup", "AdGuardHome"))
require.NoError(t, err)
assert.Equal(t, "AdGuardHome", 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))
}
func TestUpdateWindows(t *testing.T) {
wd := t.TempDir()
exePath := filepath.Join(wd, "AdGuardHome.exe")
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.exe"), 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))
// start server for returning package file
pkgData, err := os.ReadFile("testdata/AdGuardHome.zip")
require.NoError(t, err)
l, lport := startHTTPServer(string(pkgData))
testutil.CleanupAndRequireSuccess(t, l.Close)
u := NewUpdater(&Config{
Client: &http.Client{},
GOOS: "windows",
Version: "v0.103.0",
})
fakeURL := &url.URL{
Scheme: "http",
Host: net.JoinHostPort("127.0.0.1", lport),
Path: "AdGuardHome.zip",
}
u.workDir = wd
u.confName = yamlPath
u.newVersion = "v0.103.1"
u.packageURL = fakeURL.String()
require.NoError(t, u.prepare(exePath))
Pull request: 5191-update-flag Merge in DNS/adguard-home from 5191-update-flag to master Updates #5191. Updates #4223. Squashed commit of the following: commit fbace4942844dc67f2467479385e06843c3abb6a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Jan 9 12:05:16 2023 +0400 all: imp code, docs commit 8237dceb771ba95f545f79565d76cbb4ebd0d805 Merge: ca9518f2 bbdcc673 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Dec 30 14:45:55 2022 +0400 Merge branch 'master' into 5191-update-flag commit ca9518f20e5643572adf9734b93a5436ba30c865 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 20:36:33 2022 +0400 home: imp code commit 1dc6c7c3480df3df4a5f3f923f1feab7761a7945 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 18:26:08 2022 +0400 all: imp code, docs commit 7bbe893e98063b956482fd6f1c6be95a4f1956cf Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 03:44:48 2022 +0400 home: restart service on update commit e0d3c287a7e1c05b1e397f4727c447a1fcd9f7f6 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 03:22:49 2022 +0400 all: update on first run commit 0aa4e78f03bf3819425accb468ce59e747506ef3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 02:47:30 2022 +0400 all: move some code to init less commit 68aebfa050b9965afef26653e9b699ff4aaf5b8b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 00:36:00 2022 +0400 WIP commit 2c7fb97d701ac158613c5a3a4d4d35c5b79b3d59 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Dec 28 14:15:59 2022 +0400 home: imp logs commit 4b06d089da835d6d187803bbb5ca1caf9973e2d3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Dec 27 19:21:17 2022 +0400 all: fix update flag
2023-01-09 10:38:31 +00:00
require.NoError(t, u.downloadPackageFile())
require.NoError(t, u.unpack())
// assert.Nil(t, u.check())
Pull request: 5191-update-flag Merge in DNS/adguard-home from 5191-update-flag to master Updates #5191. Updates #4223. Squashed commit of the following: commit fbace4942844dc67f2467479385e06843c3abb6a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Jan 9 12:05:16 2023 +0400 all: imp code, docs commit 8237dceb771ba95f545f79565d76cbb4ebd0d805 Merge: ca9518f2 bbdcc673 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Dec 30 14:45:55 2022 +0400 Merge branch 'master' into 5191-update-flag commit ca9518f20e5643572adf9734b93a5436ba30c865 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 20:36:33 2022 +0400 home: imp code commit 1dc6c7c3480df3df4a5f3f923f1feab7761a7945 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 18:26:08 2022 +0400 all: imp code, docs commit 7bbe893e98063b956482fd6f1c6be95a4f1956cf Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 03:44:48 2022 +0400 home: restart service on update commit e0d3c287a7e1c05b1e397f4727c447a1fcd9f7f6 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 03:22:49 2022 +0400 all: update on first run commit 0aa4e78f03bf3819425accb468ce59e747506ef3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 02:47:30 2022 +0400 all: move some code to init less commit 68aebfa050b9965afef26653e9b699ff4aaf5b8b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 00:36:00 2022 +0400 WIP commit 2c7fb97d701ac158613c5a3a4d4d35c5b79b3d59 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Dec 28 14:15:59 2022 +0400 home: imp logs commit 4b06d089da835d6d187803bbb5ca1caf9973e2d3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Dec 27 19:21:17 2022 +0400 all: fix update flag
2023-01-09 10:38:31 +00:00
require.NoError(t, u.backup(false))
require.NoError(t, u.replace())
u.clean()
// check backup files
d, err := os.ReadFile(filepath.Join(wd, "agh-backup", "AdGuardHome.yaml"))
require.NoError(t, err)
assert.Equal(t, "AdGuardHome.yaml", string(d))
d, err = os.ReadFile(filepath.Join(wd, "agh-backup", "AdGuardHome.exe"))
require.NoError(t, err)
assert.Equal(t, "AdGuardHome.exe", 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))
}
func TestUpdater_VersionInto_ARM(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_armv7": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_armv7.tar.gz"
}`
l, lport := startHTTPServer(jsonData)
testutil.CleanupAndRequireSuccess(t, l.Close)
u := NewUpdater(&Config{
Client: &http.Client{},
Version: "v0.103.0-beta.1",
Channel: version.ChannelBeta,
GOARCH: "arm",
GOOS: "linux",
GOARM: "7",
})
fakeURL := &url.URL{
Scheme: "http",
Host: net.JoinHostPort("127.0.0.1", lport),
Path: path.Join("adguardhome", version.ChannelBeta, "version.json"),
}
u.versionCheckURL = fakeURL.String()
info, err := u.VersionInfo(false)
require.NoError(t, err)
assert.Equal(t, "v0.103.0-beta.2", info.NewVersion)
assert.Equal(t, "AdGuard Home v0.103.0-beta.2 is now available!", info.Announcement)
assert.Equal(t, "https://github.com/AdguardTeam/AdGuardHome/internal/releases", info.AnnouncementURL)
assert.Equal(t, aghalg.NBTrue, info.CanAutoUpdate)
}
func TestUpdater_VersionInto_MIPS(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_mips_softfloat": "https://static.adtidy.org/adguardhome/beta/AdGuardHome_linux_mips_softfloat.tar.gz"
}`
l, lport := startHTTPServer(jsonData)
testutil.CleanupAndRequireSuccess(t, l.Close)
u := NewUpdater(&Config{
Client: &http.Client{},
Version: "v0.103.0-beta.1",
Channel: version.ChannelBeta,
GOARCH: "mips",
GOOS: "linux",
GOMIPS: "softfloat",
})
fakeURL := &url.URL{
Scheme: "http",
Host: net.JoinHostPort("127.0.0.1", lport),
Path: path.Join("adguardhome", version.ChannelBeta, "version.json"),
}
u.versionCheckURL = fakeURL.String()
info, err := u.VersionInfo(false)
require.NoError(t, err)
assert.Equal(t, "v0.103.0-beta.2", info.NewVersion)
assert.Equal(t, "AdGuard Home v0.103.0-beta.2 is now available!", info.Announcement)
assert.Equal(t, "https://github.com/AdguardTeam/AdGuardHome/internal/releases", info.AnnouncementURL)
assert.Equal(t, aghalg.NBTrue, info.CanAutoUpdate)
}