github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/state/pool_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/state"
    11  	statetesting "github.com/juju/juju/state/testing"
    12  )
    13  
    14  type statePoolSuite struct {
    15  	statetesting.StateSuite
    16  	State1, State2                    *state.State
    17  	ModelUUID, ModelUUID1, ModelUUID2 string
    18  }
    19  
    20  var _ = gc.Suite(&statePoolSuite{})
    21  
    22  func (s *statePoolSuite) SetUpTest(c *gc.C) {
    23  	s.StateSuite.SetUpTest(c)
    24  	s.ModelUUID = s.State.ModelUUID()
    25  
    26  	s.State1 = s.Factory.MakeModel(c, nil)
    27  	s.AddCleanup(func(*gc.C) { s.State1.Close() })
    28  	s.ModelUUID1 = s.State1.ModelUUID()
    29  
    30  	s.State2 = s.Factory.MakeModel(c, nil)
    31  	s.AddCleanup(func(*gc.C) { s.State2.Close() })
    32  	s.ModelUUID2 = s.State2.ModelUUID()
    33  }
    34  
    35  func (s *statePoolSuite) TestGet(c *gc.C) {
    36  	p := state.NewStatePool(s.State)
    37  	defer p.Close()
    38  
    39  	st1, err := p.Get(s.ModelUUID1)
    40  	c.Assert(err, jc.ErrorIsNil)
    41  	c.Assert(st1.ModelUUID(), gc.Equals, s.ModelUUID1)
    42  
    43  	st2, err := p.Get(s.ModelUUID2)
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	c.Assert(st2.ModelUUID(), gc.Equals, s.ModelUUID2)
    46  
    47  	// Check that the same instances are returned
    48  	// when a State for the same env is re-requested.
    49  	st1_, err := p.Get(s.ModelUUID1)
    50  	c.Assert(err, jc.ErrorIsNil)
    51  	c.Assert(st1_, gc.Equals, st1)
    52  
    53  	st2_, err := p.Get(s.ModelUUID2)
    54  	c.Assert(err, jc.ErrorIsNil)
    55  	c.Assert(st2_, gc.Equals, st2)
    56  }
    57  
    58  func (s *statePoolSuite) TestGetWithControllerEnv(c *gc.C) {
    59  	p := state.NewStatePool(s.State)
    60  	defer p.Close()
    61  
    62  	// When a State for the controller env is requested, the same
    63  	// State that was original passed in should be returned.
    64  	st0, err := p.Get(s.ModelUUID)
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	c.Assert(st0, gc.Equals, s.State)
    67  }
    68  
    69  func (s *statePoolSuite) TestSystemState(c *gc.C) {
    70  	p := state.NewStatePool(s.State)
    71  	defer p.Close()
    72  
    73  	st0 := p.SystemState()
    74  	c.Assert(st0, gc.Equals, s.State)
    75  }
    76  
    77  func (s *statePoolSuite) TestClose(c *gc.C) {
    78  	p := state.NewStatePool(s.State)
    79  	defer p.Close()
    80  
    81  	// Get some State instances.
    82  	st1, err := p.Get(s.ModelUUID1)
    83  	c.Assert(err, jc.ErrorIsNil)
    84  
    85  	st2, err := p.Get(s.ModelUUID1)
    86  	c.Assert(err, jc.ErrorIsNil)
    87  
    88  	// Now close them.
    89  	err = p.Close()
    90  	c.Assert(err, jc.ErrorIsNil)
    91  
    92  	// Confirm that controller State isn't closed.
    93  	_, err = s.State.Model()
    94  	c.Assert(err, jc.ErrorIsNil)
    95  
    96  	// Ensure that new ones are returned if further States are
    97  	// requested.
    98  	st1_, err := p.Get(s.ModelUUID1)
    99  	c.Assert(err, jc.ErrorIsNil)
   100  	c.Assert(st1_, gc.Not(gc.Equals), st1)
   101  
   102  	st2_, err := p.Get(s.ModelUUID2)
   103  	c.Assert(err, jc.ErrorIsNil)
   104  	c.Assert(st2_, gc.Not(gc.Equals), st2)
   105  }