github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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/api/watcher"
    11  	"github.com/juju/juju/environs"
    12  	"github.com/juju/juju/environs/config"
    13  	"github.com/juju/juju/state"
    14  	statetesting "github.com/juju/juju/state/testing"
    15  )
    16  
    17  const (
    18  	HasSecrets = true
    19  	NoSecrets  = false
    20  )
    21  
    22  type EnvironWatcherFacade interface {
    23  	WatchForEnvironConfigChanges() (watcher.NotifyWatcher, error)
    24  	EnvironConfig() (*config.Config, error)
    25  }
    26  
    27  type EnvironWatcherTests struct {
    28  	facade     EnvironWatcherFacade
    29  	state      *state.State
    30  	hasSecrets bool
    31  }
    32  
    33  func NewEnvironWatcherTests(
    34  	facade EnvironWatcherFacade,
    35  	st *state.State,
    36  	hasSecrets bool) *EnvironWatcherTests {
    37  	return &EnvironWatcherTests{
    38  		facade:     facade,
    39  		state:      st,
    40  		hasSecrets: hasSecrets,
    41  	}
    42  }
    43  
    44  func (s *EnvironWatcherTests) TestEnvironConfig(c *gc.C) {
    45  	envConfig, err := s.state.EnvironConfig()
    46  	c.Assert(err, jc.ErrorIsNil)
    47  
    48  	conf, err := s.facade.EnvironConfig()
    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 environment 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 *EnvironWatcherTests) TestWatchForEnvironConfigChanges(c *gc.C) {
    70  	envConfig, err := s.state.EnvironConfig()
    71  	c.Assert(err, jc.ErrorIsNil)
    72  
    73  	w, err := s.facade.WatchForEnvironConfigChanges()
    74  	c.Assert(err, jc.ErrorIsNil)
    75  	defer statetesting.AssertStop(c, w)
    76  	wc := statetesting.NewNotifyWatcherC(c, s.state, w)
    77  
    78  	// Initial event.
    79  	wc.AssertOneChange()
    80  
    81  	// Change the environment configuration by updating an existing attribute, check it's detected.
    82  	newAttrs := map[string]interface{}{"logging-config": "juju=ERROR"}
    83  	err = s.state.UpdateEnvironConfig(newAttrs, nil, nil)
    84  	c.Assert(err, jc.ErrorIsNil)
    85  	wc.AssertOneChange()
    86  
    87  	// Change the environment configuration by adding a new attribute, check it's detected.
    88  	newAttrs = map[string]interface{}{"foo": "bar"}
    89  	err = s.state.UpdateEnvironConfig(newAttrs, nil, nil)
    90  	c.Assert(err, jc.ErrorIsNil)
    91  	wc.AssertOneChange()
    92  
    93  	// Change the environment configuration by removing an attribute, check it's detected.
    94  	err = s.state.UpdateEnvironConfig(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.UpdateEnvironConfig(oldAttrs, nil, nil)
   101  	c.Assert(err, jc.ErrorIsNil)
   102  	wc.AssertOneChange()
   103  
   104  	statetesting.AssertStop(c, w)
   105  	wc.AssertClosed()
   106  }