Skip to content

Commit

Permalink
Adding context version of Check and Response.Apply methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
henvic committed Nov 1, 2017
1 parent f24972f commit 1e7f0a2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ language: go
go:
- 1.4
- 1.5
- 1.7
- 1.9
script: go test -v github.com/equinox-io/equinox github.com/equinox-io/equinox/proto
36 changes: 29 additions & 7 deletions sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,24 @@ func (o *Options) SetPublicKeyPEM(pembytes []byte) error {
// a successful check that found no update from other errors like a failed
// network connection.
func Check(appID string, opts Options) (Response, error) {
var r Response
var req, err = checkRequest(appID, &opts)

if err != nil {
return Response{}, err
}

return doCheckRequest(opts, req)
}

func checkRequest(appID string, opts *Options) (*http.Request, error) {
if opts.Channel == "" {
opts.Channel = "stable"
}
if opts.TargetPath == "" {
var err error
opts.TargetPath, err = osext.Executable()
if err != nil {
return r, err
return nil, err
}
}
if opts.OS == "" {
Expand Down Expand Up @@ -172,11 +180,15 @@ func Check(appID string, opts Options) (Response, error) {

req, err := http.NewRequest("POST", opts.CheckURL, bytes.NewReader(payload))
if err != nil {
return r, err
return nil, err
}

req.Header.Set("Accept", fmt.Sprintf("application/json; q=1; version=%s; charset=utf-8", protocolVersion))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
return req, err
}

func doCheckRequest(opts Options, req *http.Request) (r Response, err error) {
resp, err := opts.HTTPClient.Do(req)
if err != nil {
return r, err
Expand Down Expand Up @@ -236,6 +248,16 @@ func computeChecksum(path string) string {
//
// Error is nil if and only if the entire update completes successfully.
func (r Response) Apply() error {
var req, opts, err = r.applyRequest()

if err != nil {
return err
}

return r.applyUpdate(req, opts)
}

func (r Response) applyRequest() (*http.Request, update.Options, error) {
opts := update.Options{
TargetPath: r.opts.TargetPath,
TargetMode: r.opts.TargetMode,
Expand All @@ -250,14 +272,14 @@ func (r Response) Apply() error {
}

if err := opts.CheckPermissions(); err != nil {
return err
return nil, opts, err
}

req, err := http.NewRequest("GET", r.downloadURL, nil)
if err != nil {
return err
}
return req, opts, err
}

func (r Response) applyUpdate(req *http.Request, opts update.Options) error {
// fetch the update
resp, err := r.opts.HTTPClient.Do(req)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions sdk_ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// +build go1.7

package equinox

import (
"context"
)

// CheckContext is like Check but includes a context.
func CheckContext(ctx context.Context, appID string, opts Options) (Response, error) {
var req, err = checkRequest(appID, &opts)

if err != nil {
return Response{}, err
}

req = req.WithContext(ctx)
return doCheckRequest(opts, req)
}

// ApplyContext is like Apply but includes a context.
func (r Response) ApplyContext(ctx context.Context) error {
var req, opts, err = r.applyRequest()

if err != nil {
return err
}

req = req.WithContext(ctx)

return r.applyUpdate(req, opts)
}
45 changes: 45 additions & 0 deletions sdk_ctx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// +build go1.7

package equinox

import (
"bytes"
"context"
"io/ioutil"
"testing"
"time"

"github.com/equinox-io/equinox/proto"
)

func TestEndToEndContext(t *testing.T) {
opts := setup(t, "TestEndtoEnd", proto.Response{
Available: true,
Release: proto.Release{
Version: "0.1.2.3",
Title: "Release Title",
Description: "Release Description",
CreateDate: time.Now(),
},
Checksum: newSHA,
Signature: signature,
})
defer cleanup(opts)

resp, err := CheckContext(context.Background(), fakeAppID, opts)
if err != nil {
t.Fatalf("Failed check: %v", err)
}
err = resp.ApplyContext(context.Background())
if err != nil {
t.Fatalf("Failed apply: %v", err)
}

buf, err := ioutil.ReadFile(opts.TargetPath)
if err != nil {
t.Fatalf("Failed to read file: %v", err)
}
if !bytes.Equal(buf, newFakeBinary) {
t.Fatalf("Binary did not update to new expected value. Got %v, expected %v", buf, newFakeBinary)
}
}

0 comments on commit 1e7f0a2

Please sign in to comment.