github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/common/cmdblockhelper.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/api"
    13  	"github.com/juju/juju/api/block"
    14  	cmdblock "github.com/juju/juju/cmd/juju/block"
    15  )
    16  
    17  // CmdBlockHelper is a helper struct used to block commands.
    18  type CmdBlockHelper struct {
    19  	blockClient *block.Client
    20  }
    21  
    22  // NewCmdBlockHelper creates a block switch used in testing
    23  // to manage desired juju blocks.
    24  func NewCmdBlockHelper(st api.Connection) CmdBlockHelper {
    25  	return CmdBlockHelper{
    26  		blockClient: block.NewClient(st),
    27  	}
    28  }
    29  
    30  // on switches on desired block and
    31  // asserts that no errors were encountered.
    32  func (s *CmdBlockHelper) on(c *gc.C, blockType, msg string) {
    33  	c.Assert(s.blockClient.SwitchBlockOn(cmdblock.TypeFromOperation(blockType), msg), gc.IsNil)
    34  }
    35  
    36  // BlockAllChanges switches changes block on.
    37  // This prevents all changes to juju environment.
    38  func (s *CmdBlockHelper) BlockAllChanges(c *gc.C, msg string) {
    39  	s.on(c, "all-changes", msg)
    40  }
    41  
    42  // BlockRemoveObject switches remove block on.
    43  // This prevents any object/entity removal on juju environment
    44  func (s *CmdBlockHelper) BlockRemoveObject(c *gc.C, msg string) {
    45  	s.on(c, "remove-object", msg)
    46  }
    47  
    48  // BlockDestroyModel switches destroy block on.
    49  // This prevents juju environment destruction.
    50  func (s *CmdBlockHelper) BlockDestroyModel(c *gc.C, msg string) {
    51  	s.on(c, "destroy-model", msg)
    52  }
    53  
    54  func (s *CmdBlockHelper) Close() {
    55  	s.blockClient.Close()
    56  }
    57  
    58  func (s *CmdBlockHelper) AssertBlocked(c *gc.C, err error, msg string) {
    59  	c.Assert(err, gc.ErrorMatches, cmd.ErrSilent.Error())
    60  	// msg is logged
    61  	stripped := strings.Replace(c.GetTestLog(), "\n", "", -1)
    62  	c.Check(stripped, gc.Matches, msg)
    63  }