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

Delta Between Two Patch Sets: worker/deployer/deployer_test.go

Issue 10447045: Add machine instance metadata to state (Closed)
Left Patch Set: Add machine instance metadata to state Created 11 years, 9 months ago
Right Patch Set: Add machine instance metadata to state Created 11 years, 9 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 // Copyright 2012, 2013 Canonical Ltd. 1 // Copyright 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 deployer_test 4 package deployer_test
5 5
6 import ( 6 import (
7 "sort" 7 "sort"
8 "strings" 8 "strings"
9 stdtesting "testing" 9 stdtesting "testing"
10 "time" 10 "time"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 c.Assert(err, IsNil) 49 c.Assert(err, IsNil)
50 svc, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpr ess")) 50 svc, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpr ess"))
51 c.Assert(err, IsNil) 51 c.Assert(err, IsNil)
52 u0, err := svc.AddUnit() 52 u0, err := svc.AddUnit()
53 c.Assert(err, IsNil) 53 c.Assert(err, IsNil)
54 u1, err := svc.AddUnit() 54 u1, err := svc.AddUnit()
55 c.Assert(err, IsNil) 55 c.Assert(err, IsNil)
56 56
57 // Create a deployer acting on behalf of the machine. 57 // Create a deployer acting on behalf of the machine.
58 ctx := s.getContext(c, m.Tag()) 58 ctx := s.getContext(c, m.Tag())
59 » dep := deployer.NewDeployer(s.State, ctx, m.WatchPrincipalUnits()) 59 » dep := deployer.NewDeployer(s.State, ctx, m.Id())
60 defer stop(c, dep) 60 defer stop(c, dep)
61 61
62 // Assign one unit, and wait for it to be deployed. 62 // Assign one unit, and wait for it to be deployed.
63 err = u0.AssignToMachine(m) 63 err = u0.AssignToMachine(m)
64 c.Assert(err, IsNil) 64 c.Assert(err, IsNil)
65 s.waitFor(c, isDeployed(ctx, u0.Name())) 65 s.waitFor(c, isDeployed(ctx, u0.Name()))
66 66
67 // Assign another unit, and wait for that to be deployed. 67 // Assign another unit, and wait for that to be deployed.
68 err = u1.AssignToMachine(m) 68 err = u1.AssignToMachine(m)
69 c.Assert(err, IsNil) 69 c.Assert(err, IsNil)
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 // have been deployed. But it's instructive to check that the right thin g 117 // have been deployed. But it's instructive to check that the right thin g
118 // would happen if it were possible to have a dying unit in this situati on. 118 // would happen if it were possible to have a dying unit in this situati on.
119 err = u1.SetStatus(params.StatusInstalled, "") 119 err = u1.SetStatus(params.StatusInstalled, "")
120 c.Assert(err, IsNil) 120 c.Assert(err, IsNil)
121 err = u1.Destroy() 121 err = u1.Destroy()
122 c.Assert(err, IsNil) 122 c.Assert(err, IsNil)
123 123
124 // When the deployer is started, in each case (1) no unit agent is deplo yed 124 // When the deployer is started, in each case (1) no unit agent is deplo yed
125 // and (2) the non-Alive unit is been removed from state. 125 // and (2) the non-Alive unit is been removed from state.
126 ctx := s.getContext(c, m.Tag()) 126 ctx := s.getContext(c, m.Tag())
127 » dep := deployer.NewDeployer(s.State, ctx, m.WatchPrincipalUnits()) 127 » dep := deployer.NewDeployer(s.State, ctx, m.Id())
128 defer stop(c, dep) 128 defer stop(c, dep)
129 s.waitFor(c, isRemoved(s.State, u0.Name())) 129 s.waitFor(c, isRemoved(s.State, u0.Name()))
130 s.waitFor(c, isRemoved(s.State, u1.Name())) 130 s.waitFor(c, isRemoved(s.State, u1.Name()))
131 s.waitFor(c, isDeployed(ctx)) 131 s.waitFor(c, isDeployed(ctx))
132 } 132 }
133 133
134 func (s *DeployerSuite) prepareSubordinates(c *C) (*state.Unit, []*state.Relatio nUnit) { 134 func (s *DeployerSuite) prepareSubordinates(c *C) (*state.Unit, []*state.Relatio nUnit) {
135 m, err := s.State.AddMachine("series", state.JobHostUnits)
136 c.Assert(err, IsNil)
135 svc, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpr ess")) 137 svc, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpr ess"))
136 c.Assert(err, IsNil) 138 c.Assert(err, IsNil)
137 u, err := svc.AddUnit() 139 u, err := svc.AddUnit()
140 c.Assert(err, IsNil)
141 err = u.AssignToMachine(m)
138 c.Assert(err, IsNil) 142 c.Assert(err, IsNil)
139 rus := []*state.RelationUnit{} 143 rus := []*state.RelationUnit{}
140 logging := s.AddTestingCharm(c, "logging") 144 logging := s.AddTestingCharm(c, "logging")
141 for _, name := range []string{"subsvc0", "subsvc1"} { 145 for _, name := range []string{"subsvc0", "subsvc1"} {
142 _, err := s.State.AddService(name, logging) 146 _, err := s.State.AddService(name, logging)
143 c.Assert(err, IsNil) 147 c.Assert(err, IsNil)
144 eps, err := s.State.InferEndpoints([]string{"wordpress", name}) 148 eps, err := s.State.InferEndpoints([]string{"wordpress", name})
145 c.Assert(err, IsNil) 149 c.Assert(err, IsNil)
146 rel, err := s.State.AddRelation(eps...) 150 rel, err := s.State.AddRelation(eps...)
147 c.Assert(err, IsNil) 151 c.Assert(err, IsNil)
148 ru, err := rel.Unit(u) 152 ru, err := rel.Unit(u)
149 c.Assert(err, IsNil) 153 c.Assert(err, IsNil)
150 rus = append(rus, ru) 154 rus = append(rus, ru)
151 } 155 }
152 return u, rus 156 return u, rus
153 } 157 }
154 158
155 func (s *DeployerSuite) TestDeployRecallRemoveSubordinates(c *C) { 159 func (s *DeployerSuite) TestDeployRecallRemoveSubordinates(c *C) {
156 // Create a deployer acting on behalf of the principal. 160 // Create a deployer acting on behalf of the principal.
157 u, rus := s.prepareSubordinates(c) 161 u, rus := s.prepareSubordinates(c)
158 ctx := s.getContext(c, u.Tag()) 162 ctx := s.getContext(c, u.Tag())
159 » dep := deployer.NewDeployer(s.State, ctx, u.WatchSubordinateUnits()) 163 » machineId, err := u.AssignedMachineId()
164 » c.Assert(err, IsNil)
165 » dep := deployer.NewDeployer(s.State, ctx, machineId)
160 defer stop(c, dep) 166 defer stop(c, dep)
161 167
162 // Add a subordinate, and wait for it to be deployed. 168 // Add a subordinate, and wait for it to be deployed.
163 » err := rus[0].EnterScope(nil) 169 » err = rus[0].EnterScope(nil)
164 c.Assert(err, IsNil) 170 c.Assert(err, IsNil)
165 sub0, err := s.State.Unit("subsvc0/0") 171 sub0, err := s.State.Unit("subsvc0/0")
166 c.Assert(err, IsNil) 172 c.Assert(err, IsNil)
167 » s.waitFor(c, isDeployed(ctx, sub0.Name())) 173 » // Make sure the principal is deployed first, then the subordinate
174 » s.waitFor(c, isDeployed(ctx, u.Name(), sub0.Name()))
168 175
169 // And another. 176 // And another.
170 err = rus[1].EnterScope(nil) 177 err = rus[1].EnterScope(nil)
171 c.Assert(err, IsNil) 178 c.Assert(err, IsNil)
172 sub1, err := s.State.Unit("subsvc1/0") 179 sub1, err := s.State.Unit("subsvc1/0")
173 c.Assert(err, IsNil) 180 c.Assert(err, IsNil)
174 » s.waitFor(c, isDeployed(ctx, sub0.Name(), sub1.Name())) 181 » s.waitFor(c, isDeployed(ctx, u.Name(), sub0.Name(), sub1.Name()))
175 182
176 // Set one to Dying; check nothing happens. 183 // Set one to Dying; check nothing happens.
177 err = sub1.Destroy() 184 err = sub1.Destroy()
178 c.Assert(err, IsNil) 185 c.Assert(err, IsNil)
179 s.State.StartSync() 186 s.State.StartSync()
180 c.Assert(isRemoved(s.State, sub1.Name())(c), Equals, false) 187 c.Assert(isRemoved(s.State, sub1.Name())(c), Equals, false)
181 » s.waitFor(c, isDeployed(ctx, sub0.Name(), sub1.Name())) 188 » s.waitFor(c, isDeployed(ctx, u.Name(), sub0.Name(), sub1.Name()))
182 189
183 // Set the other to Dead; check it's recalled and removed. 190 // Set the other to Dead; check it's recalled and removed.
184 err = sub0.EnsureDead() 191 err = sub0.EnsureDead()
185 c.Assert(err, IsNil) 192 c.Assert(err, IsNil)
186 » s.waitFor(c, isDeployed(ctx, sub1.Name())) 193 » s.waitFor(c, isDeployed(ctx, u.Name(), sub1.Name()))
187 s.waitFor(c, isRemoved(s.State, sub0.Name())) 194 s.waitFor(c, isRemoved(s.State, sub0.Name()))
188 } 195 }
189 196
190 func (s *DeployerSuite) TestNonAliveSubordinates(c *C) { 197 func (s *DeployerSuite) TestNonAliveSubordinates(c *C) {
191 // Add two subordinate units and set them to Dead/Dying respectively. 198 // Add two subordinate units and set them to Dead/Dying respectively.
192 u, rus := s.prepareSubordinates(c) 199 u, rus := s.prepareSubordinates(c)
193 err := rus[0].EnterScope(nil) 200 err := rus[0].EnterScope(nil)
194 c.Assert(err, IsNil) 201 c.Assert(err, IsNil)
195 sub0, err := s.State.Unit("subsvc0/0") 202 sub0, err := s.State.Unit("subsvc0/0")
196 c.Assert(err, IsNil) 203 c.Assert(err, IsNil)
197 err = sub0.EnsureDead() 204 err = sub0.EnsureDead()
198 c.Assert(err, IsNil) 205 c.Assert(err, IsNil)
199 err = rus[1].EnterScope(nil) 206 err = rus[1].EnterScope(nil)
200 c.Assert(err, IsNil) 207 c.Assert(err, IsNil)
201 sub1, err := s.State.Unit("subsvc1/0") 208 sub1, err := s.State.Unit("subsvc1/0")
202 c.Assert(err, IsNil) 209 c.Assert(err, IsNil)
203 err = sub1.Destroy() 210 err = sub1.Destroy()
204 c.Assert(err, IsNil) 211 c.Assert(err, IsNil)
205 212
206 // When we start a new deployer, neither unit will be deployed and 213 // When we start a new deployer, neither unit will be deployed and
207 // both will be removed. 214 // both will be removed.
208 ctx := s.getContext(c, u.Tag()) 215 ctx := s.getContext(c, u.Tag())
209 » dep := deployer.NewDeployer(s.State, ctx, u.WatchSubordinateUnits()) 216 » machineId, err := u.AssignedMachineId()
217 » c.Assert(err, IsNil)
218 » dep := deployer.NewDeployer(s.State, ctx, machineId)
210 defer stop(c, dep) 219 defer stop(c, dep)
211 s.waitFor(c, isRemoved(s.State, sub0.Name())) 220 s.waitFor(c, isRemoved(s.State, sub0.Name()))
212 s.waitFor(c, isRemoved(s.State, sub1.Name())) 221 s.waitFor(c, isRemoved(s.State, sub1.Name()))
213 } 222 }
214 223
215 func (s *DeployerSuite) waitFor(c *C, t func(c *C) bool) { 224 func (s *DeployerSuite) waitFor(c *C, t func(c *C) bool) {
216 s.State.StartSync() 225 s.State.StartSync()
217 if t(c) { 226 if t(c) {
218 return 227 return
219 } 228 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 } 261 }
253 } 262 }
254 263
255 type stopper interface { 264 type stopper interface {
256 Stop() error 265 Stop() error
257 } 266 }
258 267
259 func stop(c *C, stopper stopper) { 268 func stop(c *C, stopper stopper) {
260 c.Assert(stopper.Stop(), IsNil) 269 c.Assert(stopper.Stop(), IsNil)
261 } 270 }
LEFTRIGHT

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