Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit bdecbe6

Browse files
committed
Framework for testing on Cent6 / PG9
This commit does the following: * Adds a CentOS6 vm to the Vagrantfile * Reorganizes the spec test file a bit so that it will be easy to test various things on different VMs. This is in preparation for adding some PG9 tests to run on Cent6.
1 parent 5585b6b commit bdecbe6

File tree

2 files changed

+163
-99
lines changed

2 files changed

+163
-99
lines changed

spec/Vagrantfile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@
2121

2222
Vagrant::Config.run do |config|
2323

24-
# Test on 64 bit lucid
25-
config.vm.box = "lucid64"
26-
config.vm.box_url = "http://files.vagrantup.com/lucid64.box"
24+
config.vm.define :lucid do |lucid_config|
25+
# Test on 64 bit lucid
26+
lucid_config.vm.box = "lucid64"
27+
lucid_config.vm.box_url = "http://files.vagrantup.com/lucid64.box"
28+
end
29+
30+
config.vm.define :cent6 do |cent_config|
31+
cent_config.vm.box = "cent63_64"
32+
cent_config.vm.box_url = "https://dl.dropbox.com/u/7225008/Vagrant/CentOS-6.3-x86_64-minimal.box"
33+
end
2734

2835
# Resolve DNS via NAT
2936
config.vm.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]

spec/postgresql_spec.rb

Lines changed: 153 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,25 @@
3030

3131
describe "postgresql" do
3232

33-
def sudo_and_log(*args)
34-
@logger.debug("Running command: '#{args[0]}'")
33+
# Tests are pretty slow w/sahara, and when we destroy the VMs at the beginning
34+
# of the test run. This can be a hindrance for development but is very
35+
# valuable for final testing. This constant allows you to toggle between
36+
# strict testing and less strict testing--the latter being useful for
37+
# development purposes.
38+
HardCoreTesting = false
39+
40+
41+
if HardCoreTesting
42+
# this will just make sure that we throw an error if the user tries to
43+
# run w/o having Sahara installed
44+
require 'sahara'
45+
end
46+
47+
48+
def sudo_and_log(vm, cmd)
49+
@logger.debug("Running command: '#{cmd}'")
3550
result = ""
36-
@env.primary_vm.channel.sudo(args[0]) do |ch, data|
51+
@env.vms[vm].channel.sudo(cmd) do |ch, data|
3752
result << data
3853
@logger.debug(data)
3954
end
@@ -49,124 +64,166 @@ def sudo_and_log(*args)
4964

5065
# Sahara ignores :cwd so we have to chdir for now, see https://github.com/jedi4ever/sahara/issues/9
5166
Dir.chdir(vagrant_dir)
52-
53-
@env.cli("destroy --force") # Takes too long
54-
@env.cli("up")
55-
56-
# We are not testing the "package" resource type, so pull stuff in in advance
57-
sudo_and_log('apt-get update')
58-
sudo_and_log('apt-get install --yes --download-only postgresql-8.4')
59-
@env.cli("sandbox", "on")
60-
end
61-
62-
after(:each) do
63-
@env.cli("sandbox", "rollback")
6467
end
6568

66-
describe 'postgresql::initdb' do
67-
it "should idempotently create a working --pgdata directory so postgres can run" do
68-
@logger.info("starting")
69+
basic_testing_vms = [:lucid]
70+
71+
basic_testing_vms.each do |vm|
72+
describe "basic (system default postgres) tests (vm: #{vm})" do
73+
before (:all) do
74+
if HardCoreTesting
75+
@env.cli("destroy", vm.to_s, "--force") # Takes too long
76+
end
77+
@env.cli("up", vm.to_s)
78+
79+
# We are not testing the "package" resource type, so pull stuff in in advance
80+
sudo_and_log(vm, 'apt-get update')
81+
sudo_and_log(vm, 'apt-get install --yes --download-only postgresql-8.4')
82+
if HardCoreTesting
83+
@env.cli("sandbox", "on", vm.to_s)
84+
end
85+
end
6986

70-
# A bare-minimum class to initdb the specified dir
71-
test_class = 'class {"postgresql_tests::test_initdb": }'
72-
73-
# Run once to check for crashes
74-
sudo_and_log("puppet apply -e '#{test_class}'")
87+
after(:each) do
88+
if HardCoreTesting
89+
@env.cli("sandbox", "rollback", vm.to_s)
90+
end
91+
end
7592

