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

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package block_test
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/apiserver/common"
    15  	"github.com/juju/juju/cmd/juju/block"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type BlockCommandSuite struct {
    20  	ProtectionCommandSuite
    21  	mockClient *block.MockBlockClient
    22  }
    23  
    24  func (s *BlockCommandSuite) SetUpTest(c *gc.C) {
    25  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    26  	s.mockClient = &block.MockBlockClient{}
    27  	s.PatchValue(block.BlockClient, func(p *block.BaseBlockCommand) (block.BlockClientAPI, error) {
    28  		return s.mockClient, nil
    29  	})
    30  }
    31  
    32  var _ = gc.Suite(&BlockCommandSuite{})
    33  
    34  func (s *BlockCommandSuite) assertBlock(c *gc.C, operation, message string) {
    35  	expectedOp := block.TypeFromOperation(operation)
    36  	c.Assert(s.mockClient.BlockType, gc.DeepEquals, expectedOp)
    37  	c.Assert(s.mockClient.Msg, gc.DeepEquals, message)
    38  }
    39  
    40  func (s *BlockCommandSuite) TestBlockCmdMoreArgs(c *gc.C) {
    41  	_, err := testing.RunCommand(c, block.NewDestroyCommand(), "change", "too much")
    42  	c.Assert(
    43  		err,
    44  		gc.ErrorMatches,
    45  		`.*can only specify block message.*`)
    46  }
    47  
    48  func (s *BlockCommandSuite) TestBlockCmdNoMessage(c *gc.C) {
    49  	command := block.NewDestroyCommand()
    50  	_, err := testing.RunCommand(c, command)
    51  	c.Assert(err, jc.ErrorIsNil)
    52  	s.assertBlock(c, command.Info().Name, "")
    53  }
    54  
    55  func (s *BlockCommandSuite) TestBlockDestroyOperations(c *gc.C) {
    56  	command := block.NewDestroyCommand()
    57  	_, err := testing.RunCommand(c, command, "TestBlockDestroyOperations")
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	s.assertBlock(c, command.Info().Name, "TestBlockDestroyOperations")
    60  }
    61  
    62  func (s *BlockCommandSuite) TestBlockRemoveOperations(c *gc.C) {
    63  	command := block.NewRemoveCommand()
    64  	_, err := testing.RunCommand(c, command, "TestBlockRemoveOperations")
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	s.assertBlock(c, command.Info().Name, "TestBlockRemoveOperations")
    67  }
    68  
    69  func (s *BlockCommandSuite) TestBlockChangeOperations(c *gc.C) {
    70  	command := block.NewChangeCommand()
    71  	_, err := testing.RunCommand(c, command, "TestBlockChangeOperations")
    72  	c.Assert(err, jc.ErrorIsNil)
    73  	s.assertBlock(c, command.Info().Name, "TestBlockChangeOperations")
    74  }
    75  
    76  func (s *BlockCommandSuite) processErrorTest(c *gc.C, tstError error, blockType block.Block, expectedError error, expectedWarning string) {
    77  	if tstError != nil {
    78  		c.Assert(errors.Cause(block.ProcessBlockedError(tstError, blockType)), gc.Equals, expectedError)
    79  	} else {
    80  		c.Assert(block.ProcessBlockedError(tstError, blockType), jc.ErrorIsNil)
    81  	}
    82  	// warning displayed
    83  	logOutputText := strings.Replace(c.GetTestLog(), "\n", "", -1)
    84  	c.Assert(logOutputText, gc.Matches, expectedWarning)
    85  }
    86  
    87  func (s *BlockCommandSuite) TestProcessErrOperationBlocked(c *gc.C) {
    88  	s.processErrorTest(c, common.OperationBlockedError("operations that remove"), block.BlockRemove, cmd.ErrSilent, ".*operations that remove.*")
    89  	s.processErrorTest(c, common.OperationBlockedError("destroy-model operation has been blocked"), block.BlockDestroy, cmd.ErrSilent, ".*destroy-model operation has been blocked.*")
    90  }
    91  
    92  func (s *BlockCommandSuite) TestProcessErrNil(c *gc.C) {
    93  	s.processErrorTest(c, nil, block.BlockDestroy, nil, "")
    94  }
    95  
    96  func (s *BlockCommandSuite) TestProcessErrAny(c *gc.C) {
    97  	err := errors.New("Test error Processing")
    98  	s.processErrorTest(c, err, block.BlockDestroy, err, "")
    99  }