github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/storage/poolcreate_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  	"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 PoolCreateSuite struct {
    17  	SubStorageSuite
    18  	mockAPI *mockPoolCreateAPI
    19  }
    20  
    21  var _ = gc.Suite(&PoolCreateSuite{})
    22  
    23  func (s *PoolCreateSuite) SetUpTest(c *gc.C) {
    24  	s.SubStorageSuite.SetUpTest(c)
    25  
    26  	s.mockAPI = &mockPoolCreateAPI{APIVersion: 5}
    27  }
    28  
    29  func (s *PoolCreateSuite) runPoolCreate(c *gc.C, args []string) (*cmd.Context, error) {
    30  	return cmdtesting.RunCommand(c, storage.NewPoolCreateCommandForTest(s.mockAPI, s.store), args...)
    31  }
    32  
    33  func (s *PoolCreateSuite) TestPoolCreateOneArg(c *gc.C) {
    34  	_, err := s.runPoolCreate(c, []string{"sunshine"})
    35  	c.Check(err, gc.ErrorMatches, "pool creation requires names, provider type and optional attributes for configuration")
    36  }
    37  
    38  func (s *PoolCreateSuite) TestPoolCreateNoArgs(c *gc.C) {
    39  	_, err := s.runPoolCreate(c, []string{""})
    40  	c.Check(err, gc.ErrorMatches, "pool creation requires names, provider type and optional attributes for configuration")
    41  }
    42  
    43  func (s *PoolCreateSuite) TestPoolCreateTwoArgs(c *gc.C) {
    44  	_, err := s.runPoolCreate(c, []string{"sunshine", "lollypop"})
    45  	c.Check(err, jc.ErrorIsNil)
    46  	c.Assert(len(s.mockAPI.Creates), gc.Equals, 1)
    47  	createdConfigs := s.mockAPI.Creates[0]
    48  	c.Assert(createdConfigs.Name, gc.Equals, "sunshine")
    49  	c.Assert(createdConfigs.Provider, gc.Equals, "lollypop")
    50  	c.Assert(createdConfigs.Config, gc.DeepEquals, map[string]interface{}{})
    51  }
    52  
    53  func (s *PoolCreateSuite) TestPoolCreateAttrMissingKey(c *gc.C) {
    54  	_, err := s.runPoolCreate(c, []string{"sunshine", "lollypop", "=too"})
    55  	c.Check(err, gc.ErrorMatches, `expected "key=value", got "=too"`)
    56  }
    57  
    58  func (s *PoolCreateSuite) TestPoolCreateAttrMissingPoolName(c *gc.C) {
    59  	_, err := s.runPoolCreate(c, []string{"sunshine=again", "lollypop"})
    60  	c.Check(err, gc.ErrorMatches, `pool creation requires names and provider type before optional attributes for configuration`)
    61  }
    62  
    63  func (s *PoolCreateSuite) TestPoolCreateAttrMissingProvider(c *gc.C) {
    64  	_, err := s.runPoolCreate(c, []string{"sunshine", "lollypop=again"})
    65  	c.Check(err, gc.ErrorMatches, `pool creation requires names and provider type before optional attributes for configuration`)
    66  }
    67  
    68  func (s *PoolCreateSuite) TestPoolCreateAttrMissingValue(c *gc.C) {
    69  	_, err := s.runPoolCreate(c, []string{"sunshine", "lollypop", "something="})
    70  	c.Check(err, gc.ErrorMatches, `expected "key=value", got "something="`)
    71  }
    72  
    73  func (s *PoolCreateSuite) TestPoolCreateAttrEmptyValue(c *gc.C) {
    74  	_, err := s.runPoolCreate(c, []string{"sunshine", "lollypop", `something=""`})
    75  	c.Check(err, jc.ErrorIsNil)
    76  	c.Assert(len(s.mockAPI.Creates), gc.Equals, 1)
    77  	createdConfigs := s.mockAPI.Creates[0]
    78  	c.Assert(createdConfigs.Name, gc.Equals, "sunshine")
    79  	c.Assert(createdConfigs.Provider, gc.Equals, "lollypop")
    80  	c.Assert(createdConfigs.Config, gc.DeepEquals, map[string]interface{}{"something": "\"\""})
    81  }
    82  
    83  func (s *PoolCreateSuite) TestPoolCreateOneAttr(c *gc.C) {
    84  	_, err := s.runPoolCreate(c, []string{"sunshine", "lollypop", "something=too"})
    85  	c.Check(err, jc.ErrorIsNil)
    86  	c.Assert(len(s.mockAPI.Creates), gc.Equals, 1)
    87  	createdConfigs := s.mockAPI.Creates[0]
    88  	c.Assert(createdConfigs.Name, gc.Equals, "sunshine")
    89  	c.Assert(createdConfigs.Provider, gc.Equals, "lollypop")
    90  	c.Assert(createdConfigs.Config, gc.DeepEquals, map[string]interface{}{"something": "too"})
    91  }
    92  
    93  func (s *PoolCreateSuite) TestPoolCreateEmptyAttr(c *gc.C) {
    94  	_, err := s.runPoolCreate(c, []string{"sunshine", "lollypop", ""})
    95  	c.Check(err, gc.ErrorMatches, `expected "key=value", got ""`)
    96  }
    97  
    98  func (s *PoolCreateSuite) TestPoolCreateManyAttrs(c *gc.C) {
    99  	_, err := s.runPoolCreate(c, []string{"sunshine", "lollypop", "something=too", "another=one"})
   100  	c.Check(err, jc.ErrorIsNil)
   101  	c.Assert(len(s.mockAPI.Creates), gc.Equals, 1)
   102  	createdConfigs := s.mockAPI.Creates[0]
   103  	c.Assert(createdConfigs.Name, gc.Equals, "sunshine")
   104  	c.Assert(createdConfigs.Provider, gc.Equals, "lollypop")
   105  	c.Assert(createdConfigs.Config, gc.DeepEquals, map[string]interface{}{"something": "too", "another": "one"})
   106  }
   107  
   108  type mockCreateData struct {
   109  	Name     string
   110  	Provider string
   111  	Config   map[string]interface{}
   112  }
   113  
   114  type mockPoolCreateAPI struct {
   115  	APIVersion int
   116  	Creates    []mockCreateData
   117  }
   118  
   119  func (s *mockPoolCreateAPI) CreatePool(pname, ptype string, pconfig map[string]interface{}) error {
   120  	s.Creates = append(s.Creates, mockCreateData{Name: pname, Provider: ptype, Config: pconfig})
   121  	return nil
   122  }
   123  
   124  func (s mockPoolCreateAPI) Close() error {
   125  	return nil
   126  }
   127  
   128  func (s mockPoolCreateAPI) BestAPIVersion() int {
   129  	return s.APIVersion
   130  }