github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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 cloud.Region{ 89 Name: "unused-region", 90 Endpoint: "unused-endpoint", 91 IdentityEndpoint: "unused-identity-endpoint", 92 StorageEndpoint: "unused-storage-endpoint", 93 }, 94 }, 95 RegionConfig: args.RegionConfig, 96 }, 97 MongoInfo: mgoInfo, 98 MongoDialOpts: dialOpts, 99 NewPolicy: args.NewPolicy, 100 }) 101 c.Assert(err, jc.ErrorIsNil) 102 return st 103 } 104 105 func StorageProviders() storage.ProviderRegistry { 106 return storage.ChainedProviderRegistry{ 107 storage.StaticProviderRegistry{ 108 map[storage.ProviderType]storage.Provider{ 109 "static": &dummystorage.StorageProvider{IsDynamic: false}, 110 "environscoped": &dummystorage.StorageProvider{ 111 StorageScope: storage.ScopeEnviron, 112 IsDynamic: true, 113 }, 114 "environscoped-block": &dummystorage.StorageProvider{ 115 StorageScope: storage.ScopeEnviron, 116 IsDynamic: true, 117 SupportsFunc: func(k storage.StorageKind) bool { 118 return k == storage.StorageKindBlock 119 }, 120 }, 121 "machinescoped": &dummystorage.StorageProvider{ 122 StorageScope: storage.ScopeMachine, 123 IsDynamic: true, 124 }, 125 }, 126 }, 127 provider.CommonStorageProviders(), 128 } 129 } 130 131 // NewMongoInfo returns information suitable for 132 // connecting to the testing controller's mongo database. 133 func NewMongoInfo() *mongo.MongoInfo { 134 return &mongo.MongoInfo{ 135 Info: mongo.Info{ 136 Addrs: []string{jujutesting.MgoServer.Addr()}, 137 CACert: testing.CACert, 138 }, 139 } 140 } 141 142 // NewState initializes a new state with default values for testing and 143 // returns it. 144 func NewState(c *gc.C) *state.State { 145 owner := names.NewLocalUserTag("test-admin") 146 cfg := testing.ModelConfig(c) 147 newPolicy := func(*state.State) state.Policy { return &MockPolicy{} } 148 return Initialize(c, owner, cfg, nil, nil, newPolicy) 149 }