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

     1  // Copyright 2015 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/apiserver/params"
    13  	"github.com/juju/juju/cmd/juju/block"
    14  	"github.com/juju/juju/testing"
    15  )
    16  
    17  var _ = gc.Suite(&listCommandSuite{})
    18  
    19  type listCommandSuite struct {
    20  	testing.FakeJujuXDGDataHomeSuite
    21  }
    22  
    23  func (s *listCommandSuite) TestInit(c *gc.C) {
    24  	cmd := block.NewListCommand()
    25  	err := testing.InitCommand(cmd, nil)
    26  	c.Check(err, jc.ErrorIsNil)
    27  
    28  	err = testing.InitCommand(cmd, []string{"anything"})
    29  	c.Check(err.Error(), gc.Equals, `unrecognized args: ["anything"]`)
    30  }
    31  
    32  func (s *listCommandSuite) TestListEmpty(c *gc.C) {
    33  	ctx, err := testing.RunCommand(c, block.NewListCommandForTest(&mockListClient{}, nil))
    34  	c.Assert(err, jc.ErrorIsNil)
    35  	c.Assert(testing.Stderr(ctx), gc.Equals, "No commands are currently disabled.\n")
    36  }
    37  
    38  func (s *listCommandSuite) TestListError(c *gc.C) {
    39  	_, err := testing.RunCommand(c, block.NewListCommandForTest(
    40  		&mockListClient{err: errors.New("boom")}, nil))
    41  	c.Assert(err, gc.ErrorMatches, "boom")
    42  }
    43  
    44  func (s *listCommandSuite) mock() *mockListClient {
    45  	return &mockListClient{
    46  		blocks: []params.Block{
    47  			{
    48  				Type:    "BlockDestroy",
    49  				Message: "Sysadmins in control.",
    50  			}, {
    51  				Type:    "BlockChange",
    52  				Message: "just temporary",
    53  			},
    54  		},
    55  		modelBlocks: []params.ModelBlockInfo{
    56  			{
    57  				Name:     "controller",
    58  				UUID:     "fake-uuid-1",
    59  				OwnerTag: "user-admin@local",
    60  				Blocks:   []string{"BlockDestroy", "BlockRemove"},
    61  			}, {
    62  				Name:     "model-a",
    63  				UUID:     "fake-uuid-2",
    64  				OwnerTag: "user-bob@external",
    65  				Blocks:   []string{"BlockChange"},
    66  			}, {
    67  				Name:     "model-b",
    68  				UUID:     "fake-uuid-3",
    69  				OwnerTag: "user-charlie@external",
    70  				Blocks:   []string{"BlockDestroy", "BlockChange"},
    71  			},
    72  		},
    73  	}
    74  }
    75  
    76  func (s *listCommandSuite) TestList(c *gc.C) {
    77  	cmd := block.NewListCommandForTest(s.mock(), nil)
    78  	ctx, err := testing.RunCommand(c, cmd)
    79  	c.Assert(err, jc.ErrorIsNil)
    80  	c.Assert(testing.Stderr(ctx), gc.Equals, "")
    81  	c.Assert(testing.Stdout(ctx), gc.Equals, ""+
    82  		"DISABLED COMMANDS  MESSAGE\n"+
    83  		"destroy-model      Sysadmins in control.\n"+
    84  		"all                just temporary\n"+
    85  		"\n",
    86  	)
    87  }
    88  
    89  func (s *listCommandSuite) TestListYAML(c *gc.C) {
    90  	cmd := block.NewListCommandForTest(s.mock(), nil)
    91  	ctx, err := testing.RunCommand(c, cmd, "--format", "yaml")
    92  	c.Assert(err, jc.ErrorIsNil)
    93  	c.Assert(testing.Stdout(ctx), gc.Equals, ""+
    94  		"- command-set: destroy-model\n"+
    95  		"  message: Sysadmins in control.\n"+
    96  		"- command-set: all\n"+
    97  		"  message: just temporary\n",
    98  	)
    99  }
   100  
   101  func (s *listCommandSuite) TestListJSONEmpty(c *gc.C) {
   102  	ctx, err := testing.RunCommand(c, block.NewListCommandForTest(&mockListClient{}, nil), "--format", "json")
   103  	c.Assert(err, jc.ErrorIsNil)
   104  	c.Assert(testing.Stdout(ctx), gc.Equals, "[]\n")
   105  }
   106  
   107  func (s *listCommandSuite) TestListJSON(c *gc.C) {
   108  	cmd := block.NewListCommandForTest(s.mock(), nil)
   109  	ctx, err := testing.RunCommand(c, cmd, "--format", "json")
   110  	c.Assert(err, jc.ErrorIsNil)
   111  	c.Assert(testing.Stdout(ctx), gc.Equals, ""+
   112  		`[{"command-set":"destroy-model","message":"Sysadmins in control."},`+
   113  		`{"command-set":"all","message":"just temporary"}]`+"\n")
   114  }
   115  
   116  func (s *listCommandSuite) TestListAll(c *gc.C) {
   117  	cmd := block.NewListCommandForTest(s.mock(), nil)
   118  	ctx, err := testing.RunCommand(c, cmd, "--all")
   119  	c.Assert(err, jc.ErrorIsNil)
   120  	c.Assert(testing.Stderr(ctx), gc.Equals, "")
   121  	c.Assert(testing.Stdout(ctx), gc.Equals, ""+
   122  		"NAME        MODEL UUID   OWNER             DISABLED COMMANDS\n"+
   123  		"controller  fake-uuid-1  admin@local       destroy-model, remove-object\n"+
   124  		"model-a     fake-uuid-2  bob@external      all\n"+
   125  		"model-b     fake-uuid-3  charlie@external  all, destroy-model\n"+
   126  		"\n")
   127  }
   128  
   129  func (s *listCommandSuite) TestListAllYAML(c *gc.C) {
   130  	cmd := block.NewListCommandForTest(s.mock(), nil)
   131  	ctx, err := testing.RunCommand(c, cmd, "--format", "yaml", "--all")
   132  	c.Assert(err, jc.ErrorIsNil)
   133  	c.Assert(testing.Stdout(ctx), gc.Equals, ""+
   134  		"- name: controller\n"+
   135  		"  model-uuid: fake-uuid-1\n"+
   136  		"  owner: admin@local\n"+
   137  		"  disabled-commands:\n"+
   138  		"  - destroy-model\n"+
   139  		"  - remove-object\n"+
   140  		"- name: model-a\n"+
   141  		"  model-uuid: fake-uuid-2\n"+
   142  		"  owner: bob@external\n"+
   143  		"  disabled-commands:\n"+
   144  		"  - all\n"+
   145  		"- name: model-b\n"+
   146  		"  model-uuid: fake-uuid-3\n"+
   147  		"  owner: charlie@external\n"+
   148  		"  disabled-commands:\n"+
   149  		"  - all\n"+
   150  		"  - destroy-model\n")
   151  }
   152  
   153  func (s *listCommandSuite) TestListAllJSON(c *gc.C) {
   154  	cmd := block.NewListCommandForTest(s.mock(), nil)
   155  	ctx, err := testing.RunCommand(c, cmd, "--format", "json", "--all")
   156  	c.Assert(err, jc.ErrorIsNil)
   157  	c.Assert(testing.Stdout(ctx), gc.Equals, "["+
   158  		`{"name":"controller","model-uuid":"fake-uuid-1","owner":"admin@local","disabled-commands":["destroy-model","remove-object"]},`+
   159  		`{"name":"model-a","model-uuid":"fake-uuid-2","owner":"bob@external","disabled-commands":["all"]},`+
   160  		`{"name":"model-b","model-uuid":"fake-uuid-3","owner":"charlie@external","disabled-commands":["all","destroy-model"]}`+
   161  		"]\n")
   162  }
   163  
   164  type mockListClient struct {
   165  	blocks      []params.Block
   166  	modelBlocks []params.ModelBlockInfo
   167  	err         error
   168  }
   169  
   170  func (c *mockListClient) Close() error {
   171  	return nil
   172  }
   173  
   174  func (c *mockListClient) List() ([]params.Block, error) {
   175  	return c.blocks, c.err
   176  }
   177  
   178  func (c *mockListClient) ListBlockedModels() ([]params.ModelBlockInfo, error) {
   179  	return c.modelBlocks, c.err
   180  }