Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(82)

Side by Side Diff: environs/config_test.go

Issue 13421050: environs: add Environs.Config method
Patch Set: environs: add Environs.Config method Created 11 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « environs/config.go ('k') | environs/open.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011, 2012, 2013 Canonical Ltd. 1 // Copyright 2011, 2012, 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details. 2 // Licensed under the AGPLv3, see LICENCE file for details.
3 3
4 package environs_test 4 package environs_test
5 5
6 import ( 6 import (
7 "os" 7 "os"
8 "path/filepath" 8 "path/filepath"
9 9
10 gc "launchpad.net/gocheck" 10 gc "launchpad.net/gocheck"
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 environments: 60 environments:
61 only: 61 only:
62 foo: bar 62 foo: bar
63 `, "only", `environment "only" has no type`, 63 `, "only", `environment "only" has no type`,
64 }, {` 64 }, {`
65 environments: 65 environments:
66 only: 66 only:
67 foo: bar 67 foo: bar
68 type: crazy 68 type: crazy
69 `, "only", `environment "only" has an unknown provider type "crazy"`, 69 `, "only", `environment "only" has an unknown provider type "crazy"`,
70 }, {`
71 environments:
72 only:
73 type: dummy
74 `, "only", `.*state-server: expected bool, got nothing`,
75 }, 70 },
76 } 71 }
77 72
78 func (suite) TestInvalidEnv(c *gc.C) { 73 func (suite) TestInvalidEnv(c *gc.C) {
79 defer testing.MakeFakeHomeNoEnvironments(c, "only").Restore() 74 defer testing.MakeFakeHomeNoEnvironments(c, "only").Restore()
80 for i, t := range invalidEnvTests { 75 for i, t := range invalidEnvTests {
81 c.Logf("running test %v", i) 76 c.Logf("running test %v", i)
82 es, err := environs.ReadEnvironsBytes([]byte(t.env)) 77 es, err := environs.ReadEnvironsBytes([]byte(t.env))
83 c.Check(err, gc.IsNil) 78 c.Check(err, gc.IsNil)
84 » » e, err := es.Open(t.name) 79 » » cfg, err := es.Config(t.name)
85 c.Check(err, gc.ErrorMatches, t.err) 80 c.Check(err, gc.ErrorMatches, t.err)
86 » » c.Check(e, gc.IsNil) 81 » » c.Check(cfg, gc.IsNil)
87 } 82 }
88 } 83 }
89 84
90 func (suite) TestNoEnv(c *gc.C) { 85 func (suite) TestNoEnv(c *gc.C) {
91 defer testing.MakeFakeHomeNoEnvironments(c).Restore() 86 defer testing.MakeFakeHomeNoEnvironments(c).Restore()
92 es, err := environs.ReadEnvirons("") 87 es, err := environs.ReadEnvirons("")
93 c.Assert(es, gc.IsNil) 88 c.Assert(es, gc.IsNil)
94 c.Assert(err, jc.Satisfies, environs.IsNoEnv) 89 c.Assert(err, jc.Satisfies, environs.IsNoEnv)
95 } 90 }
96 91
97 var configTests = []struct { 92 var configTests = []struct {
98 env string 93 env string
99 » check func(c *gc.C, es *environs.Environs) 94 » check func(c *gc.C, envs *environs.Environs)
100 }{ 95 }{
101 {` 96 {`
102 environments: 97 environments:
103 only: 98 only:
104 type: dummy 99 type: dummy
105 state-server: false 100 state-server: false
106 `, func(c *gc.C, es *environs.Environs) { 101 `, func(c *gc.C, envs *environs.Environs) {
107 » » e, err := es.Open("") 102 » » cfg, err := envs.Config("")
108 c.Assert(err, gc.IsNil) 103 c.Assert(err, gc.IsNil)
109 » » c.Assert(e.Name(), gc.Equals, "only") 104 » » c.Assert(cfg.Name(), gc.Equals, "only")
110 }}, {` 105 }}, {`
111 default: 106 default:
112 invalid 107 invalid
113 environments: 108 environments:
114 valid: 109 valid:
115 type: dummy 110 type: dummy
116 state-server: false 111 state-server: false
117 invalid: 112 invalid:
118 type: crazy 113 type: crazy
119 `, func(c *gc.C, es *environs.Environs) { 114 `, func(c *gc.C, envs *environs.Environs) {
120 » » e, err := es.Open("") 115 » » cfg, err := envs.Config("")
121 c.Assert(err, gc.ErrorMatches, `environment "invalid" has an unk nown provider type "crazy"`) 116 c.Assert(err, gc.ErrorMatches, `environment "invalid" has an unk nown provider type "crazy"`)
122 » » c.Assert(e, gc.IsNil) 117 » » c.Assert(cfg, gc.IsNil)
123 » » e, err = es.Open("valid") 118 » » cfg, err = envs.Config("valid")
124 c.Assert(err, gc.IsNil) 119 c.Assert(err, gc.IsNil)
125 » » c.Assert(e.Name(), gc.Equals, "valid") 120 » » c.Assert(cfg.Name(), gc.Equals, "valid")
126 }}, {` 121 }}, {`
127 environments: 122 environments:
128 one: 123 one:
129 type: dummy 124 type: dummy
130 state-server: false 125 state-server: false
131 two: 126 two:
132 type: dummy 127 type: dummy
133 state-server: false 128 state-server: false
134 `, func(c *gc.C, es *environs.Environs) { 129 `, func(c *gc.C, envs *environs.Environs) {
135 » » e, err := es.Open("") 130 » » cfg, err := envs.Config("")
136 c.Assert(err, gc.ErrorMatches, `no default environment found`) 131 c.Assert(err, gc.ErrorMatches, `no default environment found`)
137 » » c.Assert(e, gc.IsNil) 132 » » c.Assert(cfg, gc.IsNil)
138 }}, 133 }},
139 } 134 }
140 135
141 func (suite) TestConfig(c *gc.C) { 136 func (suite) TestConfig(c *gc.C) {
142 defer testing.MakeFakeHomeNoEnvironments(c, "only", "valid", "one", "two ").Restore() 137 defer testing.MakeFakeHomeNoEnvironments(c, "only", "valid", "one", "two ").Restore()
143 for i, t := range configTests { 138 for i, t := range configTests {
144 c.Logf("running test %v", i) 139 c.Logf("running test %v", i)
145 » » es, err := environs.ReadEnvironsBytes([]byte(t.env)) 140 » » envs, err := environs.ReadEnvironsBytes([]byte(t.env))
146 c.Assert(err, gc.IsNil) 141 c.Assert(err, gc.IsNil)
147 » » t.check(c, es) 142 » » t.check(c, envs)
148 } 143 }
149 } 144 }
150 145
151 func (suite) TestDefaultConfigFile(c *gc.C) { 146 func (suite) TestDefaultConfigFile(c *gc.C) {
152 defer testing.MakeEmptyFakeHome(c).Restore() 147 defer testing.MakeEmptyFakeHome(c).Restore()
153 148
154 env := ` 149 env := `
155 environments: 150 environments:
156 only: 151 only:
157 type: dummy 152 type: dummy
158 state-server: false 153 state-server: false
159 authorized-keys: i-am-a-key 154 authorized-keys: i-am-a-key
160 ` 155 `
161 outfile, err := environs.WriteEnvirons("", env) 156 outfile, err := environs.WriteEnvirons("", env)
162 c.Assert(err, gc.IsNil) 157 c.Assert(err, gc.IsNil)
163 path := testing.HomePath(".juju", "environments.yaml") 158 path := testing.HomePath(".juju", "environments.yaml")
164 c.Assert(path, gc.Equals, outfile) 159 c.Assert(path, gc.Equals, outfile)
165 160
166 » es, err := environs.ReadEnvirons("") 161 » envs, err := environs.ReadEnvirons("")
167 c.Assert(err, gc.IsNil) 162 c.Assert(err, gc.IsNil)
168 » e, err := es.Open("") 163 » cfg, err := envs.Config("")
169 c.Assert(err, gc.IsNil) 164 c.Assert(err, gc.IsNil)
170 » c.Assert(e.Name(), gc.Equals, "only") 165 » c.Assert(cfg.Name(), gc.Equals, "only")
171 } 166 }
172 167
173 func (suite) TestConfigPerm(c *gc.C) { 168 func (suite) TestConfigPerm(c *gc.C) {
174 defer testing.MakeSampleHome(c).Restore() 169 defer testing.MakeSampleHome(c).Restore()
175 170
176 path := testing.HomePath(".juju") 171 path := testing.HomePath(".juju")
177 info, err := os.Lstat(path) 172 info, err := os.Lstat(path)
178 c.Assert(err, gc.IsNil) 173 c.Assert(err, gc.IsNil)
179 oldPerm := info.Mode().Perm() 174 oldPerm := info.Mode().Perm()
180 env := ` 175 env := `
(...skipping 24 matching lines...) Expand all
205 only: 200 only:
206 type: dummy 201 type: dummy
207 state-server: false 202 state-server: false
208 authorized-keys: i-am-a-key 203 authorized-keys: i-am-a-key
209 ` 204 `
210 path := filepath.Join(c.MkDir(), "a-file") 205 path := filepath.Join(c.MkDir(), "a-file")
211 outfile, err := environs.WriteEnvirons(path, env) 206 outfile, err := environs.WriteEnvirons(path, env)
212 c.Assert(err, gc.IsNil) 207 c.Assert(err, gc.IsNil)
213 c.Assert(path, gc.Equals, outfile) 208 c.Assert(path, gc.Equals, outfile)
214 209
215 » es, err := environs.ReadEnvirons(path) 210 » envs, err := environs.ReadEnvirons(path)
216 c.Assert(err, gc.IsNil) 211 c.Assert(err, gc.IsNil)
217 » e, err := es.Open("") 212 » cfg, err := envs.Config("")
218 c.Assert(err, gc.IsNil) 213 c.Assert(err, gc.IsNil)
219 » c.Assert(e.Name(), gc.Equals, "only") 214 » c.Assert(cfg.Name(), gc.Equals, "only")
220 } 215 }
221 216
222 func (suite) TestConfigRoundTrip(c *gc.C) { 217 func (suite) TestConfigRoundTrip(c *gc.C) {
223 cfg, err := config.New(config.NoDefaults, dummySampleConfig()) 218 cfg, err := config.New(config.NoDefaults, dummySampleConfig())
224 c.Assert(err, gc.IsNil) 219 c.Assert(err, gc.IsNil)
225 provider, err := environs.Provider(cfg.Type()) 220 provider, err := environs.Provider(cfg.Type())
226 c.Assert(err, gc.IsNil) 221 c.Assert(err, gc.IsNil)
227 cfg, err = provider.Validate(cfg, nil) 222 cfg, err = provider.Validate(cfg, nil)
228 c.Assert(err, gc.IsNil) 223 c.Assert(err, gc.IsNil)
229 env, err := environs.New(cfg) 224 env, err := environs.New(cfg)
(...skipping 21 matching lines...) Expand all
251 246
252 cfg1, err := environs.BootstrapConfig(cfg) 247 cfg1, err := environs.BootstrapConfig(cfg)
253 c.Assert(err, gc.IsNil) 248 c.Assert(err, gc.IsNil)
254 249
255 expect := cfg.AllAttrs() 250 expect := cfg.AllAttrs()
256 delete(expect, "secret") 251 delete(expect, "secret")
257 expect["admin-secret"] = "" 252 expect["admin-secret"] = ""
258 expect["ca-private-key"] = "" 253 expect["ca-private-key"] = ""
259 c.Assert(cfg1.AllAttrs(), gc.DeepEquals, expect) 254 c.Assert(cfg1.AllAttrs(), gc.DeepEquals, expect)
260 } 255 }
OLDNEW
« no previous file with comments | « environs/config.go ('k') | environs/open.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b