badguardhome/internal/update/update_test.go
Eugene Burkov 6358240e9b Pull request: 2273 clean tests output
Merge in DNS/adguard-home from 2273-clean-tests-output to master

Closes #2273.

Squashed commit of the following:

commit 7571a33fc1f76300bd256578b3afa95338e399c4
Merge: f17df0f9c a19523b25
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:45:30 2020 +0300

    Merge branch 'master' into 2273-clean-tests-output

commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:35:42 2020 +0300

    home: move build constraint on top line

commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:24:50 2020 +0300

    all: add improvements to changelog

commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56
Merge: 43d4f7acf 394fc5a9d
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:04:38 2020 +0300

    Merge branch 'master' into 2273-clean-tests-output

commit 43d4f7acf188e810aa7277cb6479110c9842e8be
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 13:38:13 2020 +0300

    dnsfilter: remove redundant test logging

commit 7194c1413006b8f52fc454e89ab052bf52e4e517
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 12:19:14 2020 +0300

    testutil: improve comments

commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Sat Nov 14 01:22:08 2020 +0300

    nclient4: fix wrong function name

commit f355749149b2a4485792ba2bdcbc0bb4b629d726
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Sat Nov 14 01:07:22 2020 +0300

    testutil: fix comments and naming

commit f8c50a260bfae08d594a7f37d603941d3680a45e
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Nov 13 14:09:50 2020 +0300

    testutil: create a package and include it

commit f169cdc4084b719de65aa0cdc65200b48785322e
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Nov 12 20:15:58 2020 +0300

    agherr: discard loggers output

commit 360e736b5a2a30f2c5350448234f14b841e3ea27
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Nov 12 20:09:55 2020 +0300

    all: replace default log writer with ioutil.Discard

    Closes #2273.
2020-11-16 15:52:05 +03:00

216 lines
7.8 KiB
Go

