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
This commit is contained in:
Stanislav Chzhen
2023-09-21 17:07:57 +03:00
parent af38476efa
commit 93ab0fde23
7 changed files with 369 additions and 280 deletions

View File

@@ -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)