Permalink
Browse files

migrate from 'assert' to 'gocheck' for unit tests

Change-Id: I26c47429b7f83d22bd7893d82c00a07115e8066f
  • Loading branch information...
1 parent c58e4fc commit e87510fe99a1fca76108d788b69e6aa47d28a958 @dougm dougm committed Oct 17, 2012
Showing with 497 additions and 448 deletions.
  1. +68 −54 api_test.go
  2. +22 −19 cli_test.go
  3. +57 −54 configmanager_test.go
  4. +81 −79 control_test.go
  5. +95 −84 eventmonitor_test.go
  6. +7 −3 gonit_test.go
  7. +23 −21 log_test.go
  8. +71 −60 process_test.go
  9. +48 −44 resourcemanager_test.go
  10. +25 −22 rpc_test.go
  11. +0 −8 test/helper/helper.go
View
@@ -4,17 +4,31 @@ package gonit_test
import (
"bytes"
- "github.com/bmizerany/assert"
+ "flag"
. "github.com/cloudfoundry/gonit"
"github.com/cloudfoundry/gonit/test/helper"
"io/ioutil"
+ . "launchpad.net/gocheck"
"math"
"net/rpc"
"os"
"strings"
"testing"
)
+type ApiSuite struct{}
+
+var _ = Suite(&ApiSuite{})
+
+// Hook up gocheck into the gotest runner.
+func Test(t *testing.T) {
+ // propagate 'go test -v' to '-gocheck.v'
+ if flag.Lookup("test.v").Value.String() == "true" {
+ flag.Lookup("gocheck.v").Value.Set("true")
+ }
+ TestingT(t)
+}
+
var rpcName = "TestAPI"
var config = &ConfigManager{}
@@ -26,71 +40,71 @@ func isActionError(err error) bool {
return strings.HasPrefix(err.Error(), "ActionError")
}
-func TestActionsNoConfig(t *testing.T) {
- err := helper.WithRpcServer(func(c *rpc.Client) {
+func (s *ApiSuite) TestActionsNoConfig(c *C) {
+ err := helper.WithRpcServer(func(client *rpc.Client) {
reply := &ActionResult{}
- err := c.Call(rpcName+".StartAll", "", reply)
- assert.Equal(t, nil, err)
+ err := client.Call(rpcName+".StartAll", "", reply)
+ c.Check(err, IsNil)
// Total == 0 since no processes are configured
- assert.Equal(t, 0, reply.Total)
- assert.Equal(t, 0, reply.Errors)
+ c.Check(0, Equals, reply.Total)
+ c.Check(0, Equals, reply.Errors)
reply = &ActionResult{}
- err = c.Call(rpcName+".StartProcess", "foo", reply)
- assert.NotEqual(t, nil, err)
- assert.Equal(t, true, isActionError(err))
+ err = client.Call(rpcName+".StartProcess", "foo", reply)
+ c.Check(err, NotNil)
+ c.Check(true, Equals, isActionError(err))
reply = &ActionResult{}
- err = c.Call(rpcName+".StartGroup", "bar", reply)
- assert.NotEqual(t, nil, err)
- assert.Equal(t, true, isActionError(err))
+ err = client.Call(rpcName+".StartGroup", "bar", reply)
+ c.Check(err, NotNil)
+ c.Check(true, Equals, isActionError(err))
})
- assert.Equal(t, nil, err)
+ c.Check(err, IsNil)
}
-func TestAbout(t *testing.T) {
- err := helper.WithRpcServer(func(c *rpc.Client) {
+func (s *ApiSuite) TestAbout(c *C) {
+ err := helper.WithRpcServer(func(client *rpc.Client) {
params := struct{}{}
about := About{}
- err := c.Call(rpcName+".About", &params, &about)
- assert.Equal(t, nil, err)
+ err := client.Call(rpcName+".About", &params, &about)
+ c.Check(err, IsNil)
- assert.Equal(t, VERSION, about.Version)
+ c.Check(VERSION, Equals, about.Version)
// need not be same type, just requires same fields
var gabout struct {
Version string
}
- err = c.Call(rpcName+".About", &params, &gabout)
- assert.Equal(t, nil, err)
+ err = client.Call(rpcName+".About", &params, &gabout)
+ c.Check(err, IsNil)
- assert.Equal(t, VERSION, gabout.Version)
+ c.Check(VERSION, Equals, gabout.Version)
})
- assert.Equal(t, nil, err)
+ c.Check(err, IsNil)
}
-func tmpPidFile(t *testing.T, pid int) string {
+func tmpPidFile(c *C, pid int) string {
file, err := ioutil.TempFile("", "api_test")
if err != nil {
- t.Error(err)
+ c.Error(err)
}
if err := WritePidFile(pid, file.Name()); err != nil {
- t.Fatal(err)
+ c.Fatal(err)
}
return file.Name()
}
// simple exercise of CliFormatters
-func testCliPrint(t *testing.T, value CliFormatter) {
+func testCliPrint(c *C, value CliFormatter) {
buf := new(bytes.Buffer)
value.Print(buf)
- assert.NotEqual(t, 0, buf.Len())
+ c.Check(0, Not(Equals), buf.Len())
}
-func TestStatus(t *testing.T) {
+func (s *ApiSuite) TestStatus(c *C) {
nprocesses := 0
type testProcess struct {
@@ -127,7 +141,7 @@ func TestStatus(t *testing.T) {
for _, group := range groups {
for _, process := range group.processes {
// write pidfile for use by process.IsRunning()
- pidfile := tmpPidFile(t, process.pid)
+ pidfile := tmpPidFile(c, process.pid)
defer os.Remove(pidfile)
config.AddProcess(group.name, &Process{
@@ -139,55 +153,55 @@ func TestStatus(t *testing.T) {
}
}
- err := helper.WithRpcServer(func(c *rpc.Client) {
+ err := helper.WithRpcServer(func(client *rpc.Client) {
statusGroup := rpcName + ".StatusGroup"
statusProcess := rpcName + ".StatusProcess"
// should get an error if group does not exist
- err := c.Call(statusGroup, "enogroup", &ProcessGroupStatus{})
- assert.NotEqual(t, nil, err)
+ err := client.Call(statusGroup, "enogroup", &ProcessGroupStatus{})
+ c.Check(err, NotNil)
// should get an error if process does not exist
- err = c.Call(statusProcess, "enoprocess", &ProcessStatus{})
- assert.NotEqual(t, nil, err)
+ err = client.Call(statusProcess, "enoprocess", &ProcessStatus{})
+ c.Check(err, NotNil)
for _, group := range groups {
gstat := &ProcessGroupStatus{}
- err := c.Call(statusGroup, group.name, gstat)
- assert.Equal(t, nil, err)
- assert.Equal(t, len(group.processes), len(gstat.Group))
- testCliPrint(t, gstat)
+ err := client.Call(statusGroup, group.name, gstat)
+ c.Check(err, IsNil)
+ c.Check(len(group.processes), Equals, len(gstat.Group))
+ testCliPrint(c, gstat)
for _, process := range group.processes {
stat := &ProcessStatus{}
- err := c.Call(statusProcess, process.name, stat)
- assert.Equal(t, nil, err)
+ err := client.Call(statusProcess, process.name, stat)
+ c.Check(err, IsNil)
running := stat.Summary.Running
- assert.Equal(t, process.running, running)
- testCliPrint(t, stat)
+ c.Check(process.running, Equals, running)
+ testCliPrint(c, stat)
if !running {
continue
}
- assert.Equal(t, process.pid, stat.Pid)
- assert.Equal(t, process.ppid, stat.State.Ppid)
+ c.Check(process.pid, Equals, stat.Pid)
+ c.Check(process.ppid, Equals, stat.State.Ppid)
}
}
all := &ProcessGroupStatus{}
- err = c.Call(rpcName+".StatusAll", "", all)
- assert.Equal(t, nil, err)
- assert.Equal(t, nprocesses, len(all.Group))
- testCliPrint(t, all)
+ err = client.Call(rpcName+".StatusAll", "", all)
+ c.Check(err, IsNil)
+ c.Check(nprocesses, Equals, len(all.Group))
+ testCliPrint(c, all)
summary := &Summary{}
- err = c.Call(rpcName+".Summary", "", summary)
- assert.Equal(t, nil, err)
- assert.Equal(t, nprocesses, len(summary.Processes))
- testCliPrint(t, summary)
+ err = client.Call(rpcName+".Summary", "", summary)
+ c.Check(err, IsNil)
+ c.Check(nprocesses, Equals, len(summary.Processes))
+ testCliPrint(c, summary)
})
- assert.Equal(t, nil, err)
+ c.Check(err, IsNil)
}
View
@@ -4,13 +4,16 @@ package gonit_test
import (
"fmt"
- "github.com/bmizerany/assert"
. "github.com/cloudfoundry/gonit"
"github.com/cloudfoundry/gonit/test/helper"
+ . "launchpad.net/gocheck"
"net/rpc"
- "testing"
)
+type CliSuite struct{}
+
+var _ = Suite(&CliSuite{})
+
var mockAPI = &MockAPI{api: &API{}}
func init() {
@@ -44,49 +47,49 @@ func (m *MockAPI) About(args interface{}, about *About) error {
}
// run tests via RPC or local reflection
-func runTests(t *testing.T, client CliClient) {
+func runTests(c *C, client CliClient) {
method, name := RpcArgs("stop", "foo", false)
_, err := client.Call(method, name)
- assert.Equal(t, nil, err)
+ c.Check(err, IsNil)
method, name = RpcArgs("stop", "bar", false)
_, err = client.Call(method, name)
- assert.NotEqual(t, nil, err)
+ c.Check(err, NotNil)
method, name = RpcArgs("unmonitor", "all", false)
_, err = client.Call(method, name)
- assert.Equal(t, nil, err)
+ c.Check(err, IsNil)
method, name = RpcArgs("start", "vcap", true)
_, err = client.Call(method, name)
- assert.Equal(t, nil, err)
+ c.Check(err, IsNil)
method, name = RpcArgs("start", "bar", true)
_, err = client.Call(method, name)
- assert.NotEqual(t, nil, err)
+ c.Check(err, NotNil)
method, name = RpcArgs("about", "", false)
reply, err := client.Call(method, name)
- assert.Equal(t, nil, err)
+ c.Check(err, IsNil)
about, ok := reply.(*About)
- assert.Equal(t, true, ok)
- assert.Equal(t, VERSION, about.Version)
+ c.Check(true, Equals, ok)
+ c.Check(VERSION, Equals, about.Version)
reply, err = client.Call("ENOENT", "")
- assert.NotEqual(t, nil, err)
+ c.Check(err, NotNil)
}
-func TestRemote(t *testing.T) {
- err := helper.WithRpcServer(func(c *rpc.Client) {
- client := NewRemoteClient(c, mockAPI)
- runTests(t, client)
+func (s *CliSuite) TestRemote(c *C) {
+ err := helper.WithRpcServer(func(rc *rpc.Client) {
+ client := NewRemoteClient(rc, mockAPI)
+ runTests(c, client)
})
- assert.Equal(t, nil, err)
+ c.Check(err, IsNil)
}
-func TestLocal(t *testing.T) {
+func (s *CliSuite) TestLocal(c *C) {
client := NewLocalClient(mockAPI)
- runTests(t, client)
+ runTests(c, client)
}
Oops, something went wrong.

0 comments on commit e87510f

Please sign in to comment.