github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/state/testing/conn.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  	"github.com/juju/names"
     8  	jujutesting "github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/environs/config"
    13  	"github.com/juju/juju/mongo"
    14  	"github.com/juju/juju/mongo/mongotest"
    15  	"github.com/juju/juju/state"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  // Initialize initializes the state and returns it. If state was not
    20  // already initialized, and cfg is nil, the minimal default model
    21  // configuration will be used.
    22  func Initialize(c *gc.C, owner names.UserTag, cfg *config.Config, policy state.Policy) *state.State {
    23  	if cfg == nil {
    24  		cfg = testing.ModelConfig(c)
    25  	}
    26  	mgoInfo := NewMongoInfo()
    27  	dialOpts := mongotest.DialOpts()
    28  
    29  	st, err := state.Initialize(owner, mgoInfo, cfg, dialOpts, policy)
    30  	c.Assert(err, jc.ErrorIsNil)
    31  	return st
    32  }
    33  
    34  // NewMongoInfo returns information suitable for
    35  // connecting to the testing controller's mongo database.
    36  func NewMongoInfo() *mongo.MongoInfo {
    37  	return &mongo.MongoInfo{
    38  		Info: mongo.Info{
    39  			Addrs:  []string{jujutesting.MgoServer.Addr()},
    40  			CACert: testing.CACert,
    41  		},
    42  	}
    43  }
    44  
    45  // NewState initializes a new state with default values for testing and
    46  // returns it.
    47  func NewState(c *gc.C) *state.State {
    48  	owner := names.NewLocalUserTag("test-admin")
    49  	cfg := testing.ModelConfig(c)
    50  	policy := MockPolicy{}
    51  	return Initialize(c, owner, cfg, &policy)
    52  }