Skip to content

Write facts as shared examples, move to Debian 11 and follow rspec-puppet path conventions #1311

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

Merged
merged 6 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/puppet/functions/postgresql/postgresql_password.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'openssl'
require 'base64'

Expand Down Expand Up @@ -42,12 +43,12 @@ def default_impl(username, password, sensitive = false, hash = 'md5', salt = nil

def pg_sha256(password, salt)
digest = digest_key(password, salt)
'SCRAM-SHA-256$%s:%s$%s:%s' % [
'4096',
Base64.strict_encode64(salt),
Base64.strict_encode64(client_key(digest)),
Base64.strict_encode64(server_key(digest))
]
'SCRAM-SHA-256$%{iterations}:%{salt}$%{client_key}:%{server_key}' % {
iterations: '4096',
salt: Base64.strict_encode64(salt),
client_key: Base64.strict_encode64(client_key(digest)),
server_key: Base64.strict_encode64(server_key(digest)),
}
end

def digest_key(password, salt)
Expand All @@ -56,7 +57,7 @@ def digest_key(password, salt)
salt: salt,
iterations: 4096,
length: 32,
hash: OpenSSL::Digest::SHA256.new
hash: OpenSSL::Digest::SHA256.new,
)
end

Expand Down
10 changes: 5 additions & 5 deletions manifests/server/default_privileges.pp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# @param connect_settings Specifies a hash of environment variables used when connecting to a remote server.
# @param psql_path Specifies the path to the psql command.
define postgresql::server::default_privileges (
Optional[String] $target_role = undef,
String $role,
String $db,
String $privilege,
Expand All @@ -36,6 +35,7 @@
] $ensure = 'present',
String $group = $postgresql::server::group,
String $psql_path = $postgresql::server::psql_path,
Optional[String] $target_role = undef,
) {

# If possible use the version of the remote database, otherwise
Expand Down Expand Up @@ -74,16 +74,16 @@
}

if $target_role != undef {
$_target_role = " FOR ROLE $target_role"
$_check_target_role = "/$target_role"
$_target_role = " FOR ROLE ${target_role}"
$_check_target_role = "/${target_role}"
} else {
$_target_role = ''
$_check_target_role = ''
}

