~fwereade/juju-core/uniter-relations-working-but-somewhat-too-big

« back to all changes in this revision

Viewing changes to cmd/jujud/agent_test.go

cmd/jujud: make agents change passwords ASAP

R=niemeyer
CC=
https://codereview.appspot.com/6655044

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
        "launchpad.net/gnuflag"
7
7
        . "launchpad.net/gocheck"
8
8
        "launchpad.net/juju-core/cmd"
 
9
        "launchpad.net/juju-core/environs"
 
10
        "launchpad.net/juju-core/juju/testing"
9
11
        "launchpad.net/juju-core/state"
10
 
        "launchpad.net/juju-core/testing"
 
12
        coretesting "launchpad.net/juju-core/testing"
11
13
        "launchpad.net/juju-core/version"
12
14
        "launchpad.net/tomb"
 
15
        "path/filepath"
13
16
        "time"
14
17
)
15
18
 
16
19
var _ = Suite(&agentSuite{})
17
20
 
18
21
type agentSuite struct {
19
 
        testing.LoggingSuite
 
22
        coretesting.LoggingSuite
20
23
}
21
24
 
22
25
func assertDead(c *C, tasks []*testTask) {
206
209
        }
207
210
        return initCmd(ac, append(common, args...))
208
211
}
 
212
 
 
213
type runner interface {
 
214
        Run(*cmd.Context) error
 
215
        Stop() error
 
216
}
 
217
 
 
218
// runWithTimeout runs an agent and waits
 
219
// for it to complete within a reasonable time. 
 
220
func runWithTimeout(r runner) error {
 
221
        done := make(chan error)
 
222
        go func() {
 
223
                done <- r.Run(nil)
 
224
        }()
 
225
        select {
 
226
        case err := <-done:
 
227
                return err
 
228
        case <-time.After(5 * time.Second):
 
229
        }
 
230
        err := r.Stop()
 
231
        return fmt.Errorf("timed out waiting for agent to finish; stop error: %v", err)
 
232
}
 
233
 
 
234
// runStop runs an agent, immediately stops it,
 
235
// and returns the resulting error status.
 
236
func runStop(r runner) error {
 
237
        done := make(chan error, 1)
 
238
        go func() {
 
239
                done <- r.Run(nil)
 
240
        }()
 
241
        go func() {
 
242
                done <- r.Stop()
 
243
        }()
 
244
        select {
 
245
        case err := <-done:
 
246
                return err
 
247
        case <-time.After(5 * time.Second):
 
248
        }
 
249
        return fmt.Errorf("timed out waiting for agent to finish")
 
250
}
 
251
 
 
252
type entity interface {
 
253
        EntityName() string
 
254
        SetPassword(string) error
 
255
}
 
256
 
 
257
func testAgentPasswordChanging(s *testing.JujuConnSuite, c *C, ent entity, dataDir string, newAgent func(initialPassword string) runner) {
 
258
        // Check that it starts initially and changes the password
 
259
        err := ent.SetPassword("initial")
 
260
        c.Assert(err, IsNil)
 
261
 
 
262
        err = runStop(newAgent("initial"))
 
263
        c.Assert(err, IsNil)
 
264
 
 
265
        // Check that we can no longer gain access with the initial password.
 
266
        info := s.StateInfo(c)
 
267
        info.EntityName = ent.EntityName()
 
268
        info.Password = "initial"
 
269
        testOpenState(c, info, state.ErrUnauthorized)
 
270
 
 
271
        // Read the password file and check that we can connect it.
 
272
        pwfile := filepath.Join(environs.AgentDir(dataDir, ent.EntityName()), "password")
 
273
        data, err := ioutil.ReadFile(pwfile)
 
274
        c.Assert(err, IsNil)
 
275
        newPassword := string(data)
 
276
 
 
277
        info.Password = newPassword
 
278
        testOpenState(c, info, nil)
 
279
 
 
280
        // Check that it starts again ok
 
281
        err = runStop(newAgent("initial"))
 
282
        c.Assert(err, IsNil)
 
283
 
 
284
        // Change the password file and check
 
285
        // that it falls back to using the initial password
 
286
        err = ioutil.WriteFile(pwfile, []byte("spurious"), 0700)
 
287
        c.Assert(err, IsNil)
 
288
        err = runStop(newAgent(newPassword))
 
289
        c.Assert(err, IsNil)
 
290
 
 
291
        // Check that it's changed the password again
 
292
        data, err = ioutil.ReadFile(pwfile)
 
293
        c.Assert(err, IsNil)
 
294
        c.Assert(string(data), Not(Equals), "spurious")
 
295
        c.Assert(string(data), Not(Equals), newPassword)
 
296
 
 
297
        info.Password = string(data)
 
298
        testOpenState(c, info, nil)
 
299
}