github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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/state"
    15  	"github.com/juju/juju/testing"
    16  )
    17  
    18  // Initialize initializes the state and returns it. If state was not
    19  // already initialized, and cfg is nil, the minimal default environment
    20  // configuration will be used.
    21  func Initialize(c *gc.C, owner names.UserTag, cfg *config.Config, policy state.Policy) *state.State {
    22  	if cfg == nil {
    23  		cfg = testing.EnvironConfig(c)
    24  	}
    25  	mgoInfo := NewMongoInfo()
    26  	dialOpts := NewDialOpts()
    27  
    28  	st, err := state.Initialize(owner, mgoInfo, cfg, dialOpts, policy)
    29  	c.Assert(err, jc.ErrorIsNil)
    30  	return st
    31  }
    32  
    33  // NewMongoInfo returns information suitable for
    34  // connecting to the testing state server's mongo database.
    35  func NewMongoInfo() *mongo.MongoInfo {
    36  	return &mongo.MongoInfo{
    37  		Info: mongo.Info{
    38  			Addrs:  []string{jujutesting.MgoServer.Addr()},
    39  			CACert: testing.CACert,
    40  		},
    41  	}
    42  }
    43  
    44  // NewDialOpts returns configuration parameters for
    45  // connecting to the testing state server.
    46  func NewDialOpts() mongo.DialOpts {
    47  	return mongo.DialOpts{
    48  		Timeout: testing.LongWait,
    49  	}
    50  }
    51  
    52  // NewState initializes a new state with default values for testing and
    53  // returns it.
    54  func NewState(c *gc.C) *state.State {
    55  	owner := names.NewLocalUserTag("test-admin")
    56  	cfg := testing.EnvironConfig(c)
    57  	policy := MockPolicy{}
    58  	return Initialize(c, owner, cfg, &policy)
    59  }