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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     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/controller"
    14  	"github.com/juju/juju/jujuclient"
    15  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    16  	_ "github.com/juju/juju/provider/dummy"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  type ListBlocksSuite struct {
    21  	testing.FakeJujuXDGDataHomeSuite
    22  	api      *fakeListBlocksAPI
    23  	apierror error
    24  	store    *jujuclienttesting.MemStore
    25  }
    26  
    27  var _ = gc.Suite(&ListBlocksSuite{})
    28  
    29  // fakeListBlocksAPI mocks out the controller API
    30  type fakeListBlocksAPI struct {
    31  	err    error
    32  	blocks []params.ModelBlockInfo
    33  }
    34  
    35  func (f *fakeListBlocksAPI) Close() error { return nil }
    36  
    37  func (f *fakeListBlocksAPI) ListBlockedModels() ([]params.ModelBlockInfo, error) {
    38  	return f.blocks, f.err
    39  }
    40  
    41  func (s *ListBlocksSuite) SetUpTest(c *gc.C) {
    42  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    43  	s.apierror = nil
    44  	s.api = &fakeListBlocksAPI{
    45  		blocks: []params.ModelBlockInfo{
    46  			params.ModelBlockInfo{
    47  				Name:     "test1",
    48  				UUID:     "test1-uuid",
    49  				OwnerTag: "cheryl@local",
    50  				Blocks: []string{
    51  					"BlockDestroy",
    52  				},
    53  			},
    54  			params.ModelBlockInfo{
    55  				Name:     "test2",
    56  				UUID:     "test2-uuid",
    57  				OwnerTag: "bob@local",
    58  				Blocks: []string{
    59  					"BlockDestroy",
    60  					"BlockChange",
    61  				},
    62  			},
    63  		},
    64  	}
    65  	s.store = jujuclienttesting.NewMemStore()
    66  	s.store.Controllers["dummysys"] = jujuclient.ControllerDetails{}
    67  }
    68  
    69  func (s *ListBlocksSuite) runListBlocksCommand(c *gc.C, args ...string) (*cmd.Context, error) {
    70  	cmd := controller.NewListBlocksCommandForTest(s.api, s.apierror, s.store)
    71  	args = append(args, []string{"-c", "dummysys"}...)
    72  	return testing.RunCommand(c, cmd, args...)
    73  }
    74  
    75  func (s *ListBlocksSuite) TestListBlocksCannotConnectToAPI(c *gc.C) {
    76  	s.apierror = errors.New("connection refused")
    77  	_, err := s.runListBlocksCommand(c)
    78  	c.Assert(err, gc.ErrorMatches, "cannot connect to the API: connection refused")
    79  }
    80  
    81  func (s *ListBlocksSuite) TestListBlocksError(c *gc.C) {
    82  	s.api.err = errors.New("unexpected api error")
    83  	s.runListBlocksCommand(c)
    84  	testLog := c.GetTestLog()
    85  	c.Check(testLog, jc.Contains, "Unable to list blocked models: unexpected api error")
    86  }
    87  
    88  func (s *ListBlocksSuite) TestListBlocksTabular(c *gc.C) {
    89  	ctx, err := s.runListBlocksCommand(c)
    90  	c.Check(err, jc.ErrorIsNil)
    91  	c.Check(testing.Stdout(ctx), gc.Equals, ""+
    92  		"NAME   MODEL UUID  OWNER         BLOCKS\n"+
    93  		"test1  test1-uuid  cheryl@local  destroy-model\n"+
    94  		"test2  test2-uuid  bob@local     destroy-model,all-changes\n"+
    95  		"\n")
    96  }
    97  
    98  func (s *ListBlocksSuite) TestListBlocksJSON(c *gc.C) {
    99  	ctx, err := s.runListBlocksCommand(c, "--format", "json")
   100  	c.Check(err, jc.ErrorIsNil)
   101  	c.Check(testing.Stdout(ctx), gc.Equals, "["+
   102  		`{"name":"test1","model-uuid":"test1-uuid","owner-tag":"cheryl@local",`+
   103  		`"blocks":["BlockDestroy"]},`+
   104  		`{"name":"test2","model-uuid":"test2-uuid","owner-tag":"bob@local",`+
   105  		`"blocks":["BlockDestroy","BlockChange"]}`+
   106  		"]\n")
   107  }
   108  
   109  func (s *ListBlocksSuite) TestListBlocksYAML(c *gc.C) {
   110  	ctx, err := s.runListBlocksCommand(c, "--format", "yaml")
   111  	c.Check(err, jc.ErrorIsNil)
   112  	c.Check(testing.Stdout(ctx), gc.Equals, ""+
   113  		"- name: test1\n"+
   114  		"  uuid: test1-uuid\n"+
   115  		"  ownertag: cheryl@local\n"+
   116  		"  blocks:\n"+
   117  		"  - BlockDestroy\n"+
   118  		"- name: test2\n"+
   119  		"  uuid: test2-uuid\n"+
   120  		"  ownertag: bob@local\n"+
   121  		"  blocks:\n"+
   122  		"  - BlockDestroy\n"+
   123  		"  - BlockChange\n")
   124  }