-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: refactor postgresql addon and add scripts ut #984
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
87d4718
refactor pg addon
Y-Rookie 7cbee8f
add pg component short name
Y-Rookie 5d1d032
rename component shortname
Y-Rookie e5a09fe
add switchover todo
Y-Rookie b7a1b84
modify switchover.sh
Y-Rookie 4894602
rename pg shell file name
Y-Rookie f176cdb
remove etcd-clean.sh
Y-Rookie 18bdfa4
Merge branch 'main' into support/refactor-pg-addon-and-add-scripts-ut
Y-Rookie 27364ed
add ut for postgresql scripts
Y-Rookie 68572bc
remove etcd-clean
Y-Rookie 1549b2e
update repo
Y-Rookie 4932186
update shortname
Y-Rookie e22e68a
modify pg switchover
Y-Rookie 1fd888f
merge main branch
Y-Rookie bba7c2e
Merge branch 'main' into support/refactor-pg-addon-and-add-scripts-ut
Y-Rookie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# shellcheck shell=bash | ||
# shellcheck disable=SC2034 | ||
|
||
# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run. | ||
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then | ||
echo "pgbouncer_setup_spec.sh skip all cases because dependency bash version 4 or higher is not installed." | ||
exit 0 | ||
fi | ||
|
||
source ./utils.sh | ||
|
||
# The unit test needs to rely on the common library functions defined in kblib. | ||
# Therefore, we first dynamically generate the required common library files from the kblib library chart. | ||
common_library_file="./common.sh" | ||
generate_common_library $common_library_file | ||
|
||
Describe "PgBouncer Configuration and Startup Script Tests" | ||
|
||
Include ../scripts/pgbouncer-setup.sh | ||
Include $common_library_file | ||
|
||
init() { | ||
pgbouncer_conf_dir="./conf" | ||
pgbouncer_log_dir="./logs" | ||
pgbouncer_tmp_dir="./tmp" | ||
pgbouncer_conf_file="./conf/pgbouncer.ini" | ||
pgbouncer_user_list_file="./conf/userlist.txt" | ||
pgbouncer_template_conf_file="./pgbouncer.ini" | ||
touch $pgbouncer_template_conf_file | ||
echo "[pgbouncer] | ||
listen_addr = * | ||
listen_port = 6432 | ||
unix_socket_dir = /tmp/ | ||
unix_socket_mode = 0777 | ||
auth_file = /opt/bitnami/pgbouncer/conf/userlist.txt | ||
auth_user = postgres | ||
auth_query = SELECT usename, passwd FROM pg_shadow WHERE usename=$1 | ||
pidfile =/opt/bitnami/pgbouncer/tmp/pgbouncer.pid | ||
logfile =/opt/bitnami/pgbouncer/logs/pgbouncer.log | ||
auth_type = md5 | ||
pool_mode = session | ||
ignore_startup_parameters = extra_float_digits | ||
admin_users = postgres | ||
;;; [database]" > $pgbouncer_template_conf_file | ||
} | ||
BeforeAll "init" | ||
|
||
cleanup() { | ||
rm -rf ./conf ./logs ./tmp | ||
rm -f $common_library_file | ||
rm -f $pgbouncer_template_conf_file | ||
} | ||
AfterAll 'cleanup' | ||
|
||
Describe "build_pgbouncer_conf()" | ||
setup() { | ||
POSTGRESQL_USERNAME="testuser" | ||
POSTGRESQL_PASSWORD="testpassword" | ||
CURRENT_POD_IP="127.0.0.1" | ||
} | ||
Before 'setup' | ||
|
||
un_setup() { | ||
unset POSTGRESQL_USERNAME | ||
unset POSTGRESQL_PASSWORD | ||
unset CURRENT_POD_IP | ||
} | ||
After 'un_setup' | ||
|
||
It "builds the PgBouncer configuration files" | ||
When call build_pgbouncer_conf | ||
The path "$pgbouncer_conf_dir" should be directory | ||
The path "$pgbouncer_log_dir" should be directory | ||
The path "$pgbouncer_tmp_dir" should be directory | ||
The contents of file "$pgbouncer_user_list_file" should include "\"testuser\" \"testpassword\"" | ||
The contents of file "$pgbouncer_conf_file" should include "listen_addr = *" | ||
The contents of file "$pgbouncer_conf_file" should include "listen_port = 6432" | ||
The contents of file "$pgbouncer_conf_file" should include "admin_users = postgres" | ||
The contents of file "$pgbouncer_conf_file" should include "[databases]" | ||
The contents of file "$pgbouncer_conf_file" should include "postgres=host=127.0.0.1 port=5432 dbname=postgres" | ||
The contents of file "$pgbouncer_conf_file" should include "*=host=127.0.0.1 port=5432" | ||
# ignore useradd commands | ||
The status should be failure | ||
The stderr should be present | ||
End | ||
End | ||
End |
44 changes: 44 additions & 0 deletions
44
addons/postgresql/scripts-ut-spec/postgres_pre_setup_spec.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# shellcheck shell=bash | ||
# shellcheck disable=SC2034 | ||
|
||
# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run. | ||
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then | ||
echo "postgres_pre_setup_spec.sh skip all cases because dependency bash version 4 or higher is not installed." | ||
exit 0 | ||
fi | ||
|
||
Describe "PostgreSQL Configuration Script Tests" | ||
|
||
Include ../scripts/postgres-pre-setup.sh | ||
|
||
init() { | ||
postgres_template_conf_file="./postgresql.conf" | ||
postgres_conf_dir="./pgdata/" | ||
postgres_conf_file="./pgdata/postgresql.conf" | ||
touch $postgres_template_conf_file | ||
echo "listen_addresses = '*' | ||
port = '5432' | ||
archive_command = '/bin/true' | ||
archive_mode = 'on' | ||
auto_explain.log_analyze = 'False' | ||
auto_explain.log_buffers = 'False'" > $postgres_template_conf_file | ||
} | ||
BeforeAll "init" | ||
|
||
cleanup() { | ||
rm -rf $postgres_template_conf_file $postgres_conf_dir $postgres_conf_file | ||
} | ||
AfterAll 'cleanup' | ||
|
||
Describe "build_real_postgres_conf()" | ||
It "builds the PostgreSQL configuration file" | ||
When call build_real_postgres_conf | ||
The status should be success | ||
The path "$postgres_conf_dir" should be directory | ||
The path "$postgres_conf_file" should be file | ||
The contents of file "$postgres_conf_file" should include "listen_addresses = '*'" | ||
The contents of file "$postgres_conf_file" should include "port = '5432'" | ||
The contents of file "$postgres_conf_file" should include "archive_command = '/bin/true'" | ||
End | ||
End | ||
End |
117 changes: 117 additions & 0 deletions
117
addons/postgresql/scripts-ut-spec/postgres_setup_spec.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# shellcheck shell=bash | ||
# shellcheck disable=SC2034 | ||
|
||
# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run. | ||
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then | ||
echo "postgres_setup_spec.sh skip all cases because dependency bash version 4 or higher is not installed." | ||
exit 0 | ||
fi | ||
|
||
source ./utils.sh | ||
|
||
# The unit test needs to rely on the common library functions defined in kblib. | ||
# Therefore, we first dynamically generate the required common library files from the kblib library chart. | ||
common_library_file="./common.sh" | ||
generate_common_library $common_library_file | ||
|
||
Describe "PostgreSQL Initialization Script Tests" | ||
|
||
Include ../scripts/postgres-setup.sh | ||
Include $common_library_file | ||
|
||
cleanup() { | ||
rm -f $common_library_file | ||
rm -f ./tmp_patroni.yaml | ||
} | ||
AfterAll 'cleanup' | ||
|
||
Describe "init_etcd_dcs_config_if_needed()" | ||
Context "when PATRONI_DCS_ETCD_SERVICE_ENDPOINT is set" | ||
setup() { | ||
PATRONI_DCS_ETCD_SERVICE_ENDPOINT="http://etcd-cluster:2379" | ||
} | ||
Before 'setup' | ||
|
||
un_setup() { | ||
unset PATRONI_DCS_ETCD_SERVICE_ENDPOINT | ||
unset ETCDCTL_API | ||
unset DCS_ENABLE_KUBERNETES_API | ||
unset ETCD3_HOSTS | ||
unset ETCD_HOSTS | ||
} | ||
After 'un_setup' | ||
|
||
It "sets the etcd configuration" | ||
When call init_etcd_dcs_config_if_needed | ||
The variable ETCDCTL_API should equal "2" | ||
The variable DCS_ENABLE_KUBERNETES_API should equal "" | ||
The variable ETCD_HOSTS should equal "http://etcd-cluster:2379" | ||
The variable ETCD3_HOSTS should be undefined | ||
The output should include "PATRONI_DCS_ETCD_SERVICE_ENDPOINT is set. Use etcd as DCS backend and unset DCS_ENABLE_KUBERNETES_API" | ||
End | ||
End | ||
|
||
Context "when PATRONI_DCS_ETCD_VERSION is set to 3" | ||
setup() { | ||
PATRONI_DCS_ETCD_VERSION="3" | ||
PATRONI_DCS_ETCD_SERVICE_ENDPOINT="http://etcd-cluster:2380" | ||
} | ||
Before 'setup' | ||
|
||
un_setup() { | ||
unset PATRONI_DCS_ETCD_VERSION | ||
unset PATRONI_DCS_ETCD_SERVICE_ENDPOINT | ||
unset ETCDCTL_API | ||
unset DCS_ENABLE_KUBERNETES_API | ||
unset ETCD3_HOSTS | ||
unset ETCD_HOSTS | ||
} | ||
After 'un_setup' | ||
|
||
It "sets the etcd configuration for version 3" | ||
When call init_etcd_dcs_config_if_needed | ||
The variable ETCDCTL_API should equal "3" | ||
The variable DCS_ENABLE_KUBERNETES_API should equal "" | ||
The variable ETCD3_HOSTS should equal "http://etcd-cluster:2380" | ||
The variable ETCD_HOSTS should be undefined | ||
The output should include "PATRONI_DCS_ETCD_SERVICE_ENDPOINT is set. Use etcd as DCS backend and unset DCS_ENABLE_KUBERNETES_API" | ||
End | ||
End | ||
End | ||
|
||
Describe "regenerate_spilo_configuration_and_start_postgres()" | ||
setup() { | ||
tmp_patroni_yaml="./tmp_patroni.yaml" | ||
touch $tmp_patroni_yaml | ||
} | ||
Before 'setup' | ||
|
||
un_setup() { | ||
unset RESTORE_DATA_DIR | ||
unset SPILO_CONFIGURATION | ||
rm -f $tmp_patroni_yaml | ||
} | ||
After 'un_setup' | ||
|
||
It "regenerates the Spilo configuration and starts PostgreSQL" | ||
# mock python3 /kb-scripts/generate_patroni_yaml.py tmp_patroni.yaml | ||
python3() { | ||
echo "bootstrap: | ||
initdb: | ||
- auth-host: md5 | ||
- auth-local: trust" > "$tmp_patroni_yaml" | ||
} | ||
exec() { | ||
: | ||
} | ||
When call regenerate_spilo_configuration_and_start_postgres | ||
The status should be success | ||
The file "tmp_patroni.yaml" should be exist | ||
The contents of file "tmp_patroni.yaml" should include "bootstrap:" | ||
The contents of file "tmp_patroni.yaml" should include "auth-host: md5" | ||
The variable SPILO_CONFIGURATION should not be undefined | ||
The variable SPILO_CONFIGURATION should include "bootstrap:" | ||
The variable SPILO_CONFIGURATION should include "auth-host: md5" | ||
End | ||
End | ||
End |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how to specify the pg version when install the chart?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not within the scope of this PR. The version will be specified through serviceVersion in the future, but the management of serviceVersion requires further design.