github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/api/testing/environwatcher.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/environs"
    11  	"github.com/juju/juju/environs/config"
    12  	"github.com/juju/juju/state"
    13  	"github.com/juju/juju/watcher"
    14  	"github.com/juju/juju/watcher/watchertest"
    15  )
    16  
    17  const (
    18  	HasSecrets = true
    19  	NoSecrets  = false
    20  )
    21  
    22  type ModelWatcherFacade interface {
    23  	WatchForModelConfigChanges() (watcher.NotifyWatcher, error)
    24  	ModelConfig() (*config.Config, error)
    25  }
    26  
    27  type ModelWatcherTests struct {
    28  	facade     ModelWatcherFacade
    29  	state      *state.State
    30  	hasSecrets bool
    31  }
    32  
    33  func NewModelWatcherTests(
    34  	facade ModelWatcherFacade,
    35  	st *state.State,
    36  	hasSecrets bool) *ModelWatcherTests {
    37  	return &ModelWatcherTests{
    38  		facade:     facade,
    39  		state:      st,
    40  		hasSecrets: hasSecrets,
    41  	}
    42  }
    43  
    44  func (s *ModelWatcherTests) TestModelConfig(c *gc.C) {
    45  	envConfig, err := s.state.ModelConfig()
    46  	c.Assert(err, jc.ErrorIsNil)
    47  
    48  	conf, err := s.facade.ModelConfig()
    49  	c.Assert(err, jc.ErrorIsNil)
    50  
    51  	// If the facade doesn't have secrets, we need to replace the config
    52  	// values in our model to compare against with the secrets replaced.
    53  	if !s.hasSecrets {
    54  		env, err := environs.New(envConfig)
    55  		c.Assert(err, jc.ErrorIsNil)
    56  		secretAttrs, err := env.Provider().SecretAttrs(envConfig)
    57  		c.Assert(err, jc.ErrorIsNil)
    58  		secrets := make(map[string]interface{})
    59  		for key := range secretAttrs {
    60  			secrets[key] = "not available"
    61  		}
    62  		envConfig, err = envConfig.Apply(secrets)
    63  		c.Assert(err, jc.ErrorIsNil)
    64  	}
    65  
    66  	c.Assert(conf, jc.DeepEquals, envConfig)
    67  }
    68  
    69  func (s *ModelWatcherTests) TestWatchForModelConfigChanges(c *gc.C) {
    70  	envConfig, err := s.state.ModelConfig()
    71  	c.Assert(err, jc.ErrorIsNil)
    72  
    73  	w, err := s.facade.WatchForModelConfigChanges()
    74  	c.Assert(err, jc.ErrorIsNil)
    75  	wc := watchertest.NewNotifyWatcherC(c, w, s.state.StartSync)
    76  	defer wc.AssertStops()
    77  
    78  	// Initial event.
    79  	wc.AssertOneChange()
    80  
    81  	// Change the model configuration by updating an existing attribute, check it's detected.
    82  	newAttrs := map[string]interface{}{"logging-config": "juju=ERROR"}
    83  	err = s.state.UpdateModelConfig(newAttrs, nil, nil)
    84  	c.Assert(err, jc.ErrorIsNil)
    85  	wc.AssertOneChange()
    86  
    87  	// Change the model configuration by adding a new attribute, check it's detected.
    88  	newAttrs = map[string]interface{}{"foo": "bar"}
    89  	err = s.state.UpdateModelConfig(newAttrs, nil, nil)
    90  	c.Assert(err, jc.ErrorIsNil)
    91  	wc.AssertOneChange()
    92  
    93  	// Change the model configuration by removing an attribute, check it's detected.
    94  	err = s.state.UpdateModelConfig(map[string]interface{}{}, []string{"foo"}, nil)
    95  	c.Assert(err, jc.ErrorIsNil)
    96  	wc.AssertOneChange()
    97  
    98  	// Change it back to the original config.
    99  	oldAttrs := map[string]interface{}{"logging-config": envConfig.AllAttrs()["logging-config"]}
   100  	err = s.state.UpdateModelConfig(oldAttrs, nil, nil)
   101  	c.Assert(err, jc.ErrorIsNil)
   102  	wc.AssertOneChange()
   103  }