~rogpeppe/juju-core/themue-058-debug-log-api

« back to all changes in this revision

Viewing changes to state/apiserver/common/unitswatcher_test.go

  • Committer: Frank Mueller
  • Date: 2014-01-23 14:14:49 UTC
  • mfrom: (2152.1.95 juju-core)
  • Revision ID: frank.mueller@canonical.com-20140123141449-b30l57y7gs3wjkpw
debugger: merged trunk and fixed permission and interface problems

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package common_test
 
5
 
 
6
import (
 
7
        "fmt"
 
8
 
 
9
        gc "launchpad.net/gocheck"
 
10
 
 
11
        "launchpad.net/juju-core/state"
 
12
        "launchpad.net/juju-core/state/api/params"
 
13
        "launchpad.net/juju-core/state/apiserver/common"
 
14
        apiservertesting "launchpad.net/juju-core/state/apiserver/testing"
 
15
        jc "launchpad.net/juju-core/testing/checkers"
 
16
)
 
17
 
 
18
type unitsWatcherSuite struct{}
 
19
 
 
20
var _ = gc.Suite(&unitsWatcherSuite{})
 
21
 
 
22
type fakeUnitsWatcher struct {
 
23
        state.UnitsWatcher
 
24
        initial []string
 
25
        fetchError
 
26
}
 
27
 
 
28
func (f *fakeUnitsWatcher) WatchUnits() state.StringsWatcher {
 
29
        changes := make(chan []string, 1)
 
30
        // Simulate initial event.
 
31
        changes <- f.initial
 
32
        return &fakeStringsWatcher{changes}
 
33
}
 
34
 
 
35
type fakeStringsWatcher struct {
 
36
        changes chan []string
 
37
}
 
38
 
 
39
func (*fakeStringsWatcher) Stop() error {
 
40
        return nil
 
41
}
 
42
 
 
43
func (*fakeStringsWatcher) Kill() {}
 
44
 
 
45
func (*fakeStringsWatcher) Wait() error {
 
46
        return nil
 
47
}
 
48
 
 
49
func (*fakeStringsWatcher) Err() error {
 
50
        return nil
 
51
}
 
52
 
 
53
func (w *fakeStringsWatcher) Changes() <-chan []string {
 
54
        return w.changes
 
55
}
 
56
 
 
57
func (*unitsWatcherSuite) TestWatchUnits(c *gc.C) {
 
58
        st := &fakeState{
 
59
                entities: map[string]entityWithError{
 
60
                        "x0": &fakeUnitsWatcher{fetchError: "x0 fails"},
 
61
                        "x1": &fakeUnitsWatcher{initial: []string{"foo", "bar"}},
 
62
                        "x2": &fakeUnitsWatcher{},
 
63
                },
 
64
        }
 
65
        getCanWatch := func() (common.AuthFunc, error) {
 
66
                return func(tag string) bool {
 
67
                        switch tag {
 
68
                        case "x0", "x1":
 
69
                                return true
 
70
                        }
 
71
                        return false
 
72
                }, nil
 
73
        }
 
74
        resources := common.NewResources()
 
75
        w := common.NewUnitsWatcher(st, resources, getCanWatch)
 
76
        entities := params.Entities{[]params.Entity{
 
77
                {"x0"}, {"x1"}, {"x2"}, {"x3"},
 
78
        }}
 
79
        result, err := w.WatchUnits(entities)
 
80
        c.Assert(err, gc.IsNil)
 
81
        c.Assert(result, jc.DeepEquals, params.StringsWatchResults{
 
82
                Results: []params.StringsWatchResult{
 
83
                        {Error: &params.Error{Message: "x0 fails"}},
 
84
                        {"1", []string{"foo", "bar"}, nil},
 
85
                        {Error: apiservertesting.ErrUnauthorized},
 
86
                        {Error: apiservertesting.ErrUnauthorized},
 
87
                },
 
88
        })
 
89
}
 
90
 
 
91
func (*unitsWatcherSuite) TestWatchUnitsError(c *gc.C) {
 
92
        getCanWatch := func() (common.AuthFunc, error) {
 
93
                return nil, fmt.Errorf("pow")
 
94
        }
 
95
        resources := common.NewResources()
 
96
        w := common.NewUnitsWatcher(
 
97
                &fakeState{},
 
98
                resources,
 
99
                getCanWatch,
 
100
        )
 
101
        _, err := w.WatchUnits(params.Entities{[]params.Entity{{"x0"}}})
 
102
        c.Assert(err, gc.ErrorMatches, "pow")
 
103
}
 
104
 
 
105
func (*unitsWatcherSuite) TestWatchNoArgsNoError(c *gc.C) {
 
106
        getCanWatch := func() (common.AuthFunc, error) {
 
107
                return nil, fmt.Errorf("pow")
 
108
        }
 
109
        resources := common.NewResources()
 
110
        w := common.NewUnitsWatcher(
 
111
                &fakeState{},
 
112
                resources,
 
113
                getCanWatch,
 
114
        )
 
115
        result, err := w.WatchUnits(params.Entities{})
 
116
        c.Assert(err, gc.IsNil)
 
117
        c.Assert(result.Results, gc.HasLen, 0)
 
118
}