github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/cachedimages/list_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for info. 3 4 package cachedimages_test 5 6 import ( 7 "time" 8 9 "github.com/juju/cmd" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/apiserver/params" 14 "github.com/juju/juju/cmd/juju/cachedimages" 15 "github.com/juju/juju/testing" 16 ) 17 18 type listImagesCommandSuite struct { 19 testing.FakeJujuXDGDataHomeSuite 20 mockAPI *fakeImagesListAPI 21 } 22 23 var _ = gc.Suite(&listImagesCommandSuite{}) 24 25 type fakeImagesListAPI struct{} 26 27 func (*fakeImagesListAPI) Close() error { 28 return nil 29 } 30 31 func (f *fakeImagesListAPI) ListImages(kind, series, arch string) ([]params.ImageMetadata, error) { 32 if kind != "lxd" { 33 return nil, nil 34 } 35 result := []params.ImageMetadata{ 36 { 37 Kind: kind, 38 Series: series, 39 Arch: arch, 40 URL: "http://image", 41 Created: time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC), 42 }, 43 } 44 return result, nil 45 } 46 47 func (s *listImagesCommandSuite) SetUpTest(c *gc.C) { 48 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 49 s.mockAPI = &fakeImagesListAPI{} 50 s.PatchValue(cachedimages.GetListImagesAPI, func(_ *cachedimages.CachedImagesCommandBase) (cachedimages.ListImagesAPI, error) { 51 return s.mockAPI, nil 52 }) 53 } 54 55 func runListCommand(c *gc.C, args ...string) (*cmd.Context, error) { 56 return testing.RunCommand(c, cachedimages.NewListCommandForTest(), args...) 57 } 58 59 func (*listImagesCommandSuite) TestListImagesNone(c *gc.C) { 60 context, err := runListCommand(c, "--kind", "kvm") 61 c.Assert(err, jc.ErrorIsNil) 62 c.Assert(testing.Stdout(context), gc.Equals, "no matching images found\n") 63 } 64 65 func (*listImagesCommandSuite) TestListImagesFormatJson(c *gc.C) { 66 context, err := runListCommand(c, "--format", "json", "--kind", "lxd", "--series", "trusty", "--arch", "amd64") 67 c.Assert(err, jc.ErrorIsNil) 68 c.Assert(testing.Stdout(context), gc.Equals, "Cached images:\n["+ 69 `{"kind":"lxd","series":"trusty","arch":"amd64","source-url":"http://image","created":"Thu, 01 Jan 2015 00:00:00 UTC"}`+ 70 "]\n") 71 } 72 73 func (*listImagesCommandSuite) TestListImagesFormatYaml(c *gc.C) { 74 context, err := runListCommand(c, "--format", "yaml", "--kind", "lxd", "--series", "trusty", "--arch", "amd64") 75 c.Assert(err, jc.ErrorIsNil) 76 c.Assert(testing.Stdout(context), gc.Equals, "Cached images:\n"+ 77 "- kind: lxd\n"+ 78 " series: trusty\n"+ 79 " arch: amd64\n"+ 80 " source-url: http://image\n"+ 81 " created: Thu, 01 Jan 2015 00:00:00 UTC\n") 82 } 83 84 func (*listImagesCommandSuite) TestTooManyArgs(c *gc.C) { 85 _, err := runListCommand(c, "bad") 86 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["bad"\]`) 87 }