github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/block/enablecommand_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package block_test
     5  
     6  import (
     7  	"errors"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/cmd/juju/block"
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  var _ = gc.Suite(&enableCommandSuite{})
    17  
    18  type enableCommandSuite struct {
    19  	testing.FakeJujuXDGDataHomeSuite
    20  }
    21  
    22  func (s *enableCommandSuite) TestInit(c *gc.C) {
    23  	for _, test := range []struct {
    24  		args []string
    25  		err  string
    26  	}{
    27  		{
    28  			err: "missing command set (all, destroy-model, remove-object)",
    29  		}, {
    30  			args: []string{"other"},
    31  			err:  "bad command set, valid options: all, destroy-model, remove-object",
    32  		}, {
    33  			args: []string{"all"},
    34  		}, {
    35  			args: []string{"destroy-model"},
    36  		}, {
    37  			args: []string{"remove-object"},
    38  		}, {
    39  			args: []string{"all", "extra"},
    40  			err:  `unrecognized args: ["extra"]`,
    41  		},
    42  	} {
    43  		cmd := block.NewEnableCommand()
    44  		err := testing.InitCommand(cmd, test.args)
    45  		if test.err == "" {
    46  			c.Check(err, jc.ErrorIsNil)
    47  		} else {
    48  			c.Check(err.Error(), gc.Equals, test.err)
    49  		}
    50  	}
    51  }
    52  
    53  func (s *enableCommandSuite) TestRunGetAPIError(c *gc.C) {
    54  	cmd := block.NewEnableCommandForTest(nil, errors.New("boom"))
    55  	_, err := testing.RunCommand(c, cmd, "all")
    56  	c.Assert(err.Error(), gc.Equals, "cannot connect to the API: boom")
    57  }
    58  
    59  func (s *enableCommandSuite) TestRun(c *gc.C) {
    60  	for _, test := range []struct {
    61  		args  []string
    62  		type_ string
    63  	}{{
    64  		args:  []string{"all"},
    65  		type_: "BlockChange",
    66  	}, {
    67  		args:  []string{"destroy-model"},
    68  		type_: "BlockDestroy",
    69  	}, {
    70  		args:  []string{"remove-object"},
    71  		type_: "BlockRemove",
    72  	}} {
    73  		mockClient := &mockUnblockClient{}
    74  		cmd := block.NewEnableCommandForTest(mockClient, nil)
    75  		_, err := testing.RunCommand(c, cmd, test.args...)
    76  		c.Check(err, jc.ErrorIsNil)
    77  		c.Check(mockClient.blockType, gc.Equals, test.type_)
    78  	}
    79  }
    80  
    81  func (s *enableCommandSuite) TestRunError(c *gc.C) {
    82  	mockClient := &mockUnblockClient{err: errors.New("boom")}
    83  	cmd := block.NewEnableCommandForTest(mockClient, nil)
    84  	_, err := testing.RunCommand(c, cmd, "all")
    85  	c.Check(err, gc.ErrorMatches, "boom")
    86  }
    87  
    88  type mockUnblockClient struct {
    89  	blockType string
    90  	err       error
    91  }
    92  
    93  func (c *mockUnblockClient) Close() error {
    94  	return nil
    95  }
    96  
    97  func (c *mockUnblockClient) SwitchBlockOff(blockType string) error {
    98  	c.blockType = blockType
    99  	return c.err
   100  }