AdGuardHome/internal/updater/updater_test.go
Stanislav Chzhen 93ab0fde23 Pull request 2015: AG-23168-imp-updater-tests
Squashed commit of the following:

commit 402ac873839fcf4dfb762872497b740bd55b2dcc
Merge: 209e1b171 af38476ef
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Sep 21 16:46:47 2023 +0300

    Merge branch 'master' into AG-23168-imp-updater-tests

commit 209e1b171cb4c55d177ef5a4198cf24c2bc41195
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Sep 21 14:09:43 2023 +0300

    updater: fix windows build

commit d112ca53540e0c999fe126442e8c526ff7e33bf5
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Sep 20 18:57:54 2023 +0300

    all: imp tests, docs

commit f940724f81a90af6c07de523c8fcce66240dbec9
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Sep 20 16:07:48 2023 +0300

    all: imp tests

commit 8915818e81360a7f6ff5b652a983ca034ad6d0c4
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Sep 19 19:33:20 2023 +0300

    all: add test

commit c1cb8df2b056cefc5d7f1b32c089cd0ede0af9d2
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Sep 14 19:12:47 2023 +0300

    updater: imp tests
2023-09-21 17:07:57 +03:00

136 lines
3.3 KiB
Go

package updater_test
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"testing"
"github.com/AdguardTeam/AdGuardHome/internal/updater"
"github.com/AdguardTeam/AdGuardHome/internal/version"
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
testutil.DiscardLogOutput(m)
}
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_unix.tar.gz")
require.NoError(t, err)
mux := http.NewServeMux()
mux.HandleFunc(packagePath, func(w http.ResponseWriter, _ *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 := updater.NewUpdater(&updater.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
_, err = os.Stat(exePath)
require.NoError(t, err)
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))
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")
}
err = u.Update(false)
assert.NoError(t, err)
})
t.Run("api_fail", func(t *testing.T) {
srv.Close()
err = u.Update(true)
var urlErr *url.Error
assert.ErrorAs(t, err, &urlErr)
})
}