github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/provider/openstack/provider_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package openstack_test
     5  
     6  import (
     7  	"flag"
     8  	"testing"
     9  
    10  	gc "launchpad.net/gocheck"
    11  	"launchpad.net/goose/identity"
    12  	"launchpad.net/goose/nova"
    13  
    14  	"github.com/juju/juju/provider/openstack"
    15  )
    16  
    17  var live = flag.Bool("live", false, "Include live OpenStack tests")
    18  
    19  func Test(t *testing.T) {
    20  	if *live {
    21  		cred, err := identity.CompleteCredentialsFromEnv()
    22  		if err != nil {
    23  			t.Fatalf("Error setting up test suite: %s", err.Error())
    24  		}
    25  		registerLiveTests(cred)
    26  	}
    27  	registerLocalTests()
    28  	gc.TestingT(t)
    29  }
    30  
    31  // localTests contains tests which do not require a live service or test double to run.
    32  type localTests struct{}
    33  
    34  var _ = gc.Suite(&localTests{})
    35  
    36  // ported from lp:juju/juju/providers/openstack/tests/test_machine.py
    37  var addressTests = []struct {
    38  	summary  string
    39  	private  []nova.IPAddress
    40  	public   []nova.IPAddress
    41  	networks []string
    42  	expected string
    43  	failure  error
    44  }{
    45  	{
    46  		summary:  "missing",
    47  		expected: "",
    48  	},
    49  	{
    50  		summary:  "empty",
    51  		private:  []nova.IPAddress{},
    52  		networks: []string{"private"},
    53  		expected: "",
    54  	},
    55  	{
    56  		summary:  "private only",
    57  		private:  []nova.IPAddress{{4, "192.168.0.1"}},
    58  		networks: []string{"private"},
    59  		expected: "192.168.0.1",
    60  	},
    61  	{
    62  		summary:  "private plus (HP cloud)",
    63  		private:  []nova.IPAddress{{4, "10.0.0.1"}, {4, "8.8.4.4"}},
    64  		networks: []string{"private"},
    65  		expected: "8.8.4.4",
    66  	},
    67  	{
    68  		summary:  "public only",
    69  		public:   []nova.IPAddress{{4, "8.8.8.8"}},
    70  		networks: []string{"", "public"},
    71  		expected: "8.8.8.8",
    72  	},
    73  	{
    74  		summary:  "public and private",
    75  		private:  []nova.IPAddress{{4, "10.0.0.4"}},
    76  		public:   []nova.IPAddress{{4, "8.8.4.4"}},
    77  		networks: []string{"private", "public"},
    78  		expected: "8.8.4.4",
    79  	},
    80  	{
    81  		summary:  "public private plus",
    82  		private:  []nova.IPAddress{{4, "127.0.0.4"}, {4, "192.168.0.1"}},
    83  		public:   []nova.IPAddress{{4, "8.8.8.8"}},
    84  		networks: []string{"private", "public"},
    85  		expected: "8.8.8.8",
    86  	},
    87  	{
    88  		summary:  "custom only",
    89  		private:  []nova.IPAddress{{4, "192.168.0.1"}},
    90  		networks: []string{"special"},
    91  		expected: "192.168.0.1",
    92  	},
    93  	{
    94  		summary:  "custom and public",
    95  		private:  []nova.IPAddress{{4, "172.16.0.1"}},
    96  		public:   []nova.IPAddress{{4, "8.8.8.8"}},
    97  		networks: []string{"special", "public"},
    98  		expected: "8.8.8.8",
    99  	},
   100  	{
   101  		summary:  "non-IPv4",
   102  		private:  []nova.IPAddress{{6, "::dead:beef:f00d"}},
   103  		networks: []string{"private"},
   104  		expected: "",
   105  	},
   106  }
   107  
   108  func (t *localTests) TestGetServerAddresses(c *gc.C) {
   109  	for i, t := range addressTests {
   110  		c.Logf("#%d. %s -> %s (%v)", i, t.summary, t.expected, t.failure)
   111  		addresses := make(map[string][]nova.IPAddress)
   112  		if t.private != nil {
   113  			if len(t.networks) < 1 {
   114  				addresses["private"] = t.private
   115  			} else {
   116  				addresses[t.networks[0]] = t.private
   117  			}
   118  		}
   119  		if t.public != nil {
   120  			if len(t.networks) < 2 {
   121  				addresses["public"] = t.public
   122  			} else {
   123  				addresses[t.networks[1]] = t.public
   124  			}
   125  		}
   126  		addr := openstack.InstanceAddress(addresses)
   127  		c.Assert(addr, gc.Equals, t.expected)
   128  	}
   129  }