github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/storage/show_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage_test
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/names"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/cmd/envcmd"
    16  	"github.com/juju/juju/cmd/juju/storage"
    17  	_ "github.com/juju/juju/provider/dummy"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  type ShowSuite struct {
    22  	SubStorageSuite
    23  	mockAPI *mockShowAPI
    24  }
    25  
    26  var _ = gc.Suite(&ShowSuite{})
    27  
    28  func (s *ShowSuite) SetUpTest(c *gc.C) {
    29  	s.SubStorageSuite.SetUpTest(c)
    30  
    31  	s.mockAPI = &mockShowAPI{}
    32  	s.PatchValue(storage.GetStorageShowAPI, func(c *storage.ShowCommand) (storage.StorageShowAPI, error) {
    33  		return s.mockAPI, nil
    34  	})
    35  
    36  }
    37  
    38  func runShow(c *gc.C, args []string) (*cmd.Context, error) {
    39  	return testing.RunCommand(c, envcmd.Wrap(&storage.ShowCommand{}), args...)
    40  }
    41  
    42  func (s *ShowSuite) TestShowNoMatch(c *gc.C) {
    43  	s.mockAPI.noMatch = true
    44  	s.assertValidShow(
    45  		c,
    46  		[]string{"fluff/0"},
    47  		`
    48  {}
    49  `[1:],
    50  	)
    51  }
    52  
    53  func (s *ShowSuite) TestShow(c *gc.C) {
    54  	s.assertValidShow(
    55  		c,
    56  		[]string{"shared-fs/0"},
    57  		// Default format is yaml
    58  		`
    59  postgresql/0:
    60    shared-fs/0:
    61      storage: shared-fs
    62      kind: block
    63      status: pending
    64      persistent: false
    65  transcode/0:
    66    shared-fs/0:
    67      storage: shared-fs
    68      kind: filesystem
    69      status: attached
    70      persistent: false
    71      location: a location
    72  `[1:],
    73  	)
    74  }
    75  
    76  func (s *ShowSuite) TestShowInvalidId(c *gc.C) {
    77  	_, err := runShow(c, []string{"foo"})
    78  	c.Assert(err, gc.ErrorMatches, ".*invalid storage id foo.*")
    79  }
    80  
    81  func (s *ShowSuite) TestShowJSON(c *gc.C) {
    82  	s.assertValidShow(
    83  		c,
    84  		[]string{"shared-fs/0", "--format", "json"},
    85  		`{"postgresql/0":{"shared-fs/0":{"storage":"shared-fs","kind":"block","status":"pending","persistent":false}},"transcode/0":{"shared-fs/0":{"storage":"shared-fs","kind":"filesystem","status":"attached","persistent":false,"location":"a location"}}}
    86  `,
    87  	)
    88  }
    89  
    90  func (s *ShowSuite) TestShowMultipleReturn(c *gc.C) {
    91  	s.assertValidShow(
    92  		c,
    93  		[]string{"shared-fs/0", "db-dir/1000"},
    94  		`
    95  postgresql/0:
    96    db-dir/1000:
    97      storage: db-dir
    98      kind: block
    99      status: pending
   100      persistent: true
   101    shared-fs/0:
   102      storage: shared-fs
   103      kind: block
   104      status: pending
   105      persistent: false
   106  transcode/0:
   107    shared-fs/0:
   108      storage: shared-fs
   109      kind: filesystem
   110      status: attached
   111      persistent: false
   112      location: a location
   113  `[1:],
   114  	)
   115  }
   116  
   117  func (s *ShowSuite) assertValidShow(c *gc.C, args []string, expected string) {
   118  	context, err := runShow(c, args)
   119  	c.Assert(err, jc.ErrorIsNil)
   120  
   121  	obtained := testing.Stdout(context)
   122  	c.Assert(obtained, gc.Equals, expected)
   123  }
   124  
   125  type mockShowAPI struct {
   126  	noMatch bool
   127  }
   128  
   129  func (s mockShowAPI) Close() error {
   130  	return nil
   131  }
   132  
   133  func (s mockShowAPI) Show(tags []names.StorageTag) ([]params.StorageDetails, error) {
   134  	if s.noMatch {
   135  		return nil, nil
   136  	}
   137  	all := make([]params.StorageDetails, len(tags))
   138  	for i, tag := range tags {
   139  		all[i] = params.StorageDetails{
   140  			StorageTag: tag.String(),
   141  			UnitTag:    "unit-postgresql-0",
   142  			Kind:       params.StorageKindBlock,
   143  			Status:     "pending",
   144  		}
   145  		if i == 1 {
   146  			all[i].Persistent = true
   147  		}
   148  	}
   149  	for _, tag := range tags {
   150  		if strings.Contains(tag.String(), "shared") {
   151  			all = append(all, params.StorageDetails{
   152  				StorageTag: tag.String(),
   153  				OwnerTag:   "unit-transcode-0",
   154  				UnitTag:    "unit-transcode-0",
   155  				Kind:       params.StorageKindFilesystem,
   156  				Location:   "a location",
   157  				Status:     "attached",
   158  			})
   159  		}
   160  	}
   161  	return all, nil
   162  }