Do not assume http expected_codes ordering

This fixes the unit tests[1] that breaks with a randomized
PYTHONHASHSEED (see the bug report).

The test assumed that expected_codes had elements in particular order.
Found with PYTHONHASHSEED=1.

The fix refactors the test case by injecting values using the same
ordering.

[1] neutron_lbaas.tests.unit.services.loadbalancer.drivers.haproxy.\
    test_jinja_cfg

Partial-bug: #1348818

Note: There are several other unrelated unit tests that also break with
a randomized PYTHONHASHSEED, but they are not addressed here. They will
be addressed in separate patches.

Change-Id: I5d761c392bf37a0b3626abc64e0e428f978638df
This commit is contained in:
Cedric Brandily 2015-06-01 21:06:46 +02:00
parent a52a82598b
commit 52d4085141
2 changed files with 25 additions and 15 deletions

View File

@ -19,6 +19,9 @@ RET_PERSISTENCE = {
'type': 'HTTP_COOKIE', 'type': 'HTTP_COOKIE',
'cookie_name': 'HTTP_COOKIE'} 'cookie_name': 'HTTP_COOKIE'}
HASHSEED_ORDERED_CODES = list({'404', '405', '500'})
PIPED_CODES = '|'.join(HASHSEED_ORDERED_CODES)
RET_MONITOR = { RET_MONITOR = {
'id': 'sample_monitor_id_1', 'id': 'sample_monitor_id_1',
'type': 'HTTP', 'type': 'HTTP',
@ -27,7 +30,7 @@ RET_MONITOR = {
'max_retries': 3, 'max_retries': 3,
'http_method': 'GET', 'http_method': 'GET',
'url_path': '/index.html', 'url_path': '/index.html',
'expected_codes': '405|404|500', 'expected_codes': PIPED_CODES,
'admin_state_up': True} 'admin_state_up': True}
RET_MEMBER_1 = { RET_MEMBER_1 = {
@ -270,11 +273,12 @@ def sample_base_expected_config(frontend=None, backend=None):
" cookie SRV insert indirect nocache\n" " cookie SRV insert indirect nocache\n"
" timeout check 31\n" " timeout check 31\n"
" option httpchk GET /index.html\n" " option httpchk GET /index.html\n"
" http-check expect rstatus 405|404|500\n" " http-check expect rstatus %s\n"
" server sample_member_id_1 10.0.0.99:82 weight 13 " " server sample_member_id_1 10.0.0.99:82 weight 13 "
"check inter 30s fall 3 cookie sample_member_id_1\n" "check inter 30s fall 3 cookie sample_member_id_1\n"
" server sample_member_id_2 10.0.0.98:82 weight 13 " " server sample_member_id_2 10.0.0.98:82 weight 13 "
"check inter 30s fall 3 cookie sample_member_id_2\n") "check inter 30s fall 3 cookie sample_member_id_2\n"
% PIPED_CODES)
return ("# Configuration for test-lb\n" return ("# Configuration for test-lb\n"
"global\n" "global\n"
" daemon\n" " daemon\n"

View File

@ -69,13 +69,14 @@ class TestHaproxyCfg(base.BaseTestCase):
" cookie SRV insert indirect nocache\n" " cookie SRV insert indirect nocache\n"
" timeout check 31\n" " timeout check 31\n"
" option httpchk GET /index.html\n" " option httpchk GET /index.html\n"
" http-check expect rstatus 405|404|500\n" " http-check expect rstatus %s\n"
" option forwardfor\n" " option forwardfor\n"
" server sample_member_id_1 10.0.0.99:82" " server sample_member_id_1 10.0.0.99:82"
" weight 13 check inter 30s fall 3 cookie sample_member_id_1\n" " weight 13 check inter 30s fall 3 cookie sample_member_id_1\n"
" server sample_member_id_2 10.0.0.98:82" " server sample_member_id_2 10.0.0.98:82"
" weight 13 check inter 30s fall 3 cookie " " weight 13 check inter 30s fall 3 cookie "
"sample_member_id_2\n\n") "sample_member_id_2\n\n"
% sample_configs.PIPED_CODES)
with mock.patch('os.makedirs'): with mock.patch('os.makedirs'):
with mock.patch('os.listdir'): with mock.patch('os.listdir'):
with mock.patch.object(jinja_cfg, 'utils'): with mock.patch.object(jinja_cfg, 'utils'):
@ -114,12 +115,13 @@ class TestHaproxyCfg(base.BaseTestCase):
" cookie SRV insert indirect nocache\n" " cookie SRV insert indirect nocache\n"
" timeout check 31\n" " timeout check 31\n"
" option httpchk GET /index.html\n" " option httpchk GET /index.html\n"
" http-check expect rstatus 405|404|500\n" " http-check expect rstatus %s\n"
" option forwardfor\n" " option forwardfor\n"
" server sample_member_id_1 10.0.0.99:82 " " server sample_member_id_1 10.0.0.99:82 "
"weight 13 check inter 30s fall 3 cookie sample_member_id_1\n" "weight 13 check inter 30s fall 3 cookie sample_member_id_1\n"
" server sample_member_id_2 10.0.0.98:82 " " server sample_member_id_2 10.0.0.98:82 "
"weight 13 check inter 30s fall 3 cookie sample_member_id_2\n\n") "weight 13 check inter 30s fall 3 cookie sample_member_id_2\n\n"
% sample_configs.PIPED_CODES)
with mock.patch('os.makedirs'): with mock.patch('os.makedirs'):
with mock.patch('neutron.agent.linux.utils.replace_file'): with mock.patch('neutron.agent.linux.utils.replace_file'):
with mock.patch('os.listdir'): with mock.patch('os.listdir'):
@ -146,12 +148,13 @@ class TestHaproxyCfg(base.BaseTestCase):
" cookie SRV insert indirect nocache\n" " cookie SRV insert indirect nocache\n"
" timeout check 31\n" " timeout check 31\n"
" option httpchk GET /index.html\n" " option httpchk GET /index.html\n"
" http-check expect rstatus 405|404|500\n" " http-check expect rstatus %s\n"
" option forwardfor\n" " option forwardfor\n"
" server sample_member_id_1 10.0.0.99:82 " " server sample_member_id_1 10.0.0.99:82 "
"weight 13 check inter 30s fall 3 cookie sample_member_id_1\n" "weight 13 check inter 30s fall 3 cookie sample_member_id_1\n"
" server sample_member_id_2 10.0.0.98:82 " " server sample_member_id_2 10.0.0.98:82 "
"weight 13 check inter 30s fall 3 cookie sample_member_id_2\n\n") "weight 13 check inter 30s fall 3 cookie sample_member_id_2\n\n"
% sample_configs.PIPED_CODES)
rendered_obj = jinja_cfg.render_loadbalancer_obj( rendered_obj = jinja_cfg.render_loadbalancer_obj(
sample_configs.sample_loadbalancer_tuple(), sample_configs.sample_loadbalancer_tuple(),
'nogroup', '/sock_path', '/v2') 'nogroup', '/sock_path', '/v2')
@ -172,12 +175,13 @@ class TestHaproxyCfg(base.BaseTestCase):
" cookie SRV insert indirect nocache\n" " cookie SRV insert indirect nocache\n"
" timeout check 31\n" " timeout check 31\n"
" option httpchk GET /index.html\n" " option httpchk GET /index.html\n"
" http-check expect rstatus 405|404|500\n" " http-check expect rstatus %s\n"
" option ssl-hello-chk\n" " option ssl-hello-chk\n"
" server sample_member_id_1 10.0.0.99:82 " " server sample_member_id_1 10.0.0.99:82 "
"weight 13 check inter 30s fall 3 cookie sample_member_id_1\n" "weight 13 check inter 30s fall 3 cookie sample_member_id_1\n"
" server sample_member_id_2 10.0.0.98:82 " " server sample_member_id_2 10.0.0.98:82 "
"weight 13 check inter 30s fall 3 cookie sample_member_id_2\n\n") "weight 13 check inter 30s fall 3 cookie sample_member_id_2\n\n"
% sample_configs.PIPED_CODES)
rendered_obj = jinja_cfg.render_loadbalancer_obj( rendered_obj = jinja_cfg.render_loadbalancer_obj(
sample_configs.sample_loadbalancer_tuple(proto='HTTPS'), sample_configs.sample_loadbalancer_tuple(proto='HTTPS'),
'nogroup', '/sock_path', '/v2') 'nogroup', '/sock_path', '/v2')
@ -264,12 +268,13 @@ class TestHaproxyCfg(base.BaseTestCase):
" stick on src\n" " stick on src\n"
" timeout check 31\n" " timeout check 31\n"
" option httpchk GET /index.html\n" " option httpchk GET /index.html\n"
" http-check expect rstatus 405|404|500\n" " http-check expect rstatus %s\n"
" option forwardfor\n" " option forwardfor\n"
" server sample_member_id_1 10.0.0.99:82 " " server sample_member_id_1 10.0.0.99:82 "
"weight 13 check inter 30s fall 3\n" "weight 13 check inter 30s fall 3\n"
" server sample_member_id_2 10.0.0.98:82 " " server sample_member_id_2 10.0.0.98:82 "
"weight 13 check inter 30s fall 3\n\n") "weight 13 check inter 30s fall 3\n\n"
% sample_configs.PIPED_CODES)
rendered_obj = jinja_cfg.render_loadbalancer_obj( rendered_obj = jinja_cfg.render_loadbalancer_obj(
sample_configs.sample_loadbalancer_tuple( sample_configs.sample_loadbalancer_tuple(
persistence_type='SOURCE_IP'), persistence_type='SOURCE_IP'),
@ -288,12 +293,13 @@ class TestHaproxyCfg(base.BaseTestCase):
" appsession APP_COOKIE len 56 timeout 3h\n" " appsession APP_COOKIE len 56 timeout 3h\n"
" timeout check 31\n" " timeout check 31\n"
" option httpchk GET /index.html\n" " option httpchk GET /index.html\n"
" http-check expect rstatus 405|404|500\n" " http-check expect rstatus %s\n"
" option forwardfor\n" " option forwardfor\n"
" server sample_member_id_1 10.0.0.99:82 " " server sample_member_id_1 10.0.0.99:82 "
"weight 13 check inter 30s fall 3\n" "weight 13 check inter 30s fall 3\n"
" server sample_member_id_2 10.0.0.98:82 " " server sample_member_id_2 10.0.0.98:82 "
"weight 13 check inter 30s fall 3\n\n") "weight 13 check inter 30s fall 3\n\n"
% sample_configs.PIPED_CODES)
rendered_obj = jinja_cfg.render_loadbalancer_obj( rendered_obj = jinja_cfg.render_loadbalancer_obj(
sample_configs.sample_loadbalancer_tuple( sample_configs.sample_loadbalancer_tuple(
persistence_type='APP_COOKIE'), persistence_type='APP_COOKIE'),