github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/apiserver/common/testing/block.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 "fmt" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/api" 13 "github.com/juju/juju/api/block" 14 "github.com/juju/juju/apiserver/params" 15 "github.com/juju/juju/state/multiwatcher" 16 ) 17 18 // BlockHelper helps manage blocks for apiserver tests. 19 // It provides easy access to switch blocks on 20 // as well as test whether operations are blocked or not. 21 type BlockHelper struct { 22 ApiState api.Connection 23 client *block.Client 24 } 25 26 // NewBlockHelper creates a block switch used in testing 27 // to manage desired juju blocks. 28 func NewBlockHelper(st api.Connection) BlockHelper { 29 return BlockHelper{ 30 ApiState: st, 31 client: block.NewClient(st), 32 } 33 } 34 35 // on switches on desired block and 36 // asserts that no errors were encountered. 37 func (s BlockHelper) on(c *gc.C, blockType multiwatcher.BlockType, msg string) { 38 c.Assert( 39 s.client.SwitchBlockOn( 40 fmt.Sprintf("%v", blockType), 41 msg), 42 gc.IsNil) 43 } 44 45 // BlockAllChanges blocks all operations that could change environment. 46 func (s BlockHelper) BlockAllChanges(c *gc.C, msg string) { 47 s.on(c, multiwatcher.BlockChange, msg) 48 } 49 50 // BlockRemoveObject blocks all operations that remove 51 // machines, services, units or relations. 52 func (s BlockHelper) BlockRemoveObject(c *gc.C, msg string) { 53 s.on(c, multiwatcher.BlockRemove, msg) 54 } 55 56 func (s BlockHelper) Close() { 57 s.client.Close() 58 s.ApiState.Close() 59 } 60 61 // BlockDestroyEnvironment blocks destroy-environment. 62 func (s BlockHelper) BlockDestroyEnvironment(c *gc.C, msg string) { 63 s.on(c, multiwatcher.BlockDestroy, msg) 64 } 65 66 // AssertBlocked checks if given error is 67 // related to switched block. 68 func (s BlockHelper) AssertBlocked(c *gc.C, err error, msg string) { 69 c.Assert(params.IsCodeOperationBlocked(err), jc.IsTrue) 70 c.Assert(err, gc.ErrorMatches, msg) 71 }