github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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  	jujutesting "github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	"github.com/juju/utils/clock"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/cloud"
    14  	"github.com/juju/juju/environs/config"
    15  	"github.com/juju/juju/mongo"
    16  	"github.com/juju/juju/mongo/mongotest"
    17  	"github.com/juju/juju/state"
    18  	"github.com/juju/juju/storage"
    19  	"github.com/juju/juju/storage/provider"
    20  	dummystorage "github.com/juju/juju/storage/provider/dummy"
    21  	"github.com/juju/juju/testing"
    22  )
    23  
    24  type InitializeArgs struct {
    25  	Owner                     names.UserTag
    26  	InitialConfig             *config.Config
    27  	ControllerInheritedConfig map[string]interface{}
    28  	RegionConfig              cloud.RegionConfig
    29  	NewPolicy                 state.NewPolicyFunc
    30  	Clock                     clock.Clock
    31  }
    32  
    33  // Initialize initializes the state and returns it. If state was not
    34  // already initialized, and cfg is nil, the minimal default model
    35  // configuration will be used.
    36  // This provides for tests still using a real clock from utils as tests are
    37  // migrated to use the testing clock
    38  func Initialize(c *gc.C, owner names.UserTag, cfg *config.Config, controllerInheritedConfig map[string]interface{}, regionConfig cloud.RegionConfig, newPolicy state.NewPolicyFunc) *state.State {
    39  	return InitializeWithArgs(c, InitializeArgs{
    40  		Owner:                     owner,
    41  		InitialConfig:             cfg,
    42  		ControllerInheritedConfig: controllerInheritedConfig,
    43  		RegionConfig:              regionConfig,
    44  		NewPolicy:                 newPolicy,
    45  		Clock:                     &clock.WallClock,
    46  	})
    47  }
    48  
    49  // InitializeWithArgs initializes the state and returns it. If state was not
    50  // already initialized, and args.Config is nil, the minimal default model
    51  // configuration will be used.
    52  func InitializeWithArgs(c *gc.C, args InitializeArgs) *state.State {
    53  	if args.InitialConfig == nil {
    54  		args.InitialConfig = testing.ModelConfig(c)
    55  	}
    56  	mgoInfo := NewMongoInfo()
    57  	dialOpts := mongotest.DialOpts()
    58  
    59  	controllerCfg := testing.FakeControllerConfig()
    60  	st, err := state.Initialize(state.InitializeParams{
    61  		Clock:            args.Clock,
    62  		ControllerConfig: controllerCfg,
    63  		ControllerModelArgs: state.ModelArgs{
    64  			CloudName:   "dummy",
    65  			CloudRegion: "dummy-region",
    66  			Config:      args.InitialConfig,
    67  			Owner:       args.Owner,
    68  			StorageProviderRegistry: StorageProviders(),
    69  		},
    70  		ControllerInheritedConfig: args.ControllerInheritedConfig,
    71  		CloudName:                 "dummy",
    72  		Cloud: cloud.Cloud{
    73  			Type:      "dummy",
    74  			AuthTypes: []cloud.AuthType{cloud.EmptyAuthType},
    75  			Regions: []cloud.Region{
    76  				cloud.Region{
    77  					Name:             "dummy-region",
    78  					Endpoint:         "dummy-endpoint",
    79  					IdentityEndpoint: "dummy-identity-endpoint",
    80  					StorageEndpoint:  "dummy-storage-endpoint",
    81  				},
    82  				cloud.Region{
    83  					Name:             "nether-region",
    84  					Endpoint:         "nether-endpoint",
    85  					IdentityEndpoint: "nether-identity-endpoint",
    86  					StorageEndpoint:  "nether-storage-endpoint",
    87  				},
    88  			},
    89  			RegionConfig: args.RegionConfig,
    90  		},
    91  		MongoInfo:     mgoInfo,
    92  		MongoDialOpts: dialOpts,
    93  		NewPolicy:     args.NewPolicy,
    94  	})
    95  	c.Assert(err, jc.ErrorIsNil)
    96  	return st
    97  }
    98  
    99  func StorageProviders() storage.ProviderRegistry {
   100  	return storage.ChainedProviderRegistry{
   101  		storage.StaticProviderRegistry{
   102  			map[storage.ProviderType]storage.Provider{
   103  				"static": &dummystorage.StorageProvider{IsDynamic: false},
   104  				"environscoped": &dummystorage.StorageProvider{
   105  					StorageScope: storage.ScopeEnviron,
   106  					IsDynamic:    true,
   107  				},
   108  				"environscoped-block": &dummystorage.StorageProvider{
   109  					StorageScope: storage.ScopeEnviron,
   110  					IsDynamic:    true,
   111  					SupportsFunc: func(k storage.StorageKind) bool {
   112  						return k == storage.StorageKindBlock
   113  					},
   114  				},
   115  				"machinescoped": &dummystorage.StorageProvider{
   116  					StorageScope: storage.ScopeMachine,
   117  					IsDynamic:    true,
   118  				},
   119  			},
   120  		},
   121  		provider.CommonStorageProviders(),
   122  	}
   123  }
   124  
   125  // NewMongoInfo returns information suitable for
   126  // connecting to the testing controller's mongo database.
   127  func NewMongoInfo() *mongo.MongoInfo {
   128  	return &mongo.MongoInfo{
   129  		Info: mongo.Info{
   130  			Addrs:  []string{jujutesting.MgoServer.Addr()},
   131  			CACert: testing.CACert,
   132  		},
   133  	}
   134  }
   135  
   136  // NewState initializes a new state with default values for testing and
   137  // returns it.
   138  func NewState(c *gc.C) *state.State {
   139  	owner := names.NewLocalUserTag("test-admin")
   140  	cfg := testing.ModelConfig(c)
   141  	newPolicy := func(*state.State) state.Policy { return &MockPolicy{} }
   142  	return Initialize(c, owner, cfg, nil, nil, newPolicy)
   143  }