package update
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"testing"
"github.com/AdguardTeam/AdGuardHome/internal/testutil"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
testutil.DiscardLogOutput(m)
}
func startHTTPServer(data string) (net.Listener, uint16) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *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, uint16(listener.Addr().(*net.TCPAddr).Port)
}
func TestUpdateGetVersion(t *testing.T) {
const jsonData = `{
"version": "v0.103.0-beta2",
"announcement": "AdGuard Home v0.103.0-beta2 is now available!",
"announcement_url": "https://github.com/AdguardTeam/AdGuardHome/internal/releases",
"selfupdate_min_version": "v0.0",
"download_windows_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_windows_amd64.zip",
"download_windows_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_windows_386.zip",
"download_darwin_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_darwin_amd64.zip",
"download_darwin_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_darwin_386.zip",
"download_linux_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_amd64.tar.gz",
"download_linux_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_386.tar.gz",
"download_linux_arm": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz",
"download_linux_armv5": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv5.tar.gz",
"download_linux_armv6": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz",
"download_linux_armv7": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv7.tar.gz",
"download_linux_arm64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_arm64.tar.gz",
"download_linux_mips": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mips_softfloat.tar.gz",
"download_linux_mipsle": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mipsle_softfloat.tar.gz",
"download_linux_mips64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mips64_softfloat.tar.gz",
"download_linux_mips64le": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mips64le_softfloat.tar.gz",
"download_freebsd_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_386.tar.gz",
"download_freebsd_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_amd64.tar.gz",
"download_freebsd_arm": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv6.tar.gz",
"download_freebsd_armv5": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv5.tar.gz",
"download_freebsd_armv6": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv6.tar.gz",
"download_freebsd_armv7": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv7.tar.gz",
"download_freebsd_arm64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_arm64.tar.gz"
}`
l, lport := startHTTPServer(jsonData)
defer func() { _ = l.Close() }()
u := NewUpdater(Config{
Client: &http.Client{},
VersionURL: fmt.Sprintf("http://127.0.0.1:%d/", lport),
OS: "linux",
Arch: "arm",
VersionString: "v0.103.0-beta1",
})
info, err := u.GetVersionResponse(false)
assert.Nil(t, err)
assert.Equal(t, "v0.103.0-beta2", info.NewVersion)
assert.Equal(t, "AdGuard Home v0.103.0-beta2 is now available!", info.Announcement)
assert.Equal(t, "https://github.com/AdguardTeam/AdGuardHome/internal/releases", info.AnnouncementURL)
assert.Equal(t, "v0.0", info.SelfUpdateMinVersion)
assert.True(t, info.CanAutoUpdate)
_ = l.Close()
// check cached
_, err = u.GetVersionResponse(false)
assert.Nil(t, err)
}
func TestUpdate(t *testing.T) {
_ = os.Mkdir("aghtest", 0755)
defer func() {
_ = os.RemoveAll("aghtest")
}()
// create "current" files
assert.Nil(t, ioutil.WriteFile("aghtest/AdGuardHome", []byte("AdGuardHome"), 0755))
assert.Nil(t, ioutil.WriteFile("aghtest/README.md", []byte("README.md"), 0644))
assert.Nil(t, ioutil.WriteFile("aghtest/LICENSE.txt", []byte("LICENSE.txt"), 0644))
assert.Nil(t, ioutil.WriteFile("aghtest/AdGuardHome.yaml", []byte("AdGuardHome.yaml"), 0644))
// start server for returning package file
pkgData, err := ioutil.ReadFile("test/AdGuardHome.tar.gz")
assert.Nil(t, err)
l, lport := startHTTPServer(string(pkgData))
defer func() { _ = l.Close() }()
u := NewUpdater(Config{
Client: &http.Client{},
PackageURL: fmt.Sprintf("http://127.0.0.1:%d/AdGuardHome.tar.gz", lport),
VersionString: "v0.103.0",
NewVersion: "v0.103.1",
ConfigName: "aghtest/AdGuardHome.yaml",
WorkDir: "aghtest",
})
assert.Nil(t, u.prepare())
u.currentExeName = "aghtest/AdGuardHome"
assert.Nil(t, u.downloadPackageFile(u.PackageURL, u.packageName))
assert.Nil(t, u.unpack())
// assert.Nil(t, u.check())
assert.Nil(t, u.backup())
assert.Nil(t, u.replace())
u.clean()
// check backup files
d, err := ioutil.ReadFile("aghtest/agh-backup/AdGuardHome.yaml")
assert.Nil(t, err)
assert.Equal(t, "AdGuardHome.yaml", string(d))
d, err = ioutil.ReadFile("aghtest/agh-backup/AdGuardHome")
assert.Nil(t, err)
assert.Equal(t, "AdGuardHome", string(d))
// check updated files
d, err = ioutil.ReadFile("aghtest/AdGuardHome")
assert.Nil(t, err)
assert.Equal(t, "1", string(d))
d, err = ioutil.ReadFile("aghtest/README.md")
assert.Nil(t, err)
assert.Equal(t, "2", string(d))
d, err = ioutil.ReadFile("aghtest/LICENSE.txt")
assert.Nil(t, err)
assert.Equal(t, "3", string(d))
d, err = ioutil.ReadFile("aghtest/AdGuardHome.yaml")
assert.Nil(t, err)
assert.Equal(t, "AdGuardHome.yaml", string(d))
}
func TestUpdateWindows(t *testing.T) {
_ = os.Mkdir("aghtest", 0755)
defer func() {
_ = os.RemoveAll("aghtest")
}()
// create "current" files
assert.Nil(t, ioutil.WriteFile("aghtest/AdGuardHome.exe", []byte("AdGuardHome.exe"), 0755))
assert.Nil(t, ioutil.WriteFile("aghtest/README.md", []byte("README.md"), 0644))
assert.Nil(t, ioutil.WriteFile("aghtest/LICENSE.txt", []byte("LICENSE.txt"), 0644))
assert.Nil(t, ioutil.WriteFile("aghtest/AdGuardHome.yaml", []byte("AdGuardHome.yaml"), 0644))
// start server for returning package file
pkgData, err := ioutil.ReadFile("test/AdGuardHome.zip")
assert.Nil(t, err)
l, lport := startHTTPServer(string(pkgData))
defer func() { _ = l.Close() }()
u := NewUpdater(Config{
WorkDir: "aghtest",
Client: &http.Client{},
PackageURL: fmt.Sprintf("http://127.0.0.1:%d/AdGuardHome.zip", lport),
OS: "windows",
VersionString: "v0.103.0",
NewVersion: "v0.103.1",
ConfigName: "aghtest/AdGuardHome.yaml",
})
assert.Nil(t, u.prepare())
u.currentExeName = "aghtest/AdGuardHome.exe"
assert.Nil(t, u.downloadPackageFile(u.PackageURL, u.packageName))
assert.Nil(t, u.unpack())
// assert.Nil(t, u.check())
assert.Nil(t, u.backup())
assert.Nil(t, u.replace())
u.clean()
// check backup files
d, err := ioutil.ReadFile("aghtest/agh-backup/AdGuardHome.yaml")
assert.Nil(t, err)
assert.Equal(t, "AdGuardHome.yaml", string(d))
d, err = ioutil.ReadFile("aghtest/agh-backup/AdGuardHome.exe")
assert.Nil(t, err)
assert.Equal(t, "AdGuardHome.exe", string(d))
// check updated files
d, err = ioutil.ReadFile("aghtest/AdGuardHome.exe")
assert.Nil(t, err)
assert.Equal(t, "1", string(d))
d, err = ioutil.ReadFile("aghtest/README.md")
assert.Nil(t, err)
assert.Equal(t, "2", string(d))
d, err = ioutil.ReadFile("aghtest/LICENSE.txt")
assert.Nil(t, err)
assert.Equal(t, "3", string(d))
d, err = ioutil.ReadFile("aghtest/AdGuardHome.yaml")
assert.Nil(t, err)
assert.Equal(t, "AdGuardHome.yaml", string(d))
}