launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/apiserver/common/environwatcher_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  	"launchpad.net/errgo/errors"
     8  	gc "launchpad.net/gocheck"
     9  
    10  	"launchpad.net/juju-core/environs"
    11  	"launchpad.net/juju-core/environs/config"
    12  	"launchpad.net/juju-core/environs/configstore"
    13  	"launchpad.net/juju-core/provider/dummy"
    14  	"launchpad.net/juju-core/state"
    15  	"launchpad.net/juju-core/state/api/params"
    16  	"launchpad.net/juju-core/state/apiserver/common"
    17  	apiservertesting "launchpad.net/juju-core/state/apiserver/testing"
    18  	jc "launchpad.net/juju-core/testing/checkers"
    19  	"launchpad.net/juju-core/testing/testbase"
    20  )
    21  
    22  type environWatcherSuite struct {
    23  	testbase.LoggingSuite
    24  
    25  	testingEnvConfig *config.Config
    26  }
    27  
    28  var _ = gc.Suite(&environWatcherSuite{})
    29  
    30  type fakeEnvironAccessor struct {
    31  	envConfig      *config.Config
    32  	envConfigError error
    33  }
    34  
    35  func (*fakeEnvironAccessor) WatchForEnvironConfigChanges() state.NotifyWatcher {
    36  	changes := make(chan struct{}, 1)
    37  	// Simulate initial event.
    38  	changes <- struct{}{}
    39  	return &fakeNotifyWatcher{changes}
    40  }
    41  
    42  func (f *fakeEnvironAccessor) EnvironConfig() (*config.Config, error) {
    43  	if f.envConfigError != nil {
    44  		return nil, f.envConfigError
    45  	}
    46  	return f.envConfig, nil
    47  }
    48  
    49  func (s *environWatcherSuite) TearDownTest(c *gc.C) {
    50  	dummy.Reset()
    51  	s.LoggingSuite.TearDownTest(c)
    52  }
    53  
    54  func (*environWatcherSuite) TestWatchSuccess(c *gc.C) {
    55  	getCanWatch := func() (common.AuthFunc, error) {
    56  		return func(tag string) bool {
    57  			return true
    58  		}, nil
    59  	}
    60  	resources := common.NewResources()
    61  	e := common.NewEnvironWatcher(
    62  		&fakeEnvironAccessor{},
    63  		resources,
    64  		getCanWatch,
    65  		nil,
    66  	)
    67  	result, err := e.WatchForEnvironConfigChanges()
    68  	c.Assert(err, gc.IsNil)
    69  	c.Assert(result, gc.DeepEquals, params.NotifyWatchResult{"1", nil})
    70  	c.Assert(resources.Count(), gc.Equals, 1)
    71  }
    72  
    73  func (*environWatcherSuite) TestWatchGetAuthError(c *gc.C) {
    74  	getCanWatch := func() (common.AuthFunc, error) {
    75  		return nil, errors.Newf("pow")
    76  	}
    77  	resources := common.NewResources()
    78  	e := common.NewEnvironWatcher(
    79  		&fakeEnvironAccessor{},
    80  		resources,
    81  		getCanWatch,
    82  		nil,
    83  	)
    84  	_, err := e.WatchForEnvironConfigChanges()
    85  	c.Assert(err, gc.ErrorMatches, "pow")
    86  	c.Assert(resources.Count(), gc.Equals, 0)
    87  }
    88  
    89  func (*environWatcherSuite) TestWatchAuthError(c *gc.C) {
    90  	getCanWatch := func() (common.AuthFunc, error) {
    91  		return func(tag string) bool {
    92  			return false
    93  		}, nil
    94  	}
    95  	resources := common.NewResources()
    96  	e := common.NewEnvironWatcher(
    97  		&fakeEnvironAccessor{},
    98  		resources,
    99  		getCanWatch,
   100  		nil,
   101  	)
   102  	result, err := e.WatchForEnvironConfigChanges()
   103  	c.Assert(err, gc.ErrorMatches, "permission denied")
   104  	c.Assert(result.Error, jc.DeepEquals, apiservertesting.ErrUnauthorized)
   105  	c.Assert(resources.Count(), gc.Equals, 0)
   106  }
   107  
   108  func (*environWatcherSuite) TestEnvironConfigSuccess(c *gc.C) {
   109  	getCanReadSecrets := func() (common.AuthFunc, error) {
   110  		return func(tag string) bool {
   111  			return true
   112  		}, nil
   113  	}
   114  
   115  	testingEnvConfig := testingEnvConfig(c)
   116  	e := common.NewEnvironWatcher(
   117  		&fakeEnvironAccessor{envConfig: testingEnvConfig},
   118  		nil,
   119  		nil,
   120  		getCanReadSecrets,
   121  	)
   122  	result, err := e.EnvironConfig()
   123  	c.Assert(err, gc.IsNil)
   124  	c.Assert(result.Error, gc.IsNil)
   125  	// Make sure we can read the secret attribute (i.e. it's not masked).
   126  	c.Check(result.Config["secret"], gc.Equals, "pork")
   127  	c.Check(map[string]interface{}(result.Config), jc.DeepEquals, testingEnvConfig.AllAttrs())
   128  }
   129  
   130  func (*environWatcherSuite) TestEnvironConfigFetchError(c *gc.C) {
   131  	getCanReadSecrets := func() (common.AuthFunc, error) {
   132  		return func(tag string) bool {
   133  			return true
   134  		}, nil
   135  	}
   136  	e := common.NewEnvironWatcher(
   137  		&fakeEnvironAccessor{
   138  			envConfigError: errors.Newf("pow"),
   139  		},
   140  		nil,
   141  		nil,
   142  		getCanReadSecrets,
   143  	)
   144  	_, err := e.EnvironConfig()
   145  	c.Assert(err, gc.ErrorMatches, "pow")
   146  }
   147  
   148  func (*environWatcherSuite) TestEnvironConfigGetAuthError(c *gc.C) {
   149  	getCanReadSecrets := func() (common.AuthFunc, error) {
   150  		return nil, errors.Newf("pow")
   151  	}
   152  	e := common.NewEnvironWatcher(
   153  		&fakeEnvironAccessor{envConfig: testingEnvConfig(c)},
   154  		nil,
   155  		nil,
   156  		getCanReadSecrets,
   157  	)
   158  	_, err := e.EnvironConfig()
   159  	c.Assert(err, gc.ErrorMatches, "pow")
   160  }
   161  
   162  func (*environWatcherSuite) TestEnvironConfigReadSecretsFalse(c *gc.C) {
   163  	getCanReadSecrets := func() (common.AuthFunc, error) {
   164  		return func(tag string) bool {
   165  			return false
   166  		}, nil
   167  	}
   168  
   169  	testingEnvConfig := testingEnvConfig(c)
   170  	e := common.NewEnvironWatcher(
   171  		&fakeEnvironAccessor{envConfig: testingEnvConfig},
   172  		nil,
   173  		nil,
   174  		getCanReadSecrets,
   175  	)
   176  	result, err := e.EnvironConfig()
   177  	c.Assert(err, gc.IsNil)
   178  	c.Assert(result.Error, gc.IsNil)
   179  	// Make sure the secret attribute is masked.
   180  	c.Check(result.Config["secret"], gc.Equals, "not available")
   181  	// And only that is masked.
   182  	result.Config["secret"] = "pork"
   183  	c.Check(map[string]interface{}(result.Config), jc.DeepEquals, testingEnvConfig.AllAttrs())
   184  }
   185  
   186  func testingEnvConfig(c *gc.C) *config.Config {
   187  	cfg, err := config.New(config.NoDefaults, dummy.SampleConfig())
   188  	c.Assert(err, gc.IsNil)
   189  	env, err := environs.Prepare(cfg, configstore.NewMem())
   190  	c.Assert(err, gc.IsNil)
   191  	return env.Config()
   192  }