| 1 |
package hook |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
gc "launchpad.net/gocheck" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func Test(t *testing.T) { |
| 10 |
gc.TestingT(t) |
| 11 |
} |
| 12 |
|
| 13 |
var _ = gc.Suite(&ServiceSuite{}) |
| 14 |
|
| 15 |
type ServiceSuite struct { |
| 16 |
ts *testService |
| 17 |
} |
| 18 |
|
| 19 |
func (s *ServiceSuite) SetUpTest(c *gc.C) { |
| 20 |
s.ts = newTestService() |
| 21 |
|
| 22 |
s.ts.RegisterControlPoint("foo", functionControlHook) |
| 23 |
|
| 24 |
s.ts.RegisterControlPoint("foobar", namedControlHook) |
| 25 |
} |
| 26 |
|
| 27 |
type testService struct { |
| 28 |
TestService |
| 29 |
label string |
| 30 |
} |
| 31 |
|
| 32 |
func newTestService() *testService { |
| 33 |
return &testService{ |
| 34 |
TestService: TestService{ |
| 35 |
ControlHooks: make(map[string]ControlProcessor), |
| 36 |
}, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func functionControlHook(s ServiceControl, args ...interface{}) error { |
| 41 |
label := args[0].(string) |
| 42 |
returnError := args[1].(bool) |
| 43 |
if returnError { |
| 44 |
return fmt.Errorf("An error occurred") |
| 45 |
} |
| 46 |
s.(*testService).label = label |
| 47 |
return nil |
| 48 |
} |
| 49 |
|
| 50 |
func namedControlHook(s ServiceControl, args ...interface{}) error { |
| 51 |
s.(*testService).label = "foobar" |
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
func (s *testService) foo(label string, returnError bool) error { |
| 56 |
if err := s.ProcessFunctionHook(s, label, returnError); err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
return nil |
| 60 |
} |
| 61 |
|
| 62 |
func (s *testService) bar() error { |
| 63 |
if err := s.ProcessControlHook("foobar", s); err != nil { |
| 64 |
return err |
| 65 |
} |
| 66 |
return nil |
| 67 |
} |
| 68 |
|
| 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") |
| 73 |
} |
| 74 |
|
| 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, "") |
| 79 |
} |
| 80 |
|
| 81 |
func (s *ServiceSuite) TestNamedHook(c *gc.C) { |
| 82 |
err := s.ts.bar() |
| 83 |
c.Assert(err, gc.IsNil) |
| 84 |
c.Assert(s.ts.label, gc.Equals, "foobar") |
| 85 |
} |
| 86 |
|
| 87 |
func (s *ServiceSuite) TestHookCleanup(c *gc.C) { |
| 88 |
|
| 89 |
s.ts.RegisterControlPoint("foo", nil) |
| 90 |
|
| 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 |
|
| 96 |
cleanup() |
| 97 |
err = s.ts.foo("again", false) |
| 98 |
c.Assert(err, gc.IsNil) |
| 99 |
c.Assert(s.ts.label, gc.Equals, "cleanuptest") |
| 100 |
|
| 101 |
err = s.ts.bar() |
| 102 |
c.Assert(err, gc.IsNil) |
| 103 |
c.Assert(s.ts.label, gc.Equals, "foobar") |
| 104 |
|
| 105 |
} |