github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/tools/lxdclient/addserver_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build go1.3
     5  
     6  package lxdclient
     7  
     8  import (
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  )
    13  
    14  var _ = gc.Suite(&fixAddrSuite{})
    15  
    16  type fixAddrSuite struct {
    17  	testing.IsolationSuite
    18  }
    19  
    20  type addrTest struct {
    21  	addr     string
    22  	expected string
    23  	err      string
    24  }
    25  
    26  var typicalAddrTests = []addrTest{{
    27  	addr:     "",
    28  	expected: "",
    29  }, {
    30  	addr:     "a.b.c",
    31  	expected: "https://a.b.c:8443",
    32  }, {
    33  	addr:     "https://a.b.c",
    34  	expected: "https://a.b.c:8443",
    35  }, {
    36  	addr:     "https://a.b.c/",
    37  	expected: "https://a.b.c:8443",
    38  }, {
    39  	addr:     "a.b.c:1234",
    40  	expected: "https://a.b.c:1234",
    41  }, {
    42  	addr:     "https://a.b.c:1234",
    43  	expected: "https://a.b.c:1234",
    44  }, {
    45  	addr:     "https://a.b.c:1234/",
    46  	expected: "https://a.b.c:1234",
    47  }, {
    48  	addr:     "a.b.c/x/y/z",
    49  	expected: "https://a.b.c:8443/x/y/z",
    50  }, {
    51  	addr:     "https://a.b.c/x/y/z",
    52  	expected: "https://a.b.c:8443/x/y/z",
    53  }, {
    54  	addr:     "https://a.b.c:1234/x/y/z",
    55  	expected: "https://a.b.c:1234/x/y/z",
    56  }, {
    57  	addr:     "http://a.b.c",
    58  	expected: "https://a.b.c:8443",
    59  }, {
    60  	addr:     "1.2.3.4",
    61  	expected: "https://1.2.3.4:8443",
    62  }, {
    63  	addr:     "1.2.3.4:1234",
    64  	expected: "https://1.2.3.4:1234",
    65  }, {
    66  	addr:     "https://1.2.3.4",
    67  	expected: "https://1.2.3.4:8443",
    68  }, {
    69  	addr:     "127.0.0.1",
    70  	expected: "https://127.0.0.1:8443",
    71  }, {
    72  	addr:     "2001:db8::ff00:42:8329",
    73  	expected: "https://[2001:db8::ff00:42:8329]:8443",
    74  }, {
    75  	addr:     "[2001:db8::ff00:42:8329]:1234",
    76  	expected: "https://[2001:db8::ff00:42:8329]:1234",
    77  }, {
    78  	addr:     "https://2001:db8::ff00:42:8329",
    79  	expected: "https://[2001:db8::ff00:42:8329]:8443",
    80  }, {
    81  	addr:     "https://[2001:db8::ff00:42:8329]:1234",
    82  	expected: "https://[2001:db8::ff00:42:8329]:1234",
    83  }, {
    84  	addr:     "::1",
    85  	expected: "https://[::1]:8443",
    86  }, {
    87  	addr:     "a.b.c",
    88  	expected: "https://a.b.c:8443",
    89  }}
    90  
    91  func (s *fixAddrSuite) TestFixAddrTypical(c *gc.C) {
    92  	for i, test := range typicalAddrTests {
    93  		c.Logf("test %d: checking %q", i, test.addr)
    94  		c.Assert(test.err, gc.Equals, "")
    95  
    96  		fixed, err := fixAddr(test.addr)
    97  
    98  		if !c.Check(err, jc.ErrorIsNil) {
    99  			continue
   100  		}
   101  		c.Check(fixed, gc.Equals, test.expected)
   102  	}
   103  }
   104  
   105  // TODO(ericsnow) Add failure tests for bad domain names
   106  // and IP addresses (v4/v6).
   107  
   108  var failureAddrTests = []addrTest{{
   109  	// a malformed URL
   110  	addr: ":a.b.c",
   111  	err:  `.*`,
   112  }, {
   113  	addr: "unix://",
   114  	err:  `.*unix socket URLs not supported.*`,
   115  }, {
   116  	addr: "unix:///x/y/z",
   117  	err:  `.*unix socket URLs not supported.*`,
   118  }, {
   119  	addr: "unix:/x/y/z",
   120  	err:  `.*unix socket URLs not supported.*`,
   121  }, {
   122  	addr: "/x/y/z",
   123  	err:  `.*unix socket URLs not supported.*`,
   124  }, {
   125  	addr: "https://a.b.c:xyz",
   126  	err:  `.*invalid port.*`,
   127  }, {
   128  	addr: "https://a.b.c:0",
   129  	err:  `.*invalid port.*`,
   130  }, {
   131  	addr: "https://a.b.c:99999",
   132  	err:  `.*invalid port.*`,
   133  }, {
   134  	addr: "spam://a.b.c",
   135  	err:  `.*unsupported URL scheme.*`,
   136  }, {
   137  	addr: "https://a.b.c?d=e",
   138  	err:  `.*URL queries not supported.*`,
   139  }, {
   140  	addr: "https://a.b.c#d",
   141  	err:  `.*URL fragments not supported.*`,
   142  }}
   143  
   144  func (s *fixAddrSuite) TestFixAddrFailures(c *gc.C) {
   145  	for i, test := range failureAddrTests {
   146  		c.Logf("test %d: checking %q", i, test.addr)
   147  		c.Assert(test.err, gc.Not(gc.Equals), "")
   148  
   149  		_, err := fixAddr(test.addr)
   150  
   151  		c.Check(err, gc.ErrorMatches, test.err)
   152  	}
   153  }