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

Commit

Permalink
Tests (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed May 5, 2016
1 parent 913880d commit 95437b7
Show file tree
Hide file tree
Showing 18 changed files with 333 additions and 84 deletions.
13 changes: 4 additions & 9 deletions keybase/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,9 @@ func (c *config) load() error {
}

func (c *config) loadFromPath(path string) error {
if _, serr := os.Stat(path); os.IsNotExist(serr) {
c.log.Warningf("Unable to load config, %s doesn't exist", path)
return serr
}

file, err := os.Open(path)
if err != nil {
return err
return fmt.Errorf("Unable to open config file: %s", err)
}
if file == nil {
return fmt.Errorf("No file")
Expand Down Expand Up @@ -119,7 +114,7 @@ func (c config) saveToPath(path string) error {
return fmt.Errorf("Error marshaling config: %s", err)
}
file := util.NewFile(path, b, 0600)
err = util.MakeParentDirs(path, 0700)
err = util.MakeParentDirs(path, 0700, c.log)
if err != nil {
return err
}
Expand All @@ -146,7 +141,7 @@ func (c *config) SetInstallID(installID string) error {
}

func (c config) updaterOptions() updater.UpdateOptions {
version := c.keybaseExecVersion()
version := c.keybaseVersion()
osVersion := c.osVersion()

return updater.UpdateOptions{
Expand All @@ -165,7 +160,7 @@ func (c config) keybasePath() string {
return c.pathToKeybase
}

func (c config) keybaseExecVersion() string {
func (c config) keybaseVersion() string {
result, err := command.Exec(c.keybasePath(), []string{"version", "-S"}, 5*time.Second, c.log)
if err != nil {
c.log.Warningf("Couldn't get keybase version: %s (%s)", err, result.CombinedOutput())
Expand Down
14 changes: 9 additions & 5 deletions keybase/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ import (
)

func testConfig(t *testing.T) (config, error) {
testAppName, err := util.RandString("KeybaseTest", 20)
if err != nil {
t.Fatalf("Unable to resolve test app name: %s", err)
}
testPathToKeybase := filepath.Join(os.Getenv("GOPATH"), "src/github.com/keybase/go-updater/test/keybase-version.sh")
return newConfig(testAppName, testPathToKeybase, log)
return newConfig("KeybaseTest", testPathToKeybase, log)
}

func TestConfig(t *testing.T) {
Expand All @@ -32,6 +28,7 @@ func TestConfig(t *testing.T) {
assert.NotEqual(t, path, "", "No config path")

configDir, err := cfg.dir()
defer util.RemoveFileAtPath(configDir)
assert.NoError(t, err)
assert.NotEqual(t, configDir, "", "Config dir empty")
defer util.RemoveFileAtPath(configDir)
Expand Down Expand Up @@ -144,3 +141,10 @@ func TestConfigPartial(t *testing.T) {
assert.False(t, auto)
assert.False(t, autoSet)
}

func TestKeybaseVersionInvalid(t *testing.T) {
testPathToKeybase := filepath.Join(os.Getenv("GOPATH"), "src/github.com/keybase/go-updater/test/err.sh")
cfg, _ := newConfig("KeybaseTest", testPathToKeybase, log)
version := cfg.keybaseVersion()
assert.Equal(t, "", version)
}
4 changes: 2 additions & 2 deletions keybase/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (c *context) UpdateOptions() updater.UpdateOptions {
}

// GetUpdateUI returns Update UI
func (c *context) GetUpdateUI() (updater.UpdateUI, error) {
return c, nil
func (c *context) GetUpdateUI() updater.UpdateUI {
return c
}

// GetLog returns log
Expand Down
11 changes: 8 additions & 3 deletions keybase/platform_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ import (
)

// destinationPath returns the app bundle path where this executable is located
func (c config) destinationPath() string {
func (c config) execPath() string {
path, err := osext.Executable()
if err != nil {
c.log.Warningf("Error trying to determine our destination path: %s", err)
c.log.Warningf("Error trying to determine our executable path: %s", err)
return ""
}
return appBundleForPath(path)
return path
}

// destinationPath returns the app bundle path where this executable is located
func (c config) destinationPath() string {
return appBundleForPath(c.execPath())
}

func appBundleForPath(path string) string {
Expand Down
31 changes: 31 additions & 0 deletions keybase/platform_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
package keybase

import (
"os"
"path/filepath"
"testing"
"time"

"github.com/keybase/go-updater"
"github.com/keybase/go-updater/process"
"github.com/stretchr/testify/assert"
)

Expand All @@ -20,3 +25,29 @@ func TestAppBundleForPath(t *testing.T) {
assert.Equal(t, "", appBundleForPath("/Applications/Keybase.ap"))
assert.Equal(t, "/Applications/Keybase.app", appBundleForPath("/Applications/Keybase.app/"))
}

type testConfigDarwin struct {
config
}

func (c testConfigDarwin) destinationPath() string {
return filepath.Join(os.Getenv("GOPATH"), "src/github.com/keybase/go-updater/test/Test.app")
}

func (c testConfigDarwin) promptPath() (string, error) {
return filepath.Join(os.Getenv("GOPATH"), "src/github.com/keybase/go-updater/test/prompt-apply.sh"), nil
}

func TestUpdatePrompt(t *testing.T) {
ctx := newContext(&testConfigDarwin{}, log)
resp, err := ctx.UpdatePrompt(testUpdate, testOptions, updater.UpdatePromptOptions{})
assert.NoError(t, err)
assert.NotNil(t, resp)
}

func TestRestart(t *testing.T) {
ctx := newContext(&testConfigDarwin{}, log)
err := ctx.Restart()
defer process.TerminateAll(ctx.config.destinationPath(), 200*time.Millisecond, log)
assert.NoError(t, err)
}
2 changes: 1 addition & 1 deletion saltpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func checkSender(key saltpack.BasePublicKey, validKIDs map[string]bool, log logg
if !validKIDs[skid] {
return fmt.Errorf("Unknown signer KID: %s", skid)
}
log.Debug("Valid KID: %s", skid)
log.Debugf("Valid KID: %s", skid)
return nil
}

Expand Down
7 changes: 4 additions & 3 deletions saltpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ const signature1 = `BEGIN KEYBASE SALTPACK DETACHED SIGNATURE.

var testZipPath = filepath.Join(os.Getenv("GOPATH"), "src/github.com/keybase/go-updater/test/test.zip")

// keybase sign -d -i test.zip
const testZipSignature = `BEGIN KEYBASE SALTPACK DETACHED SIGNATURE.
kXR7VktZdyH7rvq v5wcIkPOwDJ1n11 M8RnkLKQGO2f3Bb fzCeMYz4S6oxLAy
Cco4N255JFdUr6I 7XDXrDPRUsFPSRq RK1iOiDNoTlRIXC u4Zi7tLmajqLHUU
Eo0ng5CsVDR7e4Y DF4S9ioSsqGaQtX euRrI6tMO2EVmEx pQqidbB0aJsKLle
B. END KEYBASE SALTPACK DETACHED SIGNATURE.`
Cco4N255JFTAn8O 78IT0oJCfKGRxGx NGkFZsPsFKcFSjt pXUAgKgYFpxs8XM
2Nbn5qzg3t3rky3 bX8iMOXbqWLewah a7GnOT5bOlbzf8V 1uhiECJ0N6IvRBp
D. END KEYBASE SALTPACK DETACHED SIGNATURE.`

func TestSaltpackVerify(t *testing.T) {
reader := bytes.NewReader([]byte(message1))
Expand Down
3 changes: 3 additions & 0 deletions test/err.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

exit 1
Binary file modified test/test.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions test/update.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"asset": {
"name": "Test-1.2.3-400+abcdef.zip",
"url": "test.zip",
"digest": "4769edbb33b86cf960cebd39af33cb3baabf38f8e43a8dc89ff420faf1cf0d36",
"signature": "BEGIN KEYBASE SALTPACK DETACHED SIGNATURE. kXR7VktZdyH7rvq v5wcIkPOwDJ1n11 M8RnkLKQGO2f3Bb fzCeMYz4S6oxLAy Cco4N255JGM2Aac zvDRSSxB7AFoTH3 LkOrRM9MI6nxspS TVtkULTGEf8aapE jBO8KuFayegXr1R 0lOGS6msmG0IrLB bo8PlHUVqyqCygO Uh3ETm30U60aYXF o. END KEYBASE SALTPACK DETACHED SIGNATURE.\n",
"digest": "3a147f31b25a6027bda15367def6f4499e29a9b531855c0ac881a8f3a83a12b9",
"signature": "BEGIN KEYBASE SALTPACK DETACHED SIGNATURE. kXR7VktZdyH7rvq v5wcIkPOwDJ1n11 M8RnkLKQGO2f3Bb fzCeMYz4S6oxLAy Cco4N255JFTAn8O 78IT0oJCfKGRxGx NGkFZsPsFKcFSjt pXUAgKgYFpxs8XM 2Nbn5qzg3t3rky3 bX8iMOXbqWLewah a7GnOT5bOlbzf8V 1uhiECJ0N6IvRBp D. END KEYBASE SALTPACK DETACHED SIGNATURE.\n",
"localPath": ""
}
}
8 changes: 4 additions & 4 deletions update_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestUpdateChecker(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
updater, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL))
updater, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{})
assert.NoError(t, err)

checker := newUpdateChecker(updater, testUpdateCheckUI{promptDelay: 10 * time.Millisecond}, log, time.Millisecond)
Expand Down Expand Up @@ -50,8 +50,8 @@ func (u testUpdateCheckUI) AfterApply(update Update) error {
return nil
}

func (u testUpdateCheckUI) GetUpdateUI() (UpdateUI, error) {
return u, nil
func (u testUpdateCheckUI) GetUpdateUI() UpdateUI {
return u
}

func (u testUpdateCheckUI) Verify(update Update) error {
Expand All @@ -75,7 +75,7 @@ func (u testUpdateCheckUI) ReportSuccess(_ *Update, _ UpdateOptions) {}
func TestUpdateCheckerError(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
updater, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL))
updater, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{})
assert.NoError(t, err)

checker := NewUpdateChecker(updater, testUpdateCheckUI{verifyError: fmt.Errorf("Test verify error")}, log)
Expand Down
7 changes: 2 additions & 5 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type UpdateSource interface {

// Context defines state during an update session
type Context interface {
GetUpdateUI() (UpdateUI, error)
GetUpdateUI() UpdateUI
UpdateOptions() UpdateOptions
Verify(update Update) error
BeforeApply(update Update) error
Expand Down Expand Up @@ -202,10 +202,7 @@ func (u *Updater) promptForUpdateAction(ctx Context, update Update, options Upda
return UpdateActionAuto, nil
}

updateUI, err := ctx.GetUpdateUI()
if err != nil {
return UpdateActionError, err
}
updateUI := ctx.GetUpdateUI()

// If auto update never set, default to true
autoUpdate := !autoSet
Expand Down
Loading

0 comments on commit 95437b7

Please sign in to comment.