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"
16
19
var _ = Suite(&agentSuite{})
18
21
type agentSuite struct {
22
coretesting.LoggingSuite
22
25
func assertDead(c *C, tasks []*testTask) {
207
210
return initCmd(ac, append(common, args...))
213
type runner interface {
214
Run(*cmd.Context) error
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)
228
case <-time.After(5 * time.Second):
231
return fmt.Errorf("timed out waiting for agent to finish; stop error: %v", err)
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)
247
case <-time.After(5 * time.Second):
249
return fmt.Errorf("timed out waiting for agent to finish")
252
type entity interface {
254
SetPassword(string) error
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")
262
err = runStop(newAgent("initial"))
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)
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)
275
newPassword := string(data)
277
info.Password = newPassword
278
testOpenState(c, info, nil)
280
// Check that it starts again ok
281
err = runStop(newAgent("initial"))
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)
288
err = runStop(newAgent(newPassword))
291
// Check that it's changed the password again
292
data, err = ioutil.ReadFile(pwfile)
294
c.Assert(string(data), Not(Equals), "spurious")
295
c.Assert(string(data), Not(Equals), newPassword)
297
info.Password = string(data)
298
testOpenState(c, info, nil)