Skip to content

Commit b5cda68

Browse files
committed
Clean up spec tests coding
1 parent e5d006d commit b5cda68

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

spec/unit/defines/server/grant_spec.rb

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"class {'postgresql::server':}"
3030
end
3131

32+
it { is_expected.to compile.with_all_deps }
3233
it { is_expected.to contain_postgresql__server__grant('test') }
3334
end
3435

@@ -46,13 +47,13 @@
4647
"class {'postgresql::server':}"
4748
end
4849

50+
it { is_expected.to compile.with_all_deps }
4951
it { is_expected.to contain_postgresql__server__grant('test') }
50-
it {
51-
is_expected.to contain_postgresql_psql('grant:test').with(
52-
'command' => %r{GRANT USAGE ON SEQUENCE "test" TO\s* "test"}m,
53-
'unless' => %r{SELECT 1 WHERE has_sequence_privilege\('test',\s* 'test', 'USAGE'\)}m,
54-
)
55-
}
52+
it do
53+
is_expected.to contain_postgresql_psql('grant:test')
54+
.with_command(%r{GRANT USAGE ON SEQUENCE "test" TO\s* "test"}m)
55+
.with_unless(%r{SELECT 1 WHERE has_sequence_privilege\('test',\s* 'test', 'USAGE'\)}m)
56+
end
5657
end
5758

5859
context 'SeQuEnCe case insensitive object_type match' do
@@ -69,13 +70,13 @@
6970
"class {'postgresql::server':}"
7071
end
7172

73+
it { is_expected.to compile.with_all_deps }
7274
it { is_expected.to contain_postgresql__server__grant('test') }
73-
it {
74-
is_expected.to contain_postgresql_psql('grant:test').with(
75-
'command' => %r{GRANT USAGE ON SEQUENCE "test" TO\s* "test"}m,
76-
'unless' => %r{SELECT 1 WHERE has_sequence_privilege\('test',\s* 'test', 'USAGE'\)}m,
77-
)
78-
}
75+
it do
76+
is_expected.to contain_postgresql_psql('grant:test')
77+
.with_command(%r{GRANT USAGE ON SEQUENCE "test" TO\s* "test"}m)
78+
.with_unless(%r{SELECT 1 WHERE has_sequence_privilege\('test',\s* 'test', 'USAGE'\)}m)
79+
end
7980
end
8081

8182
context 'all sequences' do
@@ -93,13 +94,13 @@
9394
"class {'postgresql::server':}"
9495
end
9596

97+
it { is_expected.to compile.with_all_deps }
9698
it { is_expected.to contain_postgresql__server__grant('test') }
97-
it {
98-
is_expected.to contain_postgresql_psql('grant:test').with(
99-
'command' => %r{GRANT USAGE ON ALL SEQUENCES IN SCHEMA "public" TO\s* "test"}m,
100-
'unless' => %r{SELECT 1 WHERE NOT EXISTS \(\s*SELECT sequence_name\s* FROM information_schema\.sequences\s* WHERE sequence_schema='public'\s* EXCEPT DISTINCT\s* SELECT object_name as sequence_name\s* FROM .* WHERE .*grantee='test'\s* AND object_schema='public'\s* AND privilege_type='USAGE'\s*\)}m, # rubocop:disable Metrics/LineLength
101-
)
102-
}
99+
it do
100+
is_expected.to contain_postgresql_psql('grant:test')
101+
.with_command(%r{GRANT USAGE ON ALL SEQUENCES IN SCHEMA "public" TO\s* "test"}m)
102+
.with_unless(%r{SELECT 1 WHERE NOT EXISTS \(\s*SELECT sequence_name\s* FROM information_schema\.sequences\s* WHERE sequence_schema='public'\s* EXCEPT DISTINCT\s* SELECT object_name as sequence_name\s* FROM .* WHERE .*grantee='test'\s* AND object_schema='public'\s* AND privilege_type='USAGE'\s*\)}m) # rubocop:disable Metrics/LineLength
103+
end
103104
end
104105

105106
context 'with specific db connection settings - default port' do
@@ -116,6 +117,7 @@
116117
"class {'postgresql::server':}"
117118
end
118119

