~james-page/ubuntu/wily/juju-core/mir-fixes

« back to all changes in this revision

Viewing changes to src/github.com/joyent/gosdc/localservices/hook/service_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-04-07 18:24:59 UTC
  • mfrom: (1.1.22)
  • Revision ID: package-import@ubuntu.com-20140407182459-1b6zvm5ygm4ki7yp
Tags: 1.18.0-0ubuntu1
* New upstream release (LP: #1287147), including fixes for:
  - maas/lxc: LXC permission denied issue (LP: #1299588).
  - core: mega-watcher for machines does not include container
    addresses (LP: #1301464).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        // 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)
 
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
        // 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.
 
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
        // Ensure that only the specified hook was removed and the other remaining one still works.
 
101
        err = s.ts.bar()
 
102
        c.Assert(err, gc.IsNil)
 
103
        c.Assert(s.ts.label, gc.Equals, "foobar")
 
104
 
 
105
}