github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/cmdblockhelper_test.go (about)

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