This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
provider_test.go
137 lines (120 loc) · 3.44 KB
/
provider_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package httprelation_test
import (
"sort"
"testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/juju/charm.v6-unstable"
"github.com/juju/gocharm/charmbits/httprelation"
"github.com/juju/gocharm/hook"
"github.com/juju/gocharm/hook/hooktest"
)
func TestPackage(t *testing.T) {
gc.TestingT(t)
}
var _ = gc.Suite(&providerSuite{})
type providerSuite struct {
}
func (s *providerSuite) TestRegister(c *gc.C) {
s.testRegister(c, false)
s.testRegister(c, true)
}
func (s *providerSuite) testRegister(c *gc.C, withHTTPS bool) {
r := hook.NewRegistry()
var p httprelation.Provider
p.Register(r, "foo", withHTTPS)
c.Assert(r.RegisteredRelations(), jc.DeepEquals, map[string]charm.Relation{
"foo": {
Name: "foo",
Role: charm.RoleProvider,
Interface: "http",
Optional: false,
Scope: charm.ScopeGlobal,
},
})
expectedConfig := map[string]charm.Option{
"http-port": charm.Option{
Type: "int",
Description: "Port for the HTTP server to listen on",
Default: 80,
},
}
if withHTTPS {
expectedConfig["https-certificate"] = charm.Option{
Type: "string",
Description: "Certificate and key for https server in PEM format. If this is not set, no https server will be run",
}
expectedConfig["https-port"] = charm.Option{
Type: "int",
Description: "Port for the HTTP server to listen on",
Default: 443,
}
}
c.Assert(r.RegisteredConfig(), jc.DeepEquals, expectedConfig)
hooks := r.RegisteredHooks()
sort.Strings(hooks)
c.Assert(hooks, jc.DeepEquals, []string{"config-changed", "foo-relation-joined", "install"})
}
func (s *providerSuite) TestPortsReturnZeroAtInstallTime(c *gc.C) {
r := hook.NewRegistry()
var p httprelation.Provider
p.Register(r, "foo", true)
ctxt := &context{
state: make(hooktest.MemState),
}
called := false
ctxt.runHook(c, "install", "", "", func(p *httprelation.Provider, r *hook.Registry) {
r.RegisterHook("*", func() error {
c.Assert(p.HTTPPort(), gc.Equals, 0)
c.Assert(p.HTTPSPort(), gc.Equals, 0)
called = true
return nil
})
})
c.Assert(called, jc.IsTrue)
}
func (s *providerSuite) TestOpensPortWhenConfigChanged(c *gc.C) {
ctxt := &context{
state: make(hooktest.MemState),
}
ctxt.runHook(c, "install", "", "", nil)
ctxt.runHook(c, "start", "", "", nil)
ctxt.config = map[string]interface{}{
"http-port": 1234,
}
rec := ctxt.runHook(c, "config-changed", "", "", func(p *httprelation.Provider, r *hook.Registry) {
r.RegisterHook("*", func() error {
c.Check(p.HTTPPort(), gc.Equals, 1234)
return nil
})
})
c.Assert(rec, jc.DeepEquals, [][]string{
{"open-port", "1234/tcp"},
})
}
type context struct {
withHTTPS bool
relations map[hook.RelationId]map[hook.UnitId]map[string]string
relationIds map[string][]hook.RelationId
config map[string]interface{}
state hook.PersistentState
}
func (ctxt *context) runHook(c *gc.C, hookName string, relId hook.RelationId, relUnit hook.UnitId, register func(*httprelation.Provider, *hook.Registry)) [][]string {
var p httprelation.Provider
runner := &hooktest.Runner{
RegisterHooks: func(r *hook.Registry) {
p.Register(r, "foo", ctxt.withHTTPS)
if register != nil {
register(&p, r)
}
},
HookStateDir: "/dev/null",
Relations: ctxt.relations,
RelationIds: ctxt.relationIds,
Config: ctxt.config,
Logger: c,
}
err := runner.RunHook(hookName, relId, relUnit)
c.Assert(err, gc.IsNil)
return runner.Record
}