github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 "github.com/juju/errors" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/api" 14 "github.com/juju/juju/api/block" 15 "github.com/juju/juju/apiserver/params" 16 "github.com/juju/juju/state/multiwatcher" 17 ) 18 19 // BlockHelper helps manage blocks for apiserver tests. 20 // It provides easy access to switch blocks on 21 // as well as test whether operations are blocked or not. 22 type BlockHelper struct { 23 apiState api.Connection 24 client *block.Client 25 } 26 27 // NewBlockHelper creates a block switch used in testing 28 // to manage desired juju blocks. 29 func NewBlockHelper(st api.Connection) BlockHelper { 30 return BlockHelper{ 31 apiState: st, 32 client: block.NewClient(st), 33 } 34 } 35 36 // on switches on desired block and 37 // asserts that no errors were encountered. 38 func (s BlockHelper) on(c *gc.C, blockType multiwatcher.BlockType, msg string) { 39 c.Assert(s.client.SwitchBlockOn(fmt.Sprintf("%v", blockType), msg), gc.IsNil) 40 } 41 42 // BlockAllChanges blocks all operations that could change environment. 43 func (s BlockHelper) BlockAllChanges(c *gc.C, msg string) { 44 s.on(c, multiwatcher.BlockChange, msg) 45 } 46 47 // BlockRemoveObject blocks all operations that remove 48 // machines, services, units or relations. 49 func (s BlockHelper) BlockRemoveObject(c *gc.C, msg string) { 50 s.on(c, multiwatcher.BlockRemove, msg) 51 } 52 53 func (s BlockHelper) Close() { 54 s.client.Close() 55 s.apiState.Close() 56 } 57 58 // BlockDestroyModel blocks destroy-model. 59 func (s BlockHelper) BlockDestroyModel(c *gc.C, msg string) { 60 s.on(c, multiwatcher.BlockDestroy, msg) 61 } 62 63 // AssertBlocked checks if given error is 64 // related to switched block. 65 func (s BlockHelper) AssertBlocked(c *gc.C, err error, msg string) { 66 c.Assert(params.IsCodeOperationBlocked(err), jc.IsTrue, gc.Commentf("error: %#v", err)) 67 c.Assert(errors.Cause(err), gc.DeepEquals, ¶ms.Error{ 68 Message: msg, 69 Code: "operation is blocked", 70 }) 71 }