2023-09-28 11:22:30 +02:00
from pathlib import Path
2022-02-07 15:11:05 +01:00
import nagiosplugin
from click . testing import CliRunner
2021-08-11 19:09:14 +02:00
from check_patroni . cli import main
2023-03-20 11:44:19 +01:00
2022-02-07 15:01:50 +01:00
2023-09-28 09:41:33 +02:00
def test_cluster_config_has_changed_ok_with_hash (
runner : CliRunner , fake_restapi
) - > None :
2023-09-28 10:57:24 +02:00
fake_restapi ( " cluster_config_has_changed " )
2021-08-11 19:09:14 +02:00
result = runner . invoke (
main ,
[
" -e " ,
" https://10.20.199.3:8008 " ,
" cluster_config_has_changed " ,
" --hash " ,
2023-03-12 19:57:52 +01:00
" 96b12d82571473d13e890b893734e731 " ,
2021-08-11 19:09:14 +02:00
] ,
)
assert result . exit_code == 0
2023-08-22 11:08:38 +02:00
assert (
result . stdout
== " CLUSTERCONFIGHASCHANGED OK - The hash of patroni ' s dynamic configuration has not changed (96b12d82571473d13e890b893734e731). | is_configuration_changed=0;;@1:1 \n "
)
2021-08-11 19:09:14 +02:00
2023-09-28 11:22:30 +02:00
def test_cluster_config_has_changed_ok_with_state_file (
2023-09-28 09:41:33 +02:00
runner : CliRunner , fake_restapi , tmp_path : Path
2023-09-28 11:22:30 +02:00
) - > None :
state_file = tmp_path / " cluster_config_has_changed.state_file "
with state_file . open ( " w " ) as f :
2023-03-12 19:57:52 +01:00
f . write ( ' { " hash " : " 96b12d82571473d13e890b893734e731 " } ' )
2021-08-11 19:09:14 +02:00
2023-09-28 10:57:24 +02:00
fake_restapi ( " cluster_config_has_changed " )
2021-08-11 19:09:14 +02:00
result = runner . invoke (
main ,
[
" -e " ,
" https://10.20.199.3:8008 " ,
" cluster_config_has_changed " ,
" --state-file " ,
2023-09-28 11:22:30 +02:00
str ( state_file ) ,
2021-08-11 19:09:14 +02:00
] ,
)
assert result . exit_code == 0
2023-08-22 11:08:38 +02:00
assert (
result . stdout
== " CLUSTERCONFIGHASCHANGED OK - The hash of patroni ' s dynamic configuration has not changed (96b12d82571473d13e890b893734e731). | is_configuration_changed=0;;@1:1 \n "
)
2021-08-11 19:09:14 +02:00
2023-09-28 09:41:33 +02:00
def test_cluster_config_has_changed_ko_with_hash (
runner : CliRunner , fake_restapi
) - > None :
2023-09-28 10:57:24 +02:00
fake_restapi ( " cluster_config_has_changed " )
2021-08-11 19:09:14 +02:00
result = runner . invoke (
main ,
[
" -e " ,
" https://10.20.199.3:8008 " ,
" cluster_config_has_changed " ,
" --hash " ,
2023-03-12 19:57:52 +01:00
" 96b12d82571473d13e890b8937ffffff " ,
2021-08-11 19:09:14 +02:00
] ,
)
assert result . exit_code == 2
2023-08-22 11:08:38 +02:00
assert (
result . stdout
== " CLUSTERCONFIGHASCHANGED CRITICAL - The hash of patroni ' s dynamic configuration has changed. The old hash was 96b12d82571473d13e890b8937ffffff. | is_configuration_changed=1;;@1:1 \n "
)
2021-08-11 19:09:14 +02:00
2023-09-28 11:22:30 +02:00
def test_cluster_config_has_changed_ko_with_state_file_and_save (
2023-09-28 09:41:33 +02:00
runner : CliRunner , fake_restapi , tmp_path : Path
2023-09-28 11:22:30 +02:00
) - > None :
state_file = tmp_path / " cluster_config_has_changed.state_file "
with state_file . open ( " w " ) as f :
2023-03-12 19:57:52 +01:00
f . write ( ' { " hash " : " 96b12d82571473d13e890b8937ffffff " } ' )
2021-08-11 19:09:14 +02:00
2023-09-28 10:57:24 +02:00
fake_restapi ( " cluster_config_has_changed " )
2023-03-01 16:46:55 +01:00
# test without saving the new hash
2021-08-11 19:09:14 +02:00
result = runner . invoke (
main ,
[
" -e " ,
" https://10.20.199.3:8008 " ,
" cluster_config_has_changed " ,
" --state-file " ,
2023-09-28 11:22:30 +02:00
str ( state_file ) ,
2021-08-11 19:09:14 +02:00
] ,
)
assert result . exit_code == 2
2023-08-22 11:08:38 +02:00
assert (
result . stdout
== " CLUSTERCONFIGHASCHANGED CRITICAL - The hash of patroni ' s dynamic configuration has changed. The old hash was 96b12d82571473d13e890b8937ffffff. | is_configuration_changed=1;;@1:1 \n "
)
2022-02-07 15:01:50 +01:00
2023-09-28 11:22:30 +02:00
state_file = tmp_path / " cluster_config_has_changed.state_file "
cookie = nagiosplugin . Cookie ( state_file )
2023-03-01 16:46:55 +01:00
cookie . open ( )
new_config_hash = cookie . get ( " hash " )
cookie . close ( )
2023-03-12 19:57:52 +01:00
assert new_config_hash == " 96b12d82571473d13e890b8937ffffff "
2023-03-01 16:46:55 +01:00
# test when we save the hash
result = runner . invoke (
main ,
[
" -e " ,
" https://10.20.199.3:8008 " ,
" cluster_config_has_changed " ,
" --state-file " ,
2023-09-28 11:22:30 +02:00
str ( state_file ) ,
2023-03-01 16:46:55 +01:00
" --save " ,
] ,
)
assert result . exit_code == 2
2023-08-22 11:08:38 +02:00
assert (
result . stdout
== " CLUSTERCONFIGHASCHANGED CRITICAL - The hash of patroni ' s dynamic configuration has changed. The old hash was 96b12d82571473d13e890b8937ffffff. | is_configuration_changed=1;;@1:1 \n "
)
2023-03-01 16:46:55 +01:00
2023-09-28 11:22:30 +02:00
cookie = nagiosplugin . Cookie ( state_file )
2022-02-07 15:01:50 +01:00
cookie . open ( )
new_config_hash = cookie . get ( " hash " )
cookie . close ( )
2023-03-12 19:57:52 +01:00
assert new_config_hash == " 96b12d82571473d13e890b893734e731 "
2023-08-22 11:08:38 +02:00
2023-09-28 09:41:33 +02:00
def test_cluster_config_has_changed_params (
runner : CliRunner , fake_restapi , tmp_path : Path
) - > None :
2023-08-22 11:08:38 +02:00
# This one is placed last because it seems like the exceptions are not flushed from stderr for the next tests.
2023-09-28 11:22:30 +02:00
fake_state_file = tmp_path / " fake_file_name.state_file "
2023-09-28 10:57:24 +02:00
fake_restapi ( " cluster_config_has_changed " )
2023-08-22 11:08:38 +02:00
result = runner . invoke (
main ,
[
" -e " ,
" https://10.20.199.3:8008 " ,
" cluster_config_has_changed " ,
" --hash " ,
" 640df9f0211c791723f18fc3ed9dbb95 " ,
" --state-file " ,
2023-09-28 11:22:30 +02:00
str ( fake_state_file ) ,
2023-08-22 11:08:38 +02:00
] ,
)
assert result . exit_code == 3
assert (
result . stdout
== " CLUSTERCONFIGHASCHANGED UNKNOWN: click.exceptions.UsageError: Either --hash or --state-file should be provided for this service \n "
)
result = runner . invoke (
main , [ " -e " , " https://10.20.199.3:8008 " , " cluster_config_has_changed " ]
)
assert result . exit_code == 3
assert (
result . stdout
== " CLUSTERCONFIGHASCHANGED UNKNOWN: click.exceptions.UsageError: Either --hash or --state-file should be provided for this service \n "
)