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