github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/system/listblocks_test.go (about)

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