Skip to content

Commit b07fd68

Browse files
committed
Merge pull request #2067 from kaifengz/CA-146821-master
CA-146821: Accept IPv6 address when configuring WLB
2 parents 1fbbff8 + 89ac567 commit b07fd68

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

ocaml/test/OMakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ OCAML_OBJS = \
4949
test_vm_helpers \
5050
test_xenopsd_metadata \
5151
test_ca121350 \
52+
test_workload_balancing \
5253

5354
OCamlProgram(suite, suite $(OCAML_OBJS) )
5455

ocaml/test/suite.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ let base_suite =
3939
Test_pgpu_helpers.test;
4040
Test_vm_helpers.test;
4141
Test_xenopsd_metadata.test;
42+
Test_workload_balancing.test;
4243
(* Test_ca121350.test; *)
4344
]
4445

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
(*
2+
* Copyright (C) 2014 Citrix Systems Inc.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published
6+
* by the Free Software Foundation; version 2.1 only. with the special
7+
* exception on linking described in file LICENSE.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*)
14+
15+
open OUnit
16+
open Test_common
17+
18+
let test_split_host_port () =
19+
let split = Workload_balancing.split_host_port in
20+
let assert_succeed url host port =
21+
assert_equal (split url) (host, port)
22+
in
23+
let assert_raise_url_invalid url =
24+
assert_raises_api_error Api_errors.wlb_url_invalid ~args:[url] (fun () -> split url)
25+
in
26+
27+
(* succeed cases *)
28+
assert_succeed "192.168.0.1:80" "192.168.0.1" 80;
29+
assert_succeed "hostname.com:80" "hostname.com" 80;
30+
assert_succeed "[fe80::a085:31cf:b31a:6a]:80" "fe80::a085:31cf:b31a:6a" 80;
31+
32+
(* missing port number *)
33+
assert_raise_url_invalid "192.168.0.1";
34+
assert_raise_url_invalid "hostname.noport.com";
35+
assert_raise_url_invalid "[fe80::a085:31cf:b31a:6a]";
36+
37+
(* non-integer port *)
38+
assert_raise_url_invalid "192.168.0.1:http";
39+
assert_raise_url_invalid "hostname.com:port";
40+
assert_raise_url_invalid "[fe80::a085:31cf:b31a:6a]:ipv6";
41+
42+
(* malformed IPv6 host port peers *)
43+
assert_raise_url_invalid "[fe80::a085:31cf:b31a:6a]80";
44+
assert_raise_url_invalid "[fe80::a085:31cf:b31a:6a:80";
45+
46+
(* others *)
47+
assert_raise_url_invalid "http://example.com:80/"
48+
49+
let test =
50+
"test_workload_balancing" >:::
51+
[
52+
"test_split_host_port" >:: test_split_host_port;
53+
]

ocaml/xapi/workload_balancing.ml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,21 @@ let raise_internal_error args =
7373

7474
let split_host_port url =
7575
try
76-
match String.split_f (fun a -> a = ':') url with
77-
| [host; port] ->
76+
if url.[0] = '[' then (* IPv6 *)
77+
begin
78+
let host_end = String.rindex url ']' in
79+
if url.[host_end + 1] <> ':' then
80+
raise_url_invalid url;
81+
let host = String.sub url 1 (host_end - 1) in
82+
let port = String.sub url (host_end + 2) (String.length url - host_end - 2) in
7883
(host, int_of_string port)
79-
| _ ->
80-
raise_url_invalid url
84+
end
85+
else
86+
match String.split_f (fun a -> a = ':') url with
87+
| [host; port] ->
88+
(host, int_of_string port)
89+
| _ ->
90+
raise_url_invalid url
8191
with
8292
| _ -> raise_url_invalid url
8393

0 commit comments

Comments
 (0)