github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/core/devices" 13 "github.com/juju/juju/storage" 14 ) 15 16 var _ = gc.Suite(&FlagSuite{}) 17 18 type FlagSuite struct { 19 testing.IsolationSuite 20 } 21 22 func (FlagSuite) TestStringMapNilOk(c *gc.C) { 23 // note that the map may start out nil 24 var values map[string]string 25 c.Assert(values, gc.IsNil) 26 sm := stringMap{&values} 27 err := sm.Set("foo=foovalue") 28 c.Assert(err, jc.ErrorIsNil) 29 err = sm.Set("bar=barvalue") 30 c.Assert(err, jc.ErrorIsNil) 31 32 // now the map is non-nil and filled 33 c.Assert(values, gc.DeepEquals, map[string]string{ 34 "foo": "foovalue", 35 "bar": "barvalue", 36 }) 37 } 38 39 func (FlagSuite) TestStringMapBadVal(c *gc.C) { 40 sm := stringMap{&map[string]string{}} 41 err := sm.Set("foo") 42 c.Assert(err, jc.Satisfies, errors.IsNotValid) 43 c.Assert(err, gc.ErrorMatches, "badly formatted name value pair: foo") 44 } 45 46 func (FlagSuite) TestStringMapDupVal(c *gc.C) { 47 sm := stringMap{&map[string]string{}} 48 err := sm.Set("bar=somevalue") 49 c.Assert(err, jc.ErrorIsNil) 50 err = sm.Set("bar=someothervalue") 51 c.Assert(err, gc.ErrorMatches, ".*duplicate.*bar.*") 52 } 53 54 func (FlagSuite) TestStorageFlag(c *gc.C) { 55 var stores map[string]storage.Constraints 56 flag := storageFlag{&stores, nil} 57 err := flag.Set("foo=bar") 58 c.Assert(err, jc.ErrorIsNil) 59 c.Assert(stores, jc.DeepEquals, map[string]storage.Constraints{ 60 "foo": {Pool: "bar", Count: 1}, 61 }) 62 } 63 64 func (FlagSuite) TestStorageFlagErrors(c *gc.C) { 65 flag := storageFlag{new(map[string]storage.Constraints), nil} 66 err := flag.Set("foo") 67 c.Assert(err, gc.ErrorMatches, `expected <store>=<constraints>`) 68 err = flag.Set("foo:bar=baz") 69 c.Assert(err, gc.ErrorMatches, `expected <store>=<constraints>`) 70 err = flag.Set("foo=") 71 c.Assert(err, gc.ErrorMatches, `cannot parse disk constraints: storage constraints require at least one field to be specified`) 72 } 73 74 func (FlagSuite) TestStorageFlagBundleStorage(c *gc.C) { 75 var stores map[string]storage.Constraints 76 var bundleStores map[string]map[string]storage.Constraints 77 flag := storageFlag{&stores, &bundleStores} 78 err := flag.Set("foo=bar") 79 c.Assert(err, jc.ErrorIsNil) 80 err = flag.Set("app:baz=qux") 81 c.Assert(err, jc.ErrorIsNil) 82 c.Assert(stores, jc.DeepEquals, map[string]storage.Constraints{ 83 "foo": {Pool: "bar", Count: 1}, 84 }) 85 c.Assert(bundleStores, jc.DeepEquals, map[string]map[string]storage.Constraints{ 86 "app": { 87 "baz": {Pool: "qux", Count: 1}, 88 }, 89 }) 90 } 91 92 func (FlagSuite) TestStorageFlagBundleStorageErrors(c *gc.C) { 93 flag := storageFlag{new(map[string]storage.Constraints), new(map[string]map[string]storage.Constraints)} 94 err := flag.Set("foo") 95 c.Assert(err, gc.ErrorMatches, `expected \[<application>\:]<store>=<constraints>`) 96 } 97 98 func (FlagSuite) TestAttachStorageFlag(c *gc.C) { 99 var stores []string 100 flag := attachStorageFlag{&stores} 101 err := flag.Set("foo/0,bar/1") 102 c.Assert(err, jc.ErrorIsNil) 103 c.Assert(stores, jc.DeepEquals, []string{"foo/0", "bar/1"}) 104 } 105 106 func (FlagSuite) TestAttachStorageFlagErrors(c *gc.C) { 107 flag := attachStorageFlag{new([]string)} 108 err := flag.Set("zing") 109 c.Assert(err, gc.ErrorMatches, `storage ID "zing" not valid`) 110 } 111 112 func (FlagSuite) TestDevicesFlag(c *gc.C) { 113 var devs map[string]devices.Constraints 114 flag := devicesFlag{&devs, nil} 115 err := flag.Set("foo=3,nvidia.com/gpu,gpu=nvidia-tesla-p100") 116 c.Assert(err, jc.ErrorIsNil) 117 c.Assert(devs, jc.DeepEquals, map[string]devices.Constraints{ 118 "foo": { 119 Type: "nvidia.com/gpu", 120 Count: 3, 121 Attributes: map[string]string{ 122 "gpu": "nvidia-tesla-p100", 123 }, 124 }, 125 }) 126 } 127 128 func testFlagErrors(c *gc.C, flag devicesFlag, flagStr string, expectedErr string) { 129 err := flag.Set(flagStr) 130 c.Assert(err, gc.ErrorMatches, expectedErr) 131 } 132 133 func (FlagSuite) TestDevicesFlagErrors(c *gc.C) { 134 flag := devicesFlag{new(map[string]devices.Constraints), nil} 135 testFlagErrors(c, flag, "foo", `expected <device>=<constraints>`) 136 testFlagErrors(c, flag, "foo:bar=baz", `expected <device>=<constraints>`) 137 testFlagErrors(c, flag, "foo:bar=", `expected <device>=<constraints>`) 138 139 testFlagErrors(c, flag, "foo=2,nvidia.com/gpu,gpu=nvidia-tesla-p100,a=b", `cannot parse device constraints string, supported format is \[<count>,\]<device-class>|<vendor/type>\[,<key>=<value>;...\]`) 140 testFlagErrors(c, flag, "foo=2,nvidia.com/gpu,gpu=b=c", `cannot parse device constraints: device attribute key/value pair has bad format: \"gpu=b=c\"`) 141 testFlagErrors(c, flag, "foo=badCount,nvidia.com/gpu", `cannot parse device constraints: count must be greater than zero, got \"badCount\"`) 142 testFlagErrors(c, flag, "foo=0,nvidia.com/gpu", `cannot parse device constraints: count must be greater than zero, got \"0\"`) 143 testFlagErrors(c, flag, "foo=-1,nvidia.com/gpu", `cannot parse device constraints: count must be greater than zero, got \"-1\"`) 144 } 145 146 func (FlagSuite) TestDevicesFlagBundleDevices(c *gc.C) { 147 var devs map[string]devices.Constraints 148 var bundleDevices map[string]map[string]devices.Constraints 149 flag := devicesFlag{&devs, &bundleDevices} 150 err := flag.Set("foo=bar") 151 c.Assert(err, jc.ErrorIsNil) 152 err = flag.Set("app:baz=qux") 153 c.Assert(err, jc.ErrorIsNil) 154 c.Assert(devs, jc.DeepEquals, map[string]devices.Constraints{ 155 "foo": {Type: "bar", Count: 1}, 156 }) 157 c.Assert(bundleDevices, jc.DeepEquals, map[string]map[string]devices.Constraints{ 158 "app": { 159 "baz": {Type: "qux", Count: 1}, 160 }, 161 }) 162 } 163 164 func (FlagSuite) TestDevicesFlagBundleDevicesErrors(c *gc.C) { 165 flag := devicesFlag{new(map[string]devices.Constraints), new(map[string]map[string]devices.Constraints)} 166 err := flag.Set("foo") 167 c.Assert(err, gc.ErrorMatches, `expected \[<application>\:]<device>=<constraints>`) 168 }