github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/block/disablecommand_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  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/cmd/juju/block"
    12  	"github.com/juju/juju/testing"
    13  )
    14  
    15  var _ = gc.Suite(&disableCommandSuite{})
    16  
    17  type disableCommandSuite struct {
    18  	testing.FakeJujuXDGDataHomeSuite
    19  }
    20  
    21  func (s *disableCommandSuite) TestInit(c *gc.C) {
    22  	for _, test := range []struct {
    23  		args []string
    24  		err  string
    25  	}{
    26  		{
    27  			err: "missing command set (all, destroy-model, remove-object)",
    28  		}, {
    29  			args: []string{"other"},
    30  			err:  "bad command set, valid options: all, destroy-model, remove-object",
    31  		}, {
    32  			args: []string{"all"},
    33  		}, {
    34  			args: []string{"destroy-model"},
    35  		}, {
    36  			args: []string{"remove-object"},
    37  		}, {
    38  			args: []string{"all", "lots", "of", "args"},
    39  		},
    40  	} {
    41  		cmd := block.NewDisableCommand()
    42  		err := testing.InitCommand(cmd, test.args)
    43  		if test.err == "" {
    44  			c.Check(err, jc.ErrorIsNil)
    45  		} else {
    46  			c.Check(err.Error(), gc.Equals, test.err)
    47  		}
    48  	}
    49  }
    50  
    51  func (s *disableCommandSuite) TestRunGetAPIError(c *gc.C) {
    52  	cmd := block.NewDisableCommandForTest(nil, errors.New("boom"))
    53  	_, err := testing.RunCommand(c, cmd, "all")
    54  	c.Assert(err.Error(), gc.Equals, "cannot connect to the API: boom")
    55  }
    56  
    57  func (s *disableCommandSuite) TestRun(c *gc.C) {
    58  	for _, test := range []struct {
    59  		args    []string
    60  		type_   string
    61  		message string
    62  	}{{
    63  		args:    []string{"all", "this is a single arg message"},
    64  		type_:   "BlockChange",
    65  		message: "this is a single arg message",
    66  	}, {
    67  		args:    []string{"destroy-model", "this", "is", "many", "args"},
    68  		type_:   "BlockDestroy",
    69  		message: "this is many args",
    70  	}, {
    71  		args:    []string{"remove-object", "this is a", "mix"},
    72  		type_:   "BlockRemove",
    73  		message: "this is a mix",
    74  	}} {
    75  		mockClient := &mockBlockClient{}
    76  		cmd := block.NewDisableCommandForTest(mockClient, nil)
    77  		_, err := testing.RunCommand(c, cmd, test.args...)
    78  		c.Check(err, jc.ErrorIsNil)
    79  		c.Check(mockClient.blockType, gc.Equals, test.type_)
    80  		c.Check(mockClient.message, gc.Equals, test.message)
    81  	}
    82  }
    83  
    84  func (s *disableCommandSuite) TestRunError(c *gc.C) {
    85  	mockClient := &mockBlockClient{err: errors.New("boom")}
    86  	cmd := block.NewDisableCommandForTest(mockClient, nil)
    87  	_, err := testing.RunCommand(c, cmd, "all")
    88  	c.Check(err, gc.ErrorMatches, "boom")
    89  }
    90  
    91  type mockBlockClient struct {
    92  	blockType string
    93  	message   string
    94  	err       error
    95  }
    96  
    97  func (c *mockBlockClient) Close() error {
    98  	return nil
    99  }
   100  
   101  func (c *mockBlockClient) SwitchBlockOn(blockType, message string) error {
   102  	c.blockType = blockType
   103  	c.message = message
   104  	return c.err
   105  }