github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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: identity endpoint", 137 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 138 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{IdentityEndpoint: "old_endpoint"}}, 139 expected: ` 140 1 cloud attribute changed: 141 142 changed cloud attribute: 143 - one`[1:], 144 }, { 145 description: "cloud attributes change: storage endpoint", 146 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 147 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{StorageEndpoint: "old_endpoint"}}, 148 expected: ` 149 1 cloud attribute changed: 150 151 changed cloud attribute: 152 - one`[1:], 153 }, { 154 description: "cloud attributes change: type", 155 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 156 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Type: "type"}}, 157 expected: ` 158 1 cloud attribute changed: 159 160 changed cloud attribute: 161 - one`[1:], 162 }, { 163 description: "cloud attributes change: auth type added", 164 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 165 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{AuthTypes: []jujucloud.AuthType{jujucloud.AccessKeyAuthType}}}, 166 expected: ` 167 1 cloud attribute changed: 168 169 changed cloud attribute: 170 - one`[1:], 171 }, { 172 description: "cloud attributes change: auth type deleted", 173 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{AuthTypes: []jujucloud.AuthType{jujucloud.AccessKeyAuthType}}}, 174 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 175 expected: ` 176 1 cloud attribute changed: 177 178 changed cloud attribute: 179 - one`[1:], 180 }, { 181 description: "cloud attributes change: auth type changed", 182 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{AuthTypes: []jujucloud.AuthType{jujucloud.AccessKeyAuthType}}}, 183 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{AuthTypes: []jujucloud.AuthType{jujucloud.JSONFileAuthType}}}, 184 expected: ` 185 1 cloud attribute changed: 186 187 changed cloud attribute: 188 - one`[1:], 189 }, { 190 description: "cloud attributes change: region added", 191 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 192 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}}, 193 expected: ` 194 1 cloud region added: 195 196 added cloud region: 197 - one/a`[1:], 198 }, { 199 description: "cloud attributes change: region deleted", 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{}}, 202 expected: ` 203 1 cloud region deleted: 204 205 deleted 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"}}}}, 210 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", Endpoint: "old_endpoint"}}}}, 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"}}}}, 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", Endpoint: "old_endpoint"}}}}, 228 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", Endpoint: "new_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"}}}}, 237 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", StorageEndpoint: "old_endpoint"}}}}, 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"}}}}, 247 expected: ` 248 1 cloud region changed: 249 250 changed cloud region: 251 - one/a`[1:], 252 }, { 253 description: "cloud attributes change: region changed", 254 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", StorageEndpoint: "old_endpoint"}}}}, 255 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Regions: []jujucloud.Region{jujucloud.Region{Name: "a", StorageEndpoint: "new_endpoint"}}}}, 256 expected: ` 257 1 cloud region changed: 258 259 changed cloud region: 260 - one/a`[1:], 261 }, { 262 description: "cloud details changed", 263 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Type: "type", Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}}, 264 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 265 expected: ` 266 1 cloud attribute changed as well as 1 cloud region deleted: 267 268 changed cloud attribute: 269 - one 270 deleted cloud region: 271 - one/a`[1:], 272 }, { 273 description: "cloud details changed, another way", 274 old: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{}}, 275 new: map[string]jujucloud.Cloud{"one": jujucloud.Cloud{Type: "type", Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}}, 276 expected: ` 277 1 cloud region added as well as 1 cloud attribute changed: 278 279 added cloud region: 280 - one/a 281 changed cloud attribute: 282 - one`[1:], 283 }, { 284 description: "all cloud change types", 285 old: map[string]jujucloud.Cloud{ 286 "one": jujucloud.Cloud{Type: "type", Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}, 287 "three": jujucloud.Cloud{}, // deleting 288 }, 289 new: map[string]jujucloud.Cloud{ 290 "one": jujucloud.Cloud{}, // updating 291 "two": jujucloud.Cloud{}, // adding 292 }, 293 expected: ` 294 1 cloud added; 1 cloud attribute changed as well as 1 cloud and 1 cloud region deleted: 295 296 added cloud: 297 - two 298 changed cloud attribute: 299 - one 300 deleted cloud: 301 - three 302 deleted cloud region: 303 - one/a`[1:], 304 }, { 305 description: "all cloud change types, another way", 306 old: map[string]jujucloud.Cloud{ 307 "one": jujucloud.Cloud{}, // updating 308 "two": jujucloud.Cloud{}, // deleting 309 }, 310 new: map[string]jujucloud.Cloud{ 311 "one": jujucloud.Cloud{Type: "type", Regions: []jujucloud.Region{jujucloud.Region{Name: "a"}}}, 312 "three": jujucloud.Cloud{}, // adding 313 }, 314 expected: ` 315 1 cloud and 1 cloud region added; 1 cloud attribute changed as well as 1 cloud deleted: 316 317 added cloud: 318 - three 319 added cloud region: 320 - one/a 321 changed cloud attribute: 322 - one 323 deleted cloud: 324 - two`[1:], 325 }, 326 } 327 328 func (s *cloudChangesSuite) TestDiffClouds(c *gc.C) { 329 for i, test := range diffCloudsTests { 330 c.Logf("%d: %v", i, test.description) 331 c.Check(diffClouds(test.new, test.old), gc.Equals, test.expected) 332 } 333 }