github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/client/storage/poollist_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package storage_test 5 6 import ( 7 "fmt" 8 9 "github.com/juju/collections/set" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 apiserverstorage "github.com/juju/juju/apiserver/facades/client/storage" 14 "github.com/juju/juju/apiserver/params" 15 "github.com/juju/juju/storage" 16 "github.com/juju/juju/storage/provider" 17 ) 18 19 type poolSuite struct { 20 baseStorageSuite 21 } 22 23 var _ = gc.Suite(&poolSuite{}) 24 25 const ( 26 tstName = "testpool" 27 ) 28 29 func (s *poolSuite) createPools(c *gc.C, num int) { 30 var err error 31 for i := 0; i < num; i++ { 32 poolName := fmt.Sprintf("%v%v", tstName, i) 33 s.baseStorageSuite.pools[poolName], err = 34 storage.NewConfig(poolName, provider.LoopProviderType, nil) 35 c.Assert(err, jc.ErrorIsNil) 36 } 37 } 38 39 func (s *poolSuite) TestEnsureStoragePoolFilter(c *gc.C) { 40 filter := params.StoragePoolFilter{} 41 c.Assert(filter.Providers, gc.HasLen, 0) 42 c.Assert(apiserverstorage.EnsureStoragePoolFilter(s.apiCaas, filter).Providers, jc.DeepEquals, []string{"kubernetes"}) 43 } 44 45 func (s *poolSuite) TestList(c *gc.C) { 46 s.createPools(c, 1) 47 results, err := s.api.ListPools(params.StoragePoolFilters{[]params.StoragePoolFilter{{}}}) 48 c.Assert(err, jc.ErrorIsNil) 49 c.Assert(results.Results, gc.HasLen, 1) 50 one := results.Results[0] 51 c.Assert(one.Error, gc.IsNil) 52 c.Assert(one.Result, gc.HasLen, 1) 53 c.Assert(one.Result[0].Name, gc.Equals, fmt.Sprintf("%v%v", tstName, 0)) 54 c.Assert(one.Result[0].Provider, gc.Equals, string(provider.LoopProviderType)) 55 } 56 57 func (s *poolSuite) TestListManyResults(c *gc.C) { 58 s.registry.Providers["static"] = nil 59 s.createPools(c, 2) 60 results, err := s.api.ListPools(params.StoragePoolFilters{[]params.StoragePoolFilter{{}}}) 61 c.Assert(err, jc.ErrorIsNil) 62 assertPoolNames(c, results.Results[0].Result, "testpool0", "testpool1", "static") 63 } 64 65 func (s *poolSuite) TestListByName(c *gc.C) { 66 s.createPools(c, 2) 67 tstName := fmt.Sprintf("%v%v", tstName, 1) 68 69 results, err := s.api.ListPools(params.StoragePoolFilters{ 70 []params.StoragePoolFilter{{ 71 Names: []string{tstName}, 72 }}, 73 }) 74 c.Assert(err, jc.ErrorIsNil) 75 c.Assert(results.Results, gc.HasLen, 1) 76 c.Assert(results.Results[0].Result, gc.HasLen, 1) 77 c.Assert(results.Results[0].Result[0].Name, gc.DeepEquals, tstName) 78 } 79 80 func (s *poolSuite) TestListByType(c *gc.C) { 81 s.createPools(c, 2) 82 s.registerProviders(c) 83 tstType := string(provider.TmpfsProviderType) 84 poolName := "rayofsunshine" 85 var err error 86 s.baseStorageSuite.pools[poolName], err = 87 storage.NewConfig(poolName, provider.TmpfsProviderType, nil) 88 c.Assert(err, jc.ErrorIsNil) 89 90 results, err := s.api.ListPools(params.StoragePoolFilters{ 91 []params.StoragePoolFilter{{ 92 Providers: []string{tstType}, 93 }}, 94 }) 95 c.Assert(err, jc.ErrorIsNil) 96 assertPoolNames(c, results.Results[0].Result, "rayofsunshine", "tmpfs") 97 } 98 99 func (s *poolSuite) TestListByNameAndTypeAnd(c *gc.C) { 100 s.createPools(c, 2) 101 s.registerProviders(c) 102 tstType := string(provider.TmpfsProviderType) 103 poolName := "rayofsunshine" 104 var err error 105 s.baseStorageSuite.pools[poolName], err = 106 storage.NewConfig(poolName, provider.TmpfsProviderType, nil) 107 c.Assert(err, jc.ErrorIsNil) 108 results, err := s.api.ListPools(params.StoragePoolFilters{ 109 []params.StoragePoolFilter{{ 110 Providers: []string{tstType}, 111 Names: []string{poolName}, 112 }}, 113 }) 114 c.Assert(err, jc.ErrorIsNil) 115 c.Assert(results.Results, gc.HasLen, 1) 116 c.Assert(results.Results[0].Result, gc.HasLen, 1) 117 c.Assert(results.Results[0].Result[0].Provider, gc.DeepEquals, tstType) 118 c.Assert(results.Results[0].Result[0].Name, gc.DeepEquals, poolName) 119 } 120 121 func (s *poolSuite) TestListByNamesOr(c *gc.C) { 122 s.createPools(c, 2) 123 s.registerProviders(c) 124 poolName := "rayofsunshine" 125 var err error 126 s.baseStorageSuite.pools[poolName], err = 127 storage.NewConfig(poolName, provider.TmpfsProviderType, nil) 128 c.Assert(err, jc.ErrorIsNil) 129 results, err := s.api.ListPools(params.StoragePoolFilters{ 130 []params.StoragePoolFilter{{ 131 Names: []string{ 132 fmt.Sprintf("%v%v", tstName, 1), 133 fmt.Sprintf("%v%v", tstName, 0), 134 }, 135 }}, 136 }) 137 c.Assert(err, jc.ErrorIsNil) 138 assertPoolNames(c, results.Results[0].Result, "testpool0", "testpool1") 139 } 140 141 func assertPoolNames(c *gc.C, results []params.StoragePool, expected ...string) { 142 expectedNames := set.NewStrings(expected...) 143 c.Assert(len(expectedNames), gc.Equals, len(results)) 144 for _, one := range results { 145 c.Assert(expectedNames.Contains(one.Name), jc.IsTrue) 146 } 147 } 148 149 func (s *poolSuite) TestListByTypesOr(c *gc.C) { 150 s.createPools(c, 2) 151 s.registerProviders(c) 152 tstType := string(provider.TmpfsProviderType) 153 poolName := "rayofsunshine" 154 var err error 155 s.baseStorageSuite.pools[poolName], err = 156 storage.NewConfig(poolName, provider.TmpfsProviderType, nil) 157 c.Assert(err, jc.ErrorIsNil) 158 results, err := s.api.ListPools(params.StoragePoolFilters{ 159 []params.StoragePoolFilter{{ 160 Providers: []string{tstType, string(provider.LoopProviderType)}, 161 }}, 162 }) 163 c.Assert(err, jc.ErrorIsNil) 164 assertPoolNames(c, results.Results[0].Result, "testpool0", "testpool1", "rayofsunshine", "loop", "tmpfs") 165 } 166 167 func (s *poolSuite) TestListNoPools(c *gc.C) { 168 s.registry.Providers["static"] = nil 169 results, err := s.api.ListPools(params.StoragePoolFilters{[]params.StoragePoolFilter{{}}}) 170 c.Assert(err, jc.ErrorIsNil) 171 c.Assert(results.Results, gc.HasLen, 1) 172 assertPoolNames(c, results.Results[0].Result, "static") 173 } 174 175 func (s *poolSuite) TestListFilterEmpty(c *gc.C) { 176 err := apiserverstorage.ValidatePoolListFilter(s.api, params.StoragePoolFilter{}) 177 c.Assert(err, jc.ErrorIsNil) 178 } 179 180 const ( 181 validProvider = string(provider.LoopProviderType) 182 invalidProvider = "invalid" 183 validName = "pool" 184 invalidName = "7ool" 185 ) 186 187 func (s *poolSuite) TestListFilterValidProviders(c *gc.C) { 188 s.registerProviders(c) 189 err := apiserverstorage.ValidateProviderCriteria( 190 s.api, 191 []string{validProvider}) 192 c.Assert(err, jc.ErrorIsNil) 193 } 194 195 func (s *poolSuite) TestListFilterUnregisteredProvider(c *gc.C) { 196 err := apiserverstorage.ValidateProviderCriteria( 197 s.api, 198 []string{validProvider}) 199 c.Assert(err, gc.ErrorMatches, `storage provider "loop" not found`) 200 } 201 202 func (s *poolSuite) TestListFilterUnknownProvider(c *gc.C) { 203 s.registerProviders(c) 204 err := apiserverstorage.ValidateProviderCriteria( 205 s.api, 206 []string{invalidProvider}) 207 c.Assert(err, gc.ErrorMatches, `storage provider "invalid" not found`) 208 } 209 210 func (s *poolSuite) TestListFilterValidNames(c *gc.C) { 211 err := apiserverstorage.ValidateNameCriteria( 212 s.api, 213 []string{validName}) 214 c.Assert(err, jc.ErrorIsNil) 215 } 216 217 func (s *poolSuite) TestListFilterInvalidNames(c *gc.C) { 218 err := apiserverstorage.ValidateNameCriteria( 219 s.api, 220 []string{invalidName}) 221 c.Assert(err, gc.ErrorMatches, ".*not valid.*") 222 } 223 224 func (s *poolSuite) TestListFilterValidProvidersAndNames(c *gc.C) { 225 s.registerProviders(c) 226 err := apiserverstorage.ValidatePoolListFilter( 227 s.api, 228 params.StoragePoolFilter{ 229 Providers: []string{validProvider}, 230 Names: []string{validName}}) 231 c.Assert(err, jc.ErrorIsNil) 232 } 233 234 func (s *poolSuite) TestListFilterValidProvidersAndInvalidNames(c *gc.C) { 235 s.registerProviders(c) 236 err := apiserverstorage.ValidatePoolListFilter( 237 s.api, 238 params.StoragePoolFilter{ 239 Providers: []string{validProvider}, 240 Names: []string{invalidName}}) 241 c.Assert(err, gc.ErrorMatches, ".*not valid.*") 242 } 243 244 func (s *poolSuite) TestListFilterInvalidProvidersAndValidNames(c *gc.C) { 245 err := apiserverstorage.ValidatePoolListFilter( 246 s.api, 247 params.StoragePoolFilter{ 248 Providers: []string{invalidProvider}, 249 Names: []string{validName}}) 250 c.Assert(err, gc.ErrorMatches, `storage provider "invalid" not found`) 251 } 252 253 func (s *poolSuite) TestListFilterInvalidProvidersAndNames(c *gc.C) { 254 err := apiserverstorage.ValidatePoolListFilter( 255 s.api, 256 params.StoragePoolFilter{ 257 Providers: []string{invalidProvider}, 258 Names: []string{invalidName}}) 259 c.Assert(err, gc.ErrorMatches, `storage provider "invalid" not found`) 260 } 261 262 func (s *poolSuite) registerProviders(c *gc.C) { 263 common := provider.CommonStorageProviders() 264 providerTypes, err := common.StorageProviderTypes() 265 c.Assert(err, jc.ErrorIsNil) 266 for _, providerType := range providerTypes { 267 p, err := common.StorageProvider(providerType) 268 c.Assert(err, jc.ErrorIsNil) 269 s.registry.Providers[providerType] = p 270 } 271 }