github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/plugins/juju-metadata/listimages_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/juju/cmd" 11 "github.com/juju/errors" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/apiserver/params" 16 "github.com/juju/juju/jujuclient" 17 "github.com/juju/juju/jujuclient/jujuclienttesting" 18 "github.com/juju/juju/testing" 19 ) 20 21 type BaseCloudImageMetadataSuite struct { 22 testing.FakeJujuXDGDataHomeSuite 23 store *jujuclienttesting.MemStore 24 } 25 26 func (s *BaseCloudImageMetadataSuite) SetUpTest(c *gc.C) { 27 s.setupBaseSuite(c) 28 } 29 30 func (s *BaseCloudImageMetadataSuite) setupBaseSuite(c *gc.C) { 31 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 32 33 s.store = jujuclienttesting.NewMemStore() 34 s.store.CurrentControllerName = "testing" 35 s.store.Controllers["testing"] = jujuclient.ControllerDetails{} 36 s.store.Accounts["testing"] = jujuclient.AccountDetails{ 37 User: "admin@local", 38 } 39 } 40 41 type ListSuite struct { 42 BaseCloudImageMetadataSuite 43 mockAPI *mockListAPI 44 } 45 46 var _ = gc.Suite(&ListSuite{}) 47 48 func (s *ListSuite) SetUpTest(c *gc.C) { 49 s.BaseCloudImageMetadataSuite.SetUpTest(c) 50 51 s.mockAPI = &mockListAPI{} 52 s.mockAPI.list = func(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) { 53 return testData, nil 54 } 55 s.PatchValue(&getImageMetadataListAPI, func(c *listImagesCommand) (MetadataListAPI, error) { 56 return s.mockAPI, nil 57 }) 58 } 59 60 func runList(c *gc.C, args []string) (*cmd.Context, error) { 61 return testing.RunCommand(c, newListImagesCommand(), args...) 62 } 63 64 func (s *ListSuite) TestListDefault(c *gc.C) { 65 // Default format is tabular 66 s.assertValidList(c, ` 67 SOURCE SERIES ARCH REGION IMAGE-ID STREAM VIRT-TYPE STORAGE-TYPE 68 custom vivid amd64 asia im-21 released kvm ebs 69 custom vivid amd64 europe im-21 released kvm ebs 70 custom vivid amd64 us im-21 released kvm ebs 71 custom trusty amd64 europe im-21 released kvm ebs 72 custom trusty i386 asia im-21 released kvm ebs 73 custom trusty i386 europe im-21 released kvm ebs 74 public vivid amd64 europe im-21 released kvm ebs 75 public trusty i386 europe im-21 released kvm ebs 76 public trusty i386 europe im-42 devel kvm ebs 77 public trusty i386 europe im-42 devel ebs 78 public trusty i386 europe im-42 devel kvm 79 public trusty i386 europe im-42 devel 80 81 `[1:], "") 82 } 83 84 func (s *ListSuite) TestListYAML(c *gc.C) { 85 s.assertValidList(c, ` 86 custom: 87 trusty: 88 amd64: 89 europe: 90 - image-id: im-21 91 stream: released 92 virt-type: kvm 93 storage-type: ebs 94 i386: 95 asia: 96 - image-id: im-21 97 stream: released 98 virt-type: kvm 99 storage-type: ebs 100 europe: 101 - image-id: im-21 102 stream: released 103 virt-type: kvm 104 storage-type: ebs 105 vivid: 106 amd64: 107 asia: 108 - image-id: im-21 109 stream: released 110 virt-type: kvm 111 storage-type: ebs 112 europe: 113 - image-id: im-21 114 stream: released 115 virt-type: kvm 116 storage-type: ebs 117 us: 118 - image-id: im-21 119 stream: released 120 virt-type: kvm 121 storage-type: ebs 122 public: 123 trusty: 124 i386: 125 europe: 126 - image-id: im-21 127 stream: released 128 virt-type: kvm 129 storage-type: ebs 130 - image-id: im-42 131 stream: devel 132 virt-type: kvm 133 storage-type: ebs 134 - image-id: im-42 135 stream: devel 136 storage-type: ebs 137 - image-id: im-42 138 stream: devel 139 virt-type: kvm 140 - image-id: im-42 141 stream: devel 142 vivid: 143 amd64: 144 europe: 145 - image-id: im-21 146 stream: released 147 virt-type: kvm 148 storage-type: ebs 149 `[1:], "", "--format", "yaml") 150 } 151 152 func (s *ListSuite) TestListMetadataFailed(c *gc.C) { 153 msg := "failed" 154 s.mockAPI.list = func(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) { 155 return nil, errors.New(msg) 156 } 157 158 _, err := runList(c, nil) 159 c.Assert(err, gc.ErrorMatches, fmt.Sprintf(".*%v.*", msg)) 160 } 161 162 func (s *ListSuite) TestListMetadataFilterStream(c *gc.C) { 163 msg := "stream" 164 s.mockAPI.list = func(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) { 165 c.Assert(stream, gc.DeepEquals, msg) 166 return nil, nil 167 } 168 s.assertValidList(c, "", "", "--stream", msg) 169 } 170 171 func (s *ListSuite) TestListMetadataFilterRegion(c *gc.C) { 172 msg := "region" 173 s.mockAPI.list = func(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) { 174 c.Assert(region, gc.DeepEquals, msg) 175 return nil, nil 176 } 177 s.assertValidList(c, "", "", "--region", msg) 178 } 179 180 func (s *ListSuite) TestListMetadataFilterSeries(c *gc.C) { 181 all := []string{"series1", "series2"} 182 msg := strings.Join(all, ",") 183 s.mockAPI.list = func(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) { 184 c.Assert(ser, gc.DeepEquals, all) 185 return nil, nil 186 } 187 s.assertValidList(c, "", "", "--series", msg) 188 } 189 190 func (s *ListSuite) TestListMetadataFilterArches(c *gc.C) { 191 all := []string{"arch1", "barch2"} 192 msg := strings.Join(all, ",") 193 s.mockAPI.list = func(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) { 194 c.Assert(arch, gc.DeepEquals, all) 195 return nil, nil 196 } 197 s.assertValidList(c, "", "", "--arch", msg) 198 } 199 200 func (s *ListSuite) TestListMetadataFilterVirtType(c *gc.C) { 201 msg := "virtType" 202 s.mockAPI.list = func(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) { 203 c.Assert(virtType, gc.DeepEquals, msg) 204 return nil, nil 205 } 206 s.assertValidList(c, "", "", "--virt-type", msg) 207 } 208 209 func (s *ListSuite) TestListMetadataFilterStorageType(c *gc.C) { 210 msg := "storagetype" 211 s.mockAPI.list = func(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) { 212 c.Assert(rootStorageType, gc.DeepEquals, msg) 213 return nil, nil 214 } 215 s.assertValidList(c, "", "", "--storage-type", msg) 216 } 217 218 func (s *ListSuite) TestListMetadataNoFilter(c *gc.C) { 219 s.mockAPI.list = func(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) { 220 c.Assert(rootStorageType, gc.DeepEquals, "") 221 c.Assert(virtType, gc.DeepEquals, "") 222 c.Assert(region, gc.DeepEquals, "") 223 c.Assert(stream, gc.DeepEquals, "") 224 c.Assert(ser, gc.IsNil) 225 c.Assert(arch, gc.IsNil) 226 return nil, nil 227 } 228 s.assertValidList(c, "", "") 229 } 230 231 func (s *ListSuite) TestListMetadataFewFilters(c *gc.C) { 232 streamValue := "streamValue" 233 regionValue := "regionValue" 234 typeValue := "typeValue" 235 s.mockAPI.list = func(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) { 236 c.Assert(stream, gc.DeepEquals, streamValue) 237 c.Assert(region, gc.DeepEquals, regionValue) 238 c.Assert(virtType, gc.DeepEquals, typeValue) 239 return nil, nil 240 } 241 s.assertValidList(c, "", "", "--stream", streamValue, "--region", regionValue, "--virt-type", typeValue) 242 } 243 244 func (s *ListSuite) assertValidList(c *gc.C, expectedValid, expectedErr string, args ...string) { 245 context, err := runList(c, args) 246 c.Assert(err, jc.ErrorIsNil) 247 248 obtainedErr := testing.Stderr(context) 249 c.Assert(obtainedErr, gc.Matches, expectedErr) 250 251 obtainedValid := testing.Stdout(context) 252 c.Assert(obtainedValid, gc.Matches, expectedValid) 253 } 254 255 type mockListAPI struct { 256 list func(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) 257 } 258 259 func (s mockListAPI) Close() error { 260 return nil 261 } 262 263 func (s mockListAPI) List(stream, region string, ser, arch []string, virtType, rootStorageType string) ([]params.CloudImageMetadata, error) { 264 return s.list(stream, region, ser, arch, virtType, rootStorageType) 265 } 266 267 var testData = []params.CloudImageMetadata{ 268 params.CloudImageMetadata{ 269 Source: "custom", 270 Series: "vivid", 271 Arch: "amd64", 272 Region: "asia", 273 ImageId: "im-21", 274 Stream: "released", 275 VirtType: "kvm", 276 RootStorageType: "ebs", 277 }, 278 params.CloudImageMetadata{ 279 Source: "custom", 280 Series: "vivid", 281 Arch: "amd64", 282 Region: "us", 283 ImageId: "im-21", 284 Stream: "released", 285 VirtType: "kvm", 286 RootStorageType: "ebs", 287 }, 288 params.CloudImageMetadata{ 289 Source: "custom", 290 Series: "vivid", 291 Arch: "amd64", 292 Region: "europe", 293 ImageId: "im-21", 294 Stream: "released", 295 VirtType: "kvm", 296 RootStorageType: "ebs", 297 }, 298 params.CloudImageMetadata{ 299 Source: "public", 300 Series: "vivid", 301 Arch: "amd64", 302 Region: "europe", 303 ImageId: "im-21", 304 Stream: "released", 305 VirtType: "kvm", 306 RootStorageType: "ebs", 307 }, 308 params.CloudImageMetadata{ 309 Source: "custom", 310 Series: "trusty", 311 Arch: "amd64", 312 Region: "europe", 313 ImageId: "im-21", 314 Stream: "released", 315 VirtType: "kvm", 316 RootStorageType: "ebs", 317 }, 318 params.CloudImageMetadata{ 319 Source: "custom", 320 Series: "trusty", 321 Arch: "i386", 322 Region: "europe", 323 ImageId: "im-21", 324 Stream: "released", 325 VirtType: "kvm", 326 RootStorageType: "ebs", 327 }, 328 params.CloudImageMetadata{ 329 Source: "custom", 330 Series: "trusty", 331 Arch: "i386", 332 Region: "asia", 333 ImageId: "im-21", 334 Stream: "released", 335 VirtType: "kvm", 336 RootStorageType: "ebs", 337 }, 338 params.CloudImageMetadata{ 339 Source: "public", 340 Series: "trusty", 341 Arch: "i386", 342 Region: "europe", 343 ImageId: "im-21", 344 Stream: "released", 345 VirtType: "kvm", 346 RootStorageType: "ebs", 347 }, 348 params.CloudImageMetadata{ 349 Source: "public", 350 Series: "trusty", 351 Arch: "i386", 352 Region: "europe", 353 ImageId: "im-42", 354 Stream: "devel", 355 VirtType: "kvm", 356 RootStorageType: "ebs", 357 }, 358 params.CloudImageMetadata{ 359 Source: "public", 360 Series: "trusty", 361 Arch: "i386", 362 Region: "europe", 363 ImageId: "im-42", 364 Stream: "devel", 365 RootStorageType: "ebs", 366 }, 367 params.CloudImageMetadata{ 368 Source: "public", 369 Series: "trusty", 370 Arch: "i386", 371 Region: "europe", 372 ImageId: "im-42", 373 Stream: "devel", 374 VirtType: "kvm", 375 }, 376 params.CloudImageMetadata{ 377 Source: "public", 378 Series: "trusty", 379 Arch: "i386", 380 Region: "europe", 381 ImageId: "im-42", 382 Stream: "devel", 383 }, 384 }