120+
it { is_expected.to compile.with_all_deps }
119121
it { is_expected.to contain_postgresql__server__grant('test') }
120122
it { is_expected.to contain_postgresql_psql('grant:test').with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1').with_port(5432) }
121123
end
@@ -135,6 +137,7 @@
135137
"class {'postgresql::server':}"
136138
end
137139

140+
it { is_expected.to compile.with_all_deps }
138141
it { is_expected.to contain_postgresql__server__grant('test') }
139142
it { is_expected.to contain_postgresql_psql('grant:test').with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234') }
140143
end
@@ -155,6 +158,7 @@
155158
"class {'postgresql::server':}"
156159
end
157160

161+
it { is_expected.to compile.with_all_deps }
158162
it { is_expected.to contain_postgresql__server__grant('test') }
159163
it { is_expected.to contain_postgresql_psql('grant:test').with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234').with_port('5678') }
160164
end
@@ -174,13 +178,13 @@
174178
"class {'postgresql::server':}"
175179
end
176180

181+
it { is_expected.to compile.with_all_deps }
177182
it { is_expected.to contain_postgresql__server__grant('test') }
178-
it {
179-
is_expected.to contain_postgresql_psql('grant:test').with(
180-
'command' => %r{GRANT ALL ON TABLE "myschema"."mytable" TO\s* "test"}m,
181-
'unless' => %r{SELECT 1 WHERE has_table_privilege\('test',\s*'myschema.mytable', 'INSERT'\)}m,
182-
)
183-
}
183+
it do
184+
is_expected.to contain_postgresql_psql('grant:test')
185+
.with_command(%r{GRANT ALL ON TABLE "myschema"."mytable" TO\s* "test"}m)
186+
.with_unless(%r{SELECT 1 WHERE has_table_privilege\('test',\s*'myschema.mytable', 'INSERT'\)}m)
187+
end
184188
end
185189

186190
context 'invalid object_type' do

spec/unit/defines/server/reassign_owned_by_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class {'postgresql::server':}
3636

3737
it {
3838
is_expected.to contain_postgresql_psql('reassign_owned_by:test:REASSIGN OWNED BY "test_old_role" TO "test_new_role"')
39-
.with('command' => 'REASSIGN OWNED BY "test_old_role" TO "test_new_role"',
40-
'onlyif' => %r{SELECT tablename FROM pg_catalog.pg_tables WHERE\s*schemaname NOT IN \('pg_catalog', 'information_schema'\) AND\s*tableowner = 'test_old_role'.*}m)
39+
.with_command('REASSIGN OWNED BY "test_old_role" TO "test_new_role"')
40+
.with_onlyif(%r{SELECT tablename FROM pg_catalog.pg_tables WHERE\s*schemaname NOT IN \('pg_catalog', 'information_schema'\) AND\s*tableowner = 'test_old_role'.*}m)
4141
.that_requires('Class[postgresql::server]')
4242
}
4343
end

spec/unit/defines/server/role_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@
6161
it { is_expected.to contain_postgresql__server__role('test') }
6262
it 'has create role for "test" user with password as ****' do
6363
is_expected.to contain_postgresql_psql('CREATE ROLE test ENCRYPTED PASSWORD ****')
64-
.with('command' => "CREATE ROLE \"test\" ENCRYPTED PASSWORD '$NEWPGPASSWD' LOGIN NOCREATEROLE NOCREATEDB NOSUPERUSER CONNECTION LIMIT -1", 'environment' => 'NEWPGPASSWD=new-pa$s',
65-
'unless' => "SELECT 1 FROM pg_roles WHERE rolname = 'test'", 'port' => '5432',
66-
'connect_settings' => { 'PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1',
67-
'PGUSER' => 'login-user', 'PGPASSWORD' => 'login-pass' })
64+
.with_command("CREATE ROLE \"test\" ENCRYPTED PASSWORD '$NEWPGPASSWD' LOGIN NOCREATEROLE NOCREATEDB NOSUPERUSER CONNECTION LIMIT -1")
65+
.with_environment('NEWPGPASSWD=new-pa$s')
66+
.with_unless("SELECT 1 FROM pg_roles WHERE rolname = 'test'")
67+
.with_port(5432)
68+
.with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGUSER' => 'login-user', 'PGPASSWORD' => 'login-pass')
6869
end
6970
it 'has alter role for "test" user with password as ****' do
7071
is_expected.to contain_postgresql_psql('ALTER ROLE test ENCRYPTED PASSWORD ****')

0 commit comments

Comments
 (0)