Skip to content

Commit

Permalink
Merge pull request canonical#1558 from pedronis/cleanup-cmd-snap-test…
Browse files Browse the repository at this point in the history
…-suite

client,cmd/snap: cleanup cmd/snap test suite, add extra args test

This cleans up the cmd/snap test suite:

- don't run most test twice by fixing the suite inheritance
- don't make TestWait take a long time, by exposing and using mocking the client retry times

in the process also add a test about extra args checks
  • Loading branch information
pedronis authored Jul 16, 2016
2 parents 75c4e45 + 3753c78 commit 7cedb2b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
12 changes: 12 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ var (
doTimeout = 5 * time.Second
)

// MockDoRetry mocks the delays used by the do retry loop.
func MockDoRetry(retry, timeout time.Duration) (restore func()) {
oldRetry := doRetry
oldTimeout := doTimeout
doRetry = retry
doTimeout = timeout
return func() {
doRetry = oldRetry
doTimeout = oldTimeout
}
}

// do performs a request and decodes the resulting json into the given
// value. It's low-level, for testing/experimenting only; you should
// usually use a higher level interface that builds on this.
Expand Down
13 changes: 0 additions & 13 deletions client/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package client
import (
"io"
"net/url"
"time"
)

// SetDoer sets the client's doer to the given one
Expand All @@ -35,18 +34,6 @@ func (client *Client) Do(method, path string, query url.Values, body io.Reader,
return client.do(method, path, query, nil, body, v)
}

// MockDoRetry mocks the delays used by the do retry loop.
func MockDoRetry(retry, timeout time.Duration) (restore func()) {
oldRetry := doRetry
oldTimeout := doTimeout
doRetry = retry
doTimeout = timeout
return func() {
doRetry = oldRetry
doTimeout = oldTimeout
}
}

// expose parseError for testing
var ParseErrorInTest = parseError

Expand Down
21 changes: 14 additions & 7 deletions cmd/snap/cmd_snap_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"gopkg.in/check.v1"

"github.com/snapcore/snapd/client"
snap "github.com/snapcore/snapd/cmd/snap"
)

Expand Down Expand Up @@ -73,25 +74,31 @@ func (t *snapOpTestServer) handle(w http.ResponseWriter, r *http.Request) {
}

type SnapOpSuite struct {
SnapSuite
BaseSnapSuite

restorePollTime func()
srv snapOpTestServer
restoreAll func()
srv snapOpTestServer
}

func (s *SnapOpSuite) SetUpTest(c *check.C) {
s.SnapSuite.SetUpTest(c)
s.BaseSnapSuite.SetUpTest(c)

restoreClientRetry := client.MockDoRetry(time.Millisecond, 10*time.Millisecond)
restorePollTime := snap.MockPollTime(time.Millisecond)
s.restoreAll = func() {
restoreClientRetry()
restorePollTime()
}

s.restorePollTime = snap.MockPollTime(time.Millisecond)
s.srv = snapOpTestServer{
c: c,
total: 4,
}
}

func (s *SnapOpSuite) TearDownTest(c *check.C) {
s.restorePollTime()
s.SnapSuite.TearDownTest(c)
s.restoreAll()
s.BaseSnapSuite.TearDownTest(c)
}

func (s *SnapOpSuite) TestWait(c *check.C) {
Expand Down
30 changes: 22 additions & 8 deletions cmd/snap/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ import (
// Hook up check.v1 into the "go test" runner
func Test(t *testing.T) { TestingT(t) }

type SnapSuite struct {
type BaseSnapSuite struct {
testutil.BaseTest
stdin *bytes.Buffer
stdout *bytes.Buffer
stderr *bytes.Buffer
}

var _ = Suite(&SnapSuite{})

func (s *SnapSuite) SetUpTest(c *C) {
func (s *BaseSnapSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
s.stdin = bytes.NewBuffer(nil)
s.stdout = bytes.NewBuffer(nil)
Expand All @@ -58,28 +56,34 @@ func (s *SnapSuite) SetUpTest(c *C) {
snap.Stderr = s.stderr
}

func (s *SnapSuite) TearDownTest(c *C) {
func (s *BaseSnapSuite) TearDownTest(c *C) {
snap.Stdin = os.Stdin
snap.Stdout = os.Stdout
snap.Stderr = os.Stderr
s.BaseTest.TearDownTest(c)
}

func (s *SnapSuite) Stdout() string {
func (s *BaseSnapSuite) Stdout() string {
return s.stdout.String()
}

func (s *SnapSuite) Stderr() string {
func (s *BaseSnapSuite) Stderr() string {
return s.stderr.String()
}

func (s *SnapSuite) RedirectClientToTestServer(handler func(http.ResponseWriter, *http.Request)) {
func (s *BaseSnapSuite) RedirectClientToTestServer(handler func(http.ResponseWriter, *http.Request)) {
server := httptest.NewServer(http.HandlerFunc(handler))
s.BaseTest.AddCleanup(func() { server.Close() })
snap.ClientConfig.BaseURL = server.URL
s.BaseTest.AddCleanup(func() { snap.ClientConfig.BaseURL = "" })
}

type SnapSuite struct {
BaseSnapSuite
}

var _ = Suite(&SnapSuite{})

// DecodedRequestBody returns the JSON-decoded body of the request.
func DecodedRequestBody(c *C, r *http.Request) map[string]interface{} {
var body map[string]interface{}
Expand Down Expand Up @@ -123,3 +127,13 @@ func (s *SnapSuite) TestAccessDeniedHint(c *C) {
err := snap.RunMain()
c.Assert(err, ErrorMatches, `access denied \(snap login --help\)`)
}

func (s *SnapSuite) TestExtraArgs(c *C) {
origArgs := os.Args
defer func() { os.Args = origArgs }()

os.Args = []string{"snap", "abort", "1", "xxx", "zzz"}

err := snap.RunMain()
c.Assert(err, ErrorMatches, `too many arguments for command`)
}

0 comments on commit 7cedb2b

Please sign in to comment.