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