github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/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  	stdtesting "testing"
     8  	"time"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "launchpad.net/gocheck"
    12  
    13  	"github.com/juju/juju/environs"
    14  	"github.com/juju/juju/environs/bootstrap"
    15  	"github.com/juju/juju/environs/config"
    16  	"github.com/juju/juju/environs/jujutest"
    17  	envtesting "github.com/juju/juju/environs/testing"
    18  	"github.com/juju/juju/instance"
    19  	jujutesting "github.com/juju/juju/juju/testing"
    20  	"github.com/juju/juju/network"
    21  	"github.com/juju/juju/provider/dummy"
    22  	"github.com/juju/juju/testing"
    23  )
    24  
    25  func TestPackage(t *stdtesting.T) {
    26  	testing.MgoTestPackage(t)
    27  }
    28  
    29  func init() {
    30  	gc.Suite(&liveSuite{
    31  		LiveTests: jujutest.LiveTests{
    32  			TestConfig:     dummy.SampleConfig(),
    33  			CanOpenState:   true,
    34  			HasProvisioner: false,
    35  		},
    36  	})
    37  	gc.Suite(&suite{
    38  		Tests: jujutest.Tests{
    39  			TestConfig: dummy.SampleConfig(),
    40  		},
    41  	})
    42  }
    43  
    44  type liveSuite struct {
    45  	testing.BaseSuite
    46  	jujutest.LiveTests
    47  }
    48  
    49  func (s *liveSuite) SetUpSuite(c *gc.C) {
    50  	s.BaseSuite.SetUpSuite(c)
    51  	s.LiveTests.SetUpSuite(c)
    52  }
    53  
    54  func (s *liveSuite) TearDownSuite(c *gc.C) {
    55  	s.LiveTests.TearDownSuite(c)
    56  	s.BaseSuite.TearDownSuite(c)
    57  }
    58  
    59  func (s *liveSuite) SetUpTest(c *gc.C) {
    60  	s.BaseSuite.SetUpTest(c)
    61  	s.LiveTests.SetUpTest(c)
    62  }
    63  
    64  func (s *liveSuite) TearDownTest(c *gc.C) {
    65  	s.LiveTests.TearDownTest(c)
    66  	s.BaseSuite.TearDownTest(c)
    67  }
    68  
    69  type suite struct {
    70  	testing.BaseSuite
    71  	jujutest.Tests
    72  }
    73  
    74  func (s *suite) SetUpTest(c *gc.C) {
    75  	s.BaseSuite.SetUpTest(c)
    76  	s.Tests.SetUpTest(c)
    77  }
    78  
    79  func (s *suite) TearDownTest(c *gc.C) {
    80  	s.Tests.TearDownTest(c)
    81  	dummy.Reset()
    82  	s.BaseSuite.TearDownTest(c)
    83  }
    84  
    85  func (s *suite) bootstrapTestEnviron(c *gc.C) environs.Environ {
    86  	cfg, err := config.New(config.NoDefaults, s.TestConfig)
    87  	c.Assert(err, gc.IsNil)
    88  	e, err := environs.Prepare(cfg, testing.Context(c), s.ConfigStore)
    89  	c.Assert(err, gc.IsNil, gc.Commentf("preparing environ %#v", s.TestConfig))
    90  	c.Assert(e, gc.NotNil)
    91  
    92  	envtesting.UploadFakeTools(c, e.Storage())
    93  	err = bootstrap.EnsureNotBootstrapped(e)
    94  	c.Assert(err, gc.IsNil)
    95  	err = bootstrap.Bootstrap(testing.Context(c), e, environs.BootstrapParams{})
    96  	c.Assert(err, gc.IsNil)
    97  	return e
    98  }
    99  
   100  func (s *suite) TestAllocateAddress(c *gc.C) {
   101  	e := s.bootstrapTestEnviron(c)
   102  
   103  	inst, _ := jujutesting.AssertStartInstance(c, e, "0")
   104  	c.Assert(inst, gc.NotNil)
   105  	netId := network.Id("net1")
   106  
   107  	opc := make(chan dummy.Operation, 200)
   108  	dummy.Listen(opc)
   109  
   110  	expectAddress := network.NewAddress("0.1.2.1", network.ScopeCloudLocal)
   111  	address, err := e.AllocateAddress(inst.Id(), netId)
   112  	c.Assert(err, gc.IsNil)
   113  	c.Assert(address, gc.DeepEquals, expectAddress)
   114  
   115  	assertAllocateAddress(c, e, opc, inst.Id(), netId, expectAddress)
   116  
   117  	expectAddress = network.NewAddress("0.1.2.2", network.ScopeCloudLocal)
   118  	address, err = e.AllocateAddress(inst.Id(), netId)
   119  	c.Assert(err, gc.IsNil)
   120  	c.Assert(address, gc.DeepEquals, expectAddress)
   121  	assertAllocateAddress(c, e, opc, inst.Id(), netId, expectAddress)
   122  }
   123  
   124  func (s *suite) TestListNetworks(c *gc.C) {
   125  	e := s.bootstrapTestEnviron(c)
   126  
   127  	opc := make(chan dummy.Operation, 200)
   128  	dummy.Listen(opc)
   129  
   130  	expectInfo := []network.BasicInfo{
   131  		{CIDR: "0.10.0.0/8", ProviderId: "dummy-private"},
   132  		{CIDR: "0.20.0.0/24", ProviderId: "dummy-public"},
   133  	}
   134  	netInfo, err := e.ListNetworks()
   135  	c.Assert(err, gc.IsNil)
   136  	c.Assert(netInfo, jc.DeepEquals, expectInfo)
   137  	assertListNetworks(c, e, opc, expectInfo)
   138  }
   139  
   140  func assertAllocateAddress(c *gc.C, e environs.Environ, opc chan dummy.Operation, expectInstId instance.Id, expectNetId network.Id, expectAddress network.Address) {
   141  	select {
   142  	case op := <-opc:
   143  		addrOp, ok := op.(dummy.OpAllocateAddress)
   144  		if !ok {
   145  			c.Fatalf("unexpected op: %#v", op)
   146  		}
   147  		c.Check(addrOp.NetworkId, gc.Equals, expectNetId)
   148  		c.Check(addrOp.InstanceId, gc.Equals, expectInstId)
   149  		c.Check(addrOp.Address, gc.Equals, expectAddress)
   150  		return
   151  	case <-time.After(testing.ShortWait):
   152  		c.Fatalf("time out wating for operation")
   153  	}
   154  }
   155  
   156  func assertListNetworks(c *gc.C, e environs.Environ, opc chan dummy.Operation, expectInfo []network.BasicInfo) {
   157  	select {
   158  	case op := <-opc:
   159  		netOp, ok := op.(dummy.OpListNetworks)
   160  		if !ok {
   161  			c.Fatalf("unexpected op: %#v", op)
   162  		}
   163  		c.Check(netOp.Info, jc.DeepEquals, expectInfo)
   164  		return
   165  	case <-time.After(testing.ShortWait):
   166  		c.Fatalf("time out wating for operation")
   167  	}
   168  }