github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/provider/rackspace/environ_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package rackspace_test 5 6 import ( 7 "io" 8 "os" 9 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/errors" 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/instance" 18 "github.com/juju/juju/network" 19 "github.com/juju/juju/provider/common" 20 "github.com/juju/juju/provider/rackspace" 21 "github.com/juju/juju/status" 22 "github.com/juju/juju/storage" 23 "github.com/juju/juju/testing" 24 "github.com/juju/juju/tools" 25 "github.com/juju/utils/ssh" 26 "github.com/juju/version" 27 ) 28 29 type environSuite struct { 30 testing.BaseSuite 31 environ environs.Environ 32 innerEnviron *fakeEnviron 33 } 34 35 var _ = gc.Suite(&environSuite{}) 36 37 func (s *environSuite) SetUpTest(c *gc.C) { 38 s.innerEnviron = new(fakeEnviron) 39 s.environ = rackspace.NewEnviron(s.innerEnviron) 40 } 41 42 func (s *environSuite) TestBootstrap(c *gc.C) { 43 s.PatchValue(rackspace.Bootstrap, func(ctx environs.BootstrapContext, env environs.Environ, args environs.BootstrapParams) (*environs.BootstrapResult, error) { 44 return s.innerEnviron.Bootstrap(ctx, args) 45 }) 46 s.environ.Bootstrap(nil, environs.BootstrapParams{ 47 ControllerConfig: testing.FakeControllerConfig(), 48 }) 49 c.Check(s.innerEnviron.Pop().name, gc.Equals, "Bootstrap") 50 } 51 52 func (s *environSuite) TestStartInstance(c *gc.C) { 53 configurator := &fakeConfigurator{} 54 s.PatchValue(rackspace.WaitSSH, func(stdErr io.Writer, interrupted <-chan os.Signal, client ssh.Client, checkHostScript string, inst common.InstanceRefresher, timeout environs.BootstrapDialOpts) (addr string, err error) { 55 addresses, err := inst.Addresses() 56 if err != nil { 57 return "", err 58 } 59 return addresses[0].Value, nil 60 }) 61 s.PatchValue(rackspace.NewInstanceConfigurator, func(host string) common.InstanceConfigurator { 62 return configurator 63 }) 64 config, err := config.New(config.UseDefaults, map[string]interface{}{ 65 "name": "some-name", 66 "type": "some-type", 67 "uuid": testing.ModelTag.Id(), 68 "controller-uuid": testing.ControllerTag.Id(), 69 "authorized-keys": "key", 70 }) 71 c.Assert(err, gc.IsNil) 72 err = s.environ.SetConfig(config) 73 c.Assert(err, gc.IsNil) 74 _, err = s.environ.StartInstance(environs.StartInstanceParams{ 75 InstanceConfig: &instancecfg.InstanceConfig{}, 76 Tools: tools.List{&tools.Tools{ 77 Version: version.Binary{Series: "trusty"}, 78 }}, 79 }) 80 c.Check(err, gc.IsNil) 81 c.Check(s.innerEnviron.Pop().name, gc.Equals, "StartInstance") 82 dropParams := configurator.Pop() 83 c.Check(dropParams.name, gc.Equals, "DropAllPorts") 84 c.Check(dropParams.params[1], gc.Equals, "1.1.1.1") 85 } 86 87 type methodCall struct { 88 name string 89 params []interface{} 90 } 91 92 type fakeEnviron struct { 93 config *config.Config 94 methodCalls []methodCall 95 } 96 97 func (p *fakeEnviron) Push(name string, params ...interface{}) { 98 p.methodCalls = append(p.methodCalls, methodCall{name, params}) 99 } 100 101 func (p *fakeEnviron) Pop() methodCall { 102 m := p.methodCalls[0] 103 p.methodCalls = p.methodCalls[1:] 104 return m 105 } 106 107 func (p *fakeEnviron) Open(cfg *config.Config) (environs.Environ, error) { 108 p.Push("Open", cfg) 109 return nil, nil 110 } 111 112 func (e *fakeEnviron) Create(args environs.CreateParams) error { 113 e.Push("Create", args) 114 return nil 115 } 116 117 func (e *fakeEnviron) PrepareForBootstrap(ctx environs.BootstrapContext) error { 118 e.Push("PrepareForBootstrap", ctx) 119 return nil 120 } 121 122 func (e *fakeEnviron) Bootstrap(ctx environs.BootstrapContext, params environs.BootstrapParams) (*environs.BootstrapResult, error) { 123 e.Push("Bootstrap", ctx, params) 124 return nil, nil 125 } 126 127 func (e *fakeEnviron) BootstrapMessage() string { 128 return "" 129 } 130 131 func (e *fakeEnviron) StartInstance(args environs.StartInstanceParams) (*environs.StartInstanceResult, error) { 132 e.Push("StartInstance", args) 133 return &environs.StartInstanceResult{ 134 Instance: &fakeInstance{}, 135 }, nil 136 } 137 138 func (e *fakeEnviron) StopInstances(ids ...instance.Id) error { 139 e.Push("StopInstances", ids) 140 return nil 141 } 142 143 func (e *fakeEnviron) AllInstances() ([]instance.Instance, error) { 144 e.Push("AllInstances") 145 return nil, nil 146 } 147 148 func (e *fakeEnviron) MaintainInstance(args environs.StartInstanceParams) error { 149 e.Push("MaintainInstance", args) 150 return nil 151 } 152 153 func (e *fakeEnviron) Config() *config.Config { 154 return e.config 155 } 156 157 func (e *fakeEnviron) ConstraintsValidator() (constraints.Validator, error) { 158 e.Push("ConstraintsValidator") 159 return nil, nil 160 } 161 162 func (e *fakeEnviron) SetConfig(cfg *config.Config) error { 163 e.config = cfg 164 return nil 165 } 166 167 func (e *fakeEnviron) Instances(ids []instance.Id) ([]instance.Instance, error) { 168 e.Push("Instances", ids) 169 return []instance.Instance{&fakeInstance{}}, nil 170 } 171 172 func (e *fakeEnviron) ControllerInstances(_ string) ([]instance.Id, error) { 173 e.Push("ControllerInstances") 174 return nil, nil 175 } 176 177 func (e *fakeEnviron) Destroy() error { 178 e.Push("Destroy") 179 return nil 180 } 181 182 func (e *fakeEnviron) DestroyController(controllerUUID string) error { 183 e.Push("Destroy") 184 return nil 185 } 186 187 func (e *fakeEnviron) OpenPorts(ports []network.PortRange) error { 188 e.Push("OpenPorts", ports) 189 return nil 190 } 191 192 func (e *fakeEnviron) ClosePorts(ports []network.PortRange) error { 193 e.Push("ClosePorts", ports) 194 return nil 195 } 196 197 func (e *fakeEnviron) Ports() ([]network.PortRange, error) { 198 e.Push("Ports") 199 return nil, nil 200 } 201 202 func (e *fakeEnviron) Provider() environs.EnvironProvider { 203 e.Push("Provider") 204 return nil 205 } 206 207 func (e *fakeEnviron) PrecheckInstance(series string, cons constraints.Value, placement string) error { 208 e.Push("PrecheckInstance", series, cons, placement) 209 return nil 210 } 211 212 func (e *fakeEnviron) StorageProviderTypes() ([]storage.ProviderType, error) { 213 e.Push("StorageProviderTypes") 214 return nil, nil 215 } 216 217 func (e *fakeEnviron) StorageProvider(t storage.ProviderType) (storage.Provider, error) { 218 e.Push("StorageProvider", t) 219 return nil, errors.NotImplementedf("StorageProvider") 220 } 221 222 type fakeConfigurator struct { 223 methodCalls []methodCall 224 } 225 226 func (p *fakeConfigurator) Push(name string, params ...interface{}) { 227 p.methodCalls = append(p.methodCalls, methodCall{name, params}) 228 } 229 230 func (p *fakeConfigurator) Pop() methodCall { 231 m := p.methodCalls[0] 232 p.methodCalls = p.methodCalls[1:] 233 return m 234 } 235 236 func (e *fakeConfigurator) DropAllPorts(exceptPorts []int, addr string) error { 237 e.Push("DropAllPorts", exceptPorts, addr) 238 return nil 239 } 240 241 func (e *fakeConfigurator) ConfigureExternalIpAddress(apiPort int) error { 242 e.Push("ConfigureExternalIpAddress", apiPort) 243 return nil 244 } 245 246 func (e *fakeConfigurator) ChangePorts(ipAddress string, insert bool, ports []network.PortRange) error { 247 e.Push("ChangePorts", ipAddress, insert, ports) 248 return nil 249 } 250 251 func (e *fakeConfigurator) FindOpenPorts() ([]network.PortRange, error) { 252 e.Push("FindOpenPorts") 253 return nil, nil 254 } 255 256 func (e *fakeConfigurator) AddIpAddress(nic string, addr string) error { 257 e.Push("AddIpAddress", nic, addr) 258 return nil 259 } 260 261 func (e *fakeConfigurator) ReleaseIpAddress(addr string) error { 262 e.Push("AddIpAddress", addr) 263 return nil 264 } 265 266 type fakeInstance struct { 267 methodCalls []methodCall 268 } 269 270 func (p *fakeInstance) Push(name string, params ...interface{}) { 271 p.methodCalls = append(p.methodCalls, methodCall{name, params}) 272 } 273 274 func (p *fakeInstance) Pop() methodCall { 275 m := p.methodCalls[0] 276 p.methodCalls = p.methodCalls[1:] 277 return m 278 } 279 280 func (e *fakeInstance) Id() instance.Id { 281 e.Push("Id") 282 return instance.Id("") 283 } 284 285 func (e *fakeInstance) Status() instance.InstanceStatus { 286 e.Push("Status") 287 return instance.InstanceStatus{ 288 Status: status.Provisioning, 289 Message: "a message", 290 } 291 } 292 293 func (e *fakeInstance) Refresh() error { 294 e.Push("Refresh") 295 return nil 296 } 297 298 func (e *fakeInstance) Addresses() ([]network.Address, error) { 299 e.Push("Addresses") 300 return []network.Address{network.Address{ 301 Value: "1.1.1.1", 302 Type: network.IPv4Address, 303 Scope: network.ScopePublic, 304 }}, nil 305 } 306 307 func (e *fakeInstance) OpenPorts(machineId string, ports []network.PortRange) error { 308 e.Push("OpenPorts", machineId, ports) 309 return nil 310 } 311 312 func (e *fakeInstance) ClosePorts(machineId string, ports []network.PortRange) error { 313 e.Push("ClosePorts", machineId, ports) 314 return nil 315 } 316 317 func (e *fakeInstance) Ports(machineId string) ([]network.PortRange, error) { 318 e.Push("Ports", machineId) 319 return nil, nil 320 }