5
gc "launchpad.net/gocheck"
9
func Test(t *testing.T) {
13
var _ = gc.Suite(&ServiceSuite{})
15
type ServiceSuite struct {
19
func (s *ServiceSuite) SetUpTest(c *gc.C) {
20
s.ts = newTestService()
21
// This hook is called based on the function name.
22
s.ts.RegisterControlPoint("foo", functionControlHook)
23
// This hook is called based on a user specified hook name.
24
s.ts.RegisterControlPoint("foobar", namedControlHook)
27
type testService struct {
32
func newTestService() *testService {
34
TestService: TestService{
35
ControlHooks: make(map[string]ControlProcessor),
40
func functionControlHook(s ServiceControl, args ...interface{}) error {
41
label := args[0].(string)
42
returnError := args[1].(bool)
44
return fmt.Errorf("An error occurred")
46
s.(*testService).label = label
50
func namedControlHook(s ServiceControl, args ...interface{}) error {
51
s.(*testService).label = "foobar"
55
func (s *testService) foo(label string, returnError bool) error {
56
if err := s.ProcessFunctionHook(s, label, returnError); err != nil {
62
func (s *testService) bar() error {
63
if err := s.ProcessControlHook("foobar", s); err != nil {
69
func (s *ServiceSuite) TestFunctionHookNoError(c *gc.C) {
70
err := s.ts.foo("success", false)
71
c.Assert(err, gc.IsNil)
72
c.Assert(s.ts.label, gc.Equals, "success")
75
func (s *ServiceSuite) TestHookWithError(c *gc.C) {
76
err := s.ts.foo("success", true)
77
c.Assert(err, gc.Not(gc.IsNil))
78
c.Assert(s.ts.label, gc.Equals, "")
81
func (s *ServiceSuite) TestNamedHook(c *gc.C) {
83
c.Assert(err, gc.IsNil)
84
c.Assert(s.ts.label, gc.Equals, "foobar")
87
func (s *ServiceSuite) TestHookCleanup(c *gc.C) {
88
// Manually delete the existing control point.
89
s.ts.RegisterControlPoint("foo", nil)
90
// Register a new hook and ensure it works.
91
cleanup := s.ts.RegisterControlPoint("foo", functionControlHook)
92
err := s.ts.foo("cleanuptest", false)
93
c.Assert(err, gc.IsNil)
94
c.Assert(s.ts.label, gc.Equals, "cleanuptest")
95
// Use the cleanup func to remove the hook and check the result.
97
err = s.ts.foo("again", false)
98
c.Assert(err, gc.IsNil)
99
c.Assert(s.ts.label, gc.Equals, "cleanuptest")
100
// Ensure that only the specified hook was removed and the other remaining one still works.
102
c.Assert(err, gc.IsNil)
103
c.Assert(s.ts.label, gc.Equals, "foobar")