Skip to content

Commit 5205c98

Browse files
henvickyleconroy
authored andcommitted
Adding context version of Check and Response.Apply methods. (#8)
* Adding context version of Check and Response.Apply methods. * Adding Go 1.10-1.11 + tip on Travis. * Fixing Go versions on Travis. * sdk test: fixing assertion.
1 parent 20069b8 commit 5205c98

File tree

5 files changed

+115
-10
lines changed

5 files changed

+115
-10
lines changed

.travis.yml

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
language: go
22
go:
3-
- 1.4
4-
- 1.5
3+
- "1.4"
4+
- "1.5"
5+
- "1.7"
6+
- "1.9"
7+
- "1.10"
8+
- "1.11"
9+
- "tip"
10+
matrix:
11+
allow_failures:
12+
- go: tip
513
script: go test -v github.com/equinox-io/equinox github.com/equinox-io/equinox/proto

sdk.go

+30-7
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,24 @@ func (o *Options) SetPublicKeyPEM(pembytes []byte) error {
131131
// a successful check that found no update from other errors like a failed
132132
// network connection.
133133
func Check(appID string, opts Options) (Response, error) {
134-
var r Response
134+
var req, err = checkRequest(appID, &opts)
135135

136+
if err != nil {
137+
return Response{}, err
138+
}
139+
140+
return doCheckRequest(opts, req)
141+
}
142+
143+
func checkRequest(appID string, opts *Options) (*http.Request, error) {
136144
if opts.Channel == "" {
137145
opts.Channel = "stable"
138146
}
139147
if opts.TargetPath == "" {
140148
var err error
141149
opts.TargetPath, err = osext.Executable()
142150
if err != nil {
143-
return r, err
151+
return nil, err
144152
}
145153
}
146154
if opts.OS == "" {
@@ -172,12 +180,17 @@ func Check(appID string, opts Options) (Response, error) {
172180

173181
req, err := http.NewRequest("POST", opts.CheckURL, bytes.NewReader(payload))
174182
if err != nil {
175-
return r, err
183+
return nil, err
176184
}
185+
177186
req.Header.Set("Accept", fmt.Sprintf("application/json; q=1; version=%s; charset=utf-8", protocolVersion))
178187
req.Header.Set("Content-Type", "application/json; charset=utf-8")
179188
req.Close = true
180189

190+
return req, err
191+
}
192+
193+
func doCheckRequest(opts Options, req *http.Request) (r Response, err error) {
181194
resp, err := opts.HTTPClient.Do(req)
182195
if err != nil {
183196
return r, err
@@ -237,6 +250,16 @@ func computeChecksum(path string) string {
237250
//
238251
// Error is nil if and only if the entire update completes successfully.
239252
func (r Response) Apply() error {
253+
var req, opts, err = r.applyRequest()
254+
255+
if err != nil {
256+
return err
257+
}
258+
259+
return r.applyUpdate(req, opts)
260+
}
261+
262+
func (r Response) applyRequest() (*http.Request, update.Options, error) {
240263
opts := update.Options{
241264
TargetPath: r.opts.TargetPath,
242265
TargetMode: r.opts.TargetMode,
@@ -251,14 +274,14 @@ func (r Response) Apply() error {
251274
}
252275

253276
if err := opts.CheckPermissions(); err != nil {
254-
return err
277+
return nil, opts, err
255278
}
256279

257280
req, err := http.NewRequest("GET", r.downloadURL, nil)
258-
if err != nil {
259-
return err
260-
}
281+
return req, opts, err
282+
}
261283

284+
func (r Response) applyUpdate(req *http.Request, opts update.Options) error {
262285
// fetch the update
263286
req.Close = true
264287
resp, err := r.opts.HTTPClient.Do(req)

sdk_ctx.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// +build go1.7
2+
3+
package equinox
4+
5+
import (
6+
"context"
7+
)
8+
9+
// CheckContext is like Check but includes a context.
10+
func CheckContext(ctx context.Context, appID string, opts Options) (Response, error) {
11+
var req, err = checkRequest(appID, &opts)
12+
13+
if err != nil {
14+
return Response{}, err
15+
}
16+
17+
return doCheckRequest(opts, req.WithContext(ctx))
18+
}
19+
20+
// ApplyContext is like Apply but includes a context.
21+
func (r Response) ApplyContext(ctx context.Context) error {
22+
var req, opts, err = r.applyRequest()
23+
24+
if err != nil {
25+
return err
26+
}
27+
28+
return r.applyUpdate(req.WithContext(ctx), opts)
29+
}

sdk_ctx_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// +build go1.7
2+
3+
package equinox
4+
5+
import (
6+
"bytes"
7+
"context"
8+
"io/ioutil"
9+
"testing"
10+
"time"
11+
12+
"github.com/equinox-io/equinox/proto"
13+
)
14+
15+
func TestEndToEndContext(t *testing.T) {
16+
opts := setup(t, "TestEndtoEnd", proto.Response{
17+
Available: true,
18+
Release: proto.Release{
19+
Version: "0.1.2.3",
20+
Title: "Release Title",
21+
Description: "Release Description",
22+
CreateDate: time.Now(),
23+
},
24+
Checksum: newSHA,
25+
Signature: signature,
26+
})
27+
defer cleanup(opts)
28+
29+
resp, err := CheckContext(context.Background(), fakeAppID, opts)
30+
if err != nil {
31+
t.Fatalf("Failed check: %v", err)
32+
}
33+
err = resp.ApplyContext(context.Background())
34+
if err != nil {
35+
t.Fatalf("Failed apply: %v", err)
36+
}
37+
38+
buf, err := ioutil.ReadFile(opts.TargetPath)
39+
if err != nil {
40+
t.Fatalf("Failed to read file: %v", err)
41+
}
42+
if !bytes.Equal(buf, newFakeBinary) {
43+
t.Fatalf("Binary did not update to new expected value. Got %v, expected %v", buf, newFakeBinary)
44+
}
45+
}

sdk_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func setup(t *testing.T, name string, resp proto.Response) Options {
139139
}
140140
if resp.Available {
141141
if req.AppID != fakeAppID {
142-
t.Fatalf("Unexpected app ID. Got %v, expected %v", err)
142+
t.Fatalf("Unexpected app ID. Got %v, expected %v", req.AppID, fakeAppID)
143143
}
144144
if req.CurrentSHA256 != sha {
145145
t.Fatalf("Unexpected request SHA: %v", sha)

0 commit comments

Comments
 (0)