github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/storage/list_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  	"errors"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/apiserver/params"
    14  	"github.com/juju/juju/cmd/juju/storage"
    15  	_ "github.com/juju/juju/provider/dummy"
    16  	"github.com/juju/juju/status"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  type ListSuite struct {
    21  	SubStorageSuite
    22  	mockAPI *mockListAPI
    23  }
    24  
    25  var _ = gc.Suite(&ListSuite{})
    26  
    27  func (s *ListSuite) SetUpTest(c *gc.C) {
    28  	s.SubStorageSuite.SetUpTest(c)
    29  
    30  	s.mockAPI = &mockListAPI{}
    31  }
    32  
    33  func (s *ListSuite) runList(c *gc.C, args []string) (*cmd.Context, error) {
    34  	return testing.RunCommand(c, storage.NewListCommandForTest(s.mockAPI, s.store), args...)
    35  }
    36  
    37  func (s *ListSuite) TestList(c *gc.C) {
    38  	s.assertValidList(
    39  		c,
    40  		nil,
    41  		// Default format is tabular
    42  		`
    43  \[Storage\]    
    44  UNIT         ID          LOCATION STATUS   MESSAGE 
    45  postgresql/0 db-dir/1100 hither   attached         
    46  transcode/0  db-dir/1000 thither  pending          
    47  transcode/0  shared-fs/0 there    attached         
    48  transcode/1  shared-fs/0 here     attached         
    49  
    50  `[1:])
    51  }
    52  
    53  func (s *ListSuite) TestListYAML(c *gc.C) {
    54  	s.assertValidList(
    55  		c,
    56  		[]string{"--format", "yaml"},
    57  		`
    58  storage:
    59    db-dir/1000:
    60      kind: block
    61      status:
    62        current: pending
    63        since: .*
    64      persistent: false
    65      attachments:
    66        units:
    67          transcode/0:
    68            location: thither
    69    db-dir/1100:
    70      kind: block
    71      status:
    72        current: attached
    73        since: .*
    74      persistent: true
    75      attachments:
    76        units:
    77          postgresql/0:
    78            location: hither
    79    shared-fs/0:
    80      kind: filesystem
    81      status:
    82        current: attached
    83        since: .*
    84      persistent: true
    85      attachments:
    86        units:
    87          transcode/0:
    88            location: there
    89          transcode/1:
    90            location: here
    91  `[1:])
    92  }
    93  
    94  func (s *ListSuite) TestListOwnerStorageIdSort(c *gc.C) {
    95  	s.assertValidList(
    96  		c,
    97  		nil,
    98  		// Default format is tabular
    99  		`
   100  \[Storage\]    
   101  UNIT         ID          LOCATION STATUS   MESSAGE 
   102  postgresql/0 db-dir/1100 hither   attached         
   103  transcode/0  db-dir/1000 thither  pending          
   104  transcode/0  shared-fs/0 there    attached         
   105  transcode/1  shared-fs/0 here     attached         
   106  
   107  `[1:])
   108  }
   109  
   110  func (s *ListSuite) TestListError(c *gc.C) {
   111  	s.mockAPI.listErrors = true
   112  	context, err := s.runList(c, nil)
   113  	c.Assert(err, gc.ErrorMatches, "list fails")
   114  	stderr := testing.Stderr(context)
   115  	c.Assert(stderr, gc.Equals, "")
   116  	stdout := testing.Stdout(context)
   117  	c.Assert(stdout, gc.Equals, "")
   118  }
   119  
   120  func (s *ListSuite) assertValidList(c *gc.C, args []string, expectedValid string) {
   121  	context, err := s.runList(c, args)
   122  	c.Assert(err, jc.ErrorIsNil)
   123  
   124  	obtainedErr := testing.Stderr(context)
   125  	c.Assert(obtainedErr, gc.Equals, "")
   126  
   127  	obtainedValid := testing.Stdout(context)
   128  	c.Assert(obtainedValid, gc.Matches, expectedValid)
   129  }
   130  
   131  type mockListAPI struct {
   132  	listErrors      bool
   133  	listFilesystems func([]string) ([]params.FilesystemDetailsListResult, error)
   134  	listVolumes     func([]string) ([]params.VolumeDetailsListResult, error)
   135  }
   136  
   137  func (s mockListAPI) Close() error {
   138  	return nil
   139  }
   140  
   141  func (s mockListAPI) ListStorageDetails() ([]params.StorageDetails, error) {
   142  	if s.listErrors {
   143  		return nil, errors.New("list fails")
   144  	}
   145  
   146  	// postgresql/0 has "db-dir/1100"
   147  	// transcode/1 has "db-dir/1000"
   148  	// transcode/0 and transcode/1 share "shared-fs/0"
   149  	//
   150  	// there is also a storage instance "db-dir/1010" which
   151  	// returns an error when listed.
   152  	results := []params.StorageDetails{{
   153  		StorageTag: "storage-db-dir-1000",
   154  		OwnerTag:   "unit-transcode-0",
   155  		Kind:       params.StorageKindBlock,
   156  		Status: params.EntityStatus{
   157  			Status: status.StatusPending,
   158  			Since:  &epoch,
   159  		},
   160  		Attachments: map[string]params.StorageAttachmentDetails{
   161  			"unit-transcode-0": params.StorageAttachmentDetails{
   162  				Location: "thither",
   163  			},
   164  		},
   165  	}, {
   166  		StorageTag: "storage-db-dir-1100",
   167  		OwnerTag:   "unit-postgresql-0",
   168  		Kind:       params.StorageKindBlock,
   169  		Status: params.EntityStatus{
   170  			Status: status.StatusAttached,
   171  			Since:  &epoch,
   172  		},
   173  		Persistent: true,
   174  		Attachments: map[string]params.StorageAttachmentDetails{
   175  			"unit-postgresql-0": params.StorageAttachmentDetails{
   176  				Location: "hither",
   177  			},
   178  		},
   179  	}, {
   180  		StorageTag: "storage-shared-fs-0",
   181  		OwnerTag:   "service-transcode",
   182  		Kind:       params.StorageKindFilesystem,
   183  		Status: params.EntityStatus{
   184  			Status: status.StatusAttached,
   185  			Since:  &epoch,
   186  		},
   187  		Persistent: true,
   188  		Attachments: map[string]params.StorageAttachmentDetails{
   189  			"unit-transcode-0": params.StorageAttachmentDetails{
   190  				Location: "there",
   191  			},
   192  			"unit-transcode-1": params.StorageAttachmentDetails{
   193  				Location: "here",
   194  			},
   195  		},
   196  	}}
   197  	return results, nil
   198  }