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