Merge pull request #27 from yifan-gu/smarter_trivial_server
omaha: Make the trivial server to only return newer packages.
This commit is contained in:
commit
22285faf56
2 changed files with 55 additions and 1 deletions
|
@ -18,6 +18,8 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"github.com/blang/semver"
|
||||
)
|
||||
|
||||
const pkg_prefix = "/packages/"
|
||||
|
@ -32,7 +34,22 @@ func (tu *trivialUpdater) CheckUpdate(req *Request, app *AppRequest) (*Update, e
|
|||
if len(tu.Manifest.Packages) == 0 {
|
||||
return nil, NoUpdate
|
||||
}
|
||||
|
||||
v1, err := semver.Make(app.Version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v2, err := semver.Make(tu.Manifest.Version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if v1.LT(v2) {
|
||||
return &tu.Update, nil
|
||||
}
|
||||
|
||||
return nil, NoUpdate
|
||||
}
|
||||
|
||||
// trivialHandler serves up a single file.
|
||||
|
@ -98,3 +115,8 @@ func (ts *TrivialServer) AddPackage(file, name string) error {
|
|||
ts.Mux.Handle(pkg_prefix+name, &trivialHandler{file})
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetVersion sets the manifest's version with the provided one.
|
||||
func (ts *TrivialServer) SetVersion(version string) {
|
||||
ts.tu.Manifest.Version = version
|
||||
}
|
||||
|
|
|
@ -59,6 +59,7 @@ func TestTrivialServer(t *testing.T) {
|
|||
if err := s.AddPackage(tmp.Name(), "update.gz"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetVersion(testAppVer)
|
||||
go s.Serve()
|
||||
|
||||
buf, err := mkUpdateReq()
|
||||
|
@ -82,6 +83,37 @@ func TestTrivialServer(t *testing.T) {
|
|||
t.Fatalf("failed to parse body: %v", err)
|
||||
}
|
||||
|
||||
// Should get zero update because the version is already the latest.
|
||||
if len(resp.Apps) != 1 ||
|
||||
resp.Apps[0].UpdateCheck == nil ||
|
||||
resp.Apps[0].UpdateCheck.Status != NoUpdate {
|
||||
t.Fatalf("unexpected response: %#v", resp)
|
||||
}
|
||||
|
||||
// Should get an update now.
|
||||
s.SetVersion("999.999.999")
|
||||
|
||||
buf, err = mkUpdateReq()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
endpoint = fmt.Sprintf("http://%s/v1/update/", s.Addr())
|
||||
res, err = http.Post(endpoint, "text/xml", buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != 200 {
|
||||
t.Fatalf("failed to post: %v", res.Status)
|
||||
}
|
||||
|
||||
dec = xml.NewDecoder(res.Body)
|
||||
resp = &Response{}
|
||||
if err := dec.Decode(resp); err != nil {
|
||||
t.Fatalf("failed to parse body: %v", err)
|
||||
}
|
||||
|
||||
if len(resp.Apps) != 1 ||
|
||||
resp.Apps[0].UpdateCheck == nil ||
|
||||
resp.Apps[0].UpdateCheck.Status != UpdateOK ||
|
||||
|
|
Loading…
Reference in a new issue