github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/apiserver/params/network_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package params_test
     5  
     6  import (
     7  	"encoding/json"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/network"
    14  )
    15  
    16  type (
    17  	P struct {
    18  		prot string
    19  		num  int
    20  	}
    21  	IS []interface{}
    22  	M  map[string]interface{}
    23  )
    24  
    25  type NetworkSuite struct{}
    26  
    27  var _ = gc.Suite(&NetworkSuite{})
    28  
    29  func (s *NetworkSuite) TestPortsResults(c *gc.C) {
    30  	// Convenience helpers.
    31  	mkPortsResults := func(prs ...params.PortsResult) params.PortsResults {
    32  		return params.PortsResults{
    33  			Results: prs,
    34  		}
    35  	}
    36  	mkPortsResult := func(msg, code string, ports ...P) params.PortsResult {
    37  		pr := params.PortsResult{}
    38  		if msg != "" {
    39  			pr.Error = &params.Error{msg, code}
    40  		}
    41  		for _, p := range ports {
    42  			pr.Ports = append(pr.Ports, params.Port{p.prot, p.num})
    43  		}
    44  		return pr
    45  	}
    46  	mkResults := func(rs ...interface{}) M {
    47  		return M{"Results": rs}
    48  	}
    49  	mkResult := func(err, ports interface{}) M {
    50  		return M{"Error": err, "Ports": ports}
    51  	}
    52  	mkError := func(msg, code string) M {
    53  		return M{"Message": msg, "Code": code}
    54  	}
    55  	mkPort := func(prot string, num int) M {
    56  		return M{"Protocol": prot, "Number": num}
    57  	}
    58  	// Tests.
    59  	tests := []struct {
    60  		about    string
    61  		results  params.PortsResults
    62  		expected M
    63  	}{{
    64  		about:    "empty result set",
    65  		results:  mkPortsResults(),
    66  		expected: mkResults(),
    67  	}, {
    68  		about: "one error",
    69  		results: mkPortsResults(
    70  			mkPortsResult("I failed", "ERR42")),
    71  		expected: mkResults(
    72  			mkResult(mkError("I failed", "ERR42"), nil)),
    73  	}, {
    74  		about: "one succes with one port",
    75  		results: mkPortsResults(
    76  			mkPortsResult("", "", P{"tcp", 80})),
    77  		expected: mkResults(
    78  			mkResult(nil, IS{mkPort("tcp", 80)})),
    79  	}, {
    80  		about: "two results, one error and one success with two ports",
    81  		results: mkPortsResults(
    82  			mkPortsResult("I failed", "ERR42"),
    83  			mkPortsResult("", "", P{"tcp", 80}, P{"tcp", 443})),
    84  		expected: mkResults(
    85  			mkResult(mkError("I failed", "ERR42"), nil),
    86  			mkResult(nil, IS{mkPort("tcp", 80), mkPort("tcp", 443)})),
    87  	}}
    88  	for i, test := range tests {
    89  		c.Logf("\ntest %d: %s", i, test.about)
    90  		output, err := json.Marshal(test.results)
    91  		if !c.Check(err, jc.ErrorIsNil) {
    92  			continue
    93  		}
    94  		c.Logf("\nJSON output:\n%v", string(output))
    95  		c.Check(string(output), jc.JSONEquals, test.expected)
    96  	}
    97  }
    98  
    99  func (s *NetworkSuite) TestHostPort(c *gc.C) {
   100  	mkHostPort := func(v, t, n, s string, p int) M {
   101  		return M{
   102  			"Value":       v,
   103  			"Type":        t,
   104  			"NetworkName": n,
   105  			"Scope":       s,
   106  			"Port":        p,
   107  		}
   108  	}
   109  	tests := []struct {
   110  		about    string
   111  		hostPort params.HostPort
   112  		expected M
   113  	}{{
   114  		about: "address only value; port is 1234",
   115  		hostPort: params.HostPort{
   116  			Address: params.Address{
   117  				Value: "foo",
   118  			},
   119  			Port: 1234,
   120  		},
   121  		expected: mkHostPort("foo", "", "", "", 1234),
   122  	}, {
   123  		about: "address value and type; port is 1234",
   124  		hostPort: params.HostPort{
   125  			Address: params.Address{
   126  				Value: "foo",
   127  				Type:  "ipv4",
   128  			},
   129  			Port: 1234,
   130  		},
   131  		expected: mkHostPort("foo", "ipv4", "", "", 1234),
   132  	}, {
   133  		about: "address value, type, and network name, port is 1234",
   134  		hostPort: params.HostPort{
   135  			Address: params.Address{
   136  				Value:       "foo",
   137  				Type:        "ipv4",
   138  				NetworkName: "bar",
   139  			},
   140  			Port: 1234,
   141  		},
   142  		expected: mkHostPort("foo", "ipv4", "bar", "", 1234),
   143  	}, {
   144  		about: "address all fields, port is 1234",
   145  		hostPort: params.HostPort{
   146  			Address: params.Address{
   147  				Value:       "foo",
   148  				Type:        "ipv4",
   149  				NetworkName: "bar",
   150  				Scope:       "public",
   151  			},
   152  			Port: 1234,
   153  		},
   154  		expected: mkHostPort("foo", "ipv4", "bar", "public", 1234),
   155  	}, {
   156  		about: "address all fields, port is 0",
   157  		hostPort: params.HostPort{
   158  			Address: params.Address{
   159  				Value:       "foo",
   160  				Type:        "ipv4",
   161  				NetworkName: "bar",
   162  				Scope:       "public",
   163  			},
   164  			Port: 0,
   165  		},
   166  		expected: mkHostPort("foo", "ipv4", "bar", "public", 0),
   167  	}}
   168  	for i, test := range tests {
   169  		c.Logf("\ntest %d: %s", i, test.about)
   170  		output, err := json.Marshal(test.hostPort)
   171  		if !c.Check(err, jc.ErrorIsNil) {
   172  			continue
   173  		}
   174  		c.Logf("\nJSON output:\n%v", string(output))
   175  		c.Check(string(output), jc.JSONEquals, test.expected)
   176  	}
   177  }
   178  
   179  func (s *NetworkSuite) TestMachinePortRange(c *gc.C) {
   180  	mkPortRange := func(u, r string, f, t int, p string) M {
   181  		return M{
   182  			"UnitTag":     u,
   183  			"RelationTag": r,
   184  			"PortRange": M{
   185  				"FromPort": f,
   186  				"ToPort":   t,
   187  				"Protocol": p,
   188  			},
   189  		}
   190  	}
   191  	tests := []struct {
   192  		about            string
   193  		machinePortRange params.MachinePortRange
   194  		expected         M
   195  	}{{
   196  		about: "all values",
   197  		machinePortRange: params.MachinePortRange{
   198  			UnitTag:     "foo/0",
   199  			RelationTag: "foo.db#bar.server",
   200  			PortRange: params.PortRange{
   201  				FromPort: 100,
   202  				ToPort:   200,
   203  				Protocol: "tcp",
   204  			},
   205  		},
   206  		expected: mkPortRange("foo/0", "foo.db#bar.server", 100, 200, "tcp"),
   207  	}, {
   208  		about: "only port range, missing from",
   209  		machinePortRange: params.MachinePortRange{
   210  			PortRange: params.PortRange{
   211  				ToPort:   200,
   212  				Protocol: "tcp",
   213  			},
   214  		},
   215  		expected: mkPortRange("", "", 0, 200, "tcp"),
   216  	}, {
   217  		about: "only port range, missing to",
   218  		machinePortRange: params.MachinePortRange{
   219  			PortRange: params.PortRange{
   220  				FromPort: 100,
   221  				Protocol: "tcp",
   222  			},
   223  		},
   224  		expected: mkPortRange("", "", 100, 0, "tcp"),
   225  	}, {
   226  		about: "only port range, missing protocol",
   227  		machinePortRange: params.MachinePortRange{
   228  			PortRange: params.PortRange{
   229  				FromPort: 100,
   230  				ToPort:   200,
   231  			},
   232  		},
   233  		expected: mkPortRange("", "", 100, 200, ""),
   234  	}, {
   235  		about:            "no field values",
   236  		machinePortRange: params.MachinePortRange{},
   237  		expected:         mkPortRange("", "", 0, 0, ""),
   238  	}}
   239  	for i, test := range tests {
   240  		c.Logf("\ntest %d: %s", i, test.about)
   241  		output, err := json.Marshal(test.machinePortRange)
   242  		if !c.Check(err, jc.ErrorIsNil) {
   243  			continue
   244  		}
   245  		c.Logf("\nJSON output:\n%v", string(output))
   246  		c.Check(string(output), jc.JSONEquals, test.expected)
   247  	}
   248  }
   249  
   250  func (s *NetworkSuite) TestPortConvenience(c *gc.C) {
   251  	networkPort := network.Port{
   252  		Protocol: "udp",
   253  		Number:   55555,
   254  	}
   255  	paramsPort := params.FromNetworkPort(networkPort)
   256  	c.Assert(networkPort, jc.DeepEquals, paramsPort.NetworkPort())
   257  }
   258  
   259  func (s *NetworkSuite) TestPortRangeConvenience(c *gc.C) {
   260  	networkPortRange := network.PortRange{
   261  		FromPort: 61001,
   262  		ToPort:   61010,
   263  		Protocol: "tcp",
   264  	}
   265  	paramsPortRange := params.FromNetworkPortRange(networkPortRange)
   266  	networkPortRangeBack := paramsPortRange.NetworkPortRange()
   267  	c.Assert(networkPortRange, jc.DeepEquals, networkPortRangeBack)
   268  }
   269  
   270  func (s *NetworkSuite) TestAddressConvenience(c *gc.C) {
   271  	networkAddress := network.Address{
   272  		Value:       "foo",
   273  		Type:        network.IPv4Address,
   274  		NetworkName: "bar",
   275  		Scope:       network.ScopePublic,
   276  	}
   277  	paramsAddress := params.FromNetworkAddress(networkAddress)
   278  	c.Assert(networkAddress, jc.DeepEquals, paramsAddress.NetworkAddress())
   279  }
   280  
   281  func (s *NetworkSuite) TestHostPortConvenience(c *gc.C) {
   282  	networkAddress := network.Address{
   283  		Value:       "foo",
   284  		Type:        network.IPv4Address,
   285  		NetworkName: "bar",
   286  		Scope:       network.ScopePublic,
   287  	}
   288  	networkHostPort := network.HostPort{networkAddress, 4711}
   289  	paramsHostPort := params.FromNetworkHostPort(networkHostPort)
   290  	c.Assert(networkHostPort, jc.DeepEquals, paramsHostPort.NetworkHostPort())
   291  }