Merge lp:~axwalk/juju-core/fix-debug-hooks-tests into lp:~go-bot/juju-core/trunk

Proposed by Andrew Wilkins
Status: Merged
Approved by: Andrew Wilkins
Approved revision: no longer in the source branch.
Merged at revision: 1818
Proposed branch: lp:~axwalk/juju-core/fix-debug-hooks-tests
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 183 lines (+67/-53)
2 files modified
worker/uniter/debug/server.go (+8/-2)
worker/uniter/debug/server_test.go (+59/-51)
To merge this branch: bzr merge lp:~axwalk/juju-core/fix-debug-hooks-tests
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+185423@code.launchpad.net

Commit message

worker/apiuniter/debug: fix debug-hook tests

DebugHooksServerSuite.TestRunHookExceptional has
been changed to no longer be timing-based at all,
which has the added benefit of speeding up the
tests by 1s.

DebugHooksServerSuite.TestRunHook is still timing
based (it shells out, so mocking isn't really
an option here), but the timeout is significantly
increased (to 5s, from 100ms).

Fixes #1224768

https://codereview.appspot.com/13549045/

Description of the change

worker/apiuniter/debug: fix debug-hook tests

DebugHooksServerSuite.TestRunHookExceptional has
been changed to no longer be timing-based at all,
which has the added benefit of speeding up the
tests by 1s.

DebugHooksServerSuite.TestRunHook is still timing
based (it shells out, so mocking isn't really
an option here), but the timeout is significantly
increased (to 5s, from 100ms).

Fixes #1224768

https://codereview.appspot.com/13549045/

To post a comment you must log in.
Revision history for this message
Andrew Wilkins (axwalk) wrote :

Reviewers: mp+185423_code.launchpad.net,

Message:
Please take a look.

Description:
worker/apiuniter/debug: fix debug-hook tests

DebugHooksServerSuite.TestRunHookExceptional has
been changed to no longer be timing-based at all,
which has the added benefit of speeding up the
tests by 1s.

DebugHooksServerSuite.TestRunHook is still timing
based (it shells out, so mocking isn't really
an option here), but the timeout is significantly
increased (to 5s, from 100ms).

Fixes #1224768

https://code.launchpad.net/~axwalk/juju-core/fix-debug-hooks-tests/+merge/185423

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/13549045/

Affected files (+69, -55 lines):
   A [revision details]
   M worker/apiuniter/debug/server.go
   M worker/apiuniter/debug/server_test.go

Revision history for this message
William Reade (fwereade) wrote :
Revision history for this message
Dimiter Naydenov (dimitern) wrote :

NOT LGTM, please don't land this - worker/apiuniter is gone.

https://codereview.appspot.com/13549045/

Revision history for this message
Andrew Wilkins (axwalk) wrote :

On 2013/09/13 10:46:10, dimitern wrote:
> NOT LGTM, please don't land this - worker/apiuniter is gone.

Yep, no worries. I'll redo this on worker/api/debug after the weekend.

https://codereview.appspot.com/13549045/

Revision history for this message
Andrew Wilkins (axwalk) wrote :

On 2013/09/13 10:55:20, axw1 wrote:
> On 2013/09/13 10:46:10, dimitern wrote:
> > NOT LGTM, please don't land this - worker/apiuniter is gone.

> Yep, no worries. I'll redo this on worker/api/debug after the weekend.

Er, worker/uniter/debug, but you probably got that.

https://codereview.appspot.com/13549045/

Revision history for this message
Andrew Wilkins (axwalk) wrote :

On 2013/09/13 10:55:39, axw1 wrote:
> On 2013/09/13 10:55:20, axw1 wrote:
> > On 2013/09/13 10:46:10, dimitern wrote:
> > > NOT LGTM, please don't land this - worker/apiuniter is gone.
> >
> > Yep, no worries. I'll redo this on worker/api/debug after the
weekend.

> Er, worker/uniter/debug, but you probably got that.