76-
# Run again to check for idempotence via --detailed-exitcodes
77-
sudo_and_log("puppet apply --detailed-exitcodes -e '#{test_class}'")
93+
describe 'postgresql::initdb' do
94+
it "should idempotently create a working --pgdata directory so postgres can run" do
95+
@logger.info("starting")
7896

79-
sudo_and_log("service postgresql-8.4 restart")
97+
# A bare-minimum class to initdb the specified dir
98+
test_class = 'class {"postgresql_tests::test_initdb": }'
8099

81-
# Connect to it and list the databases
82-
sudo_and_log('sudo -n -u postgres /usr/lib/postgresql/8.4/bin/psql --list --tuples-only')
83-
end
84-
end
100+
# Run once to check for crashes
101+
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
85102

86-
describe 'postgresql::db' do
87-
it 'should idempotently create a db that we can connect to' do
88-
89-
# A bare-minimum class to add a DB to postgres, which will be running due to ubuntu
90-
test_class = 'class {"postgresql_tests::test_db": db => "postgresql_test_db" }'
103+
# Run again to check for idempotence via --detailed-exitcodes
104+
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
91105

92-
begin
93-
# Run once to check for crashes
94-
sudo_and_log("puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]")
106+
sudo_and_log(vm, "service postgresql-8.4 restart")
107+
108+
# Connect to it and list the databases
109+
sudo_and_log(vm, 'sudo -n -u postgres /usr/lib/postgresql/8.4/bin/psql --list --tuples-only')
110+
end
111+
end
95112

96-
# Run again to check for idempotence
97-
sudo_and_log("puppet apply --detailed-exitcodes -e '#{test_class}'")
113+
describe 'postgresql::db' do
114+
it 'should idempotently create a db that we can connect to' do
98115

99-
# Check that the database name is present
100-
sudo_and_log('sudo -u postgres psql postgresql_test_db --command="select datname from pg_database limit 1"')
101-
ensure
102-
sudo_and_log('sudo -u postgres psql --command="drop database postgresql_test_db" postgres')
116+
# A bare-minimum class to add a DB to postgres, which will be running due to ubuntu
117+
test_class = 'class {"postgresql_tests::test_db": db => "postgresql_test_db" }'
118+
119+
begin
120+
# Run once to check for crashes
121+
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]")
122+
123+
# Run again to check for idempotence
124+
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
125+
126+
# Check that the database name is present
127+
sudo_and_log(vm, 'sudo -u postgres psql postgresql_test_db --command="select datname from pg_database limit 1"')
128+
ensure
129+
sudo_and_log(vm, 'sudo -u postgres psql --command="drop database postgresql_test_db" postgres')
130+
end
131+
end
103132
end
104-
end
105-
end
106133

107-
describe 'postgresql::psql' do
108-
it 'should emit a deprecation warning' do
109-
test_class = 'class {"postgresql_tests::test_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
134+
describe 'postgresql::psql' do
135+
it 'should emit a deprecation warning' do
136+
test_class = 'class {"postgresql_tests::test_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
110137

111-
data = sudo_and_log("puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]")
138+
data = sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]")
112139

113-
data.should match /postgresql::psql is deprecated/
140+
data.should match /postgresql::psql is deprecated/
114141

115-
end
116-
end
142+
end
143+
end
117144

118-
describe 'postgresql_psql' do
119-
it 'should run some SQL when the unless query returns no rows' do
120-
test_class = 'class {"postgresql_tests::test_ruby_psql": command => "SELECT 1", unless => "SELECT 1 WHERE 1=2" }'
145+
describe 'postgresql_psql' do
146+
it 'should run some SQL when the unless query returns no rows' do
147+
test_class = 'class {"postgresql_tests::test_ruby_psql": command => "SELECT 1", unless => "SELECT 1 WHERE 1=2" }'
121148

122-
# Run once to get all packages set up
123-
sudo_and_log("puppet apply -e '#{test_class}'")
149+
# Run once to get all packages set up
150+
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
124151

125-
# Check for exit code 2
126-
sudo_and_log("puppet apply --detailed-exitcodes -e '#{test_class}' ; [ $? == 2 ]")
127-
end
152+
# Check for exit code 2
153+
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}' ; [ $? == 2 ]")
154+
end
128155

