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

Side by Side Diff: environs/ec2/config_test.go

Issue 6408049: environs: turn environment configuration into a concrete type
Patch Set: environs: turn environment configuration into a concrete type Created 12 years, 8 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/ec2/config.go ('k') | environs/ec2/ec2.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 package ec2 1 package ec2
2 2
3 import ( 3 import (
4 "fmt"
5 "io/ioutil" 4 "io/ioutil"
6 "launchpad.net/goamz/aws" 5 "launchpad.net/goamz/aws"
7 . "launchpad.net/gocheck" 6 . "launchpad.net/gocheck"
8 "launchpad.net/juju-core/environs" 7 "launchpad.net/juju-core/environs"
9 "os" 8 "os"
10 "path/filepath" 9 "path/filepath"
11 "strings" 10 "strings"
12 ) 11 )
13 12
14 // Use local suite since this file lives in the ec2 package 13 // Use local suite since this file lives in the ec2 package
15 // for testing internals. 14 // for testing internals.
16 type configSuite struct { 15 type ConfigSuite struct {
17 savedHome, savedAccessKey, savedSecretKey string 16 savedHome, savedAccessKey, savedSecretKey string
18 } 17 }
19 18
20 var _ = Suite(configSuite{}) 19 var _ = Suite(&ConfigSuite{})
21 20
22 var configTestRegion = aws.Region{ 21 var configTestRegion = aws.Region{
23 Name: "configtest", 22 Name: "configtest",
24 EC2Endpoint: "testregion.nowhere:1234", 23 EC2Endpoint: "testregion.nowhere:1234",
25 } 24 }
26 25
27 var testAuth = aws.Auth{"gopher", "long teeth"} 26 var testAuth = aws.Auth{"gopher", "long teeth"}
28 27
29 // the mandatory fields in config. 28 // the mandatory fields in config.
30 var baseConfig = "control-bucket: x\n" 29 var baseConfig = "control-bucket: x\n"
31 30
32 // the result of parsing baseConfig.
33 var baseConfigResult = providerConfig{
34 name: "testenv",
35 region: "us-east-1",
36 bucket: "x",
37 auth: testAuth,
38 authorizedKeys: "sshkey\n",
39 }
40
41 // configTest specifies a config parsing test, checking that env when 31 // configTest specifies a config parsing test, checking that env when
42 // parsed as the ec2 section of a config file matches baseConfigResult 32 // parsed as the ec2 section of a config file matches baseConfigResult
43 // when mutated by the mutate function, or that the parse matches the 33 // when mutated by the mutate function, or that the parse matches the
44 // given error. 34 // given error.
45 type configTest struct { 35 type configTest struct {
46 » env string 36 » yaml string
47 » mutate func(*providerConfig) 37 » region string
48 » err string 38 » bucket string
39 » pbucket string
40 » accessKey string
41 » secretKey string
42 » err string
49 } 43 }
50 44
51 func (t configTest) check(c *C) { 45 func (t configTest) check(c *C) {
52 » envs, err := environs.ReadEnvironsBytes(makeEnv(t.env)) 46 » envs, err := environs.ReadEnvironsBytes(makeEnv(t.yaml))
47 » c.Check(err, IsNil)
48
49 » e, err := envs.Open("testenv")
53 if t.err != "" { 50 if t.err != "" {
54 c.Check(err, ErrorMatches, t.err) 51 c.Check(err, ErrorMatches, t.err)
55 return 52 return
56 } 53 }
57 c.Check(err, IsNil)
58 e, err := envs.Open("testenv")
59 c.Assert(err, IsNil) 54 c.Assert(err, IsNil)
60 c.Assert(e, NotNil) 55 c.Assert(e, NotNil)
61 c.Assert(e, FitsTypeOf, (*environ)(nil)) 56 c.Assert(e, FitsTypeOf, (*environ)(nil))
62 » tconfig := baseConfigResult 57
63 » t.mutate(&tconfig) 58 » config := e.(*environ).config()
64 » c.Check(e.(*environ).config(), DeepEquals, &tconfig) 59 » c.Assert(config.Name(), Equals, "testenv")
65 » c.Check(e.(*environ).name, Equals, tconfig.name) 60 » c.Assert(config.bucket, Equals, "x")
61 » if t.region != "" {
62 » » c.Assert(config.region, Equals, t.region)
63 » }
64 » if t.pbucket != "" {
65 » » c.Assert(config.publicBucket, Equals, t.pbucket)
66 » }
67 » if t.accessKey != "" {
68 » » auth := aws.Auth{t.accessKey, t.secretKey}
69 » » c.Assert(config.auth, Equals, auth)
70 » } else {
71 » » c.Assert(config.auth, DeepEquals, testAuth)
72 » }
66 } 73 }
67 74
68 var configTests = []configTest{ 75 var configTests = []configTest{
69 { 76 {
70 » » baseConfig, 77 » » yaml: baseConfig,
71 » » func(cfg *providerConfig) {},
72 » » "",
73 }, 78 },
74 { 79 {
75 » » "region: eu-west-1\n" + baseConfig, 80 » » yaml: "region: eu-west-1\n" + baseConfig,
76 » » func(cfg *providerConfig) { 81 » » region: "eu-west-1",
77 » » » cfg.region = "eu-west-1"
78 » » },
79 » » "",
80 }, 82 },
81 { 83 {
82 » » "region: unknown\n" + baseConfig, 84 » » yaml: "region: unknown\n" + baseConfig,
83 » » nil, 85 » » err: ".*invalid region name.*",
84 » » ".*invalid region name.*",
85 }, 86 },
86 { 87 {
87 » » "region: configtest\n" + baseConfig, 88 » » yaml: "region: configtest\n" + baseConfig,
88 » » func(cfg *providerConfig) { 89 » » region: "configtest",
89 » » » cfg.region = "configtest"
90 » » },
91 » » "",
92 }, 90 },
93 { 91 {
94 » » "region: 666\n" + baseConfig, 92 » » yaml: "region: 666\n" + baseConfig,
95 » » nil, 93 » » err: ".*expected string, got 666",
96 » » ".*expected string, got 666",
97 }, 94 },
98 { 95 {
99 » » "access-key: 666\n" + baseConfig, 96 » » yaml: "access-key: 666\n" + baseConfig,
100 » » nil, 97 » » err: ".*expected string, got 666",
101 » » ".*expected string, got 666",
102 }, 98 },
103 { 99 {
104 » » "secret-key: 666\n" + baseConfig, 100 » » yaml: "secret-key: 666\n" + baseConfig,
105 » » nil, 101 » » err: ".*expected string, got 666",
106 » » ".*expected string, got 666",
107 }, 102 },
108 { 103 {
109 » » "control-bucket: 666\n", 104 » » yaml: "control-bucket: 666\n",
110 » » nil, 105 » » err: ".*expected string, got 666",
111 » » ".*expected string, got 666",
112 }, 106 },
113 { 107 {
114 » » "public-bucket: 666\n" + baseConfig, 108 » » yaml: "public-bucket: 666\n" + baseConfig,
115 » » nil, 109 » » err: ".*expected string, got 666",
116 » » ".*expected string, got 666",
117 }, 110 },
118 { 111 {
119 » » "public-bucket: foo\n" + baseConfig, 112 » » yaml: "public-bucket: foo\n" + baseConfig,
120 » » func(cfg *providerConfig) { 113 » » pbucket: "foo",
121 » » » cfg.publicBucket = "foo"
122 » » },
123 » » "",
124 }, 114 },
125 { 115 {
126 » » "access-key: jujuer\nsecret-key: open sesame\n" + baseConfig, 116 » » yaml: "access-key: jujuer\nsecret-key: open sesame\n" + bas eConfig,
127 » » func(cfg *providerConfig) { 117 » » accessKey: "jujuer",
128 » » » cfg.auth = aws.Auth{ 118 » » secretKey: "open sesame",
129 » » » » AccessKey: "jujuer",
130 » » » » SecretKey: "open sesame",
131 » » » }
132 » » },
133 » » "",
134 }, 119 },
135 { 120 {
136 » » "authorized-keys-path: \n" + baseConfig, 121 » » yaml: "access-key: jujuer\n" + baseConfig,
137 » » nil, 122 » » err: ".*environment has access-key but no secret-key",
138 » » "",
139 }, 123 },
140 { 124 {
141 » » "authorized-keys: authkeys\n" + baseConfig, 125 » » yaml: "secret-key: badness\n" + baseConfig,
142 » » func(cfg *providerConfig) { 126 » » err: ".*environment has secret-key but no access-key",
143 » » » cfg.authorizedKeys = "authkeys"
144 » » },
145 » » "",
146 » },
147 » {
148 » » "access-key: jujuer\n" + baseConfig,
149 » » nil,
150 » » ".*environment has access-key but no secret-key",
151 » },
152 » {
153 » » "secret-key: badness\n" + baseConfig,
154 » » nil,
155 » » ".*environment has secret-key but no access-key",
156 » },
157
158 » // unknown fields are discarded
159 » {
160 » » "unknown-something: 666\n" + baseConfig,
161 » » func(cfg *providerConfig) {},
162 » » "",
163 }, 127 },
164 } 128 }
165 129
166 func indent(s string, with string) string { 130 func indent(s string, with string) string {
167 var r string 131 var r string
168 lines := strings.Split(s, "\n") 132 lines := strings.Split(s, "\n")
169 for _, l := range lines { 133 for _, l := range lines {
170 r += with + l + "\n" 134 r += with + l + "\n"
171 } 135 }
172 return r 136 return r
173 } 137 }
174 138
175 func makeEnv(s string) []byte { 139 func makeEnv(s string) []byte {
176 return []byte("environments:\n testenv:\n type: ec2\n" + indent(s, " ")) 140 return []byte("environments:\n testenv:\n type: ec2\n" + indent(s, " "))
177 } 141 }
178 142
179 func (s *configSuite) SetUpTest(c *C) { 143 func (s *ConfigSuite) SetUpTest(c *C) {
180 s.savedHome = os.Getenv("HOME") 144 s.savedHome = os.Getenv("HOME")
181 s.savedAccessKey = os.Getenv("AWS_ACCESS_KEY_ID") 145 s.savedAccessKey = os.Getenv("AWS_ACCESS_KEY_ID")
182 s.savedSecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY") 146 s.savedSecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
183 147
184 home := c.MkDir() 148 home := c.MkDir()
185 sshDir := filepath.Join(home, ".ssh") 149 sshDir := filepath.Join(home, ".ssh")
186 err := os.Mkdir(sshDir, 0777) 150 err := os.Mkdir(sshDir, 0777)
187 c.Assert(err, IsNil) 151 c.Assert(err, IsNil)
188 err = ioutil.WriteFile(filepath.Join(sshDir, "id_rsa.pub"), []byte("sshk ey\n"), 0666) 152 err = ioutil.WriteFile(filepath.Join(sshDir, "id_rsa.pub"), []byte("sshk ey\n"), 0666)
189 c.Assert(err, IsNil) 153 c.Assert(err, IsNil)
190 154
191 os.Setenv("HOME", home) 155 os.Setenv("HOME", home)
192 os.Setenv("AWS_ACCESS_KEY_ID", testAuth.AccessKey) 156 os.Setenv("AWS_ACCESS_KEY_ID", testAuth.AccessKey)
193 os.Setenv("AWS_SECRET_ACCESS_KEY", testAuth.SecretKey) 157 os.Setenv("AWS_SECRET_ACCESS_KEY", testAuth.SecretKey)
194 aws.Regions["configtest"] = configTestRegion 158 aws.Regions["configtest"] = configTestRegion
195 } 159 }
196 160
197 func (s *configSuite) TearDownTest(c *C) { 161 func (s *ConfigSuite) TearDownTest(c *C) {
198 os.Setenv("HOME", s.savedHome) 162 os.Setenv("HOME", s.savedHome)
199 os.Setenv("AWS_ACCESS_KEY_ID", s.savedAccessKey) 163 os.Setenv("AWS_ACCESS_KEY_ID", s.savedAccessKey)
200 os.Setenv("AWS_SECRET_ACCESS_KEY", s.savedSecretKey) 164 os.Setenv("AWS_SECRET_ACCESS_KEY", s.savedSecretKey)
201 delete(aws.Regions, "configtest") 165 delete(aws.Regions, "configtest")
202 } 166 }
203 167
204 func (s *configSuite) TestConfig(c *C) { 168 func (s *ConfigSuite) TestConfig(c *C) {
205 for i, t := range configTests { 169 for i, t := range configTests {
206 » » c.Logf("test %d (environ %q)", i, t.env) 170 » » c.Logf("test %d: %q", i, t.yaml)
207 t.check(c) 171 t.check(c)
208 } 172 }
209 } 173 }
210 174
211 func (s *configSuite) TestMissingAuth(c *C) { 175 func (s *ConfigSuite) TestMissingAuth(c *C) {
212 os.Setenv("AWS_ACCESS_KEY_ID", "") 176 os.Setenv("AWS_ACCESS_KEY_ID", "")
213 os.Setenv("AWS_SECRET_ACCESS_KEY", "") 177 os.Setenv("AWS_SECRET_ACCESS_KEY", "")
214 test := configTests[0] 178 test := configTests[0]
215 test.err = ".*not found in environment" 179 test.err = ".*not found in environment"
216 test.check(c) 180 test.check(c)
217 } 181 }
218
219 func (s *configSuite) TestAuthorizedKeysPath(c *C) {
220 dir := c.MkDir()
221 path := filepath.Join(dir, "something")
222 err := ioutil.WriteFile(path, []byte("another-sshkey\n"), 0666)
223 c.Assert(err, IsNil)
224 confLine := fmt.Sprintf("authorized-keys-path: %s\n", path)
225 test := configTest{
226 confLine + baseConfig,
227 func(cfg *providerConfig) {
228 cfg.authorizedKeys = "another-sshkey\n"
229 },
230 "",
231 }
232 test.check(c)
233 }
OLDNEW
« no previous file with comments | « environs/ec2/config.go ('k') | environs/ec2/ec2.go » ('j') | no next file with comments »

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