github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/provider/dummy/environs_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package dummy_test
     5  
     6  import (
     7  	"net"
     8  	"strings"
     9  	stdtesting "testing"
    10  	"time"
    11  
    12  	"github.com/juju/errors"
    13  	gitjujutesting "github.com/juju/testing"
    14  	jc "github.com/juju/testing/checkers"
    15  	gc "gopkg.in/check.v1"
    16  
    17  	"github.com/juju/juju/environs"
    18  	"github.com/juju/juju/environs/bootstrap"
    19  	"github.com/juju/juju/environs/config"
    20  	"github.com/juju/juju/environs/jujutest"
    21  	envtesting "github.com/juju/juju/environs/testing"
    22  	"github.com/juju/juju/feature"
    23  	"github.com/juju/juju/instance"
    24  	jujutesting "github.com/juju/juju/juju/testing"
    25  	"github.com/juju/juju/network"
    26  	"github.com/juju/juju/provider/dummy"
    27  	"github.com/juju/juju/testing"
    28  )
    29  
    30  func TestPackage(t *stdtesting.T) {
    31  	testing.MgoTestPackage(t)
    32  }
    33  
    34  func init() {
    35  	gc.Suite(&liveSuite{
    36  		LiveTests: jujutest.LiveTests{
    37  			TestConfig:     dummy.SampleConfig(),
    38  			CanOpenState:   true,
    39  			HasProvisioner: false,
    40  		},
    41  	})
    42  	gc.Suite(&suite{
    43  		Tests: jujutest.Tests{
    44  			TestConfig: dummy.SampleConfig(),
    45  		},
    46  	})
    47  }
    48  
    49  type liveSuite struct {
    50  	testing.BaseSuite
    51  	gitjujutesting.MgoSuite
    52  	jujutest.LiveTests
    53  }
    54  
    55  func (s *liveSuite) SetUpSuite(c *gc.C) {
    56  	s.BaseSuite.SetUpSuite(c)
    57  	s.MgoSuite.SetUpSuite(c)
    58  	s.LiveTests.SetUpSuite(c)
    59  }
    60  
    61  func (s *liveSuite) TearDownSuite(c *gc.C) {
    62  	s.LiveTests.TearDownSuite(c)
    63  	s.MgoSuite.TearDownSuite(c)
    64  	s.BaseSuite.TearDownSuite(c)
    65  }
    66  
    67  func (s *liveSuite) SetUpTest(c *gc.C) {
    68  	s.BaseSuite.SetUpTest(c)
    69  	s.SetFeatureFlags(feature.AddressAllocation)
    70  	s.MgoSuite.SetUpTest(c)
    71  	s.LiveTests.SetUpTest(c)
    72  }
    73  
    74  func (s *liveSuite) TearDownTest(c *gc.C) {
    75  	s.Destroy(c)
    76  	s.LiveTests.TearDownTest(c)
    77  	s.MgoSuite.TearDownTest(c)
    78  	s.BaseSuite.TearDownTest(c)
    79  }
    80  
    81  type suite struct {
    82  	testing.BaseSuite
    83  	gitjujutesting.MgoSuite
    84  	jujutest.Tests
    85  }
    86  
    87  func (s *suite) SetUpSuite(c *gc.C) {
    88  	s.BaseSuite.SetUpSuite(c)
    89  	s.MgoSuite.SetUpSuite(c)
    90  }
    91  
    92  func (s *suite) TearDownSuite(c *gc.C) {
    93  	s.MgoSuite.TearDownSuite(c)
    94  	s.BaseSuite.TearDownSuite(c)
    95  }
    96  
    97  func (s *suite) SetUpTest(c *gc.C) {
    98  	s.BaseSuite.SetUpTest(c)
    99  	s.SetFeatureFlags(feature.AddressAllocation)
   100  	s.MgoSuite.SetUpTest(c)
   101  	s.Tests.SetUpTest(c)
   102  }
   103  
   104  func (s *suite) TearDownTest(c *gc.C) {
   105  	s.Tests.TearDownTest(c)
   106  	s.MgoSuite.TearDownTest(c)
   107  	dummy.Reset()
   108  	s.BaseSuite.TearDownTest(c)
   109  }
   110  
   111  func (s *suite) bootstrapTestEnviron(c *gc.C, preferIPv6 bool) environs.NetworkingEnviron {
   112  	s.TestConfig["prefer-ipv6"] = preferIPv6
   113  	cfg, err := config.New(config.NoDefaults, s.TestConfig)
   114  	c.Assert(err, jc.ErrorIsNil)
   115  	env, err := environs.Prepare(cfg, envtesting.BootstrapContext(c), s.ConfigStore)
   116  	c.Assert(err, gc.IsNil, gc.Commentf("preparing environ %#v", s.TestConfig))
   117  	c.Assert(env, gc.NotNil)
   118  	netenv, supported := environs.SupportsNetworking(env)
   119  	c.Assert(supported, jc.IsTrue)
   120  
   121  	err = bootstrap.EnsureNotBootstrapped(netenv)
   122  	c.Assert(err, jc.ErrorIsNil)
   123  	err = bootstrap.Bootstrap(envtesting.BootstrapContext(c), netenv, bootstrap.BootstrapParams{})
   124  	c.Assert(err, jc.ErrorIsNil)
   125  	return netenv
   126  }
   127  
   128  func (s *suite) TestAvailabilityZone(c *gc.C) {
   129  	e := s.bootstrapTestEnviron(c, true)
   130  	defer func() {
   131  		err := e.Destroy()
   132  		c.Assert(err, jc.ErrorIsNil)
   133  	}()
   134  
   135  	inst, hwc := jujutesting.AssertStartInstance(c, e, "0")
   136  	c.Assert(inst, gc.NotNil)
   137  	c.Check(hwc.AvailabilityZone, gc.IsNil)
   138  }
   139  
   140  func (s *suite) TestSupportsAddressAllocation(c *gc.C) {
   141  	e := s.bootstrapTestEnviron(c, false)
   142  	defer func() {
   143  		err := e.Destroy()
   144  		c.Assert(err, jc.ErrorIsNil)
   145  	}()
   146  
   147  	// By default, it's supported.
   148  	supported, err := e.SupportsAddressAllocation("any-id")
   149  	c.Assert(err, jc.ErrorIsNil)
   150  	c.Assert(supported, jc.IsTrue)
   151  
   152  	// Any subnet id with prefix "noalloc-" simulates address
   153  	// allocation is not supported.
   154  	supported, err = e.SupportsAddressAllocation("noalloc-foo")
   155  	c.Assert(err, jc.ErrorIsNil)
   156  	c.Assert(supported, jc.IsFalse)
   157  
   158  	// Test we can induce an error for SupportsAddressAllocation.
   159  	s.breakMethods(c, e, "SupportsAddressAllocation")
   160  	supported, err = e.SupportsAddressAllocation("any-id")
   161  	c.Assert(err, gc.ErrorMatches, `dummy\.SupportsAddressAllocation is broken`)
   162  	c.Assert(supported, jc.IsFalse)
   163  
   164  	// Finally, test the method respects the feature flag when
   165  	// disabled.
   166  	s.SetFeatureFlags() // clear the flags.
   167  	supported, err = e.SupportsAddressAllocation("any-id")
   168  	c.Assert(err, gc.ErrorMatches, "address allocation not supported")
   169  	c.Assert(err, jc.Satisfies, errors.IsNotSupported)
   170  	c.Assert(supported, jc.IsFalse)
   171  }
   172  
   173  func (s *suite) breakMethods(c *gc.C, e environs.NetworkingEnviron, names ...string) {
   174  	cfg := e.Config()
   175  	brokenCfg, err := cfg.Apply(map[string]interface{}{
   176  		"broken": strings.Join(names, " "),
   177  	})
   178  	c.Assert(err, jc.ErrorIsNil)
   179  	err = e.SetConfig(brokenCfg)
   180  	c.Assert(err, jc.ErrorIsNil)
   181  }
   182  
   183  func (s *suite) TestAllocateAddress(c *gc.C) {
   184  	e := s.bootstrapTestEnviron(c, false)
   185  	defer func() {
   186  		err := e.Destroy()
   187  		c.Assert(err, jc.ErrorIsNil)
   188  	}()
   189  
   190  	inst, _ := jujutesting.AssertStartInstance(c, e, "0")
   191  	c.Assert(inst, gc.NotNil)
   192  	subnetId := network.Id("net1")
   193  
   194  	opc := make(chan dummy.Operation, 200)
   195  	dummy.Listen(opc)
   196  
   197  	// Test allocating a couple of addresses.
   198  	newAddress := network.NewScopedAddress("0.1.2.1", network.ScopeCloudLocal)
   199  	err := e.AllocateAddress(inst.Id(), subnetId, newAddress)
   200  	c.Assert(err, jc.ErrorIsNil)
   201  	assertAllocateAddress(c, e, opc, inst.Id(), subnetId, newAddress)
   202  
   203  	newAddress = network.NewScopedAddress("0.1.2.2", network.ScopeCloudLocal)
   204  	err = e.AllocateAddress(inst.Id(), subnetId, newAddress)
   205  	c.Assert(err, jc.ErrorIsNil)
   206  	assertAllocateAddress(c, e, opc, inst.Id(), subnetId, newAddress)
   207  
   208  	// Test we can induce errors.
   209  	s.breakMethods(c, e, "AllocateAddress")
   210  	newAddress = network.NewScopedAddress("0.1.2.3", network.ScopeCloudLocal)
   211  	err = e.AllocateAddress(inst.Id(), subnetId, newAddress)
   212  	c.Assert(err, gc.ErrorMatches, `dummy\.AllocateAddress is broken`)
   213  
   214  	// Finally, test the method respects the feature flag when
   215  	// disabled.
   216  	s.SetFeatureFlags() // clear the flags.
   217  	err = e.AllocateAddress(inst.Id(), subnetId, newAddress)
   218  	c.Assert(err, gc.ErrorMatches, "address allocation not supported")
   219  	c.Assert(err, jc.Satisfies, errors.IsNotSupported)
   220  }
   221  
   222  func (s *suite) TestReleaseAddress(c *gc.C) {
   223  	e := s.bootstrapTestEnviron(c, false)
   224  	defer func() {
   225  		err := e.Destroy()
   226  		c.Assert(err, jc.ErrorIsNil)
   227  	}()
   228  
   229  	inst, _ := jujutesting.AssertStartInstance(c, e, "0")
   230  	c.Assert(inst, gc.NotNil)
   231  	subnetId := network.Id("net1")
   232  
   233  	opc := make(chan dummy.Operation, 200)
   234  	dummy.Listen(opc)
   235  
   236  	// Release a couple of addresses.
   237  	address := network.NewScopedAddress("0.1.2.1", network.ScopeCloudLocal)
   238  	err := e.ReleaseAddress(inst.Id(), subnetId, address)
   239  	c.Assert(err, jc.ErrorIsNil)
   240  	assertReleaseAddress(c, e, opc, inst.Id(), subnetId, address)
   241  
   242  	address = network.NewScopedAddress("0.1.2.2", network.ScopeCloudLocal)
   243  	err = e.ReleaseAddress(inst.Id(), subnetId, address)
   244  	c.Assert(err, jc.ErrorIsNil)
   245  	assertReleaseAddress(c, e, opc, inst.Id(), subnetId, address)
   246  
   247  	// Test we can induce errors.
   248  	s.breakMethods(c, e, "ReleaseAddress")
   249  	address = network.NewScopedAddress("0.1.2.3", network.ScopeCloudLocal)
   250  	err = e.ReleaseAddress(inst.Id(), subnetId, address)
   251  	c.Assert(err, gc.ErrorMatches, `dummy\.ReleaseAddress is broken`)
   252  
   253  	// Finally, test the method respects the feature flag when
   254  	// disabled.
   255  	s.SetFeatureFlags() // clear the flags.
   256  	err = e.ReleaseAddress(inst.Id(), subnetId, address)
   257  	c.Assert(err, gc.ErrorMatches, "address allocation not supported")
   258  	c.Assert(err, jc.Satisfies, errors.IsNotSupported)
   259  }
   260  
   261  func (s *suite) TestNetworkInterfaces(c *gc.C) {
   262  	e := s.bootstrapTestEnviron(c, false)
   263  	defer func() {
   264  		err := e.Destroy()
   265  		c.Assert(err, jc.ErrorIsNil)
   266  	}()
   267  
   268  	opc := make(chan dummy.Operation, 200)
   269  	dummy.Listen(opc)
   270  
   271  	expectInfo := []network.InterfaceInfo{{
   272  		ProviderId:       "dummy-eth0",
   273  		ProviderSubnetId: "dummy-private",
   274  		NetworkName:      "juju-private",
   275  		CIDR:             "0.10.0.0/24",
   276  		DeviceIndex:      0,
   277  		InterfaceName:    "eth0",
   278  		VLANTag:          0,
   279  		MACAddress:       "aa:bb:cc:dd:ee:f0",
   280  		Disabled:         false,
   281  		NoAutoStart:      false,
   282  		ConfigType:       network.ConfigDHCP,
   283  		Address:          network.NewAddress("0.10.0.2"),
   284  		DNSServers:       network.NewAddresses("ns1.dummy", "ns2.dummy"),
   285  		GatewayAddress:   network.NewAddress("0.10.0.1"),
   286  		ExtraConfig:      nil,
   287  	}, {
   288  		ProviderId:       "dummy-eth1",
   289  		ProviderSubnetId: "dummy-public",
   290  		NetworkName:      "juju-public",
   291  		CIDR:             "0.20.0.0/24",
   292  		DeviceIndex:      1,
   293  		InterfaceName:    "eth1",
   294  		VLANTag:          1,
   295  		MACAddress:       "aa:bb:cc:dd:ee:f1",
   296  		Disabled:         false,
   297  		NoAutoStart:      true,
   298  		ConfigType:       network.ConfigDHCP,
   299  		Address:          network.NewAddress("0.20.0.2"),
   300  		DNSServers:       network.NewAddresses("ns1.dummy", "ns2.dummy"),
   301  		GatewayAddress:   network.NewAddress("0.20.0.1"),
   302  		ExtraConfig:      nil,
   303  	}, {
   304  		ProviderId:       "dummy-eth2",
   305  		ProviderSubnetId: "dummy-disabled",
   306  		NetworkName:      "juju-disabled",
   307  		CIDR:             "0.30.0.0/24",
   308  		DeviceIndex:      2,
   309  		InterfaceName:    "eth2",
   310  		VLANTag:          2,
   311  		MACAddress:       "aa:bb:cc:dd:ee:f2",
   312  		Disabled:         true,
   313  		NoAutoStart:      false,
   314  		ConfigType:       network.ConfigDHCP,
   315  		Address:          network.NewAddress("0.30.0.2"),
   316  		DNSServers:       network.NewAddresses("ns1.dummy", "ns2.dummy"),
   317  		GatewayAddress:   network.NewAddress("0.30.0.1"),
   318  		ExtraConfig:      nil,
   319  	}}
   320  	info, err := e.NetworkInterfaces("i-42")
   321  	c.Assert(err, jc.ErrorIsNil)
   322  	c.Assert(info, jc.DeepEquals, expectInfo)
   323  	assertInterfaces(c, e, opc, "i-42", expectInfo)
   324  
   325  	// Test that with instance id prefix "i-no-nics-" no results are
   326  	// returned.
   327  	info, err = e.NetworkInterfaces("i-no-nics-here")
   328  	c.Assert(err, jc.ErrorIsNil)
   329  	c.Assert(info, gc.HasLen, 0)
   330  	assertInterfaces(c, e, opc, "i-no-nics-here", expectInfo[:0])
   331  
   332  	// Test that with instance id prefix "i-nic-no-subnet-" we get a result
   333  	// with no associated subnet.
   334  	expectInfo = []network.InterfaceInfo{{
   335  		DeviceIndex:   0,
   336  		ProviderId:    network.Id("dummy-eth0"),
   337  		NetworkName:   "juju-public",
   338  		InterfaceName: "eth0",
   339  		MACAddress:    "aa:bb:cc:dd:ee:f0",
   340  		Disabled:      false,
   341  		NoAutoStart:   false,
   342  		ConfigType:    network.ConfigDHCP,
   343  	}}
   344  	info, err = e.NetworkInterfaces("i-nic-no-subnet-here")
   345  	c.Assert(err, jc.ErrorIsNil)
   346  	c.Assert(info, gc.HasLen, 1)
   347  	assertInterfaces(c, e, opc, "i-nic-no-subnet-here", expectInfo)
   348  
   349  	// Test that with instance id prefix "i-disabled-nic-" we get a result
   350  	// with only a disabled subnet.
   351  	expectInfo = []network.InterfaceInfo{{
   352  		ProviderId:       "dummy-eth2",
   353  		ProviderSubnetId: "dummy-disabled",
   354  		NetworkName:      "juju-disabled",
   355  		CIDR:             "0.30.0.0/24",
   356  		DeviceIndex:      2,
   357  		InterfaceName:    "eth2",
   358  		VLANTag:          2,
   359  		MACAddress:       "aa:bb:cc:dd:ee:f2",
   360  		Disabled:         true,
   361  		NoAutoStart:      false,
   362  		ConfigType:       network.ConfigDHCP,
   363  		Address:          network.NewAddress("0.30.0.2"),
   364  		DNSServers:       network.NewAddresses("ns1.dummy", "ns2.dummy"),
   365  		GatewayAddress:   network.NewAddress("0.30.0.1"),
   366  		ExtraConfig:      nil,
   367  	}}
   368  	info, err = e.NetworkInterfaces("i-disabled-nic-here")
   369  	c.Assert(err, jc.ErrorIsNil)
   370  	c.Assert(info, gc.HasLen, 1)
   371  	assertInterfaces(c, e, opc, "i-disabled-nic-here", expectInfo)
   372  
   373  	// Test we can induce errors.
   374  	s.breakMethods(c, e, "NetworkInterfaces")
   375  	info, err = e.NetworkInterfaces("i-any")
   376  	c.Assert(err, gc.ErrorMatches, `dummy\.NetworkInterfaces is broken`)
   377  	c.Assert(info, gc.HasLen, 0)
   378  }
   379  
   380  func (s *suite) TestSubnets(c *gc.C) {
   381  	e := s.bootstrapTestEnviron(c, false)
   382  	defer func() {
   383  		err := e.Destroy()
   384  		c.Assert(err, jc.ErrorIsNil)
   385  	}()
   386  
   387  	opc := make(chan dummy.Operation, 200)
   388  	dummy.Listen(opc)
   389  
   390  	expectInfo := []network.SubnetInfo{{
   391  		CIDR:              "0.10.0.0/24",
   392  		ProviderId:        "dummy-private",
   393  		AllocatableIPLow:  net.ParseIP("0.10.0.0"),
   394  		AllocatableIPHigh: net.ParseIP("0.10.0.255"),
   395  	}, {
   396  		CIDR:              "0.20.0.0/24",
   397  		ProviderId:        "dummy-public",
   398  		AllocatableIPLow:  net.ParseIP("0.20.0.0"),
   399  		AllocatableIPHigh: net.ParseIP("0.20.0.255"),
   400  	}}
   401  	// Prepare a version of the above with no allocatable range to
   402  	// test the magic "i-no-alloc-" prefix below.
   403  	noallocInfo := make([]network.SubnetInfo, len(expectInfo))
   404  	for i, exp := range expectInfo {
   405  		pid := string(exp.ProviderId)
   406  		pid = strings.TrimPrefix(pid, "dummy-")
   407  		noallocInfo[i].ProviderId = network.Id("noalloc-" + pid)
   408  		noallocInfo[i].AllocatableIPLow = nil
   409  		noallocInfo[i].AllocatableIPHigh = nil
   410  		noallocInfo[i].CIDR = exp.CIDR
   411  	}
   412  
   413  	ids := []network.Id{"dummy-private", "dummy-public", "foo-bar"}
   414  	netInfo, err := e.Subnets("i-foo", ids)
   415  	c.Assert(err, jc.ErrorIsNil)
   416  	c.Assert(netInfo, jc.DeepEquals, expectInfo)
   417  	assertSubnets(c, e, opc, "i-foo", ids, expectInfo)
   418  
   419  	// Test filtering by id(s).
   420  	netInfo, err = e.Subnets("i-foo", nil)
   421  	c.Assert(err, jc.ErrorIsNil)
   422  	c.Assert(netInfo, jc.DeepEquals, expectInfo)
   423  	assertSubnets(c, e, opc, "i-foo", nil, expectInfo)
   424  	netInfo, err = e.Subnets("i-foo", ids[0:1])
   425  	c.Assert(err, jc.ErrorIsNil)
   426  	c.Assert(netInfo, jc.DeepEquals, expectInfo[0:1])
   427  	assertSubnets(c, e, opc, "i-foo", ids[0:1], expectInfo[0:1])
   428  	netInfo, err = e.Subnets("i-foo", ids[1:])
   429  	c.Assert(err, jc.ErrorIsNil)
   430  	c.Assert(netInfo, jc.DeepEquals, expectInfo[1:])
   431  	assertSubnets(c, e, opc, "i-foo", ids[1:], expectInfo[1:])
   432  
   433  	// Test that using an instance id with prefix of either
   434  	// "i-no-subnets-" or "i-nic-no-subnet-"
   435  	// returns no results, regardless whether ids are given or not.
   436  	for _, instId := range []instance.Id{"i-no-subnets-foo", "i-nic-no-subnet-foo"} {
   437  		netInfo, err = e.Subnets(instId, nil)
   438  		c.Assert(err, jc.ErrorIsNil)
   439  		c.Assert(netInfo, gc.HasLen, 0)
   440  		assertSubnets(c, e, opc, instId, nil, expectInfo[:0])
   441  	}
   442  
   443  	netInfo, err = e.Subnets("i-no-subnets-foo", ids)
   444  	c.Assert(err, jc.ErrorIsNil)
   445  	c.Assert(netInfo, gc.HasLen, 0)
   446  	assertSubnets(c, e, opc, "i-no-subnets-foo", ids, expectInfo[:0])
   447  
   448  	// Test the behavior with "i-no-alloc-" instance id prefix.
   449  	// When # is "all", all returned subnets have no allocatable range
   450  	// set and have provider ids with "noalloc-" prefix.
   451  	netInfo, err = e.Subnets("i-no-alloc-all", nil)
   452  	c.Assert(err, jc.ErrorIsNil)
   453  	c.Assert(netInfo, jc.DeepEquals, noallocInfo)
   454  	assertSubnets(c, e, opc, "i-no-alloc-all", nil, noallocInfo)
   455  
   456  	// When # is an integer, the #-th subnet in result has no
   457  	// allocatable range set and a provider id prefix "noalloc-".
   458  	netInfo, err = e.Subnets("i-no-alloc-0", nil)
   459  	c.Assert(err, jc.ErrorIsNil)
   460  	expectResult := []network.SubnetInfo{noallocInfo[0], expectInfo[1]}
   461  	c.Assert(netInfo, jc.DeepEquals, expectResult)
   462  	assertSubnets(c, e, opc, "i-no-alloc-0", nil, expectResult)
   463  
   464  	netInfo, err = e.Subnets("i-no-alloc-1", nil)
   465  	c.Assert(err, jc.ErrorIsNil)
   466  	expectResult = []network.SubnetInfo{expectInfo[0], noallocInfo[1]}
   467  	c.Assert(netInfo, jc.DeepEquals, expectResult)
   468  	assertSubnets(c, e, opc, "i-no-alloc-1", nil, expectResult)
   469  
   470  	// For the last case above, also test the error returned when # is
   471  	// not integer or it's out of range of the results (including when
   472  	// filtering by ids is applied).
   473  	netInfo, err = e.Subnets("i-no-alloc-foo", nil)
   474  	c.Assert(err, gc.ErrorMatches, `invalid index "foo"; expected int`)
   475  	c.Assert(netInfo, gc.HasLen, 0)
   476  
   477  	netInfo, err = e.Subnets("i-no-alloc-1", ids[:1])
   478  	c.Assert(err, gc.ErrorMatches, `index 1 out of range; expected 0..0`)
   479  	c.Assert(netInfo, gc.HasLen, 0)
   480  
   481  	netInfo, err = e.Subnets("i-no-alloc-2", ids)
   482  	c.Assert(err, gc.ErrorMatches, `index 2 out of range; expected 0..1`)
   483  	c.Assert(netInfo, gc.HasLen, 0)
   484  
   485  	// Test we can induce errors.
   486  	s.breakMethods(c, e, "Subnets")
   487  	netInfo, err = e.Subnets("i-any", nil)
   488  	c.Assert(err, gc.ErrorMatches, `dummy\.Subnets is broken`)
   489  	c.Assert(netInfo, gc.HasLen, 0)
   490  }
   491  
   492  func (s *suite) TestPreferIPv6On(c *gc.C) {
   493  	e := s.bootstrapTestEnviron(c, true)
   494  	defer func() {
   495  		err := e.Destroy()
   496  		c.Assert(err, jc.ErrorIsNil)
   497  	}()
   498  
   499  	inst, _ := jujutesting.AssertStartInstance(c, e, "0")
   500  	c.Assert(inst, gc.NotNil)
   501  	addrs, err := inst.Addresses()
   502  	c.Assert(err, jc.ErrorIsNil)
   503  	c.Assert(addrs, jc.DeepEquals, network.NewAddresses("only-0.dns", "127.0.0.1", "fc00::1"))
   504  }
   505  
   506  func (s *suite) TestPreferIPv6Off(c *gc.C) {
   507  	e := s.bootstrapTestEnviron(c, false)
   508  	defer func() {
   509  		err := e.Destroy()
   510  		c.Assert(err, jc.ErrorIsNil)
   511  	}()
   512  
   513  	inst, _ := jujutesting.AssertStartInstance(c, e, "0")
   514  	c.Assert(inst, gc.NotNil)
   515  	addrs, err := inst.Addresses()
   516  	c.Assert(err, jc.ErrorIsNil)
   517  	c.Assert(addrs, jc.DeepEquals, network.NewAddresses("only-0.dns", "127.0.0.1"))
   518  }
   519  
   520  func assertAllocateAddress(c *gc.C, e environs.Environ, opc chan dummy.Operation, expectInstId instance.Id, expectSubnetId network.Id, expectAddress network.Address) {
   521  	select {
   522  	case op := <-opc:
   523  		addrOp, ok := op.(dummy.OpAllocateAddress)
   524  		if !ok {
   525  			c.Fatalf("unexpected op: %#v", op)
   526  		}
   527  		c.Check(addrOp.SubnetId, gc.Equals, expectSubnetId)
   528  		c.Check(addrOp.InstanceId, gc.Equals, expectInstId)
   529  		c.Check(addrOp.Address, gc.Equals, expectAddress)
   530  		return
   531  	case <-time.After(testing.ShortWait):
   532  		c.Fatalf("time out wating for operation")
   533  	}
   534  }
   535  
   536  func assertReleaseAddress(c *gc.C, e environs.Environ, opc chan dummy.Operation, expectInstId instance.Id, expectSubnetId network.Id, expectAddress network.Address) {
   537  	select {
   538  	case op := <-opc:
   539  		addrOp, ok := op.(dummy.OpReleaseAddress)
   540  		if !ok {
   541  			c.Fatalf("unexpected op: %#v", op)
   542  		}
   543  		c.Check(addrOp.SubnetId, gc.Equals, expectSubnetId)
   544  		c.Check(addrOp.InstanceId, gc.Equals, expectInstId)
   545  		c.Check(addrOp.Address, gc.Equals, expectAddress)
   546  		return
   547  	case <-time.After(testing.ShortWait):
   548  		c.Fatalf("time out wating for operation")
   549  	}
   550  }
   551  
   552  func assertInterfaces(c *gc.C, e environs.Environ, opc chan dummy.Operation, expectInstId instance.Id, expectInfo []network.InterfaceInfo) {
   553  	select {
   554  	case op := <-opc:
   555  		netOp, ok := op.(dummy.OpNetworkInterfaces)
   556  		if !ok {
   557  			c.Fatalf("unexpected op: %#v", op)
   558  		}
   559  		c.Check(netOp.Env, gc.Equals, e.Config().Name())
   560  		c.Check(netOp.InstanceId, gc.Equals, expectInstId)
   561  		c.Check(netOp.Info, jc.DeepEquals, expectInfo)
   562  		return
   563  	case <-time.After(testing.ShortWait):
   564  		c.Fatalf("time out wating for operation")
   565  	}
   566  }
   567  
   568  func assertSubnets(
   569  	c *gc.C,
   570  	e environs.Environ,
   571  	opc chan dummy.Operation,
   572  	instId instance.Id,
   573  	subnetIds []network.Id,
   574  	expectInfo []network.SubnetInfo,
   575  ) {
   576  	select {
   577  	case op := <-opc:
   578  		netOp, ok := op.(dummy.OpSubnets)
   579  		if !ok {
   580  			c.Fatalf("unexpected op: %#v", op)
   581  		}
   582  		c.Check(netOp.InstanceId, gc.Equals, instId)
   583  		c.Check(netOp.SubnetIds, jc.DeepEquals, subnetIds)
   584  		c.Check(netOp.Info, jc.DeepEquals, expectInfo)
   585  		return
   586  	case <-time.After(testing.ShortWait):
   587  		c.Fatalf("time out wating for operation")
   588  	}
   589  }