github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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) StartInstance(args environs.StartInstanceParams) (*environs.StartInstanceResult, error) {
   128  	e.Push("StartInstance", args)
   129  	return &environs.StartInstanceResult{
   130  		Instance: &fakeInstance{},
   131  	}, nil
   132  }
   133  
   134  func (e *fakeEnviron) StopInstances(ids ...instance.Id) error {
   135  	e.Push("StopInstances", ids)
   136  	return nil
   137  }
   138  
   139  func (e *fakeEnviron) AllInstances() ([]instance.Instance, error) {
   140  	e.Push("AllInstances")
   141  	return nil, nil
   142  }
   143  
   144  func (e *fakeEnviron) MaintainInstance(args environs.StartInstanceParams) error {
   145  	e.Push("MaintainInstance", args)
   146  	return nil
   147  }
   148  
   149  func (e *fakeEnviron) Config() *config.Config {
   150  	return e.config
   151  }
   152  
   153  func (e *fakeEnviron) ConstraintsValidator() (constraints.Validator, error) {
   154  	e.Push("ConstraintsValidator")
   155  	return nil, nil
   156  }
   157  
   158  func (e *fakeEnviron) SetConfig(cfg *config.Config) error {
   159  	e.config = cfg
   160  	return nil
   161  }
   162  
   163  func (e *fakeEnviron) Instances(ids []instance.Id) ([]instance.Instance, error) {
   164  	e.Push("Instances", ids)
   165  	return []instance.Instance{&fakeInstance{}}, nil
   166  }
   167  
   168  func (e *fakeEnviron) ControllerInstances(_ string) ([]instance.Id, error) {
   169  	e.Push("ControllerInstances")
   170  	return nil, nil
   171  }
   172  
   173  func (e *fakeEnviron) Destroy() error {
   174  	e.Push("Destroy")
   175  	return nil
   176  }
   177  
   178  func (e *fakeEnviron) DestroyController(controllerUUID string) error {
   179  	e.Push("Destroy")
   180  	return nil
   181  }
   182  
   183  func (e *fakeEnviron) OpenPorts(ports []network.PortRange) error {
   184  	e.Push("OpenPorts", ports)
   185  	return nil
   186  }
   187  
   188  func (e *fakeEnviron) ClosePorts(ports []network.PortRange) error {
   189  	e.Push("ClosePorts", ports)
   190  	return nil
   191  }
   192  
   193  func (e *fakeEnviron) Ports() ([]network.PortRange, error) {
   194  	e.Push("Ports")
   195  	return nil, nil
   196  }
   197  
   198  func (e *fakeEnviron) Provider() environs.EnvironProvider {
   199  	e.Push("Provider")
   200  	return nil
   201  }
   202  
   203  func (e *fakeEnviron) PrecheckInstance(series string, cons constraints.Value, placement string) error {
   204  	e.Push("PrecheckInstance", series, cons, placement)
   205  	return nil
   206  }
   207  
   208  func (e *fakeEnviron) StorageProviderTypes() ([]storage.ProviderType, error) {
   209  	e.Push("StorageProviderTypes")
   210  	return nil, nil
   211  }
   212  
   213  func (e *fakeEnviron) StorageProvider(t storage.ProviderType) (storage.Provider, error) {
   214  	e.Push("StorageProvider", t)
   215  	return nil, errors.NotImplementedf("StorageProvider")
   216  }
   217  
   218  type fakeConfigurator struct {
   219  	methodCalls []methodCall
   220  }
   221  
   222  func (p *fakeConfigurator) Push(name string, params ...interface{}) {
   223  	p.methodCalls = append(p.methodCalls, methodCall{name, params})
   224  }
   225  
   226  func (p *fakeConfigurator) Pop() methodCall {
   227  	m := p.methodCalls[0]
   228  	p.methodCalls = p.methodCalls[1:]
   229  	return m
   230  }
   231  
   232  func (e *fakeConfigurator) DropAllPorts(exceptPorts []int, addr string) error {
   233  	e.Push("DropAllPorts", exceptPorts, addr)
   234  	return nil
   235  }
   236  
   237  func (e *fakeConfigurator) ConfigureExternalIpAddress(apiPort int) error {
   238  	e.Push("ConfigureExternalIpAddress", apiPort)
   239  	return nil
   240  }
   241  
   242  func (e *fakeConfigurator) ChangePorts(ipAddress string, insert bool, ports []network.PortRange) error {
   243  	e.Push("ChangePorts", ipAddress, insert, ports)
   244  	return nil
   245  }
   246  
   247  func (e *fakeConfigurator) FindOpenPorts() ([]network.PortRange, error) {
   248  	e.Push("FindOpenPorts")
   249  	return nil, nil
   250  }
   251  
   252  func (e *fakeConfigurator) AddIpAddress(nic string, addr string) error {
   253  	e.Push("AddIpAddress", nic, addr)
   254  	return nil
   255  }
   256  
   257  func (e *fakeConfigurator) ReleaseIpAddress(addr string) error {
   258  	e.Push("AddIpAddress", addr)
   259  	return nil
   260  }
   261  
   262  type fakeInstance struct {
   263  	methodCalls []methodCall
   264  }
   265  
   266  func (p *fakeInstance) Push(name string, params ...interface{}) {
   267  	p.methodCalls = append(p.methodCalls, methodCall{name, params})
   268  }
   269  
   270  func (p *fakeInstance) Pop() methodCall {
   271  	m := p.methodCalls[0]
   272  	p.methodCalls = p.methodCalls[1:]
   273  	return m
   274  }
   275  
   276  func (e *fakeInstance) Id() instance.Id {
   277  	e.Push("Id")
   278  	return instance.Id("")
   279  }
   280  
   281  func (e *fakeInstance) Status() instance.InstanceStatus {
   282  	e.Push("Status")
   283  	return instance.InstanceStatus{
   284  		Status:  status.Provisioning,
   285  		Message: "a message",
   286  	}
   287  }
   288  
   289  func (e *fakeInstance) Refresh() error {
   290  	e.Push("Refresh")
   291  	return nil
   292  }
   293  
   294  func (e *fakeInstance) Addresses() ([]network.Address, error) {
   295  	e.Push("Addresses")
   296  	return []network.Address{network.Address{
   297  		Value: "1.1.1.1",
   298  		Type:  network.IPv4Address,
   299  		Scope: network.ScopePublic,
   300  	}}, nil
   301  }
   302  
   303  func (e *fakeInstance) OpenPorts(machineId string, ports []network.PortRange) error {
   304  	e.Push("OpenPorts", machineId, ports)
   305  	return nil
   306  }
   307  
   308  func (e *fakeInstance) ClosePorts(machineId string, ports []network.PortRange) error {
   309  	e.Push("ClosePorts", machineId, ports)
   310  	return nil
   311  }
   312  
   313  func (e *fakeInstance) Ports(machineId string) ([]network.PortRange, error) {
   314  	e.Push("Ports", machineId)
   315  	return nil, nil
   316  }