github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/apiserver/imagemanager/imagemanager_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package imagemanager_test 5 6 import ( 7 "bytes" 8 "io" 9 "time" 10 11 "github.com/juju/errors" 12 "github.com/juju/names" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/apiserver/common" 17 "github.com/juju/juju/apiserver/imagemanager" 18 "github.com/juju/juju/apiserver/params" 19 apiservertesting "github.com/juju/juju/apiserver/testing" 20 jujutesting "github.com/juju/juju/juju/testing" 21 "github.com/juju/juju/state/imagestorage" 22 ) 23 24 type imageManagerSuite struct { 25 jujutesting.JujuConnSuite 26 27 imagemanager *imagemanager.ImageManagerAPI 28 resources *common.Resources 29 authoriser apiservertesting.FakeAuthorizer 30 } 31 32 var _ = gc.Suite(&imageManagerSuite{}) 33 34 func (s *imageManagerSuite) SetUpTest(c *gc.C) { 35 s.JujuConnSuite.SetUpTest(c) 36 s.resources = common.NewResources() 37 s.AddCleanup(func(_ *gc.C) { s.resources.StopAll() }) 38 39 s.authoriser = apiservertesting.FakeAuthorizer{ 40 Tag: s.AdminUserTag(c), 41 } 42 var err error 43 s.imagemanager, err = imagemanager.NewImageManagerAPI(s.State, s.resources, s.authoriser) 44 c.Assert(err, jc.ErrorIsNil) 45 } 46 47 func (s *imageManagerSuite) TestNewImageManagerAPIAcceptsClient(c *gc.C) { 48 endPoint, err := imagemanager.NewImageManagerAPI(s.State, s.resources, s.authoriser) 49 c.Assert(err, jc.ErrorIsNil) 50 c.Assert(endPoint, gc.NotNil) 51 } 52 53 func (s *imageManagerSuite) TestNewImageManagerAPIRefusesNonClient(c *gc.C) { 54 anAuthoriser := s.authoriser 55 anAuthoriser.Tag = names.NewUnitTag("mysql/0") 56 anAuthoriser.EnvironManager = false 57 endPoint, err := imagemanager.NewImageManagerAPI(s.State, s.resources, anAuthoriser) 58 c.Assert(endPoint, gc.IsNil) 59 c.Assert(err, gc.ErrorMatches, "permission denied") 60 } 61 62 func (s *imageManagerSuite) addImage(c *gc.C, content string) { 63 var r io.Reader = bytes.NewReader([]byte(content)) 64 addedMetadata := &imagestorage.Metadata{ 65 EnvUUID: s.State.EnvironUUID(), 66 Kind: "lxc", 67 Series: "trusty", 68 Arch: "amd64", 69 Size: int64(len(content)), 70 SHA256: "hash(" + content + ")", 71 SourceURL: "http://lxc-trusty-amd64", 72 } 73 stor := s.State.ImageStorage() 74 err := stor.AddImage(r, addedMetadata) 75 c.Assert(err, gc.IsNil) 76 _, rdr, err := stor.Image("lxc", "trusty", "amd64") 77 c.Assert(err, jc.ErrorIsNil) 78 rdr.Close() 79 } 80 81 func (s *imageManagerSuite) TestListAllImages(c *gc.C) { 82 s.addImage(c, "image") 83 args := params.ImageFilterParams{} 84 result, err := s.imagemanager.ListImages(args) 85 c.Assert(err, jc.ErrorIsNil) 86 c.Assert(result.Result, gc.HasLen, 1) 87 dummyTime := time.Now() 88 result.Result[0].Created = dummyTime 89 c.Assert(result.Result[0], gc.Equals, params.ImageMetadata{ 90 Kind: "lxc", Arch: "amd64", Series: "trusty", URL: "http://lxc-trusty-amd64", Created: dummyTime, 91 }) 92 } 93 94 func (s *imageManagerSuite) TestListImagesWithSingleFilter(c *gc.C) { 95 s.addImage(c, "image") 96 args := params.ImageFilterParams{ 97 Images: []params.ImageSpec{ 98 { 99 Kind: "lxc", 100 Series: "trusty", 101 Arch: "amd64", 102 }, 103 }, 104 } 105 result, err := s.imagemanager.ListImages(args) 106 c.Assert(err, jc.ErrorIsNil) 107 c.Assert(result.Result, gc.HasLen, 1) 108 dummyTime := time.Now() 109 result.Result[0].Created = dummyTime 110 c.Assert(result.Result[0], gc.Equals, params.ImageMetadata{ 111 Kind: "lxc", Arch: "amd64", Series: "trusty", URL: "http://lxc-trusty-amd64", Created: dummyTime, 112 }) 113 } 114 115 func (s *imageManagerSuite) TestListImagesWithMultipleFiltersFails(c *gc.C) { 116 s.addImage(c, "image") 117 args := params.ImageFilterParams{ 118 Images: []params.ImageSpec{ 119 { 120 Kind: "lxc", 121 Series: "trusty", 122 Arch: "amd64", 123 }, { 124 Kind: "lxc", 125 Series: "precise", 126 Arch: "amd64", 127 }, 128 }, 129 } 130 _, err := s.imagemanager.ListImages(args) 131 c.Assert(err, gc.ErrorMatches, "image filter with multiple terms not supported") 132 } 133 134 func (s *imageManagerSuite) TestDeleteImages(c *gc.C) { 135 s.addImage(c, "image") 136 args := params.ImageFilterParams{ 137 Images: []params.ImageSpec{ 138 { 139 Kind: "lxc", 140 Series: "trusty", 141 Arch: "amd64", 142 }, { 143 Kind: "lxc", 144 Series: "precise", 145 Arch: "amd64", 146 }, 147 }, 148 } 149 results, err := s.imagemanager.DeleteImages(args) 150 c.Assert(err, jc.ErrorIsNil) 151 c.Assert(results, gc.DeepEquals, params.ErrorResults{ 152 Results: []params.ErrorResult{ 153 {Error: nil}, 154 {Error: apiservertesting.NotFoundError("image lxc/precise/amd64")}, 155 }, 156 }) 157 stor := s.State.ImageStorage() 158 _, _, err = stor.Image("lxc", "trusty", "amd64") 159 c.Assert(err, gc.ErrorMatches, ".*-lxc-trusty-amd64 image metadata not found") 160 } 161 162 func (s *imageManagerSuite) TestBlockDeleteImages(c *gc.C) { 163 s.addImage(c, "image") 164 args := params.ImageFilterParams{ 165 Images: []params.ImageSpec{{ 166 Kind: "lxc", 167 Series: "trusty", 168 Arch: "amd64", 169 }}, 170 } 171 172 s.AssertConfigParameterUpdated(c, "block-all-changes", true) 173 _, err := s.imagemanager.DeleteImages(args) 174 // Check that the call is blocked 175 c.Assert(errors.Cause(err), gc.ErrorMatches, common.ErrOperationBlocked.Error()) 176 // Check the image still exists. 177 stor := s.State.ImageStorage() 178 _, rdr, err := stor.Image("lxc", "trusty", "amd64") 179 c.Assert(err, jc.ErrorIsNil) 180 rdr.Close() 181 }