github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/storage/provider/registry/providerregistry_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package registry_test 5 6 import ( 7 jc "github.com/juju/testing/checkers" 8 gc "gopkg.in/check.v1" 9 10 "github.com/juju/juju/environs" 11 // Ensure environ providers are registered. 12 _ "github.com/juju/juju/provider/all" 13 "github.com/juju/juju/storage" 14 "github.com/juju/juju/storage/provider" 15 "github.com/juju/juju/storage/provider/registry" 16 ) 17 18 type providerRegistrySuite struct{} 19 20 var _ = gc.Suite(&providerRegistrySuite{}) 21 22 type mockProvider struct { 23 storage.Provider 24 } 25 26 func (s *providerRegistrySuite) TestRegisterProvider(c *gc.C) { 27 p1 := &mockProvider{} 28 ptype := storage.ProviderType("foo") 29 registry.RegisterProvider(ptype, p1) 30 p, err := registry.StorageProvider(ptype) 31 c.Assert(err, jc.ErrorIsNil) 32 c.Assert(p, gc.Equals, p1) 33 } 34 35 func (s *providerRegistrySuite) TestUnregisterProvider(c *gc.C) { 36 ptype := storage.ProviderType("foo") 37 38 // No-op, since there's nothing registered yet. 39 registry.RegisterProvider(ptype, nil) 40 41 // Register and then unregister, ensure that the provider cannot 42 // be accessed. 43 registry.RegisterProvider(ptype, &mockProvider{}) 44 registry.RegisterProvider(ptype, nil) 45 _, err := registry.StorageProvider(storage.ProviderType("foo")) 46 c.Assert(err, gc.ErrorMatches, `storage provider "foo" not found`) 47 } 48 49 func (s *providerRegistrySuite) TestNoSuchProvider(c *gc.C) { 50 _, err := registry.StorageProvider(storage.ProviderType("foo")) 51 c.Assert(err, gc.ErrorMatches, `storage provider "foo" not found`) 52 } 53 54 func (s *providerRegistrySuite) TestRegisterProviderDuplicate(c *gc.C) { 55 defer func() { 56 if v := recover(); v != nil { 57 c.Assert(v, gc.ErrorMatches, `.*duplicate storage provider type "foo"`) 58 } 59 }() 60 p1 := &mockProvider{} 61 p2 := &mockProvider{} 62 registry.RegisterProvider(storage.ProviderType("foo"), p1) 63 registry.RegisterProvider(storage.ProviderType("foo"), p2) 64 c.Errorf("panic expected") 65 } 66 67 func (s *providerRegistrySuite) TestSupportedEnvironProviders(c *gc.C) { 68 ptypeFoo := storage.ProviderType("foo") 69 ptypeBar := storage.ProviderType("bar") 70 registry.RegisterEnvironStorageProviders("ec2", ptypeFoo, ptypeBar) 71 c.Assert(registry.IsProviderSupported("ec2", ptypeFoo), jc.IsTrue) 72 c.Assert(registry.IsProviderSupported("ec2", ptypeBar), jc.IsTrue) 73 c.Assert(registry.IsProviderSupported("ec2", storage.ProviderType("foobar")), jc.IsFalse) 74 c.Assert(registry.IsProviderSupported("openstack", ptypeBar), jc.IsFalse) 75 } 76 77 func (s *providerRegistrySuite) TestSupportedEnvironCommonProviders(c *gc.C) { 78 for _, envProvider := range environs.RegisteredProviders() { 79 for storageProvider := range provider.CommonProviders() { 80 c.Logf("Checking storage provider %v is registered for env provider %v", storageProvider, envProvider) 81 c.Assert(registry.IsProviderSupported(envProvider, storageProvider), jc.IsTrue) 82 } 83 } 84 } 85 86 func (s *providerRegistrySuite) TestRegisterEnvironProvidersMultipleCalls(c *gc.C) { 87 ptypeFoo := storage.ProviderType("foo") 88 ptypeBar := storage.ProviderType("bar") 89 registry.RegisterEnvironStorageProviders("ec2", ptypeFoo) 90 registry.RegisterEnvironStorageProviders("ec2", ptypeBar) 91 registry.RegisterEnvironStorageProviders("ec2", ptypeBar) 92 c.Assert(registry.IsProviderSupported("ec2", ptypeFoo), jc.IsTrue) 93 c.Assert(registry.IsProviderSupported("ec2", ptypeBar), jc.IsTrue) 94 } 95 96 func (s *providerRegistrySuite) TestListEnvProviderUnknownEnv(c *gc.C) { 97 all, exists := registry.EnvironStorageProviders("fluffy") 98 c.Assert(exists, jc.IsFalse) 99 c.Assert(all, gc.IsNil) 100 } 101 102 func (s *providerRegistrySuite) TestListEnvProviderKnownEnv(c *gc.C) { 103 ptypeFoo := storage.ProviderType("foo") 104 registry.RegisterEnvironStorageProviders("ec2", ptypeFoo) 105 all, exists := registry.EnvironStorageProviders("ec2") 106 c.Assert(exists, jc.IsTrue) 107 c.Assert(len(all) > 0, jc.IsTrue) 108 109 found := false 110 for _, one := range all { 111 if one == ptypeFoo { 112 found = true 113 break 114 } 115 } 116 c.Assert(found, jc.IsTrue) 117 }