github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	"github.com/juju/names"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    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"
    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  	w := apiservertesting.NewFakeNotifyWatcher()
    40  	// Simulate initial event.
    41  	w.C <- struct{}{}
    42  	return w
    43  }
    44  
    45  func (f *fakeModelAccessor) ModelConfig() (*config.Config, error) {
    46  	if f.modelConfigError != nil {
    47  		return nil, f.modelConfigError
    48  	}
    49  	return f.modelConfig, nil
    50  }
    51  
    52  func (s *environWatcherSuite) TearDownTest(c *gc.C) {
    53  	dummy.Reset(c)
    54  	s.BaseSuite.TearDownTest(c)
    55  }
    56  
    57  func (s *environWatcherSuite) TestWatchSuccess(c *gc.C) {
    58  	resources := common.NewResources()
    59  	s.AddCleanup(func(_ *gc.C) { resources.StopAll() })
    60  	e := common.NewModelWatcher(
    61  		&fakeModelAccessor{},
    62  		resources,
    63  		nil,
    64  	)
    65  	result, err := e.WatchForModelConfigChanges()
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	c.Assert(result, gc.DeepEquals, params.NotifyWatchResult{"1", nil})
    68  	c.Assert(resources.Count(), gc.Equals, 1)
    69  }
    70  
    71  func (*environWatcherSuite) TestModelConfigSuccess(c *gc.C) {
    72  	authorizer := apiservertesting.FakeAuthorizer{
    73  		Tag:            names.NewMachineTag("0"),
    74  		EnvironManager: true,
    75  	}
    76  	testingEnvConfig := testingEnvConfig(c)
    77  	e := common.NewModelWatcher(
    78  		&fakeModelAccessor{modelConfig: testingEnvConfig},
    79  		nil,
    80  		authorizer,
    81  	)
    82  	result, err := e.ModelConfig()
    83  	c.Assert(err, jc.ErrorIsNil)
    84  	// Make sure we can read the secret attribute (i.e. it's not masked).
    85  	c.Check(result.Config["secret"], gc.Equals, "pork")
    86  	c.Check(map[string]interface{}(result.Config), jc.DeepEquals, testingEnvConfig.AllAttrs())
    87  }
    88  
    89  func (*environWatcherSuite) TestModelConfigFetchError(c *gc.C) {
    90  	authorizer := apiservertesting.FakeAuthorizer{
    91  		Tag:            names.NewMachineTag("0"),
    92  		EnvironManager: true,
    93  	}
    94  	e := common.NewModelWatcher(
    95  		&fakeModelAccessor{
    96  			modelConfigError: fmt.Errorf("pow"),
    97  		},
    98  		nil,
    99  		authorizer,
   100  	)
   101  	_, err := e.ModelConfig()
   102  	c.Assert(err, gc.ErrorMatches, "pow")
   103  }
   104  
   105  func (*environWatcherSuite) TestModelConfigMaskedSecrets(c *gc.C) {
   106  	authorizer := apiservertesting.FakeAuthorizer{
   107  		Tag:            names.NewMachineTag("0"),
   108  		EnvironManager: false,
   109  	}
   110  	testingEnvConfig := testingEnvConfig(c)
   111  	e := common.NewModelWatcher(
   112  		&fakeModelAccessor{modelConfig: testingEnvConfig},
   113  		nil,
   114  		authorizer,
   115  	)
   116  	result, err := e.ModelConfig()
   117  	c.Assert(err, jc.ErrorIsNil)
   118  	// Make sure the secret attribute is masked.
   119  	c.Check(result.Config["secret"], gc.Equals, "not available")
   120  	// And only that is masked.
   121  	result.Config["secret"] = "pork"
   122  	c.Check(map[string]interface{}(result.Config), jc.DeepEquals, testingEnvConfig.AllAttrs())
   123  }
   124  
   125  func testingEnvConfig(c *gc.C) *config.Config {
   126  	env, err := environs.Prepare(
   127  		modelcmd.BootstrapContext(testing.Context(c)),
   128  		jujuclienttesting.NewMemStore(),
   129  		environs.PrepareParams{
   130  			ControllerName: "dummycontroller",
   131  			BaseConfig:     dummy.SampleConfig(),
   132  			CloudName:      "dummy",
   133  		},
   134  	)
   135  	c.Assert(err, jc.ErrorIsNil)
   136  	return env.Config()
   137  }