github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	"time"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/names"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/apiserver/params"
    16  	"github.com/juju/juju/cmd/juju/storage"
    17  	_ "github.com/juju/juju/provider/dummy"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  // epoch is the time we use for "since" in statuses. The time
    22  // is always shown as a local time, so we override the local
    23  // location to be UTC+8.
    24  var epoch = time.Unix(0, 0)
    25  
    26  type ShowSuite struct {
    27  	SubStorageSuite
    28  	mockAPI *mockShowAPI
    29  }
    30  
    31  var _ = gc.Suite(&ShowSuite{})
    32  
    33  func (s *ShowSuite) SetUpTest(c *gc.C) {
    34  	s.SubStorageSuite.SetUpTest(c)
    35  
    36  	s.mockAPI = &mockShowAPI{}
    37  	s.PatchValue(&time.Local, time.FixedZone("Australia/Perth", 3600*8))
    38  }
    39  
    40  func (s *ShowSuite) runShow(c *gc.C, args []string) (*cmd.Context, error) {
    41  	return testing.RunCommand(c, storage.NewShowCommandForTest(s.mockAPI, s.store), args...)
    42  }
    43  
    44  func (s *ShowSuite) TestShowNoMatch(c *gc.C) {
    45  	s.mockAPI.noMatch = true
    46  	s.assertValidShow(
    47  		c,
    48  		[]string{"fluff/0"},
    49  		`
    50  {}
    51  `[1:],
    52  	)
    53  }
    54  
    55  func (s *ShowSuite) TestShow(c *gc.C) {
    56  	s.assertValidShow(
    57  		c,
    58  		[]string{"shared-fs/0"},
    59  		// Default format is yaml
    60  		`
    61  shared-fs/0:
    62    kind: filesystem
    63    status:
    64      current: attached
    65      since: 01 Jan 1970 08:00:00\+08:00
    66    persistent: true
    67    attachments:
    68      units:
    69        transcode/0:
    70          machine: \"1\"
    71          location: a location
    72        transcode/1:
    73          machine: \"2\"
    74          location: b location
    75  `[1:],
    76  	)
    77  }
    78  
    79  func (s *ShowSuite) TestShowInvalidId(c *gc.C) {
    80  	_, err := s.runShow(c, []string{"foo"})
    81  	c.Assert(err, gc.ErrorMatches, ".*invalid storage id foo.*")
    82  }
    83  
    84  func (s *ShowSuite) TestShowJSON(c *gc.C) {
    85  	s.assertValidShow(
    86  		c,
    87  		[]string{"shared-fs/0", "--format", "json"},
    88  		`{"shared-fs/0":{"kind":"filesystem","status":{"current":"attached","since":"01 Jan 1970 08:00:00\+08:00"},"persistent":true,"attachments":{"units":{"transcode/0":{"machine":"1","location":"a location"},"transcode/1":{"machine":"2","location":"b location"}}}}}
    89  `,
    90  	)
    91  }
    92  
    93  func (s *ShowSuite) TestShowMultipleReturn(c *gc.C) {
    94  	s.assertValidShow(
    95  		c,
    96  		[]string{"shared-fs/0", "db-dir/1000"},
    97  		`
    98  db-dir/1000:
    99    kind: block
   100    status:
   101      current: pending
   102      since: .*
   103    persistent: true
   104    attachments:
   105      units:
   106        postgresql/0: {}
   107  shared-fs/0:
   108    kind: filesystem
   109    status:
   110      current: attached
   111      since: 01 Jan 1970 08:00:00\+08:00
   112    persistent: true
   113    attachments:
   114      units:
   115        transcode/0:
   116          machine: \"1\"
   117          location: a location
   118        transcode/1:
   119          machine: \"2\"
   120          location: b location
   121  `[1:],
   122  	)
   123  }
   124  
   125  func (s *ShowSuite) assertValidShow(c *gc.C, args []string, expected string) {
   126  	context, err := s.runShow(c, args)
   127  	c.Assert(err, jc.ErrorIsNil)
   128  
   129  	obtained := testing.Stdout(context)
   130  	c.Assert(obtained, gc.Matches, expected)
   131  }
   132  
   133  type mockShowAPI struct {
   134  	noMatch bool
   135  }
   136  
   137  func (s mockShowAPI) Close() error {
   138  	return nil
   139  }
   140  
   141  func (s mockShowAPI) StorageDetails(tags []names.StorageTag) ([]params.StorageDetailsResult, error) {
   142  	if s.noMatch {
   143  		return nil, nil
   144  	}
   145  	all := make([]params.StorageDetailsResult, len(tags))
   146  	for i, tag := range tags {
   147  		if strings.Contains(tag.String(), "shared") {
   148  			all[i].Result = &params.StorageDetails{
   149  				StorageTag: tag.String(),
   150  				OwnerTag:   "service-transcode",
   151  				Kind:       params.StorageKindFilesystem,
   152  				Status: params.EntityStatus{
   153  					Status: "attached",
   154  					Since:  &epoch,
   155  				},
   156  				Persistent: true,
   157  				Attachments: map[string]params.StorageAttachmentDetails{
   158  					"unit-transcode-0": params.StorageAttachmentDetails{
   159  						MachineTag: "machine-1",
   160  						Location:   "a location",
   161  					},
   162  					"unit-transcode-1": params.StorageAttachmentDetails{
   163  						MachineTag: "machine-2",
   164  						Location:   "b location",
   165  					},
   166  				},
   167  			}
   168  		} else {
   169  			all[i].Result = &params.StorageDetails{
   170  				StorageTag: tag.String(),
   171  				Kind:       params.StorageKindBlock,
   172  				Status: params.EntityStatus{
   173  					Status: "pending",
   174  					Since:  &epoch,
   175  				},
   176  				Attachments: map[string]params.StorageAttachmentDetails{
   177  					"unit-postgresql-0": params.StorageAttachmentDetails{},
   178  				},
   179  			}
   180  			if i == 1 {
   181  				all[i].Result.Persistent = true
   182  			}
   183  		}
   184  	}
   185  	return all, nil
   186  }