github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/storage/poolupdate_test.go (about)

     1  // Copyright 2019 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/cmd/cmdtesting"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/cmd/juju/storage"
    13  	_ "github.com/juju/juju/provider/dummy"
    14  )
    15  
    16  type PoolUpdateSuite struct {
    17  	SubStorageSuite
    18  	mockAPI *mockPoolUpdateAPI
    19  }
    20  
    21  var _ = gc.Suite(&PoolUpdateSuite{})
    22  
    23  func (s *PoolUpdateSuite) SetUpTest(c *gc.C) {
    24  	s.SubStorageSuite.SetUpTest(c)
    25  
    26  	s.mockAPI = &mockPoolUpdateAPI{APIVersion: 5}
    27  }
    28  
    29  func (s *PoolUpdateSuite) runPoolUpdate(c *gc.C, args []string) (*cmd.Context, error) {
    30  	return cmdtesting.RunCommand(c, storage.NewPoolUpdateCommandForTest(s.mockAPI, s.store), args...)
    31  }
    32  
    33  func (s *PoolUpdateSuite) TestPoolUpdateOneArg(c *gc.C) {
    34  	_, err := s.runPoolUpdate(c, []string{"sunshine"})
    35  	c.Check(err, gc.ErrorMatches, "pool update requires name and configuration attributes")
    36  	c.Assert(len(s.mockAPI.Updates), gc.Equals, 0)
    37  }
    38  
    39  func (s *PoolUpdateSuite) TestPoolUpdateNoArgs(c *gc.C) {
    40  	_, err := s.runPoolUpdate(c, []string{""})
    41  	c.Check(err, gc.ErrorMatches, "pool update requires name and configuration attributes")
    42  	c.Assert(len(s.mockAPI.Updates), gc.Equals, 0)
    43  }
    44  
    45  func (s *PoolUpdateSuite) TestPoolUpdateWithAttrArgs(c *gc.C) {
    46  	_, err := s.runPoolUpdate(c, []string{"sunshine", "lollypop=true"})
    47  	c.Check(err, jc.ErrorIsNil)
    48  	c.Assert(len(s.mockAPI.Updates), gc.Equals, 1)
    49  	updatedConfigs := s.mockAPI.Updates[0]
    50  	c.Assert(updatedConfigs.Name, gc.Equals, "sunshine")
    51  	c.Assert(updatedConfigs.Config, gc.DeepEquals, map[string]interface{}{"lollypop": "true"})
    52  }
    53  
    54  func (s *PoolUpdateSuite) TestPoolUpdateAttrMissingKey(c *gc.C) {
    55  	_, err := s.runPoolUpdate(c, []string{"sunshine", "=too"})
    56  	c.Check(err, gc.ErrorMatches, `expected "key=value", got "=too"`)
    57  	c.Assert(len(s.mockAPI.Updates), gc.Equals, 0)
    58  }
    59  
    60  func (s *PoolUpdateSuite) TestPoolUpdateAttrMissingValue(c *gc.C) {
    61  	_, err := s.runPoolUpdate(c, []string{"sunshine", "something="})
    62  	c.Check(err, gc.ErrorMatches, `expected "key=value", got "something="`)
    63  	c.Assert(len(s.mockAPI.Updates), gc.Equals, 0)
    64  }
    65  
    66  func (s *PoolUpdateSuite) TestPoolUpdateAttrEmptyValue(c *gc.C) {
    67  	_, err := s.runPoolUpdate(c, []string{"sunshine", `something=""`})
    68  	c.Check(err, jc.ErrorIsNil)
    69  	c.Assert(len(s.mockAPI.Updates), gc.Equals, 1)
    70  	updatedConfigs := s.mockAPI.Updates[0]
    71  	c.Assert(updatedConfigs.Name, gc.Equals, "sunshine")
    72  	c.Assert(updatedConfigs.Config, gc.DeepEquals, map[string]interface{}{"something": "\"\""})
    73  }
    74  
    75  func (s *PoolUpdateSuite) TestPoolUpdateOneAttr(c *gc.C) {
    76  	_, err := s.runPoolUpdate(c, []string{"sunshine", "something=too"})
    77  	c.Check(err, jc.ErrorIsNil)
    78  	c.Assert(len(s.mockAPI.Updates), gc.Equals, 1)
    79  	updatedConfigs := s.mockAPI.Updates[0]
    80  	c.Assert(updatedConfigs.Name, gc.Equals, "sunshine")
    81  	c.Assert(updatedConfigs.Config, gc.DeepEquals, map[string]interface{}{"something": "too"})
    82  }
    83  
    84  func (s *PoolUpdateSuite) TestPoolUpdateEmptyAttr(c *gc.C) {
    85  	_, err := s.runPoolUpdate(c, []string{"sunshine", ""})
    86  	c.Check(err, gc.ErrorMatches, `expected "key=value", got ""`)
    87  	c.Assert(len(s.mockAPI.Updates), gc.Equals, 0)
    88  }
    89  
    90  func (s *PoolUpdateSuite) TestPoolUpdateManyAttrs(c *gc.C) {
    91  	_, err := s.runPoolUpdate(c, []string{"sunshine", "something=too", "another=one"})
    92  	c.Check(err, jc.ErrorIsNil)
    93  	c.Assert(len(s.mockAPI.Updates), gc.Equals, 1)
    94  	updatedConfigs := s.mockAPI.Updates[0]
    95  	c.Assert(updatedConfigs.Name, gc.Equals, "sunshine")
    96  	c.Assert(updatedConfigs.Config, gc.DeepEquals, map[string]interface{}{"something": "too", "another": "one"})
    97  }
    98  
    99  func (s *PoolUpdateSuite) TestPoolUpdateUnsupportedAPIVersion(c *gc.C) {
   100  	s.mockAPI.APIVersion = 3
   101  	_, err := s.runPoolUpdate(c, []string{"sunshine", "something=too", "another=one"})
   102  	c.Check(err, gc.ErrorMatches, "updating storage pools is not supported by this version of Juju")
   103  	c.Assert(len(s.mockAPI.Updates), gc.Equals, 0)
   104  }
   105  
   106  type mockUpdateData struct {
   107  	Name     string
   108  	Provider string
   109  	Config   map[string]interface{}
   110  }
   111  
   112  type mockPoolUpdateAPI struct {
   113  	APIVersion int
   114  	Updates    []mockUpdateData
   115  }
   116  
   117  func (s *mockPoolUpdateAPI) UpdatePool(pname, provider string, pconfig map[string]interface{}) error {
   118  	s.Updates = append(s.Updates, mockUpdateData{Name: pname, Provider: provider, Config: pconfig})
   119  	return nil
   120  }
   121  
   122  func (s mockPoolUpdateAPI) Close() error {
   123  	return nil
   124  }
   125  
   126  func (s mockPoolUpdateAPI) BestAPIVersion() int {
   127  	return s.APIVersion
   128  }