Pull request: 4219-updater-1

Merge in DNS/adguard-home from 4219-updater-1 to master

Squashed commit of the following:

commit 5721a0032150c9a0b8bd940542860c262adfe646
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Wed Aug 31 14:50:56 2022 +0300

    updater: imp code

commit a18ccb2174af3d68c85e40aed5167c01523b0e67
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Wed Aug 31 13:49:23 2022 +0300

    updater: exe name

commit deb5c7bee63a3b5eb7aa722268857ebcfc7f053d
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Wed Aug 31 10:05:00 2022 +0300

    updater: exe name
This commit is contained in:
Dimitry Kolyshev 2022-08-31 14:54:46 +03:00
parent 5cc2a2cd0c
commit c098960b39
2 changed files with 34 additions and 31 deletions

View File

@ -117,7 +117,7 @@ func (u *Updater) Update() (err error) {
return err return err
} }
err = u.prepare(filepath.Base(execPath)) err = u.prepare(execPath)
if err != nil { if err != nil {
return err return err
} }
@ -169,7 +169,7 @@ func (u *Updater) VersionCheckURL() (vcu string) {
} }
// prepare fills all necessary fields in Updater object. // prepare fills all necessary fields in Updater object.
func (u *Updater) prepare(exeName string) (err error) { func (u *Updater) prepare(exePath string) (err error) {
u.updateDir = filepath.Join(u.workDir, fmt.Sprintf("agh-update-%s", u.newVersion)) u.updateDir = filepath.Join(u.workDir, fmt.Sprintf("agh-update-%s", u.newVersion))
_, pkgNameOnly := filepath.Split(u.packageURL) _, pkgNameOnly := filepath.Split(u.packageURL)
@ -185,7 +185,7 @@ func (u *Updater) prepare(exeName string) (err error) {
updateExeName = "AdGuardHome.exe" updateExeName = "AdGuardHome.exe"
} }
u.backupExeName = filepath.Join(u.backupDir, exeName) u.backupExeName = filepath.Join(u.backupDir, filepath.Base(exePath))
u.updateExeName = filepath.Join(u.updateDir, updateExeName) u.updateExeName = filepath.Join(u.updateDir, updateExeName)
log.Debug( log.Debug(
@ -195,7 +195,7 @@ func (u *Updater) prepare(exeName string) (err error) {
u.packageURL, u.packageURL,
) )
u.currentExeName = filepath.Join(u.workDir, exeName) u.currentExeName = exePath
_, err = os.Stat(u.currentExeName) _, err = os.Stat(u.currentExeName)
if err != nil { if err != nil {
return fmt.Errorf("checking %q: %w", u.currentExeName, err) return fmt.Errorf("checking %q: %w", u.currentExeName, err)

View File

@ -103,10 +103,15 @@ func TestUpdateGetVersion(t *testing.T) {
func TestUpdate(t *testing.T) { func TestUpdate(t *testing.T) {
wd := t.TempDir() wd := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(wd, "AdGuardHome"), []byte("AdGuardHome"), 0o755)) exePath := filepath.Join(wd, "AdGuardHome")
require.NoError(t, os.WriteFile(filepath.Join(wd, "README.md"), []byte("README.md"), 0o644)) yamlPath := filepath.Join(wd, "AdGuardHome.yaml")
require.NoError(t, os.WriteFile(filepath.Join(wd, "LICENSE.txt"), []byte("LICENSE.txt"), 0o644)) readmePath := filepath.Join(wd, "README.md")
require.NoError(t, os.WriteFile(filepath.Join(wd, "AdGuardHome.yaml"), []byte("AdGuardHome.yaml"), 0o644)) 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 // start server for returning package file
pkgData, err := os.ReadFile("testdata/AdGuardHome.tar.gz") pkgData, err := os.ReadFile("testdata/AdGuardHome.tar.gz")
@ -127,17 +132,13 @@ func TestUpdate(t *testing.T) {
} }
u.workDir = wd u.workDir = wd
u.confName = filepath.Join(u.workDir, "AdGuardHome.yaml") u.confName = yamlPath
u.newVersion = "v0.103.1" u.newVersion = "v0.103.1"
u.packageURL = fakeURL.String() u.packageURL = fakeURL.String()
require.NoError(t, u.prepare("AdGuardHome")) require.NoError(t, u.prepare(exePath))
u.currentExeName = filepath.Join(wd, "AdGuardHome")
require.NoError(t, u.downloadPackageFile(u.packageURL, u.packageName)) require.NoError(t, u.downloadPackageFile(u.packageURL, u.packageName))
require.NoError(t, u.unpack()) require.NoError(t, u.unpack())
// require.NoError(t, u.check()) // require.NoError(t, u.check())
require.NoError(t, u.backup()) require.NoError(t, u.backup())
require.NoError(t, u.replace()) require.NoError(t, u.replace())
@ -156,22 +157,22 @@ func TestUpdate(t *testing.T) {
assert.Equal(t, "AdGuardHome", string(d)) assert.Equal(t, "AdGuardHome", string(d))
// check updated files // check updated files
d, err = os.ReadFile(filepath.Join(wd, "AdGuardHome")) d, err = os.ReadFile(exePath)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "1", string(d)) assert.Equal(t, "1", string(d))
d, err = os.ReadFile(filepath.Join(wd, "README.md")) d, err = os.ReadFile(readmePath)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "2", string(d)) assert.Equal(t, "2", string(d))
d, err = os.ReadFile(filepath.Join(wd, "LICENSE.txt")) d, err = os.ReadFile(licensePath)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "3", string(d)) assert.Equal(t, "3", string(d))
d, err = os.ReadFile(filepath.Join(wd, "AdGuardHome.yaml")) d, err = os.ReadFile(yamlPath)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "AdGuardHome.yaml", string(d)) assert.Equal(t, "AdGuardHome.yaml", string(d))
@ -180,10 +181,15 @@ func TestUpdate(t *testing.T) {
func TestUpdateWindows(t *testing.T) { func TestUpdateWindows(t *testing.T) {
wd := t.TempDir() wd := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(wd, "AdGuardHome.exe"), []byte("AdGuardHome.exe"), 0o755)) exePath := filepath.Join(wd, "AdGuardHome.exe")
require.NoError(t, os.WriteFile(filepath.Join(wd, "README.md"), []byte("README.md"), 0o644)) yamlPath := filepath.Join(wd, "AdGuardHome.yaml")
require.NoError(t, os.WriteFile(filepath.Join(wd, "LICENSE.txt"), []byte("LICENSE.txt"), 0o644)) readmePath := filepath.Join(wd, "README.md")
require.NoError(t, os.WriteFile(filepath.Join(wd, "AdGuardHome.yaml"), []byte("AdGuardHome.yaml"), 0o644)) 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 // start server for returning package file
pkgData, err := os.ReadFile("testdata/AdGuardHome.zip") pkgData, err := os.ReadFile("testdata/AdGuardHome.zip")
@ -205,14 +211,11 @@ func TestUpdateWindows(t *testing.T) {
} }
u.workDir = wd u.workDir = wd
u.confName = filepath.Join(u.workDir, "AdGuardHome.yaml") u.confName = yamlPath
u.newVersion = "v0.103.1" u.newVersion = "v0.103.1"
u.packageURL = fakeURL.String() u.packageURL = fakeURL.String()
require.NoError(t, u.prepare("AdGuardHome.exe")) require.NoError(t, u.prepare(exePath))
u.currentExeName = filepath.Join(wd, "AdGuardHome.exe")
require.NoError(t, u.downloadPackageFile(u.packageURL, u.packageName)) require.NoError(t, u.downloadPackageFile(u.packageURL, u.packageName))
require.NoError(t, u.unpack()) require.NoError(t, u.unpack())
// assert.Nil(t, u.check()) // assert.Nil(t, u.check())
@ -233,22 +236,22 @@ func TestUpdateWindows(t *testing.T) {
assert.Equal(t, "AdGuardHome.exe", string(d)) assert.Equal(t, "AdGuardHome.exe", string(d))
// check updated files // check updated files
d, err = os.ReadFile(filepath.Join(wd, "AdGuardHome.exe")) d, err = os.ReadFile(exePath)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "1", string(d)) assert.Equal(t, "1", string(d))
d, err = os.ReadFile(filepath.Join(wd, "README.md")) d, err = os.ReadFile(readmePath)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "2", string(d)) assert.Equal(t, "2", string(d))
d, err = os.ReadFile(filepath.Join(wd, "LICENSE.txt")) d, err = os.ReadFile(licensePath)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "3", string(d)) assert.Equal(t, "3", string(d))
d, err = os.ReadFile(filepath.Join(wd, "AdGuardHome.yaml")) d, err = os.ReadFile(yamlPath)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "AdGuardHome.yaml", string(d)) assert.Equal(t, "AdGuardHome.yaml", string(d))