-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
137 lines (125 loc) · 3.57 KB
/
config_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
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package environs_test
import (
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/environs"
"github.com/juju/juju/provider/dummy"
_ "github.com/juju/juju/provider/manual"
"github.com/juju/juju/testing"
)
type suite struct {
testing.FakeJujuXDGDataHomeSuite
}
var _ = gc.Suite(&suite{})
func (s *suite) SetUpTest(c *gc.C) {
s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
s.AddCleanup(dummy.Reset)
}
// dummySampleConfig returns the dummy sample config without
// the controller configured.
// This function also exists in cloudconfig/userdata_test
// Maybe place it in dummy and export it?
func dummySampleConfig() testing.Attrs {
return dummy.SampleConfig().Merge(testing.Attrs{
"controller": false,
})
}
type dummyProvider struct {
environs.CloudEnvironProvider
}
func (s *suite) TestRegisterProvider(c *gc.C) {
s.PatchValue(environs.Providers, make(map[string]environs.EnvironProvider))
s.PatchValue(environs.ProviderAliases, make(map[string]string))
type step struct {
name string
aliases []string
err string
}
type test []step
tests := []test{
[]step{{
name: "providerName",
}},
[]step{{
name: "providerName",
aliases: []string{"providerName"},
err: "juju: duplicate provider alias \"providerName\"",
}},
[]step{{
name: "providerName",
aliases: []string{"providerAlias", "providerAlias"},
err: "juju: duplicate provider alias \"providerAlias\"",
}},
[]step{{
name: "providerName",
aliases: []string{"providerAlias1", "providerAlias2"},
}},
[]step{{
name: "providerName",
}, {
name: "providerName",
err: "juju: duplicate provider name \"providerName\"",
}},
[]step{{
name: "providerName1",
}, {
name: "providerName2",
aliases: []string{"providerName"},
}},
[]step{{
name: "providerName1",
}, {
name: "providerName2",
aliases: []string{"providerName1"},
err: "juju: duplicate provider alias \"providerName1\"",
}},
}
registerProvider := func(name string, aliases []string) (err error) {
defer func() { err, _ = recover().(error) }()
registered := &dummyProvider{}
environs.RegisterProvider(name, registered, aliases...)
p, err := environs.Provider(name)
c.Assert(err, jc.ErrorIsNil)
c.Assert(p, gc.Equals, registered)
for _, alias := range aliases {
p, err := environs.Provider(alias)
c.Assert(err, jc.ErrorIsNil)
c.Assert(p, gc.Equals, registered)
c.Assert(p, gc.Equals, registered)
}
return nil
}
for i, test := range tests {
c.Logf("test %d: %v", i, test)
for k := range *environs.Providers {
delete(*environs.Providers, k)
}
for k := range *environs.ProviderAliases {
delete(*environs.ProviderAliases, k)
}
for _, step := range test {
err := registerProvider(step.name, step.aliases)
if step.err == "" {
c.Assert(err, jc.ErrorIsNil)
} else {
c.Assert(err, gc.ErrorMatches, step.err)
}
}
}
}
func (s *suite) TestUnregisterProvider(c *gc.C) {
s.PatchValue(environs.Providers, make(map[string]environs.EnvironProvider))
s.PatchValue(environs.ProviderAliases, make(map[string]string))
registered := &dummyProvider{}
unreg := environs.RegisterProvider("test", registered, "alias1", "alias2")
unreg()
_, err := environs.Provider("test")
c.Assert(err, jc.Satisfies, errors.IsNotFound)
_, err = environs.Provider("alias1")
c.Assert(err, jc.Satisfies, errors.IsNotFound)
_, err = environs.Provider("alias2")
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}