The "debug" directory hadn't changed at all between uniter/apiuniter
(except for jam's changes to skip these breaking tests). My change is
exactly the same as before, but in worker/uniter/debug; I'll go ahead
and land.

https://codereview.appspot.com/13549045/

Revision history for this message
Andrew Wilkins (axwalk) wrote :

On 2013/09/13 10:55:39, axw1 wrote:
> On 2013/09/13 10:55:20, axw1 wrote:
> > On 2013/09/13 10:46:10, dimitern wrote:
> > > NOT LGTM, please don't land this - worker/apiuniter is gone.
> >
> > Yep, no worries. I'll redo this on worker/api/debug after the
weekend.

> Er, worker/uniter/debug, but you probably got that.

The "debug" directory hadn't changed at all between uniter/apiuniter
(except for jam's changes to skip these breaking tests). My change is
exactly the same as before, but in worker/uniter/debug; I'll go ahead
and land.

https://codereview.appspot.com/13549045/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'worker/uniter/debug/server.go'
2--- worker/uniter/debug/server.go 2013-08-16 04:10:20 +0000
3+++ worker/uniter/debug/server.go 2013-09-16 01:57:13 +0000
4@@ -27,6 +27,13 @@
5 return s.hooks.IsEmpty() || s.hooks.Contains(hookName)
6 }
7
8+// waitClientExit executes flock, waiting for the SSH client to exit.
9+// This is a var so it can be replaced for testing.
10+var waitClientExit = func(s *ServerSession) {
11+ path := s.ClientExitFileLock()
12+ exec.Command("flock", path, "-c", "true").Run()
13+}
14+
15 // RunHook "runs" the hook with the specified name via debug-hooks.
16 func (s *ServerSession) RunHook(hookName, charmDir string, env []string) error {
17 env = append(env, "JUJU_HOOK_NAME="+hookName)
18@@ -41,8 +48,7 @@
19 // Wait for the SSH client to exit (i.e. release the flock),
20 // then kill the server hook process in case the client
21 // exited uncleanly.
22- path := s.ClientExitFileLock()
23- exec.Command("flock", path, "-c", "true").Run()
24+ waitClientExit(s)
25 proc.Kill()
26 }(cmd.Process)
27 return cmd.Wait()
28
29=== modified file 'worker/uniter/debug/server_test.go'
30--- worker/uniter/debug/server_test.go 2013-08-20 01:32:02 +0000
31+++ worker/uniter/debug/server_test.go 2013-09-16 01:57:13 +0000
32@@ -1,7 +1,7 @@
33 // Copyright 2013 Canonical Ltd.
34 // Licensed under the AGPLv3, see LICENCE file for details.
35
36-package debug_test
37+package debug
38
39 import (
40 "fmt"
41@@ -15,11 +15,10 @@
42 gc "launchpad.net/gocheck"
43
44 jc "launchpad.net/juju-core/testing/checkers"
45- "launchpad.net/juju-core/worker/uniter/debug"
46 )
47
48 type DebugHooksServerSuite struct {
49- ctx *debug.HooksContext
50+ ctx *HooksContext
51 fakebin string
52 tmpdir string
53 oldenv []string
54@@ -51,7 +50,7 @@
55 err := ioutil.WriteFile(filepath.Join(s.fakebin, name), []byte(echocommand), 0777)
56 c.Assert(err, gc.IsNil)
57 }
58- s.ctx = debug.NewHooksContext("foo/8")
59+ s.ctx = NewHooksContext("foo/8")
60 s.ctx.FlockDir = s.tmpdir
61 s.setenv("JUJU_UNIT_NAME", s.ctx.Unit)
62 }
63@@ -118,18 +117,20 @@
64 err = session.RunHook("myhook", s.tmpdir, os.Environ())
65 c.Assert(err, gc.ErrorMatches, "signal: killed")
66
67- // Run the hook in debug mode with the exit flock held.
68- // This simulates the client process starting but not
69- // cleanly exiting (normally the .pid file is updated,
70- // and the server waits on the client process' death).
71- cmd := exec.Command("flock", s.ctx.ClientExitFileLock(), "-c", "sleep 1s")
72- c.Assert(cmd.Start(), gc.IsNil)
73- expected := time.Now().Add(time.Second)
74+ // Run the hook in debug mode, simulating the holding
75+ // of the exit flock. This simulates the client process
76+ // starting but not cleanly exiting (normally the .pid
77+ // file is updated, and the server waits on the client
78+ // process' death).
79+ ch := make(chan bool)
80+ var clientExited bool
81+ defer jc.Set(&waitClientExit, func(*ServerSession) {
82+ clientExited = <-ch
83+ })()
84+ go func() { ch <- true }()
85 err = session.RunHook("myhook", s.tmpdir, os.Environ())
86- after := time.Now()
87- c.Assert(after, jc.TimeBetween(expected.Add(-100*time.Millisecond), expected.Add(100*time.Millisecond)))
88+ c.Assert(clientExited, jc.IsTrue)
89 c.Assert(err, gc.ErrorMatches, "signal: killed")
90- c.Assert(cmd.Wait(), gc.IsNil)
91 }
92
93 func (s *DebugHooksServerSuite) TestRunHook(c *gc.C) {
94@@ -151,46 +152,53 @@
95 go func() {
96 ch <- session.RunHook(hookName, s.tmpdir, os.Environ())
97 }()
98+
99+ // Wait until either we find the debug dir, or the flock is released.
100+ ticker := time.Tick(10 * time.Millisecond)
101 var debugdir os.FileInfo
102- for i := 0; i < 10; i++ {
103- tmpdir, err := os.Open(s.tmpdir)
104- if err != nil {
105- c.Fatalf("Failed to open $TMPDIR: %s", err)
106- }
107- fi, err := tmpdir.Readdir(-1)
108- if err != nil {
109- c.Fatalf("Failed to read $TMPDIR: %s", err)
110- }
111- tmpdir.Close()
112- for _, fi := range fi {
113- if fi.IsDir() {
114- hooksh := filepath.Join(s.tmpdir, fi.Name(), "hook.sh")
115- if _, err = os.Stat(hooksh); err == nil {
116- debugdir = fi
117- break
118+ for debugdir == nil {
119+ select {
120+ case err = <-ch:
121+ // flock was released before we found the debug dir.
122+ c.Error("could not find hook.sh")
123+
124+ case <-ticker:
125+ tmpdir, err := os.Open(s.tmpdir)
126+ if err != nil {
127+ c.Fatalf("Failed to open $TMPDIR: %s", err)
128+ }
129+ fi, err := tmpdir.Readdir(-1)
130+ if err != nil {
131+ c.Fatalf("Failed to read $TMPDIR: %s", err)
132+ }
133+ tmpdir.Close()
134+ for _, fi := range fi {
135+ if fi.IsDir() {
136+ hooksh := filepath.Join(s.tmpdir, fi.Name(), "hook.sh")
137+ if _, err = os.Stat(hooksh); err == nil {
138+ debugdir = fi
139+ break
140+ }
141 }
142 }
143- }
144- if debugdir != nil {
145- break
146- }
147- time.Sleep(10 * time.Millisecond)
148- }
149- if debugdir == nil {
150- c.Error("could not find hook.sh")
151- } else {
152- envsh := filepath.Join(s.tmpdir, debugdir.Name(), "env.sh")
153- s.verifyEnvshFile(c, envsh, hookName)
154-
155- hookpid := filepath.Join(s.tmpdir, debugdir.Name(), "hook.pid")
156- err = ioutil.WriteFile(hookpid, []byte("not a pid"), 0777)
157- c.Assert(err, gc.IsNil)
158-
159- // RunHook should complete without waiting to be
160- // killed, and despite the exit lock being held.
161- err = <-ch
162- c.Assert(err, gc.IsNil)
163- }
164+ if debugdir != nil {
165+ break
166+ }
167+ time.Sleep(10 * time.Millisecond)
168+ }
169+ }
170+
171+ envsh := filepath.Join(s.tmpdir, debugdir.Name(), "env.sh")
172+ s.verifyEnvshFile(c, envsh, hookName)
173+
174+ hookpid := filepath.Join(s.tmpdir, debugdir.Name(), "hook.pid")
175+ err = ioutil.WriteFile(hookpid, []byte("not a pid"), 0777)
176+ c.Assert(err, gc.IsNil)
177+
178+ // RunHook should complete without waiting to be
179+ // killed, and despite the exit lock being held.
180+ err = <-ch
181+ c.Assert(err, gc.IsNil)
182 cmd.Process.Kill() // kill flock
183 }
184

Subscribers

People subscribed via source and target branches

to status/vote changes: