Skip to content

Commit

Permalink
Unregister mock providers in test cleanup to avoid contamination betw…
Browse files Browse the repository at this point in the history
…een tests
  • Loading branch information
wallyworld committed May 19, 2017
1 parent 03de24f commit 282b562
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
5 changes: 4 additions & 1 deletion cmd/juju/cloud/addcredential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ var _ = gc.Suite(&addCredentialSuite{

func (s *addCredentialSuite) SetUpSuite(c *gc.C) {
s.BaseSuite.SetUpSuite(c)
environs.RegisterProvider("mock-addcredential-provider", &mockProvider{credSchemas: &s.schema})
unreg := environs.RegisterProvider("mock-addcredential-provider", &mockProvider{credSchemas: &s.schema})
s.AddCleanup(func(_ *gc.C) {
unreg()
})
s.cloudByNameFunc = func(cloud string) (*jujucloud.Cloud, error) {
if cloud != "somecloud" && cloud != "anothercloud" {
return nil, errors.NotFoundf("cloud %v", cloud)
Expand Down
10 changes: 9 additions & 1 deletion cmd/juju/cloud/detectcredentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ import (
"github.com/juju/juju/cmd/juju/cloud"
"github.com/juju/juju/environs"
"github.com/juju/juju/jujuclient"
"github.com/juju/juju/testing"
)

type detectCredentialsSuite struct {
testing.BaseSuite

store *jujuclient.MemStore
aCredential jujucloud.CloudCredential
}
Expand Down Expand Up @@ -93,10 +96,15 @@ func (p *mockProvider) FinalizeCredential(
}

func (s *detectCredentialsSuite) SetUpSuite(c *gc.C) {
environs.RegisterProvider("mock-provider", &mockProvider{detectedCreds: &s.aCredential})
s.BaseSuite.SetUpSuite(c)
unreg := environs.RegisterProvider("mock-provider", &mockProvider{detectedCreds: &s.aCredential})
s.AddCleanup(func(_ *gc.C) {
unreg()
})
}

func (s *detectCredentialsSuite) SetUpTest(c *gc.C) {
s.BaseSuite.SetUpTest(c)
s.store = jujuclient.NewMemStore()
s.aCredential = jujucloud.CloudCredential{}
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/juju/cloud/listcredentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ var _ = gc.Suite(&listCredentialsSuite{

func (s *listCredentialsSuite) SetUpSuite(c *gc.C) {
s.BaseSuite.SetUpSuite(c)
environs.RegisterProvider("test-provider", &mockProvider{})
unreg := environs.RegisterProvider("test-provider", &mockProvider{})
s.AddCleanup(func(_ *gc.C) {
unreg()
})
}

func (s *listCredentialsSuite) SetUpTest(c *gc.C) {
Expand Down
18 changes: 17 additions & 1 deletion environs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type ProviderRegistry interface {
// given name or alias, an error will be returned.
RegisterProvider(p EnvironProvider, providerType string, providerTypeAliases ...string) error

// unregisterProvider unregisters the environment provider with the given name.
unregisterProvider(providerType string)

// RegisteredProviders returns the names of the registered environment
// providers.
RegisteredProviders() []string
Expand Down Expand Up @@ -60,6 +63,15 @@ func (r *globalProviderRegistry) RegisterProvider(p EnvironProvider, providerTyp
return nil
}

func (r *globalProviderRegistry) unregisterProvider(providerType string) {
delete(r.providers, providerType)
for a, p := range r.aliases {
if p == providerType {
delete(r.aliases, a)
}
}
}

func (r *globalProviderRegistry) RegisteredProviders() []string {
var p []string
for k := range r.providers {
Expand All @@ -86,10 +98,14 @@ func (r *globalProviderRegistry) Provider(providerType string) (EnvironProvider,
//
// RegisterProvider will panic if the provider name or any of the aliases
// are registered more than once.
func RegisterProvider(name string, p EnvironProvider, alias ...string) {
// The return function can be used to unregister the provider and is used by tests.
func RegisterProvider(name string, p EnvironProvider, alias ...string) (unregister func()) {
if err := GlobalProviderRegistry().RegisterProvider(p, name, alias...); err != nil {
panic(fmt.Errorf("juju: %v", err))
}
return func() {
GlobalProviderRegistry().unregisterProvider(name)
}
}

// RegisteredProviders enumerate all the environ providers which have been registered.
Expand Down
15 changes: 15 additions & 0 deletions environs/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package environs_test

import (
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"

Expand Down Expand Up @@ -120,3 +121,17 @@ func (s *suite) TestRegisterProvider(c *gc.C) {
}
}
}

func (s *suite) TestUnregisterProvider(c *gc.C) {
s.PatchValue(environs.Providers, make(map[string]environs.EnvironProvider))
s.PatchValue(environs.ProviderAliases, make(map[string]string))
registered := &dummyProvider{}
unreg := environs.RegisterProvider("test", registered, "alias1", "alias2")
unreg()
_, err := environs.Provider("test")
c.Assert(err, jc.Satisfies, errors.IsNotFound)
_, err = environs.Provider("alias1")
c.Assert(err, jc.Satisfies, errors.IsNotFound)
_, err = environs.Provider("alias2")
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}

0 comments on commit 282b562

Please sign in to comment.