github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/state/initialize_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state_test
     5  
     6  import (
     7  	"github.com/juju/names"
     8  	gitjujutesting "github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/constraints"
    13  	"github.com/juju/juju/environs/config"
    14  	"github.com/juju/juju/mongo/mongotest"
    15  	"github.com/juju/juju/state"
    16  	statetesting "github.com/juju/juju/state/testing"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  type InitializeSuite struct {
    21  	gitjujutesting.MgoSuite
    22  	testing.BaseSuite
    23  	State *state.State
    24  }
    25  
    26  var _ = gc.Suite(&InitializeSuite{})
    27  
    28  func (s *InitializeSuite) SetUpSuite(c *gc.C) {
    29  	s.BaseSuite.SetUpSuite(c)
    30  	s.MgoSuite.SetUpSuite(c)
    31  }
    32  
    33  func (s *InitializeSuite) TearDownSuite(c *gc.C) {
    34  	s.MgoSuite.TearDownSuite(c)
    35  	s.BaseSuite.TearDownSuite(c)
    36  }
    37  
    38  func (s *InitializeSuite) SetUpTest(c *gc.C) {
    39  	s.BaseSuite.SetUpTest(c)
    40  	s.MgoSuite.SetUpTest(c)
    41  }
    42  
    43  func (s *InitializeSuite) openState(c *gc.C, modelTag names.ModelTag) {
    44  	st, err := state.Open(
    45  		modelTag,
    46  		statetesting.NewMongoInfo(),
    47  		mongotest.DialOpts(),
    48  		state.Policy(nil),
    49  	)
    50  	c.Assert(err, jc.ErrorIsNil)
    51  	s.State = st
    52  }
    53  
    54  func (s *InitializeSuite) TearDownTest(c *gc.C) {
    55  	if s.State != nil {
    56  		err := s.State.Close()
    57  		c.Check(err, jc.ErrorIsNil)
    58  	}
    59  	s.MgoSuite.TearDownTest(c)
    60  	s.BaseSuite.TearDownTest(c)
    61  }
    62  
    63  func (s *InitializeSuite) TestInitialize(c *gc.C) {
    64  	cfg := testing.ModelConfig(c)
    65  	uuid := cfg.UUID()
    66  	initial := cfg.AllAttrs()
    67  	owner := names.NewLocalUserTag("initialize-admin")
    68  	st, err := state.Initialize(owner, statetesting.NewMongoInfo(), cfg, mongotest.DialOpts(), nil)
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	c.Assert(st, gc.NotNil)
    71  	modelTag := st.ModelTag()
    72  	c.Assert(modelTag.Id(), gc.Equals, uuid)
    73  	err = st.Close()
    74  	c.Assert(err, jc.ErrorIsNil)
    75  
    76  	s.openState(c, modelTag)
    77  
    78  	cfg, err = s.State.ModelConfig()
    79  	c.Assert(err, jc.ErrorIsNil)
    80  	c.Assert(cfg.AllAttrs(), gc.DeepEquals, initial)
    81  	// Check that the model has been created.
    82  	env, err := s.State.Model()
    83  	c.Assert(err, jc.ErrorIsNil)
    84  	c.Assert(env.Tag(), gc.Equals, modelTag)
    85  	// Check that the owner has been created.
    86  	c.Assert(env.Owner(), gc.Equals, owner)
    87  	// Check that the owner can be retrieved by the tag.
    88  	entity, err := s.State.FindEntity(env.Owner())
    89  	c.Assert(err, jc.ErrorIsNil)
    90  	c.Assert(entity.Tag(), gc.Equals, owner)
    91  	// Check that the owner has an ModelUser created for the bootstrapped model.
    92  	modelUser, err := s.State.ModelUser(env.Owner())
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	c.Assert(modelUser.UserTag(), gc.Equals, owner)
    95  	c.Assert(modelUser.ModelTag(), gc.Equals, env.Tag())
    96  
    97  	// Check that the model can be found through the tag.
    98  	entity, err = s.State.FindEntity(modelTag)
    99  	c.Assert(err, jc.ErrorIsNil)
   100  	cons, err := s.State.ModelConstraints()
   101  	c.Assert(err, jc.ErrorIsNil)
   102  	c.Assert(&cons, jc.Satisfies, constraints.IsEmpty)
   103  
   104  	addrs, err := s.State.APIHostPorts()
   105  	c.Assert(err, jc.ErrorIsNil)
   106  	c.Assert(addrs, gc.HasLen, 0)
   107  
   108  	info, err := s.State.ControllerInfo()
   109  	c.Assert(err, jc.ErrorIsNil)
   110  	c.Assert(info, jc.DeepEquals, &state.ControllerInfo{ModelTag: modelTag})
   111  }
   112  
   113  func (s *InitializeSuite) TestDoubleInitializeConfig(c *gc.C) {
   114  	cfg := testing.ModelConfig(c)
   115  	owner := names.NewLocalUserTag("initialize-admin")
   116  
   117  	mgoInfo := statetesting.NewMongoInfo()
   118  	dialOpts := mongotest.DialOpts()
   119  	st, err := state.Initialize(owner, mgoInfo, cfg, dialOpts, state.Policy(nil))
   120  	c.Assert(err, jc.ErrorIsNil)
   121  	err = st.Close()
   122  	c.Check(err, jc.ErrorIsNil)
   123  
   124  	st, err = state.Initialize(owner, mgoInfo, cfg, dialOpts, state.Policy(nil))
   125  	c.Check(err, gc.ErrorMatches, "already initialized")
   126  	if !c.Check(st, gc.IsNil) {
   127  		err = st.Close()
   128  		c.Check(err, jc.ErrorIsNil)
   129  	}
   130  }
   131  
   132  func (s *InitializeSuite) TestModelConfigWithAdminSecret(c *gc.C) {
   133  	// admin-secret blocks Initialize.
   134  	good := testing.ModelConfig(c)
   135  	badUpdateAttrs := map[string]interface{}{"admin-secret": "foo"}
   136  	bad, err := good.Apply(badUpdateAttrs)
   137  	owner := names.NewLocalUserTag("initialize-admin")
   138  
   139  	_, err = state.Initialize(owner, statetesting.NewMongoInfo(), bad, mongotest.DialOpts(), state.Policy(nil))
   140  	c.Assert(err, gc.ErrorMatches, "admin-secret should never be written to the state")
   141  
   142  	// admin-secret blocks UpdateModelConfig.
   143  	st := statetesting.Initialize(c, owner, good, nil)
   144  	st.Close()
   145  
   146  	s.openState(c, st.ModelTag())
   147  	err = s.State.UpdateModelConfig(badUpdateAttrs, nil, nil)
   148  	c.Assert(err, gc.ErrorMatches, "admin-secret should never be written to the state")
   149  
   150  	// ModelConfig remains inviolate.
   151  	cfg, err := s.State.ModelConfig()
   152  	c.Assert(err, jc.ErrorIsNil)
   153  	c.Assert(cfg.AllAttrs(), gc.DeepEquals, good.AllAttrs())
   154  }
   155  
   156  func (s *InitializeSuite) TestModelConfigWithoutAgentVersion(c *gc.C) {
   157  	// admin-secret blocks Initialize.
   158  	good := testing.ModelConfig(c)
   159  	attrs := good.AllAttrs()
   160  	delete(attrs, "agent-version")
   161  	bad, err := config.New(config.NoDefaults, attrs)
   162  	c.Assert(err, jc.ErrorIsNil)
   163  	owner := names.NewLocalUserTag("initialize-admin")
   164  
   165  	_, err = state.Initialize(owner, statetesting.NewMongoInfo(), bad, mongotest.DialOpts(), state.Policy(nil))
   166  	c.Assert(err, gc.ErrorMatches, "agent-version must always be set in state")
   167  
   168  	st := statetesting.Initialize(c, owner, good, nil)
   169  	// yay side effects
   170  	st.Close()
   171  
   172  	s.openState(c, st.ModelTag())
   173  	err = s.State.UpdateModelConfig(map[string]interface{}{}, []string{"agent-version"}, nil)
   174  	c.Assert(err, gc.ErrorMatches, "agent-version must always be set in state")
   175  
   176  	// ModelConfig remains inviolate.
   177  	cfg, err := s.State.ModelConfig()
   178  	c.Assert(err, jc.ErrorIsNil)
   179  	c.Assert(cfg.AllAttrs(), gc.DeepEquals, good.AllAttrs())
   180  }