github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/common/testing/modelwatcher.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/apiserver/common"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/state"
    13  	statetesting "github.com/juju/juju/state/testing"
    14  )
    15  
    16  type ModelWatcher interface {
    17  	WatchForModelConfigChanges() (params.NotifyWatchResult, error)
    18  	ModelConfig() (params.ModelConfigResult, error)
    19  }
    20  
    21  type ModelWatcherTest struct {
    22  	modelWatcher ModelWatcher
    23  	st           *state.State
    24  	resources    *common.Resources
    25  }
    26  
    27  func NewModelWatcherTest(
    28  	modelWatcher ModelWatcher,
    29  	st *state.State,
    30  	resources *common.Resources,
    31  ) *ModelWatcherTest {
    32  	return &ModelWatcherTest{modelWatcher, st, resources}
    33  }
    34  
    35  // AssertModelConfig provides a method to test the config from the
    36  // modelWatcher.  This allows other tests that embed this type to have
    37  // more than just the default test.
    38  func (s *ModelWatcherTest) AssertModelConfig(c *gc.C, modelWatcher ModelWatcher) {
    39  	model, err := s.st.Model()
    40  	c.Assert(err, jc.ErrorIsNil)
    41  
    42  	modelConfig, err := model.ModelConfig()
    43  	c.Assert(err, jc.ErrorIsNil)
    44  
    45  	result, err := modelWatcher.ModelConfig()
    46  	c.Assert(err, jc.ErrorIsNil)
    47  
    48  	configAttributes := modelConfig.AllAttrs()
    49  	c.Assert(result.Config, jc.DeepEquals, params.ModelConfig(configAttributes))
    50  }
    51  
    52  func (s *ModelWatcherTest) TestModelConfig(c *gc.C) {
    53  	s.AssertModelConfig(c, s.modelWatcher)
    54  }
    55  
    56  func (s *ModelWatcherTest) TestWatchForModelConfigChanges(c *gc.C) {
    57  	c.Assert(s.resources.Count(), gc.Equals, 0)
    58  
    59  	result, err := s.modelWatcher.WatchForModelConfigChanges()
    60  	c.Assert(err, jc.ErrorIsNil)
    61  	c.Assert(result, gc.DeepEquals, params.NotifyWatchResult{
    62  		NotifyWatcherId: "1",
    63  	})
    64  
    65  	// Verify the resources were registered and stop them when done.
    66  	c.Assert(s.resources.Count(), gc.Equals, 1)
    67  	resource := s.resources.Get("1")
    68  	defer statetesting.AssertStop(c, resource)
    69  
    70  	// Check that the Watch has consumed the initial event ("returned"
    71  	// in the Watch call)
    72  	wc := statetesting.NewNotifyWatcherC(c, s.st, resource.(state.NotifyWatcher))
    73  	wc.AssertNoChange()
    74  }