129-
it 'should not run SQL when the unless query returns rows' do
130-
test_class = 'class {"postgresql_tests::test_ruby_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
156+
it 'should not run SQL when the unless query returns rows' do
157+
test_class = 'class {"postgresql_tests::test_ruby_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
131158

132-
# Run once to get all packages set up
133-
sudo_and_log("puppet apply -e '#{test_class}'")
159+
# Run once to get all packages set up
160+
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
134161

135-
# Check for exit code 0
136-
sudo_and_log("puppet apply --detailed-exitcodes -e '#{test_class}'")
137-
end
162+
# Check for exit code 0
163+
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
164+
end
138165

139-
end
166+
end
167+
168+
describe 'postgresql::user' do
169+
it 'should idempotently create a user who can log in' do
170+
test_class = 'class {"postgresql_tests::test_user": user => "postgresql_test_user", password => "postgresql_test_password" }'
171+
172+
# Run once to check for crashes
173+
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
174+
175+
# Run again to check for idempotence
176+
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
177+
178+
# Check that the user can log in
179+
sudo_and_log(vm, 'sudo -u postgresql_test_user psql --command="select datname from pg_database limit 1" postgres')
180+
end
181+
end
182+
183+
describe 'postgresql::grant' do
184+
it 'should grant access so a user can create in a database' do
185+
test_class = 'class {"postgresql_tests::test_grant_create": db => "postgres", user => "psql_grant_tester", password => "psql_grant_pw" }'
186+
187+
# Run once to check for crashes
188+
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
189+
190+
# Run again to check for idempotence
191+
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
192+
193+
# Check that the user can create a table in the database
194+
sudo_and_log(vm, 'sudo -u psql_grant_tester psql --command="create table foo (foo int)" postgres')
140195

141-
describe 'postgresql::user' do
142-
it 'should idempotently create a user who can log in' do
143-
test_class = 'class {"postgresql_tests::test_user": user => "postgresql_test_user", password => "postgresql_test_password" }'
144-
145-
# Run once to check for crashes
146-
sudo_and_log("puppet apply -e '#{test_class}'")
147-
148-
# Run again to check for idempotence
149-
sudo_and_log("puppet apply --detailed-exitcodes -e '#{test_class}'")
150-
151-
# Check that the user can log in
152-
sudo_and_log('sudo -u postgresql_test_user psql --command="select datname from pg_database limit 1" postgres')
196+
sudo_and_log(vm, 'sudo -u psql_grant_tester psql --command="drop table foo" postgres')
197+
end
198+
end
153199
end
154200
end
155-
156-
describe 'postgresql::grant' do
157-
it 'should grant access so a user can create in a database' do
158-
test_class = 'class {"postgresql_tests::test_grant_create": db => "postgres", user => "psql_grant_tester", password => "psql_grant_pw" }'
159-
160-
# Run once to check for crashes
161-
sudo_and_log("puppet apply -e '#{test_class}'")
162-
163-
# Run again to check for idempotence
164-
sudo_and_log("puppet apply --detailed-exitcodes -e '#{test_class}'")
165-
166-
# Check that the user can create a table in the database
167-
sudo_and_log('sudo -u psql_grant_tester psql --command="create table foo (foo int)" postgres')
168-
169-
sudo_and_log('sudo -u psql_grant_tester psql --command="drop table foo" postgres')
201+
202+
non_default_testing_vms = [:cent6]
203+
204+
non_default_testing_vms.each do |vm|
205+
describe "non-system-default postgres version tests (vm: #{vm})" do
206+
before (:all) do
207+
if HardCoreTesting
208+
@env.cli("destroy", vm.to_s, "--force") # Takes too long
209+
end
210+
@env.cli("up", vm.to_s)
211+
212+
if HardCoreTesting
213+
@env.cli("sandbox", "on", vm.to_s)
214+
end
215+
end
216+
217+
after(:each) do
218+
if HardCoreTesting
219+
@env.cli("sandbox", "rollback", vm.to_s)
220+
end
221+
end
222+
223+
224+
it "should have the echo command" do
225+
sudo_and_log(vm, 'echo "hi"')
226+
end
170227
end
171228
end
172229
end

0 commit comments

Comments
 (0)