if $schema != '' {
$_schema = " IN SCHEMA $schema"
$_check_schema = " AND nspname = '$schema'"
$_schema = " IN SCHEMA ${schema}"
$_check_schema = " AND nspname = '${schema}'"
} else {
$_schema = ''
$_check_schema = ' AND nspname IS NULL'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@

require 'spec_helper'

describe 'postgresql::client', type: :class do
let :facts do
{
os: {
family: 'Debian',
name: 'Debian',
release: { 'full' => '8.0', 'major' => '8' },
},
}
end
describe 'postgresql::client' do
include_examples 'Debian 11'

describe 'with parameters' do
let :params do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,9 @@

require 'spec_helper'

describe 'postgresql::globals', type: :class do
context 'on a debian 8' do
let(:facts) do
{
os: {
family: 'Debian',
name: 'Debian',
release: {
full: '8.0',
major: '8',
},
distro: { 'codename' => 'jessie' },
},
osfamily: 'Debian',
lsbdistid: 'Debian',
lsbdistcodename: 'jessie',
}
end
describe 'postgresql::globals' do
context 'on a debian 11' do
include_examples 'Debian 11'

describe 'with no parameters' do
it 'works' do
Expand All @@ -40,17 +25,8 @@
end
end

context 'on redhat family systems' do
let(:facts) do
{
os: {
family: 'RedHat',
name: 'RedHat',
release: { 'full' => '7.1', 'major' => '7' },
},
osfamily: 'RedHat',
}
end
context 'on redhat 7' do
include_examples 'RedHat 7'

describe 'with no parameters' do
it 'works' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@

require 'spec_helper'

describe 'postgresql::lib::devel', type: :class do
let :facts do
{
os: {
family: 'Debian',
name: 'Debian',
release: { 'full' => '8.0', 'major' => '8' },
},
}
end
describe 'postgresql::lib::devel' do
include_examples 'Debian 11'

it { is_expected.to contain_class('postgresql::lib::devel') }

describe 'link pg_config to /usr/bin' do
it {
is_expected.not_to contain_file('/usr/bin/pg_config') \
.with_ensure('link') \
.with_target('/usr/lib/postgresql/8.4/bin/pg_config')
.with_target('/usr/lib/postgresql/13/bin/pg_config')
}
end

Expand All @@ -34,29 +26,13 @@
end

describe 'should not link pg_config on RedHat with default version' do
let(:facts) do
{
os: {
family: 'RedHat',
name: 'CentOS',
release: { 'full' => '6.3', 'major' => '6' },
},
}
end
include_examples 'RedHat 6'

it { is_expected.not_to contain_file('/usr/bin/pg_config') }
end

describe 'link pg_config on RedHat with non-default version' do
let(:facts) do
{
os: {
family: 'RedHat',
name: 'RedHat',
release: { 'full' => '6.3', 'major' => '6' },
},
}
end
include_examples 'RedHat 6'
let :pre_condition do
"class { '::postgresql::globals': version => '9.3' }"
end
Expand All @@ -69,24 +45,15 @@
end

describe 'on Gentoo' do
let :facts do
{
os: {
family: 'Gentoo',
name: 'Gentoo',
},
}
end
include_examples 'Gentoo'
let :params do
{
link_pg_config: false,
}
end

it 'fails to compile' do
expect {
is_expected.to compile
}.to raise_error(%r{is not supported})
is_expected.to compile.and_raise_error(%r{is not supported})
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@

require 'spec_helper'

describe 'postgresql::lib::java', type: :class do
describe 'postgresql::lib::java' do
describe 'on a debian based os' do
let :facts do
{
os: {
family: 'Debian',
name: 'Debian',
release: { 'full' => '8.0', 'major' => '8' },
},
}
end
include_examples 'Debian 11'

it {
is_expected.to contain_package('postgresql-jdbc').with(
Expand All @@ -24,15 +16,7 @@
end

describe 'on a redhat based os' do
let :facts do
{
os: {
family: 'RedHat',
name: 'RedHat',
release: { 'full' => '6.4', 'major' => '6' },
},
}
end
include_examples 'RedHat 8'

it {
is_expected.to contain_package('postgresql-jdbc').with(
Expand Down
27 changes: 27 additions & 0 deletions spec/classes/lib/perl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'postgresql::lib::perl' do
describe 'on redhat 8' do
include_examples 'RedHat 8'

it {
is_expected.to contain_package('perl-DBD-Pg').with(
name: 'perl-DBD-Pg',
ensure: 'present',
)
}
end

describe 'on debian 11' do
include_examples 'Debian 11'

it {
is_expected.to contain_package('perl-DBD-Pg').with(
name: 'libdbd-pg-perl',
ensure: 'present',
)
}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@

require 'spec_helper'

describe 'postgresql::lib::docs', type: :class do
describe 'postgresql::lib::docs' do
describe 'on a redhat based os' do
let :facts do
{
os: {
family: 'RedHat',
name: 'RedHat',
release: { 'full' => '6.4', 'major' => '6' },
},
}
end
include_examples 'RedHat 8'

it {
is_expected.to contain_package('postgresql-docs').with(
Expand Down
38 changes: 38 additions & 0 deletions spec/classes/lib/python_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'postgresql::lib::python' do
describe 'on redhat 7' do
include_examples 'RedHat 7'

it {
is_expected.to contain_package('python-psycopg2').with(
name: 'python-psycopg2',
ensure: 'present',
)
}
end

describe 'on redhat 8' do
include_examples 'RedHat 8'

it {
is_expected.to contain_package('python-psycopg2').with(
name: 'python3-psycopg2',
ensure: 'present',
)
}
end

describe 'on debian 11' do
include_examples 'Debian 11'

it {
is_expected.to contain_package('python-psycopg2').with(
name: 'python-psycopg2',
ensure: 'present',
)
}
end
end
13 changes: 13 additions & 0 deletions spec/classes/params_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'postgresql::params' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }

it { is_expected.to contain_class('postgresql::params') }
end
end
end
13 changes: 13 additions & 0 deletions spec/classes/repo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'postgresql::repo' do
include_examples 'Debian 11'

describe 'with no parameters' do
it 'instantiates apt_postgresql_org class' do
is_expected.to contain_class('postgresql::repo::apt_postgresql_org')
end
end
end
Loading