github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/juju/testing/instance.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/names" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/api" 13 "github.com/juju/juju/cloudconfig/instancecfg" 14 "github.com/juju/juju/constraints" 15 "github.com/juju/juju/environs" 16 "github.com/juju/juju/environs/config" 17 "github.com/juju/juju/environs/imagemetadata" 18 "github.com/juju/juju/environs/tools" 19 "github.com/juju/juju/instance" 20 "github.com/juju/juju/mongo" 21 "github.com/juju/juju/network" 22 "github.com/juju/juju/testing" 23 coretools "github.com/juju/juju/tools" 24 ) 25 26 // FakeStateInfo holds information about no state - it will always 27 // give an error when connected to. The machine id gives the machine id 28 // of the machine to be started. 29 func FakeStateInfo(machineId string) *mongo.MongoInfo { 30 return &mongo.MongoInfo{ 31 Info: mongo.Info{ 32 Addrs: []string{"0.1.2.3:1234"}, 33 CACert: testing.CACert, 34 }, 35 Tag: names.NewMachineTag(machineId), 36 Password: "unimportant", 37 } 38 } 39 40 // FakeAPIInfo holds information about no state - it will always 41 // give an error when connected to. The machine id gives the machine id 42 // of the machine to be started. 43 func FakeAPIInfo(machineId string) *api.Info { 44 return &api.Info{ 45 Addrs: []string{"0.1.2.3:1234"}, 46 Tag: names.NewMachineTag(machineId), 47 Password: "unimportant", 48 CACert: testing.CACert, 49 EnvironTag: testing.EnvironmentTag, 50 } 51 } 52 53 // WaitAddresses waits until the specified instance has addresses, and returns them. 54 func WaitInstanceAddresses(env environs.Environ, instId instance.Id) ([]network.Address, error) { 55 for a := testing.LongAttempt.Start(); a.Next(); { 56 insts, err := env.Instances([]instance.Id{instId}) 57 if err != nil { 58 return nil, err 59 } 60 addresses, err := insts[0].Addresses() 61 if err != nil { 62 return nil, err 63 } 64 if len(addresses) > 0 { 65 return addresses, nil 66 } 67 } 68 return nil, errors.Errorf("timed out trying to get addresses for %v", instId) 69 } 70 71 // AssertStartInstance is a test helper function that starts an instance with a 72 // plausible but invalid configuration, and checks that it succeeds. 73 func AssertStartInstance( 74 c *gc.C, env environs.Environ, machineId string, 75 ) ( 76 instance.Instance, *instance.HardwareCharacteristics, 77 ) { 78 inst, hc, _, err := StartInstance(env, machineId) 79 c.Assert(err, jc.ErrorIsNil) 80 return inst, hc 81 } 82 83 // StartInstance is a test helper function that starts an instance with a plausible 84 // but invalid configuration, and returns the result of Environ.StartInstance. 85 func StartInstance( 86 env environs.Environ, machineId string, 87 ) ( 88 instance.Instance, *instance.HardwareCharacteristics, []network.InterfaceInfo, error, 89 ) { 90 return StartInstanceWithConstraints(env, machineId, constraints.Value{}) 91 } 92 93 // AssertStartInstanceWithConstraints is a test helper function that starts an instance 94 // with the given constraints, and a plausible but invalid configuration, and returns 95 // the result of Environ.StartInstance. 96 func AssertStartInstanceWithConstraints( 97 c *gc.C, env environs.Environ, machineId string, cons constraints.Value, 98 ) ( 99 instance.Instance, *instance.HardwareCharacteristics, 100 ) { 101 inst, hc, _, err := StartInstanceWithConstraints(env, machineId, cons) 102 c.Assert(err, jc.ErrorIsNil) 103 return inst, hc 104 } 105 106 // StartInstanceWithConstraints is a test helper function that starts an instance 107 // with the given constraints, and a plausible but invalid configuration, and returns 108 // the result of Environ.StartInstance. 109 func StartInstanceWithConstraints( 110 env environs.Environ, machineId string, cons constraints.Value, 111 ) ( 112 instance.Instance, *instance.HardwareCharacteristics, []network.InterfaceInfo, error, 113 ) { 114 return StartInstanceWithConstraintsAndNetworks(env, machineId, cons, nil) 115 } 116 117 // AssertStartInstanceWithNetworks is a test helper function that starts an 118 // instance with the given networks, and a plausible but invalid 119 // configuration, and returns the result of Environ.StartInstance. 120 func AssertStartInstanceWithNetworks( 121 c *gc.C, env environs.Environ, machineId string, cons constraints.Value, 122 networks []string, 123 ) ( 124 instance.Instance, *instance.HardwareCharacteristics, 125 ) { 126 inst, hc, _, err := StartInstanceWithConstraintsAndNetworks( 127 env, machineId, cons, networks) 128 c.Assert(err, jc.ErrorIsNil) 129 return inst, hc 130 } 131 132 // StartInstanceWithConstraintsAndNetworks is a test helper function that 133 // starts an instance with the given networks, and a plausible but invalid 134 // configuration, and returns the result of Environ.StartInstance. 135 func StartInstanceWithConstraintsAndNetworks( 136 env environs.Environ, machineId string, cons constraints.Value, 137 networks []string, 138 ) ( 139 instance.Instance, *instance.HardwareCharacteristics, []network.InterfaceInfo, error, 140 ) { 141 params := environs.StartInstanceParams{Constraints: cons} 142 result, err := StartInstanceWithParams(env, machineId, params, networks) 143 if err != nil { 144 return nil, nil, nil, errors.Trace(err) 145 } 146 return result.Instance, result.Hardware, result.NetworkInfo, nil 147 } 148 149 // StartInstanceWithParams is a test helper function that starts an instance 150 // with the given parameters, and a plausible but invalid configuration, and 151 // returns the result of Environ.StartInstance. The provided params's 152 // InstanceConfig and Tools field values will be ignored. 153 func StartInstanceWithParams( 154 env environs.Environ, machineId string, 155 params environs.StartInstanceParams, 156 networks []string, 157 ) ( 158 *environs.StartInstanceResult, error, 159 ) { 160 series := config.PreferredSeries(env.Config()) 161 agentVersion, ok := env.Config().AgentVersion() 162 if !ok { 163 return nil, errors.New("missing agent version in environment config") 164 } 165 filter := coretools.Filter{ 166 Number: agentVersion, 167 Series: series, 168 } 169 if params.Constraints.Arch != nil { 170 filter.Arch = *params.Constraints.Arch 171 } 172 stream := tools.PreferredStream(&agentVersion, env.Config().Development(), env.Config().AgentStream()) 173 possibleTools, err := tools.FindTools(env, -1, -1, stream, filter) 174 if err != nil { 175 return nil, errors.Trace(err) 176 } 177 machineNonce := "fake_nonce" 178 stateInfo := FakeStateInfo(machineId) 179 apiInfo := FakeAPIInfo(machineId) 180 instanceConfig, err := instancecfg.NewInstanceConfig( 181 machineId, 182 machineNonce, 183 imagemetadata.ReleasedStream, 184 series, 185 true, 186 networks, 187 stateInfo, 188 apiInfo, 189 ) 190 if err != nil { 191 return nil, errors.Trace(err) 192 } 193 params.Tools = possibleTools 194 params.InstanceConfig = instanceConfig 195 return env.StartInstance(params) 196 }