Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Merge 6ca970b into 2105e6b
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Apr 27, 2016
2 parents 2105e6b + 6ca970b commit fc8a153
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 31 deletions.
2 changes: 2 additions & 0 deletions keybase/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ type context struct {
// endpoints define all the url locations for reporting, etc
type endpoints struct {
update string
action string
err string
}

var defaultEndpoints = endpoints{
update: "https://keybase.io/_/api/1.0/pkg/update.json",
action: "https://keybase.io/_/api/1.0/pkg/act.json",
err: "https://keybase.io/_/api/1.0/pkg/error.json",
}

Expand Down
16 changes: 16 additions & 0 deletions keybase/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ func (c context) reportError(updateErr updater.Error, options updater.UpdateOpti
return c.report(data, uri, timeout)
}

// ReportAction notifies the API server of a client updater action
func (c context) ReportAction(action updater.UpdateAction, options updater.UpdateOptions) {
if err := c.reportAction(action, options, defaultEndpoints.action, time.Minute); err != nil {
c.log.Warningf("Error notifying about an action (%s): %s", action, err)
}
}

func (c context) reportAction(action updater.UpdateAction, options updater.UpdateOptions, uri string, timeout time.Duration) error {
data := url.Values{}
data.Add("install_id", options.InstallID)
data.Add("version", options.Version)
data.Add("upd_version", options.UpdaterVersion)
data.Add("action", action.String())
return c.report(data, uri, timeout)
}

func (c context) report(data url.Values, uri string, timeout time.Duration) error {
req, err := http.NewRequest("POST", uri, bytes.NewBufferString(data.Encode()))
if err != nil {
Expand Down
48 changes: 32 additions & 16 deletions keybase/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package keybase

import (
"fmt"
"net/url"
"strings"
"testing"
"time"
Expand All @@ -14,22 +15,23 @@ import (
"github.com/stretchr/testify/require"
)

func TestNotifyError(t *testing.T) {
var testOptions = updater.UpdateOptions{
InstallID: "deadbeef",
Version: "1.2.3-400+abcdef",
}

func TestReportError(t *testing.T) {
server := newServer("{}")
defer server.Close()

options := updater.UpdateOptions{
InstallID: "deadbeef",
Version: "1.2.3-400+abcdef",
}
updateErr := updater.NewError(updater.PromptError, fmt.Errorf("Test error"))
ctx := testContext(t)
err := ctx.reportError(updateErr, options, server.URL, 5*time.Millisecond)
err := ctx.reportError(updateErr, testOptions, server.URL, 5*time.Millisecond)
assert.NoError(t, err)
}

func TestNotifyErrorEmpty(t *testing.T) {
server := newServer(updateJSONResponse)
func TestReportErrorEmpty(t *testing.T) {
server := newServer("{}")
defer server.Close()

updateErr := updater.NewError(updater.UnknownError, nil)
Expand All @@ -39,25 +41,39 @@ func TestNotifyErrorEmpty(t *testing.T) {
assert.NoError(t, err)
}

func TestNotifyBadResponse(t *testing.T) {
func TestReportBadResponse(t *testing.T) {
server := newServerForError(fmt.Errorf("Bad response"))
defer server.Close()

updateErr := updater.NewError(updater.UnknownError, nil)
emptyOptions := updater.UpdateOptions{}
ctx := testContext(t)
err := ctx.reportError(updateErr, emptyOptions, server.URL, 5*time.Millisecond)
err := ctx.report(url.Values{}, server.URL, 5*time.Millisecond)
assert.EqualError(t, err, "Notify error returned bad HTTP status 500 Internal Server Error")
}

func TestNotifyErrorTimeout(t *testing.T) {
func TestReportTimeout(t *testing.T) {
server := newServerWithDelay(updateJSONResponse, 5*time.Millisecond)
defer server.Close()

updateErr := updater.NewError(updater.UnknownError, nil)
emptyOptions := updater.UpdateOptions{}
ctx := testContext(t)
err := ctx.reportError(updateErr, emptyOptions, server.URL, 2*time.Millisecond)
err := ctx.report(url.Values{}, server.URL, 2*time.Millisecond)
require.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "net/http: request canceled"))
}

func TestReportActionApply(t *testing.T) {
server := newServer("{}")
defer server.Close()

ctx := testContext(t)
err := ctx.reportAction(updater.UpdateActionApply, testOptions, server.URL, 5*time.Millisecond)
assert.NoError(t, err)
}

func TestReportActionEmpty(t *testing.T) {
server := newServer("{}")
defer server.Close()

ctx := testContext(t)
err := ctx.reportAction("", updater.UpdateOptions{}, server.URL, 5*time.Millisecond)
assert.NoError(t, err)
}
5 changes: 5 additions & 0 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ const (
UpdateActionError UpdateAction = "error"
)

// String is a unique string label for the action
func (u UpdateAction) String() string {
return string(u)
}

// UpdatePromptOptions are the options for UpdatePrompt
type UpdatePromptOptions struct {
AutoUpdate bool `json:"autoUpdate"`
Expand Down
3 changes: 2 additions & 1 deletion update_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ func (u testUpdateCheckUI) UpdateOptions() UpdateOptions {
return newDefaultTestUpdateOptions()
}

func (u testUpdateCheckUI) ReportError(_ Error, _ UpdateOptions) {}
func (u testUpdateCheckUI) ReportError(_ Error, _ UpdateOptions) {}
func (u testUpdateCheckUI) ReportAction(_ UpdateAction, _ UpdateOptions) {}
34 changes: 23 additions & 11 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Context interface {
AfterApply(update Update) error
Restart() error
ReportError(err Error, options UpdateOptions)
ReportAction(action UpdateAction, options UpdateOptions)
}

// Config defines configuration for the Updater
Expand Down Expand Up @@ -89,9 +90,12 @@ func (u *Updater) update(ctx Context, options UpdateOptions) (*Update, *Error) {
return update, promptErrPtr(err)
}
switch updateAction {
case UpdateActionApply, UpdateActionAuto:
// Continue
case UpdateActionApply:
ctx.ReportAction(UpdateActionApply, options)
case UpdateActionAuto:
ctx.ReportAction(UpdateActionAuto, options)
case UpdateActionSnooze:
ctx.ReportAction(UpdateActionSnooze, options)
return update, cancelErrPtr(fmt.Errorf("Snoozed update"))
case UpdateActionCancel:
return update, cancelErrPtr(fmt.Errorf("Canceled by user"))
Expand Down Expand Up @@ -126,23 +130,31 @@ func (u *Updater) update(ctx Context, options UpdateOptions) (*Update, *Error) {
return update, verifyErrPtr(err)
}

if err := ctx.BeforeApply(*update); err != nil {
return update, applyErrPtr(err)
if err := u.apply(ctx, *update, options); err != nil {
return update, err
}

if err := u.platformApplyUpdate(*update, options); err != nil {
return update, applyErrPtr(err)
if err := ctx.Restart(); err != nil {
return update, restartErrPtr(err)
}

if err := ctx.AfterApply(*update); err != nil {
return update, applyErrPtr(err)
return update, nil
}

func (u *Updater) apply(ctx Context, update Update, options UpdateOptions) *Error {
if err := ctx.BeforeApply(update); err != nil {
return applyErrPtr(err)
}

if err := ctx.Restart(); err != nil {
return update, restartErrPtr(err)
if err := u.platformApplyUpdate(update, options); err != nil {
return applyErrPtr(err)
}

return update, nil
if err := ctx.AfterApply(update); err != nil {
return applyErrPtr(err)
}

return nil
}

// downloadAsset will download the update to a temporary path (if not cached),
Expand Down
15 changes: 12 additions & 3 deletions updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ func newTestContext(options UpdateOptions) *testUpdateUI {
}

type testUpdateUI struct {
options UpdateOptions
errReported *Error
options UpdateOptions
errReported *Error
actionReported UpdateAction
}

func (u testUpdateUI) UpdatePrompt(_ Update, _ UpdateOptions, _ UpdatePromptOptions) (*UpdatePromptResponse, error) {
Expand Down Expand Up @@ -66,6 +67,10 @@ func (u *testUpdateUI) ReportError(err Error, options UpdateOptions) {
u.errReported = &err
}

func (u *testUpdateUI) ReportAction(action UpdateAction, options UpdateOptions) {
u.actionReported = action
}

func (u testUpdateUI) UpdateOptions() UpdateOptions {
return u.options
}
Expand Down Expand Up @@ -150,7 +155,8 @@ func TestUpdater(t *testing.T) {

upr, err := newTestUpdaterWithServer(t, testServer)
assert.NoError(t, err)
update, err := upr.Update(newTestContext(newDefaultTestUpdateOptions()))
ctx := newTestContext(newDefaultTestUpdateOptions())
update, err := upr.Update(ctx)
require.NoError(t, err)
require.NotNil(t, update)
t.Logf("Update: %#v\n", *update)
Expand All @@ -161,6 +167,9 @@ func TestUpdater(t *testing.T) {
assert.True(t, auto)
assert.True(t, autoSet)
assert.Equal(t, "deadbeef", upr.config.GetInstallID())

assert.Nil(t, ctx.errReported)
assert.Equal(t, ctx.actionReported, UpdateActionApply)
}

func TestUpdaterSourceError(t *testing.T) {
Expand Down

0 comments on commit fc8a153

Please sign in to comment.