omaha: add basic but functional omaha server for testing

This server doesn't care about app id, versions, or really anything.
Once a payload has been set it will use it for all update requests.
This commit is contained in:
Michael Marineau 2016-09-13 15:58:20 -07:00
parent e5eb9eb583
commit 4d02220019
2 changed files with 208 additions and 0 deletions

100
omaha/trivial_server.go Normal file
View File

@ -0,0 +1,100 @@
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package omaha
import (
"fmt"
"net/http"
"path"
)
const pkg_prefix = "/packages/"
// trivialUpdater always responds with the given Update.
type trivialUpdater struct {
UpdaterStub
Update
}
func (tu *trivialUpdater) CheckUpdate(req *Request, app *AppRequest) (*Update, error) {
if len(tu.Manifest.Packages) == 0 {
return nil, NoUpdate
}
return &tu.Update, nil
}
// trivialHandler serves up a single file.
type trivialHandler struct {
Path string
}
func (th *trivialHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if th.Path == "" {
http.NotFound(w, r)
}
http.ServeFile(w, r, th.Path)
}
// TrivialServer is an extremely basic Omaha server that ignores all
// incoming metadata, always responding with the same update response.
// The update is constructed by calling AddPackage one or more times.
type TrivialServer struct {
*Server
tu trivialUpdater
}
func NewTrivialServer(addr string) (*TrivialServer, error) {
ts := TrivialServer{
tu: trivialUpdater{
Update: Update{
URL: URL{CodeBase: pkg_prefix},
},
},
}
s, err := NewServer(addr, &ts.tu)
if err != nil {
return nil, err
}
ts.Server = s
return &ts, nil
}
// AddPackage adds a new file to the update response.
// file is the local filesystem path, name is the final URL component.
func (ts *TrivialServer) AddPackage(file, name string) error {
// name may not include any path components
if path.Base(name) != name || name[0] == '.' {
return fmt.Errorf("invalid package name %q", name)
}
pkg, err := ts.tu.Manifest.AddPackageFromPath(file)
if err != nil {
return err
}
pkg.Name = name
// Insert the update_engine style postinstall action if
// this is the first (and probably only) package.
if len(ts.tu.Manifest.Actions) == 0 {
act := ts.tu.Manifest.AddAction("postinstall")
act.DisablePayloadBackoff = true
act.Sha256 = pkg.Sha256
}
ts.Mux.Handle(pkg_prefix+name, &trivialHandler{file})
return nil
}

View File

@ -0,0 +1,108 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package omaha
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"
)
func mkUpdateReq() (*bytes.Buffer, error) {
req := NewRequest()
app := req.AddApp(testAppId, testAppVer)
app.AddUpdateCheck()
buf := &bytes.Buffer{}
enc := xml.NewEncoder(buf)
enc.Indent("", "\t")
if err := enc.Encode(req); err != nil {
return nil, err
}
return buf, nil
}
func TestTrivialServer(t *testing.T) {
tmp, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer tmp.Close()
defer os.Remove(tmp.Name())
if _, err := tmp.WriteString("test"); err != nil {
t.Fatal(err)
}
s, err := NewTrivialServer(":0")
if err != nil {
t.Fatal(err)
}
defer s.Destroy()
if err := s.AddPackage(tmp.Name(), "update.gz"); err != nil {
t.Fatal(err)
}
go s.Serve()
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 ||
len(resp.Apps[0].UpdateCheck.URLs) != 1 ||
resp.Apps[0].UpdateCheck.Manifest == nil ||
len(resp.Apps[0].UpdateCheck.Manifest.Packages) != 1 {
t.Fatalf("unexpected response: %#v", resp)
}
pkgres, err := http.Get(resp.Apps[0].UpdateCheck.URLs[0].CodeBase +
resp.Apps[0].UpdateCheck.Manifest.Packages[0].Name)
if err != nil {
t.Fatal(err)
}
pkgdata, err := ioutil.ReadAll(pkgres.Body)
pkgres.Body.Close()
if err != nil {
t.Fatal(err)
}
if string(pkgdata) != "test" {
t.Fatalf("unexpected package data: %q", string(pkgdata))
}
}