"Fossies" - the Fresh Open Source Software Archive

Member "AdGuardHome-0.104.3/internal/update/update_test.go" (19 Nov 2020, 8000 Bytes) of package /linux/misc/dns/AdGuardHome-0.104.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "update_test.go": 0.104.1_vs_0.104.3.

    1 package update
    2 
    3 import (
    4     "fmt"
    5     "io/ioutil"
    6     "net"
    7     "net/http"
    8     "os"
    9     "testing"
   10 
   11     "github.com/AdguardTeam/AdGuardHome/internal/testutil"
   12     "github.com/stretchr/testify/assert"
   13 )
   14 
   15 func TestMain(m *testing.M) {
   16     testutil.DiscardLogOutput(m)
   17 }
   18 
   19 func startHTTPServer(data string) (net.Listener, uint16) {
   20     mux := http.NewServeMux()
   21     mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
   22         _, _ = w.Write([]byte(data))
   23     })
   24 
   25     listener, err := net.Listen("tcp", ":0")
   26     if err != nil {
   27         panic(err)
   28     }
   29 
   30     go func() { _ = http.Serve(listener, mux) }()
   31     return listener, uint16(listener.Addr().(*net.TCPAddr).Port)
   32 }
   33 
   34 func TestUpdateGetVersion(t *testing.T) {
   35     const jsonData = `{
   36   "version": "v0.103.0-beta2",
   37   "announcement": "AdGuard Home v0.103.0-beta2 is now available!",
   38   "announcement_url": "https://github.com/AdguardTeam/AdGuardHome/internal/releases",
   39   "selfupdate_min_version": "v0.0",
   40   "download_windows_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_windows_amd64.zip",
   41   "download_windows_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_windows_386.zip",
   42   "download_darwin_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_darwin_amd64.zip",
   43   "download_darwin_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_darwin_386.zip",
   44   "download_linux_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_amd64.tar.gz",
   45   "download_linux_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_386.tar.gz",
   46   "download_linux_arm": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz",
   47   "download_linux_armv5": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv5.tar.gz",
   48   "download_linux_armv6": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz",
   49   "download_linux_armv7": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv7.tar.gz",
   50   "download_linux_arm64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_arm64.tar.gz",
   51   "download_linux_mips": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mips_softfloat.tar.gz",
   52   "download_linux_mipsle": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mipsle_softfloat.tar.gz",
   53   "download_linux_mips64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mips64_softfloat.tar.gz",
   54   "download_linux_mips64le": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mips64le_softfloat.tar.gz",
   55   "download_freebsd_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_386.tar.gz",
   56   "download_freebsd_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_amd64.tar.gz",
   57   "download_freebsd_arm": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv6.tar.gz",
   58   "download_freebsd_armv5": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv5.tar.gz",
   59   "download_freebsd_armv6": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv6.tar.gz",
   60   "download_freebsd_armv7": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv7.tar.gz",
   61   "download_freebsd_arm64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_arm64.tar.gz"
   62 }`
   63 
   64     l, lport := startHTTPServer(jsonData)
   65     defer func() { _ = l.Close() }()
   66 
   67     u := NewUpdater(Config{
   68         Client:        &http.Client{},
   69         VersionURL:    fmt.Sprintf("http://127.0.0.1:%d/", lport),
   70         OS:            "linux",
   71         Arch:          "arm",
   72         VersionString: "v0.103.0-beta1",
   73     })
   74 
   75     info, err := u.GetVersionResponse(false)
   76     assert.Nil(t, err)
   77     assert.Equal(t, "v0.103.0-beta2", info.NewVersion)
   78     assert.Equal(t, "AdGuard Home v0.103.0-beta2 is now available!", info.Announcement)
   79     assert.Equal(t, "https://github.com/AdguardTeam/AdGuardHome/internal/releases", info.AnnouncementURL)
   80     assert.Equal(t, "v0.0", info.SelfUpdateMinVersion)
   81     assert.True(t, info.CanAutoUpdate)
   82 
   83     _ = l.Close()
   84 
   85     // check cached
   86     _, err = u.GetVersionResponse(false)
   87     assert.Nil(t, err)
   88 }
   89 
   90 func TestUpdate(t *testing.T) {
   91     _ = os.Mkdir("aghtest", 0755)
   92     defer func() {
   93         _ = os.RemoveAll("aghtest")
   94     }()
   95 
   96     // create "current" files
   97     assert.Nil(t, ioutil.WriteFile("aghtest/AdGuardHome", []byte("AdGuardHome"), 0755))
   98     assert.Nil(t, ioutil.WriteFile("aghtest/README.md", []byte("README.md"), 0644))
   99     assert.Nil(t, ioutil.WriteFile("aghtest/LICENSE.txt", []byte("LICENSE.txt"), 0644))
  100     assert.Nil(t, ioutil.WriteFile("aghtest/AdGuardHome.yaml", []byte("AdGuardHome.yaml"), 0644))
  101 
  102     // start server for returning package file
  103     pkgData, err := ioutil.ReadFile("test/AdGuardHome.tar.gz")
  104     assert.Nil(t, err)
  105     l, lport := startHTTPServer(string(pkgData))
  106     defer func() { _ = l.Close() }()
  107 
  108     u := NewUpdater(Config{
  109         Client:        &http.Client{},
  110         PackageURL:    fmt.Sprintf("http://127.0.0.1:%d/AdGuardHome.tar.gz", lport),
  111         VersionString: "v0.103.0",
  112         NewVersion:    "v0.103.1",
  113         ConfigName:    "aghtest/AdGuardHome.yaml",
  114         WorkDir:       "aghtest",
  115     })
  116 
  117     assert.Nil(t, u.prepare())
  118     u.currentExeName = "aghtest/AdGuardHome"
  119     assert.Nil(t, u.downloadPackageFile(u.PackageURL, u.packageName))
  120     assert.Nil(t, u.unpack())
  121     // assert.Nil(t, u.check())
  122     assert.Nil(t, u.backup())
  123     assert.Nil(t, u.replace())
  124     u.clean()
  125 
  126     // check backup files
  127     d, err := ioutil.ReadFile("aghtest/agh-backup/AdGuardHome.yaml")
  128     assert.Nil(t, err)
  129     assert.Equal(t, "AdGuardHome.yaml", string(d))
  130 
  131     d, err = ioutil.ReadFile("aghtest/agh-backup/AdGuardHome")
  132     assert.Nil(t, err)
  133     assert.Equal(t, "AdGuardHome", string(d))
  134 
  135     // check updated files
  136     d, err = ioutil.ReadFile("aghtest/AdGuardHome")
  137     assert.Nil(t, err)
  138     assert.Equal(t, "1", string(d))
  139 
  140     d, err = ioutil.ReadFile("aghtest/README.md")
  141     assert.Nil(t, err)
  142     assert.Equal(t, "2", string(d))
  143 
  144     d, err = ioutil.ReadFile("aghtest/LICENSE.txt")
  145     assert.Nil(t, err)
  146     assert.Equal(t, "3", string(d))
  147 
  148     d, err = ioutil.ReadFile("aghtest/AdGuardHome.yaml")
  149     assert.Nil(t, err)
  150     assert.Equal(t, "AdGuardHome.yaml", string(d))
  151 }
  152 
  153 func TestUpdateWindows(t *testing.T) {
  154     _ = os.Mkdir("aghtest", 0755)
  155     defer func() {
  156         _ = os.RemoveAll("aghtest")
  157     }()
  158 
  159     // create "current" files
  160     assert.Nil(t, ioutil.WriteFile("aghtest/AdGuardHome.exe", []byte("AdGuardHome.exe"), 0755))
  161     assert.Nil(t, ioutil.WriteFile("aghtest/README.md", []byte("README.md"), 0644))
  162     assert.Nil(t, ioutil.WriteFile("aghtest/LICENSE.txt", []byte("LICENSE.txt"), 0644))
  163     assert.Nil(t, ioutil.WriteFile("aghtest/AdGuardHome.yaml", []byte("AdGuardHome.yaml"), 0644))
  164 
  165     // start server for returning package file
  166     pkgData, err := ioutil.ReadFile("test/AdGuardHome.zip")
  167     assert.Nil(t, err)
  168     l, lport := startHTTPServer(string(pkgData))
  169     defer func() { _ = l.Close() }()
  170 
  171     u := NewUpdater(Config{
  172         WorkDir:       "aghtest",
  173         Client:        &http.Client{},
  174         PackageURL:    fmt.Sprintf("http://127.0.0.1:%d/AdGuardHome.zip", lport),
  175         OS:            "windows",
  176         VersionString: "v0.103.0",
  177         NewVersion:    "v0.103.1",
  178         ConfigName:    "aghtest/AdGuardHome.yaml",
  179     })
  180 
  181     assert.Nil(t, u.prepare())
  182     u.currentExeName = "aghtest/AdGuardHome.exe"
  183     assert.Nil(t, u.downloadPackageFile(u.PackageURL, u.packageName))
  184     assert.Nil(t, u.unpack())
  185     // assert.Nil(t, u.check())
  186     assert.Nil(t, u.backup())
  187     assert.Nil(t, u.replace())
  188     u.clean()
  189 
  190     // check backup files
  191     d, err := ioutil.ReadFile("aghtest/agh-backup/AdGuardHome.yaml")
  192     assert.Nil(t, err)
  193     assert.Equal(t, "AdGuardHome.yaml", string(d))
  194 
  195     d, err = ioutil.ReadFile("aghtest/agh-backup/AdGuardHome.exe")
  196     assert.Nil(t, err)
  197     assert.Equal(t, "AdGuardHome.exe", string(d))
  198 
  199     // check updated files
  200     d, err = ioutil.ReadFile("aghtest/AdGuardHome.exe")
  201     assert.Nil(t, err)
  202     assert.Equal(t, "1", string(d))
  203 
  204     d, err = ioutil.ReadFile("aghtest/README.md")
  205     assert.Nil(t, err)
  206     assert.Equal(t, "2", string(d))
  207 
  208     d, err = ioutil.ReadFile("aghtest/LICENSE.txt")
  209     assert.Nil(t, err)
  210     assert.Equal(t, "3", string(d))
  211 
  212     d, err = ioutil.ReadFile("aghtest/AdGuardHome.yaml")
  213     assert.Nil(t, err)
  214     assert.Equal(t, "AdGuardHome.yaml", string(d))
  215 }