github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/application/flags_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package application
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/storage"
    13  )
    14  
    15  var _ = gc.Suite(&FlagSuite{})
    16  
    17  type FlagSuite struct {
    18  	testing.IsolationSuite
    19  }
    20  
    21  func (FlagSuite) TestStringMapNilOk(c *gc.C) {
    22  	// note that the map may start out nil
    23  	var values map[string]string
    24  	c.Assert(values, gc.IsNil)
    25  	sm := stringMap{&values}
    26  	err := sm.Set("foo=foovalue")
    27  	c.Assert(err, jc.ErrorIsNil)
    28  	err = sm.Set("bar=barvalue")
    29  	c.Assert(err, jc.ErrorIsNil)
    30  
    31  	// now the map is non-nil and filled
    32  	c.Assert(values, gc.DeepEquals, map[string]string{
    33  		"foo": "foovalue",
    34  		"bar": "barvalue",
    35  	})
    36  }
    37  
    38  func (FlagSuite) TestStringMapBadVal(c *gc.C) {
    39  	sm := stringMap{&map[string]string{}}
    40  	err := sm.Set("foo")
    41  	c.Assert(err, jc.Satisfies, errors.IsNotValid)
    42  	c.Assert(err, gc.ErrorMatches, "badly formatted name value pair: foo")
    43  }
    44  
    45  func (FlagSuite) TestStringMapDupVal(c *gc.C) {
    46  	sm := stringMap{&map[string]string{}}
    47  	err := sm.Set("bar=somevalue")
    48  	c.Assert(err, jc.ErrorIsNil)
    49  	err = sm.Set("bar=someothervalue")
    50  	c.Assert(err, gc.ErrorMatches, ".*duplicate.*bar.*")
    51  }
    52  
    53  func (FlagSuite) TestStorageFlag(c *gc.C) {
    54  	var stores map[string]storage.Constraints
    55  	flag := storageFlag{&stores, nil}
    56  	err := flag.Set("foo=bar")
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	c.Assert(stores, jc.DeepEquals, map[string]storage.Constraints{
    59  		"foo": {Pool: "bar", Count: 1},
    60  	})
    61  }
    62  
    63  func (FlagSuite) TestStorageFlagErrors(c *gc.C) {
    64  	flag := storageFlag{new(map[string]storage.Constraints), nil}
    65  	err := flag.Set("foo")
    66  	c.Assert(err, gc.ErrorMatches, `expected <store>=<constraints>`)
    67  	err = flag.Set("foo:bar=baz")
    68  	c.Assert(err, gc.ErrorMatches, `expected <store>=<constraints>`)
    69  	err = flag.Set("foo=")
    70  	c.Assert(err, gc.ErrorMatches, `cannot parse disk constraints: storage constraints require at least one field to be specified`)
    71  }
    72  
    73  func (FlagSuite) TestStorageFlagBundleStorage(c *gc.C) {
    74  	var stores map[string]storage.Constraints
    75  	var bundleStores map[string]map[string]storage.Constraints
    76  	flag := storageFlag{&stores, &bundleStores}
    77  	err := flag.Set("foo=bar")
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	err = flag.Set("app:baz=qux")
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	c.Assert(stores, jc.DeepEquals, map[string]storage.Constraints{
    82  		"foo": {Pool: "bar", Count: 1},
    83  	})
    84  	c.Assert(bundleStores, jc.DeepEquals, map[string]map[string]storage.Constraints{
    85  		"app": map[string]storage.Constraints{
    86  			"baz": {Pool: "qux", Count: 1},
    87  		},
    88  	})
    89  }
    90  
    91  func (FlagSuite) TestStorageFlagBundleStorageErrors(c *gc.C) {
    92  	flag := storageFlag{new(map[string]storage.Constraints), new(map[string]map[string]storage.Constraints)}
    93  	err := flag.Set("foo")
    94  	c.Assert(err, gc.ErrorMatches, `expected \[<application>\:]<store>=<constraints>`)
    95  }