github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/payload/context/context_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package context_test 5 6 import ( 7 "fmt" 8 9 "github.com/juju/errors" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/payload" 14 "github.com/juju/juju/payload/context" 15 jujuctesting "github.com/juju/juju/worker/uniter/runner/jujuc/testing" 16 ) 17 18 type contextSuite struct { 19 baseSuite 20 compCtx *context.Context 21 apiClient *stubAPIClient 22 dataDir string 23 } 24 25 var _ = gc.Suite(&contextSuite{}) 26 27 func (s *contextSuite) SetUpTest(c *gc.C) { 28 s.baseSuite.SetUpTest(c) 29 30 s.apiClient = newStubAPIClient(s.Stub) 31 s.dataDir = "some-data-dir" 32 s.compCtx = context.NewContext(s.apiClient, s.dataDir) 33 34 context.AddPayloads(s.compCtx, s.payload) 35 } 36 37 func (s *contextSuite) newContext(c *gc.C, payloads ...payload.Payload) *context.Context { 38 ctx := context.NewContext(s.apiClient, s.dataDir) 39 for _, pl := range payloads { 40 c.Logf("adding payload: %s", pl.FullID()) 41 context.AddPayload(ctx, pl.FullID(), pl) 42 } 43 return ctx 44 } 45 46 func (s *contextSuite) TestNewContextEmpty(c *gc.C) { 47 ctx := context.NewContext(s.apiClient, s.dataDir) 48 payloads, err := ctx.Payloads() 49 c.Assert(err, jc.ErrorIsNil) 50 51 c.Check(payloads, gc.HasLen, 0) 52 } 53 54 func (s *contextSuite) TestNewContextPrePopulated(c *gc.C) { 55 expected := []payload.Payload{ 56 s.newPayload("A", "myplugin", "spam", "running"), 57 s.newPayload("B", "myplugin", "eggs", "running"), 58 } 59 60 ctx := s.newContext(c, expected...) 61 payloads, err := ctx.Payloads() 62 c.Assert(err, jc.ErrorIsNil) 63 64 c.Assert(payloads, gc.HasLen, 2) 65 66 // Map ordering is indeterminate, so this if-else is needed. 67 if payloads[0].Name == "A" { 68 c.Check(payloads[0], jc.DeepEquals, expected[0]) 69 c.Check(payloads[1], jc.DeepEquals, expected[1]) 70 } else { 71 c.Check(payloads[0], jc.DeepEquals, expected[1]) 72 c.Check(payloads[1], jc.DeepEquals, expected[0]) 73 } 74 } 75 76 func (s *contextSuite) TestNewContextAPIOkay(c *gc.C) { 77 expected := s.apiClient.setNew("A/xyx123") 78 79 ctx, err := context.NewContextAPI(s.apiClient, s.dataDir) 80 c.Assert(err, jc.ErrorIsNil) 81 82 payloads, err := ctx.Payloads() 83 c.Assert(err, jc.ErrorIsNil) 84 85 c.Check(payloads, jc.DeepEquals, expected) 86 } 87 88 func (s *contextSuite) TestNewContextAPICalls(c *gc.C) { 89 s.apiClient.setNew("A/xyz123") 90 91 _, err := context.NewContextAPI(s.apiClient, s.dataDir) 92 c.Assert(err, jc.ErrorIsNil) 93 94 s.Stub.CheckCallNames(c, "List") 95 } 96 97 func (s *contextSuite) TestNewContextAPIEmpty(c *gc.C) { 98 ctx, err := context.NewContextAPI(s.apiClient, s.dataDir) 99 c.Assert(err, jc.ErrorIsNil) 100 101 payloads, err := ctx.Payloads() 102 c.Assert(err, jc.ErrorIsNil) 103 104 c.Check(payloads, gc.HasLen, 0) 105 } 106 107 func (s *contextSuite) TestNewContextAPIError(c *gc.C) { 108 expected := errors.Errorf("<failed>") 109 s.Stub.SetErrors(expected) 110 111 _, err := context.NewContextAPI(s.apiClient, s.dataDir) 112 113 c.Check(errors.Cause(err), gc.Equals, expected) 114 s.Stub.CheckCallNames(c, "List") 115 } 116 117 func (s *contextSuite) TestContextComponentOkay(c *gc.C) { 118 hctx, info := s.NewHookContext() 119 expected := context.NewContext(s.apiClient, s.dataDir) 120 info.SetComponent(payload.ComponentName, expected) 121 122 compCtx, err := context.ContextComponent(hctx) 123 c.Assert(err, jc.ErrorIsNil) 124 125 c.Check(compCtx, gc.Equals, expected) 126 s.Stub.CheckCallNames(c, "Component") 127 s.Stub.CheckCall(c, 0, "Component", payload.ComponentName) 128 } 129 130 func (s *contextSuite) TestContextComponentMissing(c *gc.C) { 131 hctx, _ := s.NewHookContext() 132 _, err := context.ContextComponent(hctx) 133 134 c.Check(err, gc.ErrorMatches, fmt.Sprintf("component %q not registered", payload.ComponentName)) 135 s.Stub.CheckCallNames(c, "Component") 136 } 137 138 func (s *contextSuite) TestContextComponentWrong(c *gc.C) { 139 hctx, info := s.NewHookContext() 140 compCtx := &jujuctesting.ContextComponent{} 141 info.SetComponent(payload.ComponentName, compCtx) 142 143 _, err := context.ContextComponent(hctx) 144 145 c.Check(err, gc.ErrorMatches, "wrong component context type registered: .*") 146 s.Stub.CheckCallNames(c, "Component") 147 } 148 149 func (s *contextSuite) TestContextComponentDisabled(c *gc.C) { 150 hctx, info := s.NewHookContext() 151 info.SetComponent(payload.ComponentName, nil) 152 153 _, err := context.ContextComponent(hctx) 154 155 c.Check(err, gc.ErrorMatches, fmt.Sprintf("component %q disabled", payload.ComponentName)) 156 s.Stub.CheckCallNames(c, "Component") 157 } 158 159 func (s *contextSuite) TestPayloadsOkay(c *gc.C) { 160 expected := []payload.Payload{ 161 s.newPayload("A", "myplugin", "spam", "running"), 162 s.newPayload("B", "myplugin", "eggs", "running"), 163 s.newPayload("C", "myplugin", "ham", "running"), 164 } 165 166 ctx := s.newContext(c, expected...) 167 payloads, err := ctx.Payloads() 168 c.Assert(err, jc.ErrorIsNil) 169 170 checkPayloads(c, payloads, expected) 171 s.Stub.CheckCallNames(c) 172 } 173 174 func (s *contextSuite) TestPayloadsAPI(c *gc.C) { 175 expected := s.apiClient.setNew("A/spam", "B/eggs", "C/ham") 176 177 ctx := context.NewContext(s.apiClient, s.dataDir) 178 context.AddPayload(ctx, "A/spam", s.apiClient.payloads["A/spam"]) 179 context.AddPayload(ctx, "B/eggs", s.apiClient.payloads["B/eggs"]) 180 context.AddPayload(ctx, "C/ham", s.apiClient.payloads["C/ham"]) 181 182 payloads, err := ctx.Payloads() 183 c.Assert(err, jc.ErrorIsNil) 184 185 checkPayloads(c, payloads, expected) 186 s.Stub.CheckCallNames(c) 187 } 188 189 func (s *contextSuite) TestPayloadsEmpty(c *gc.C) { 190 ctx := context.NewContext(s.apiClient, s.dataDir) 191 payloads, err := ctx.Payloads() 192 c.Assert(err, jc.ErrorIsNil) 193 194 c.Check(payloads, gc.HasLen, 0) 195 s.Stub.CheckCallNames(c) 196 } 197 198 func (s *contextSuite) TestPayloadsAdditions(c *gc.C) { 199 expected := s.apiClient.setNew("A/spam", "B/eggs") 200 plC := s.newPayload("C", "myplugin", "xyz789", "running") 201 plD := s.newPayload("D", "myplugin", "xyzabc", "running") 202 expected = append(expected, plC, plD) 203 204 ctx := s.newContext(c, expected[0]) 205 context.AddPayload(ctx, "B/eggs", s.apiClient.payloads["B/eggs"]) 206 ctx.Track(plC) 207 ctx.Track(plD) 208 209 payloads, err := ctx.Payloads() 210 c.Assert(err, jc.ErrorIsNil) 211 212 checkPayloads(c, payloads, expected) 213 s.Stub.CheckCallNames(c) 214 } 215 216 func (s *contextSuite) TestPayloadsOverrides(c *gc.C) { 217 expected := s.apiClient.setNew("A/xyz123", "B/something-else") 218 plB := s.newPayload("B", "myplugin", "xyz456", "running") 219 plC := s.newPayload("C", "myplugin", "xyz789", "running") 220 expected = append(expected[:1], plB, plC) 221 222 ctx := context.NewContext(s.apiClient, s.dataDir) 223 context.AddPayload(ctx, "A/xyz123", s.apiClient.payloads["A/xyz123"]) 224 context.AddPayload(ctx, "B/xyz456", plB) 225 ctx.Track(plB) 226 ctx.Track(plC) 227 228 payloads, err := ctx.Payloads() 229 c.Assert(err, jc.ErrorIsNil) 230 231 checkPayloads(c, payloads, expected) 232 s.Stub.CheckCallNames(c) 233 } 234 235 func (s *contextSuite) TestGetOkay(c *gc.C) { 236 expected := s.newPayload("A", "myplugin", "spam", "running") 237 extra := s.newPayload("B", "myplugin", "eggs", "running") 238 239 ctx := s.newContext(c, expected, extra) 240 pl, err := ctx.Get("A", "spam") 241 c.Assert(err, jc.ErrorIsNil) 242 243 c.Check(*pl, jc.DeepEquals, expected) 244 s.Stub.CheckCallNames(c) 245 } 246 247 func (s *contextSuite) TestGetOverride(c *gc.C) { 248 payloads := s.apiClient.setNew("A/spam", "B/eggs") 249 expected := payloads[0] 250 251 unexpected := expected 252 unexpected.ID = "C" 253 254 ctx := s.newContext(c, payloads[1]) 255 context.AddPayload(ctx, "A/spam", unexpected) 256 context.AddPayload(ctx, "A/spam", expected) 257 258 pl, err := ctx.Get("A", "spam") 259 c.Assert(err, jc.ErrorIsNil) 260 261 c.Check(*pl, jc.DeepEquals, expected) 262 s.Stub.CheckCallNames(c) 263 } 264 265 func (s *contextSuite) TestGetNotFound(c *gc.C) { 266 ctx := context.NewContext(s.apiClient, s.dataDir) 267 _, err := ctx.Get("A", "spam") 268 269 c.Check(err, jc.Satisfies, errors.IsNotFound) 270 } 271 272 func (s *contextSuite) TestSetOkay(c *gc.C) { 273 pl := s.newPayload("A", "myplugin", "spam", "running") 274 ctx := context.NewContext(s.apiClient, s.dataDir) 275 before, err := ctx.Payloads() 276 c.Assert(err, jc.ErrorIsNil) 277 err = ctx.Track(pl) 278 c.Assert(err, jc.ErrorIsNil) 279 after, err := ctx.Payloads() 280 c.Assert(err, jc.ErrorIsNil) 281 282 c.Check(before, gc.HasLen, 0) 283 c.Check(after, jc.DeepEquals, []payload.Payload{pl}) 284 } 285 286 func (s *contextSuite) TestSetOverwrite(c *gc.C) { 287 pl := s.newPayload("A", "myplugin", "xyz123", "running") 288 other := s.newPayload("A", "myplugin", "xyz123", "stopped") 289 ctx := s.newContext(c, other) 290 before, err := ctx.Payloads() 291 c.Assert(err, jc.ErrorIsNil) 292 err = ctx.Track(pl) 293 c.Assert(err, jc.ErrorIsNil) 294 after, err := ctx.Payloads() 295 c.Assert(err, jc.ErrorIsNil) 296 297 c.Check(before, jc.DeepEquals, []payload.Payload{other}) 298 c.Check(after, jc.DeepEquals, []payload.Payload{pl}) 299 } 300 301 func (s *contextSuite) TestFlushNotDirty(c *gc.C) { 302 pl := s.newPayload("flush-not-dirty", "myplugin", "xyz123", "running") 303 ctx := s.newContext(c, pl) 304 305 err := ctx.Flush() 306 c.Assert(err, jc.ErrorIsNil) 307 308 s.Stub.CheckCallNames(c) 309 } 310 311 func (s *contextSuite) TestFlushEmpty(c *gc.C) { 312 ctx := context.NewContext(s.apiClient, s.dataDir) 313 err := ctx.Flush() 314 c.Assert(err, jc.ErrorIsNil) 315 316 s.Stub.CheckCallNames(c) 317 } 318 319 func (s *contextSuite) TestUntrackNoMatch(c *gc.C) { 320 pl := s.newPayload("A", "myplugin", "spam", "running") 321 ctx := context.NewContext(s.apiClient, s.dataDir) 322 err := ctx.Track(pl) 323 c.Assert(err, jc.ErrorIsNil) 324 before, err := ctx.Payloads() 325 c.Assert(err, jc.ErrorIsNil) 326 c.Check(before, jc.DeepEquals, []payload.Payload{pl}) 327 ctx.Untrack("uh-oh", "not gonna match") 328 after, err := ctx.Payloads() 329 c.Assert(err, jc.ErrorIsNil) 330 c.Assert(after, gc.DeepEquals, before) 331 }