Skip to content

Commit

Permalink
refactor parse deprecated port and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigzagAK committed Apr 25, 2019
1 parent 1576356 commit 01127a4
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 9 deletions.
21 changes: 13 additions & 8 deletions ngx_zookeeper_upstream_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1122,16 +1122,21 @@ ngx_zookeeper_ctx_deref(ngx_zookeeper_path_ctx_t *ctx)


static int
parse_deprecated(const char *body)
parse_deprecated(const char *body, int len)
{
int port;
static ngx_str_t prefix = ngx_string("{\"port\":");

if (body == NULL)
long port;

if (body == NULL || (size_t) len < prefix.len + 2)
return NGX_ERROR;

if (ngx_memcmp(prefix.data, body, prefix.len) != 0 || body[len - 1] != '}')
return NGX_ERROR;

if (sscanf(body, "{\"port\":%d}", &port) == 1)
if (port > 0 && port <= 65535)
return port;
port = strtol(body + prefix.len, NULL, 10);
if (port > 0 && port <= 65535)
return port;

return NGX_ERROR;
}
Expand Down Expand Up @@ -1161,13 +1166,13 @@ ngx_zookeeper_sync_upstream_host(int rc, const char *body, int len,
goto end;
}

if (body == NULL || len == 0) {
if (body == NULL || len <= 0) {

body = NULL;
len = 0;
}

port = parse_deprecated(body);
port = parse_deprecated(body, len);

if (port != NGX_ERROR) {

Expand Down
122 changes: 122 additions & 0 deletions t/deprecated.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use Test::Nginx::Socket 'no_plan';
use Net::ZooKeeper qw(:node_flags :acls);

$ENV{TEST_NGINX_ZOOKEEPER_PORT} ||= 2181;

# prepare zoo data

my $zkh = Net::ZooKeeper->new('127.0.0.1:' . $ENV{TEST_NGINX_ZOOKEEPER_PORT});

sub create {
my ($path, $val) = @_;
$zkh->create($path, $val || '', 'acl' => ZOO_OPEN_ACL_UNSAFE) or
die("unable to create node " . $path . ", err:" . $zkh->get_error());
}

sub cleanup_childrens {
my ($path) = @_;
foreach my $znode ($zkh->get_children($path)) {
cleanup($path . '/' . $znode);
}
}

sub cleanup {
my ($path) = @_;
cleanup_childrens($path);
$zkh->delete($path);
}

cleanup('/test_upstream');

create('/test_upstream');
create('/test_upstream/simple');
create('/test_upstream/simple/nodes');
create('/test_upstream/simple/nodes/127.0.0.1','{"port":1234}');
create('/test_upstream/simple/nodes/127.0.0.2','{"port":1234}');
create('/test_upstream/simple/nodes/127.0.0.3','{"port":1234}');

add_block_preprocessor(sub {
my $block = shift;
if ($block->name =~ /STEP 4/) {
create('/test_upstream/simple/nodes/127.0.0.4','{"port":1234}');
create('/test_upstream/simple/nodes/127.0.0.5','{"port":1234}');
$zkh->delete('/test_upstream/simple/nodes/127.0.0.2');
$zkh->delete('/test_upstream/simple/nodes/127.0.0.3');
}
});

add_cleanup_handler(sub {
cleanup('/test_upstream');
});

no_shuffle();
run_tests();

__DATA__
=== STEP 1: Init
--- http_config
zookeeper_upstream 127.0.0.1:$TEST_NGINX_ZOOKEEPER_PORT;
zookeeper_upstream_log_level debug;
zookeeper_upstream_recv_timeout 5000;
upstream simple {
zone shm_simple 128k;
zookeeper_sync_path /test_upstream/simple/nodes;
zookeeper_sync_file simple.peers;
}
--- config
location = /test {
return 200;
}
location = /list {
zookeeper_sync_list;
}
location = /dynamic {
dynamic_upstream;
}
location = /sleep {
echo_sleep $arg_delay;
}
--- request
GET /test
--- wait: 3
=== STEP 2: Check list
--- request
GET /list
--- response_body_like eval
qr/\[{"name":"simple","lock":"","params_tag":"\@params","filter":""}\]/
=== STEP 3: Check peers
--- request
GET /dynamic?upstream=simple
--- response_body_like eval
qr/(server 127\.0\.0\.[123]:1234 addr=127\.0\.0\.[123]:1234;\n){3}/
=== STEP 4: Update & recheck
--- request eval
[
"GET /sleep?delay=11",
"GET /dynamic?upstream=simple"
]
--- response_body_like eval
[
"",
qr/(server 127\.0\.0\.[145]:1234 addr=127\.0\.0\.[145]:1234;
){3}/
]
--- timeout: 12
2 changes: 1 addition & 1 deletion tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ret=0
for t in $(ls t/*.t)
do
echo "Tests : "$t
prove $t
prove $t $@
if [ $? -ne 0 ]; then
ret=$?
exit $ret
Expand Down

0 comments on commit 01127a4

Please sign in to comment.