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