github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/apiserver/common/modelwatcher_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/apiserver/common"
    14  	"github.com/juju/juju/apiserver/params"
    15  	apiservertesting "github.com/juju/juju/apiserver/testing"
    16  	"github.com/juju/juju/cmd/modelcmd"
    17  	"github.com/juju/juju/environs/bootstrap"
    18  	"github.com/juju/juju/environs/config"
    19  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    20  	"github.com/juju/juju/provider/dummy"
    21  	"github.com/juju/juju/state"
    22  	"github.com/juju/juju/testing"
    23  )
    24  
    25  type environWatcherSuite struct {
    26  	testing.BaseSuite
    27  
    28  	testingEnvConfig *config.Config
    29  }
    30  
    31  var _ = gc.Suite(&environWatcherSuite{})
    32  
    33  type fakeModelAccessor struct {
    34  	modelConfig      *config.Config
    35  	modelConfigError error
    36  }
    37  
    38  func (*fakeModelAccessor) WatchForModelConfigChanges() state.NotifyWatcher {
    39  	return apiservertesting.NewFakeNotifyWatcher()
    40  }
    41  
    42  func (f *fakeModelAccessor) ModelConfig() (*config.Config, error) {
    43  	if f.modelConfigError != nil {
    44  		return nil, f.modelConfigError
    45  	}
    46  	return f.modelConfig, nil
    47  }
    48  
    49  func (s *environWatcherSuite) TearDownTest(c *gc.C) {
    50  	dummy.Reset(c)
    51  	s.BaseSuite.TearDownTest(c)
    52  }
    53  
    54  func (s *environWatcherSuite) TestWatchSuccess(c *gc.C) {
    55  	resources := common.NewResources()
    56  	s.AddCleanup(func(_ *gc.C) { resources.StopAll() })
    57  	e := common.NewModelWatcher(
    58  		&fakeModelAccessor{},
    59  		resources,
    60  		nil,
    61  	)
    62  	result, err := e.WatchForModelConfigChanges()
    63  	c.Assert(err, jc.ErrorIsNil)
    64  	c.Assert(result, gc.DeepEquals, params.NotifyWatchResult{"1", nil})
    65  	c.Assert(resources.Count(), gc.Equals, 1)
    66  }
    67  
    68  func (*environWatcherSuite) TestModelConfigSuccess(c *gc.C) {
    69  	authorizer := apiservertesting.FakeAuthorizer{
    70  		Tag:            names.NewMachineTag("0"),
    71  		EnvironManager: true,
    72  	}
    73  	testingEnvConfig := testingEnvConfig(c)
    74  	e := common.NewModelWatcher(
    75  		&fakeModelAccessor{modelConfig: testingEnvConfig},
    76  		nil,
    77  		authorizer,
    78  	)
    79  	result, err := e.ModelConfig()
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	// Make sure we can read the secret attribute (i.e. it's not masked).
    82  	c.Check(result.Config["secret"], gc.Equals, "pork")
    83  	c.Check(map[string]interface{}(result.Config), jc.DeepEquals, testingEnvConfig.AllAttrs())
    84  }
    85  
    86  func (*environWatcherSuite) TestModelConfigFetchError(c *gc.C) {
    87  	authorizer := apiservertesting.FakeAuthorizer{
    88  		Tag:            names.NewMachineTag("0"),
    89  		EnvironManager: true,
    90  	}
    91  	e := common.NewModelWatcher(
    92  		&fakeModelAccessor{
    93  			modelConfigError: fmt.Errorf("pow"),
    94  		},
    95  		nil,
    96  		authorizer,
    97  	)
    98  	_, err := e.ModelConfig()
    99  	c.Assert(err, gc.ErrorMatches, "pow")
   100  }
   101  
   102  func testingEnvConfig(c *gc.C) *config.Config {
   103  	env, err := bootstrap.Prepare(
   104  		modelcmd.BootstrapContext(testing.Context(c)),
   105  		jujuclienttesting.NewMemStore(),
   106  		bootstrap.PrepareParams{
   107  			ControllerConfig: testing.FakeControllerConfig(),
   108  			ControllerName:   "dummycontroller",
   109  			ModelConfig:      dummy.SampleConfig(),
   110  			Cloud:            dummy.SampleCloudSpec(),
   111  			AdminSecret:      "admin-secret",
   112  		},
   113  	)
   114  	c.Assert(err, jc.ErrorIsNil)
   115  	return env.Config()
   116  }