Skip to content

Commit ca3cc73

Browse files
committed
Add an acceptance test and fix an edge case with test timing
During upgrade test, checking the admin socket file presence is not enough. We need to wait it's really ready to accept connections. The exec is splitted between the two cases : - Initial run and no .my.cnf file, we build the full mysql cmd - .my.cnf already exists, we use it
1 parent 4e60b7e commit ca3cc73

3 files changed

Lines changed: 89 additions & 16 deletions

File tree

manifests/service.pp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# systemd unit files replaced use of `init.d` in version 2.0.0 for some operating systems but only in 2.0.7 for CentOS/Redhat
88
if (versioncmp($proxysql::version, '2.0.7') >= 0 and fact('os.family') == 'RedHat' and fact('os.name') != 'Amazon')
9-
or (versioncmp($proxysql::version, '2') >= 0 and fact('os.family') == 'Debian') {
9+
or (versioncmp($proxysql::version, '2') >= 0 and fact('os.family') == 'Debian') {
1010
$drop_in_ensure = $proxysql::restart ? {
1111
true => 'present',
1212
false => 'absent',
@@ -53,12 +53,32 @@
5353
}
5454
}
5555

56-
exec { 'wait_for_admin_socket_to_open':
57-
command => "test -S ${proxysql::admin_listen_socket}",
58-
unless => "test -S ${proxysql::admin_listen_socket}",
59-
tries => '3',
60-
try_sleep => '10',
61-
require => Service[$proxysql::service_name],
62-
path => '/bin:/usr/bin',
56+
$socket_wait_connection_method = $proxysql::admin_listen_socket ? {
57+
'' => "-h ${proxysql::admin_listen_ip} -P ${proxysql::admin_listen_port}",
58+
default => "-S ${proxysql::admin_listen_socket}"
59+
}
60+
61+
$wait_exec_params = {
62+
tries => '10',
63+
try_sleep => '2',
64+
subscribe => Service[$proxysql::service_name],
65+
refreshonly => true,
66+
path => '/bin:/usr/bin',
67+
}
68+
69+
# It happens that the admin socket is created but not ready (at least during acceptance tests)
70+
# This first exec is used for the first run when no .my.cnf file is created yet
71+
exec { 'wait_for_admin_socket_availability_no_my_cnf':
72+
command => "mysql -u ${proxysql::admin_username} ${socket_wait_connection_method} -e 'SELECT 1'",
73+
environment => ["MYSQL_PWD=${proxysql::admin_password.unwrap}"],
74+
unless => "test -f ${proxysql::mycnf_file_name}",
75+
* => $wait_exec_params,
76+
}
77+
78+
# We prefer to use .my.cnf if available. In case of password change, we need the old credentials.
79+
exec { 'wait_for_admin_socket_availability_with_my_cnf':
80+
command => "mysql --defaults-extra-file=${proxysql::mycnf_file_name} -e 'SELECT 1'",
81+
onlyif => "test -f ${proxysql::mycnf_file_name}",
82+
* => $wait_exec_params,
6383
}
6484
}

spec/acceptance/class_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,44 @@ class { 'proxysql':
3535
end
3636
end
3737

38+
context 'Upgrading to version 3.0' do
39+
it 'works idempotently with no errors' do
40+
pp = <<-EOS
41+
class { 'proxysql':
42+
package_ensure => latest,
43+
version => '3.0.4',
44+
admin_password => Sensitive('new-admin-password'),
45+
}
46+
EOS
47+
48+
# Run it twice and test for idempotency
49+
apply_manifest(pp, catch_failures: true)
50+
apply_manifest(pp, catch_changes: true)
51+
52+
# Run it again, this time relying on proxysql_version fact
53+
pp = <<-EOS
54+
class { 'proxysql':
55+
admin_password => Sensitive('new-admin-password'),
56+
}
57+
EOS
58+
apply_manifest(pp, catch_changes: true)
59+
end
60+
61+
describe package('proxysql') do
62+
it { is_expected.to be_installed }
63+
end
64+
65+
describe service('proxysql') do
66+
it { is_expected.to be_enabled }
67+
it { is_expected.to be_running }
68+
end
69+
70+
describe command('proxysql --version') do
71+
its(:exit_status) { is_expected.to eq 0 }
72+
its(:stdout) { is_expected.to match %r{^ProxySQL version 3\.0\.} }
73+
end
74+
end
75+
3876
context 'extended testing' do
3977
# Using puppet_apply as a helper
4078
it 'works idempotently with no errors' do

spec/classes/proxysql_spec.rb

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
sys_group = 'proxysql'
5252

5353
admin_socket = '/tmp/proxysql_admin.sock'
54+
my_cnf_path = '/root/.my.cnf'
5455

5556
it do
5657
is_expected.to contain_file('proxysql-config-file').with(ensure: 'file',
@@ -73,7 +74,7 @@
7374
owner: sys_user,
7475
group: sys_group,
7576
mode: '0400',
76-
path: '/root/.my.cnf')
77+
path: my_cnf_path)
7778
end
7879

7980
it do
@@ -85,15 +86,29 @@
8586
it { is_expected.to contain_service('proxysql').with_hasrestart(true) }
8687

8788
it do
88-
is_expected.to contain_exec('wait_for_admin_socket_to_open').with(
89-
command: "test -S #{admin_socket}",
90-
unless: "test -S #{admin_socket}",
91-
tries: 3,
92-
try_sleep: 10,
93-
require: 'Service[proxysql]',
94-
path: '/bin:/usr/bin'
89+
is_expected.to contain_exec('wait_for_admin_socket_availability_no_my_cnf').with(
90+
command: "mysql -u admin -S #{admin_socket} -e 'SELECT 1'",
91+
unless: "test -f #{my_cnf_path}",
92+
environment: ["MYSQL_PWD=admin"],
93+
tries: '10',
94+
try_sleep: '2',
95+
subscribe: 'Service[proxysql]',
96+
refreshonly: true,
97+
path: '/bin:/usr/bin',
9598
)
9699
end
100+
101+
it do
102+
is_expected.to contain_exec('wait_for_admin_socket_availability_with_my_cnf').with(
103+
command: "mysql --defaults-extra-file=#{my_cnf_path} -e 'SELECT 1'",
104+
onlyif: "test -f #{my_cnf_path}",
105+
tries: '10',
106+
try_sleep: '2',
107+
subscribe: 'Service[proxysql]',
108+
refreshonly: true,
109+
path: '/bin:/usr/bin',
110+
)
111+
end
97112
end
98113

99114
context 'with parameter datadir_mode set' do

0 commit comments

Comments
 (0)