github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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/config"
    11  	"github.com/juju/juju/state"
    12  	"github.com/juju/juju/watcher"
    13  	"github.com/juju/juju/watcher/watchertest"
    14  )
    15  
    16  type ModelWatcherFacade interface {
    17  	WatchForModelConfigChanges() (watcher.NotifyWatcher, error)
    18  	ModelConfig() (*config.Config, error)
    19  }
    20  
    21  type ModelWatcherTests struct {
    22  	facade ModelWatcherFacade
    23  	state  *state.State
    24  }
    25  
    26  func NewModelWatcherTests(
    27  	facade ModelWatcherFacade,
    28  	st *state.State,
    29  ) *ModelWatcherTests {
    30  	return &ModelWatcherTests{
    31  		facade: facade,
    32  		state:  st,
    33  	}
    34  }
    35  
    36  func (s *ModelWatcherTests) TestModelConfig(c *gc.C) {
    37  	envConfig, err := s.state.ModelConfig()
    38  	c.Assert(err, jc.ErrorIsNil)
    39  
    40  	conf, err := s.facade.ModelConfig()
    41  	c.Assert(err, jc.ErrorIsNil)
    42  
    43  	c.Assert(conf, jc.DeepEquals, envConfig)
    44  }
    45  
    46  func (s *ModelWatcherTests) TestWatchForModelConfigChanges(c *gc.C) {
    47  	envConfig, err := s.state.ModelConfig()
    48  	c.Assert(err, jc.ErrorIsNil)
    49  
    50  	w, err := s.facade.WatchForModelConfigChanges()
    51  	c.Assert(err, jc.ErrorIsNil)
    52  	wc := watchertest.NewNotifyWatcherC(c, w, s.state.StartSync)
    53  	defer wc.AssertStops()
    54  
    55  	// Initial event.
    56  	wc.AssertOneChange()
    57  
    58  	// Change the model configuration by updating an existing attribute, check it's detected.
    59  	newAttrs := map[string]interface{}{"logging-config": "juju=ERROR"}
    60  	err = s.state.UpdateModelConfig(newAttrs, nil, nil)
    61  	c.Assert(err, jc.ErrorIsNil)
    62  	wc.AssertOneChange()
    63  
    64  	// Change the model configuration by adding a new attribute, check it's detected.
    65  	newAttrs = map[string]interface{}{"foo": "bar"}
    66  	err = s.state.UpdateModelConfig(newAttrs, nil, nil)
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	wc.AssertOneChange()
    69  
    70  	// Change the model configuration by removing an attribute, check it's detected.
    71  	err = s.state.UpdateModelConfig(map[string]interface{}{}, []string{"foo"}, nil)
    72  	c.Assert(err, jc.ErrorIsNil)
    73  	wc.AssertOneChange()
    74  
    75  	// Change it back to the original config.
    76  	oldAttrs := map[string]interface{}{"logging-config": envConfig.AllAttrs()["logging-config"]}
    77  	err = s.state.UpdateModelConfig(oldAttrs, nil, nil)
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	wc.AssertOneChange()
    80  }