github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/cloud/updateclouds_internal_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cloud 5 6 import ( 7 gc "gopkg.in/check.v1" 8 9 jujucloud "github.com/juju/juju/cloud" 10 "github.com/juju/juju/testing" 11 ) 12 13 type cloudChangesSuite struct { 14 testing.BaseSuite 15 } 16 17 var _ = gc.Suite(&cloudChangesSuite{}) 18 19 func (s *cloudChangesSuite) TestPluralityNone(c *gc.C) { 20 c.Assert(adjustPlurality("item", 0), gc.Equals, "") 21 } 22 23 func (s *cloudChangesSuite) TestPluralitySingular(c *gc.C) { 24 c.Assert(adjustPlurality("item", 1), gc.Equals, "1 item") 25 } 26 27 func (s *cloudChangesSuite) TestPluralityPlural(c *gc.C) { 28 c.Assert(adjustPlurality("item", 2), gc.Equals, "2 items") 29 } 30 31 func (s *cloudChangesSuite) TestFormatSliceEmpty(c *gc.C) { 32 c.Assert(formatSlice(nil, "", ""), gc.Equals, "") 33 c.Assert(formatSlice([]string{}, "", ""), gc.Equals, "") 34 } 35 36 func (s *cloudChangesSuite) TestFormatSliceOne(c *gc.C) { 37 c.Assert(formatSlice([]string{"one"}, "", ""), gc.Equals, "one") 38 } 39 40 func (s *cloudChangesSuite) TestFormatSliceTwo(c *gc.C) { 41 c.Assert(formatSlice([]string{"one", "two"}, "", " and "), gc.Equals, "one and two") 42 } 43 44 func (s *cloudChangesSuite) TestFormatSliceMany(c *gc.C) { 45 c.Assert(formatSlice([]string{"one", "two", "three"}, ", ", " and "), gc.Equals, "one, two and three") 46 } 47 48 func (s *cloudChangesSuite) TestFormatSlices(c *gc.C) { 49 c.Assert(formatSlice( 50 []string{"one add", "two and three updates", "four, five and seven deletes"}, "; ", " as well as "), 51 gc.Equals, 52 "one add; two and three updates as well as four, five and seven deletes", 53 ) 54 } 55 56 var diffCloudsTests = []struct { 57 description string 58 new map[string]jujucloud.Cloud 59 old map[string]jujucloud.Cloud 60 expected string 61 }{ 62 { 63 description: "no clouds", 64 old: nil, 65 new: nil, 66 expected: "", 67 }, { 68 description: "empty new clouds, no old clouds", 69 old: nil, 70 new: map[string]jujucloud.Cloud{}, 71 expected: "", 72 }, { 73 description: "no new clouds, empty old clouds", 74 old: map[string]jujucloud.Cloud{}, 75 new: nil, 76 expected: "", 77 }, { 78 description: "both new and old clouds are empty", 79 old: map[string]jujucloud.Cloud{}, 80 new: map[string]jujucloud.Cloud{}, 81 expected: "", 82 }, { 83 description: "added 1 cloud", 84 old: map[string]jujucloud.Cloud{}, 85 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 86 expected: ` 87 1 cloud added: 88 89 added cloud: 90 - one`[1:], 91 }, { 92 description: "added 2 cloud", 93 old: map[string]jujucloud.Cloud{}, 94 new: map[string]jujucloud.Cloud{ 95 "one": jujucloud.Cloud{}, 96 "two": jujucloud.Cloud{}, 97 }, 98 expected: ` 99 2 clouds added: 100 101 added cloud: 102 - one 103 - two`[1:], 104 }, { 105 description: "deleted 1 cloud", 106 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 107 new: map[string]jujucloud.Cloud{}, 108 expected: ` 109 1 cloud deleted: 110 111 deleted cloud: 112 - one`[1:], 113 }, { 114 description: "deleted 2 cloud", 115 old: map[string]jujucloud.Cloud{ 116 "one": jujucloud.Cloud{}, 117 "two": jujucloud.Cloud{}, 118 }, 119 new: map[string]jujucloud.Cloud{}, 120 expected: ` 121 2 clouds deleted: 122 123 deleted cloud: 124 - one 125 - two`[1:], 126 }, { 127 description: "cloud attributes change: endpoint", 128 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 129 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Endpoint: "old_endpoint"}}, 130 expected: ` 131 1 cloud attribute changed: 132 133 changed cloud attribute: 134 - one`[1:], 135 }, { 136 description: "cloud attributes change: storage endpoint", 137 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 138 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{StorageEndpoint: "old_endpoint"}}, 139 expected: ` 140 1 cloud attribute changed: 141 142 changed cloud attribute: 143 - one`[1:], 144 }, { 145 description: "cloud attributes change: type", 146 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 147 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Type: "type"}}, 148 expected: ` 149 1 cloud attribute changed: 150 151 changed cloud attribute: 152 - one`[1:], 153 }, { 154 description: "cloud attributes change: auth type added", 155 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 156 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{AuthTypes: []jujucloud.AuthType{jujucloud.AccessKeyAuthType}}}, 157 expected: ` 158 1 cloud attribute changed: 159 160 changed cloud attribute: 161 - one`[1:], 162 }, { 163 description: "cloud attributes change: auth type deleted", 164 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{AuthTypes: []jujucloud.AuthType{jujucloud.AccessKeyAuthType}}}, 165 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 166 expected: ` 167 1 cloud attribute changed: 168 169 changed cloud attribute: 170 - one`[1:], 171 }, { 172 description: "cloud attributes change: auth type changed", 173 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{AuthTypes: []jujucloud.AuthType{jujucloud.AccessKeyAuthType}}}, 174 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{AuthTypes: []jujucloud.AuthType{jujucloud.JSONFileAuthType}}}, 175 expected: ` 176 1 cloud attribute changed: 177 178 changed cloud attribute: 179 - one`[1:], 180 }, { 181 description: "cloud attributes change: region added", 182 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 183 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}}, 184 expected: ` 185 1 cloud region added: 186 187 added cloud region: 188 - one/a`[1:], 189 }, { 190 description: "cloud attributes change: region deleted", 191 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}}, 192 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 193 expected: ` 194 1 cloud region deleted: 195 196 deleted cloud region: 197 - one/a`[1:], 198 }, { 199 description: "cloud attributes change: region changed", 200 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}}, 201 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", Endpoint: "old_endpoint"}}}}, 202 expected: ` 203 1 cloud region changed: 204 205 changed cloud region: 206 - one/a`[1:], 207 }, { 208 description: "cloud attributes change: region changed", 209 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", Endpoint: "old_endpoint"}}}}, 210 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}}, 211 expected: ` 212 1 cloud region changed: 213 214 changed cloud region: 215 - one/a`[1:], 216 }, { 217 description: "cloud attributes change: region changed", 218 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", Endpoint: "old_endpoint"}}}}, 219 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", Endpoint: "new_endpoint"}}}}, 220 expected: ` 221 1 cloud region changed: 222 223 changed cloud region: 224 - one/a`[1:], 225 }, { 226 description: "cloud attributes change: region changed", 227 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}}, 228 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", StorageEndpoint: "old_endpoint"}}}}, 229 expected: ` 230 1 cloud region changed: 231 232 changed cloud region: 233 - one/a`[1:], 234 }, { 235 description: "cloud attributes change: region changed", 236 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", StorageEndpoint: "old_endpoint"}}}}, 237 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}}, 238 expected: ` 239 1 cloud region changed: 240 241 changed cloud region: 242 - one/a`[1:], 243 }, { 244 description: "cloud attributes change: region changed", 245 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", StorageEndpoint: "old_endpoint"}}}}, 246 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", StorageEndpoint: "new_endpoint"}}}}, 247 expected: ` 248 1 cloud region changed: 249 250 changed cloud region: 251 - one/a`[1:], 252 }, { 253 description: "cloud details changed", 254 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Type: "type", Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}}, 255 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 256 expected: ` 257 1 cloud attribute changed as well as 1 cloud region deleted: 258 259 changed cloud attribute: 260 - one 261 deleted cloud region: 262 - one/a`[1:], 263 }, { 264 description: "cloud details changed, another way", 265 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 266 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Type: "type", Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}}, 267 expected: ` 268 1 cloud region added as well as 1 cloud attribute changed: 269 270 added cloud region: 271 - one/a 272 changed cloud attribute: 273 - one`[1:], 274 }, { 275 description: "all cloud change types", 276 old: map[string]jujucloud.Cloud{ 277 "one": jujucloud.Cloud{Type: "type", Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}, 278 "three": jujucloud.Cloud{}, // deleting 279 }, 280 new: map[string]jujucloud.Cloud{ 281 "one": jujucloud.Cloud{}, // updating 282 "two": jujucloud.Cloud{}, // adding 283 }, 284 expected: ` 285 1 cloud added; 1 cloud attribute changed as well as 1 cloud and 1 cloud region deleted: 286 287 added cloud: 288 - two 289 changed cloud attribute: 290 - one 291 deleted cloud: 292 - three 293 deleted cloud region: 294 - one/a`[1:], 295 }, { 296 description: "all cloud change types, another way", 297 old: map[string]jujucloud.Cloud{ 298 "one": jujucloud.Cloud{}, // updating 299 "two": jujucloud.Cloud{}, // deleting 300 }, 301 new: map[string]jujucloud.Cloud{ 302 "one": jujucloud.Cloud{Type: "type", Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}, 303 "three": jujucloud.Cloud{}, // adding 304 }, 305 expected: ` 306 1 cloud and 1 cloud region added; 1 cloud attribute changed as well as 1 cloud deleted: 307 308 added cloud: 309 - three 310 added cloud region: 311 - one/a 312 changed cloud attribute: 313 - one 314 deleted cloud: 315 - two`[1:], 316 }, 317 } 318 319 func (s *cloudChangesSuite) TestDiffClouds(c *gc.C) { 320 for i, test := range diffCloudsTests { 321 c.Logf("%d: %v", i, test.description) 322 c.Check(diffClouds(test.new, test.old), gc.Equals, test.expected) 323 } 324 }