2023-09-21 15:07:57 +01:00
|
|
|
package updater_test
|
2021-01-13 13:18:51 +00:00
|
|
|
|
|
|
|
import (
|
2023-09-21 15:07:57 +01:00
|
|
|
"fmt"
|
2021-01-13 13:18:51 +00:00
|
|
|
"net/http"
|
2023-09-21 15:07:57 +01:00
|
|
|
"net/http/httptest"
|
2021-01-13 13:18:51 +00:00
|
|
|
"net/url"
|
2021-05-21 12:55:42 +01:00
|
|
|
"os"
|
2021-01-13 13:18:51 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2023-09-21 15:07:57 +01:00
|
|
|
"runtime"
|
2021-01-13 13:18:51 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/updater"
|
2021-01-25 11:09:29 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/version"
|
2021-10-22 09:58:18 +01:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-01-13 13:18:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-10-22 09:58:18 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-13 13:18:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2022-10-27 14:11:36 +01:00
|
|
|
testutil.DiscardLogOutput(m)
|
2021-01-13 13:18:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
func TestUpdater_Update(t *testing.T) {
|
2021-01-13 13:18:51 +00:00
|
|
|
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",
|
2023-09-21 15:07:57 +01:00
|
|
|
"download_linux_amd64": "%s"
|
2021-01-13 13:18:51 +00:00
|
|
|
}`
|
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
const packagePath = "/AdGuardHome.tar.gz"
|
2021-10-22 09:58:18 +01:00
|
|
|
|
2021-03-29 09:40:04 +01:00
|
|
|
wd := t.TempDir()
|
2021-01-13 13:18:51 +00:00
|
|
|
|
2022-08-31 12:54:46 +01:00
|
|
|
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))
|
2021-01-13 13:18:51 +00:00
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
pkgData, err := os.ReadFile("testdata/AdGuardHome_unix.tar.gz")
|
2021-10-22 09:58:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc(packagePath, func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
_, _ = w.Write(pkgData)
|
2021-01-13 13:18:51 +00:00
|
|
|
})
|
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
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)
|
2021-10-22 09:58:18 +01:00
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
_, _ = fmt.Fprintf(w, jsonData, u)
|
|
|
|
})
|
2021-10-22 09:58:18 +01:00
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(srv.Close)
|
2021-01-13 13:18:51 +00:00
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
versionCheckURL, err := url.JoinPath(srv.URL, versionPath)
|
2021-10-22 09:58:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
u := updater.NewUpdater(&updater.Config{
|
|
|
|
Client: srv.Client(),
|
|
|
|
GOARCH: "amd64",
|
|
|
|
GOOS: "linux",
|
|
|
|
Version: "v0.103.0",
|
|
|
|
ConfName: yamlPath,
|
|
|
|
WorkDir: wd,
|
|
|
|
ExecPath: exePath,
|
|
|
|
VersionCheckURL: versionCheckURL,
|
|
|
|
})
|
2021-01-13 13:18:51 +00:00
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
_, err = u.VersionInfo(false)
|
2021-10-22 09:58:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
err = u.Update(true)
|
2021-10-22 09:58:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
// check backup files
|
2023-09-21 15:07:57 +01:00
|
|
|
d, err := os.ReadFile(filepath.Join(wd, "agh-backup", "LICENSE.txt"))
|
2021-10-22 09:58:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
assert.Equal(t, "LICENSE.txt", string(d))
|
2021-01-13 13:18:51 +00:00
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
d, err = os.ReadFile(filepath.Join(wd, "agh-backup", "README.md"))
|
2021-10-22 09:58:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
assert.Equal(t, "README.md", string(d))
|
2021-01-13 13:18:51 +00:00
|
|
|
|
|
|
|
// check updated files
|
2023-09-21 15:07:57 +01:00
|
|
|
_, err = os.Stat(exePath)
|
2021-10-22 09:58:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-31 12:54:46 +01:00
|
|
|
d, err = os.ReadFile(readmePath)
|
2021-10-22 09:58:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
assert.Equal(t, "2", string(d))
|
|
|
|
|
2022-08-31 12:54:46 +01:00
|
|
|
d, err = os.ReadFile(licensePath)
|
2021-10-22 09:58:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
assert.Equal(t, "3", string(d))
|
|
|
|
|
2022-08-31 12:54:46 +01:00
|
|
|
d, err = os.ReadFile(yamlPath)
|
2021-10-22 09:58:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
assert.Equal(t, "AdGuardHome.yaml", string(d))
|
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
t.Run("config_check", func(t *testing.T) {
|
|
|
|
// TODO(s.chzhen): Test on Windows also.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("skipping config check test on windows")
|
|
|
|
}
|
2021-01-13 13:18:51 +00:00
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
err = u.Update(false)
|
|
|
|
assert.NoError(t, err)
|
2021-01-13 13:18:51 +00:00
|
|
|
})
|
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
t.Run("api_fail", func(t *testing.T) {
|
|
|
|
srv.Close()
|
2021-01-13 13:18:51 +00:00
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
err = u.Update(true)
|
|
|
|
var urlErr *url.Error
|
|
|
|
assert.ErrorAs(t, err, &urlErr)
|
2021-01-13 13:18:51 +00:00
|
|
|
})
|
|
|
|
}
|