A bit more refactoring of test context code

This commit is contained in:
Chris Price 2012-12-02 16:41:35 -08:00
parent df92c96791
commit cf32763937
12 changed files with 181 additions and 156 deletions

View file

@ -22,4 +22,4 @@ instead do something like:
rspec ./spec/distros/ubuntu_lucid_64 rspec ./spec/distros/ubuntu_lucid_64
For some options that might speed up the testing process a bit during development, For some options that might speed up the testing process a bit during development,
please see `spec/support/test_config.rb`. please see `spec/support/postgres_test_config.rb`.

View file

@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
class postgresql_tests::test_db($db) { class postgresql_tests::system_default::test_db($db) {
include postgresql::server include postgresql::server

View file

@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
class postgresql_tests::test_grant_create($user, $password, $db) { class postgresql_tests::system_default::test_grant_create($user, $password, $db) {
include postgresql::server include postgresql::server

View file

@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
class postgresql_tests::test_initdb { class postgresql_tests::system_default::test_initdb {
include postgresql::initdb include postgresql::initdb

View file

@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
class postgresql_tests::test_psql($command = $title, $unless) { class postgresql_tests::system_default::test_psql($command = $title, $unless) {
include postgresql::server include postgresql::server

View file

@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
class postgresql_tests::test_ruby_psql($command = $title, $unless) { class postgresql_tests::system_default::test_ruby_psql($command = $title, $unless) {
include postgresql::server include postgresql::server

View file

@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
class postgresql_tests::test_user($user, $password) { class postgresql_tests::system_default::test_user($user, $password) {
include postgresql::server include postgresql::server

View file

@ -1,14 +1,14 @@
module TestConfig module PostgresTestConfig
# Tests are pretty slow w/sahara, and when we destroy the VMs at the beginning # Tests are pretty slow w/sahara, and when we destroy the VMs at the beginning
# of the test run. This can be a hindrance for development but is very # of the test run. This can be a hindrance for development but is very
# valuable for final testing. This constant allows you to toggle between # valuable for final testing. This constant allows you to toggle between
# strict testing and less strict testing--the latter being useful for # strict testing and less strict testing--the latter being useful for
# development purposes. # development purposes.
HardCoreTesting = true HardCoreTesting = false
# If this value is set to true, then each VM will be suspended after the tests # If this value is set to true, then each VM will be suspended after the tests
# against it are completed. This will slow things down a ton during # against it are completed. This will slow things down a ton during
# iterative development, but will save a lot of system resources by not # iterative development, but will save a lot of system resources by not
# keeping all of the VMs running at the same time. # keeping all of the VMs running at the same time.
SuspendVMsAfterSuite = true SuspendVMsAfterSuite = false
end end

View file

@ -0,0 +1,15 @@
module PostgresTestUtils
def sudo_and_log(vm, cmd)
@logger.debug("Running command: '#{cmd}'")
result = ""
@env.vms[vm].channel.sudo("cd /tmp && #{cmd}") do |ch, data|
result << data
@logger.debug(data)
end
result
end
def sudo_psql_and_log(vm, psql_cmd, user = 'postgres')
sudo_and_log(vm, "su #{user} -c 'psql #{psql_cmd}'")
end
end

View file

@ -0,0 +1,54 @@
require 'logger'
require 'vagrant'
require 'support/postgres_test_config'
if PostgresTestConfig::HardCoreTesting
# this will just make sure that we throw an error if the user tries to
# run w/o having Sahara installed
require 'sahara'
end
shared_context :pg_vm_context do
before(:all) do
@logger = Logger.new(STDOUT)
@logger.level = Logger::DEBUG # TODO: get from environment or rspec?
@env = Vagrant::Environment::new(:cwd => vagrant_dir)
if PostgresTestConfig::HardCoreTesting
@env.cli("destroy", vm.to_s, "--force") # Takes too long
end
@env.cli("up", vm.to_s)
if PostgresTestConfig::HardCoreTesting
sudo_and_log(vm, '[ "$(facter osfamily)" == "Debian" ] && apt-get update')
end
install_postgres
#sudo_and_log(vm, 'puppet apply -e "include postgresql::server"')
if PostgresTestConfig::HardCoreTesting
# Sahara ignores :cwd so we have to chdir for now, see https://github.com/jedi4ever/sahara/issues/9
Dir.chdir(vagrant_dir)
@env.cli("sandbox", "on", vm.to_s)
end
end
after(:all) do
if PostgresTestConfig::SuspendVMsAfterSuite
@logger.debug("Suspending VM")
@env.cli("suspend", vm.to_s)
end
end
after(:each) do
if PostgresTestConfig::HardCoreTesting
@env.cli("sandbox", "rollback", vm.to_s)
end
end
end

View file

@ -1,4 +1,16 @@
require 'support/postgres_test_utils'
require 'support/shared_contexts/pg_vm_context'
shared_examples :non_default_postgres do shared_examples :non_default_postgres do
include PostgresTestUtils
include_context :pg_vm_context
# this method is required by the pg_vm shared context
def install_postgres
#sudo_and_log(vm, 'puppet apply -e "include postgresql::server"')
# TODO: implement
end
it "doesn't have any tests yet'" do it "doesn't have any tests yet'" do
end end
end end

View file

@ -1,175 +1,119 @@
require 'logger' require 'support/postgres_test_utils'
require 'vagrant' require 'support/shared_contexts/pg_vm_context'
require 'support/test_config'
shared_examples :system_default_postgres do shared_examples :system_default_postgres do
include PostgresTestUtils
include_context :pg_vm_context
if TestConfig::HardCoreTesting # this method is required by the pg_vm shared context
# this will just make sure that we throw an error if the user tries to def install_postgres
# run w/o having Sahara installed sudo_and_log(vm, 'puppet apply -e "include postgresql::server"')
require 'sahara'
end end
def sudo_and_log(vm, cmd) describe 'postgresql::initdb' do
@logger.debug("Running command: '#{cmd}'") it "should idempotently create a working --pgdata directory so postgres can run" do
result = "" @logger.info("starting")
@env.vms[vm].channel.sudo("cd /tmp && #{cmd}") do |ch, data|
result << data
@logger.debug(data)
end
result
end
def sudo_psql_and_log(vm, psql_cmd, user = 'postgres') # A bare-minimum class to initdb the specified dir
sudo_and_log(vm, "su #{user} -c 'psql #{psql_cmd}'") test_class = 'class {"postgresql_tests::system_default::test_initdb": }'
end
before(:all) do # Run once to check for crashes
@logger = Logger.new(STDOUT) sudo_and_log(vm, "puppet apply -e '#{test_class}'")
@logger.level = Logger::DEBUG # TODO: get from environment or rspec?
@env = Vagrant::Environment::new(:cwd => vagrant_dir) # Run again to check for idempotence via --detailed-exitcodes
end sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
after(:all) do sudo_and_log(vm, "service #{service_name} restart")
if TestConfig::SuspendVMsAfterSuite
@logger.debug("Suspending VM") # Connect to it and list the databases
@env.cli("suspend", vm.to_s) sudo_psql_and_log(vm, '--list --tuples-only')
end end
end end
describe "basic (system default postgres) tests" do describe 'postgresql::db' do
before (:all) do it 'should idempotently create a db that we can connect to' do
if TestConfig::HardCoreTesting
@env.cli("destroy", vm.to_s, "--force") # Takes too long
end
@env.cli("up", vm.to_s) # A bare-minimum class to add a DB to postgres, which will be running due to ubuntu
test_class = 'class {"postgresql_tests::system_default::test_db": db => "postgresql_test_db" }'
if TestConfig::HardCoreTesting
sudo_and_log(vm, '[ "$(facter osfamily)" == "Debian" ] && apt-get update')
end
sudo_and_log(vm, 'puppet apply -e "include postgresql::server"')
if TestConfig::HardCoreTesting
# Sahara ignores :cwd so we have to chdir for now, see https://github.com/jedi4ever/sahara/issues/9
Dir.chdir(vagrant_dir)
@env.cli("sandbox", "on", vm.to_s)
end
end
after(:each) do
if TestConfig::HardCoreTesting
@env.cli("sandbox", "rollback", vm.to_s)
end
end
describe 'postgresql::initdb' do
it "should idempotently create a working --pgdata directory so postgres can run" do
@logger.info("starting")
# A bare-minimum class to initdb the specified dir
test_class = 'class {"postgresql_tests::test_initdb": }'
begin
# Run once to check for crashes # Run once to check for crashes
sudo_and_log(vm, "puppet apply -e '#{test_class}'") sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]")
# Run again to check for idempotence via --detailed-exitcodes
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
sudo_and_log(vm, "service #{service_name} restart")
# Connect to it and list the databases
sudo_psql_and_log(vm, '--list --tuples-only')
end
end
describe 'postgresql::db' do
it 'should idempotently create a db that we can connect to' do
# A bare-minimum class to add a DB to postgres, which will be running due to ubuntu
test_class = 'class {"postgresql_tests::test_db": db => "postgresql_test_db" }'
begin
# Run once to check for crashes
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]")
# Run again to check for idempotence
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
# Check that the database name is present
sudo_psql_and_log(vm, 'postgresql_test_db --command="select datname from pg_database limit 1"')
ensure
sudo_psql_and_log(vm, '--command="drop database postgresql_test_db" postgres')
end
end
end
describe 'postgresql::psql' do
it 'should emit a deprecation warning' do
test_class = 'class {"postgresql_tests::test_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
data = sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]")
data.should match /postgresql::psql is deprecated/
end
end
describe 'postgresql_psql' do
it 'should run some SQL when the unless query returns no rows' do
test_class = 'class {"postgresql_tests::test_ruby_psql": command => "SELECT 1", unless => "SELECT 1 WHERE 1=2" }'
# Run once to get all packages set up
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
# Check for exit code 2
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}' ; [ $? == 2 ]")
end
it 'should not run SQL when the unless query returns rows' do
test_class = 'class {"postgresql_tests::test_ruby_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
# Run once to get all packages set up
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
# Check for exit code 0
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
end
end
describe 'postgresql::user' do
it 'should idempotently create a user who can log in' do
test_class = 'class {"postgresql_tests::test_user": user => "postgresql_test_user", password => "postgresql_test_password" }'
# Run once to check for crashes
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
# Run again to check for idempotence # Run again to check for idempotence
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'") sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
# Check that the user can log in # Check that the database name is present
sudo_psql_and_log(vm, '--command="select datname from pg_database limit 1" postgres', 'postgresql_test_user') sudo_psql_and_log(vm, 'postgresql_test_db --command="select datname from pg_database limit 1"')
ensure
sudo_psql_and_log(vm, '--command="drop database postgresql_test_db" postgres')
end end
end end
end
describe 'postgresql::grant' do describe 'postgresql::psql' do
it 'should grant access so a user can create in a database' do it 'should emit a deprecation warning' do
test_class = 'class {"postgresql_tests::test_grant_create": db => "postgres", user => "psql_grant_tester", password => "psql_grant_pw" }' test_class = 'class {"postgresql_tests::system_default::test_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
# Run once to check for crashes data = sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]")
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
# Run again to check for idempotence data.should match /postgresql::psql is deprecated/
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
# Check that the user can create a table in the database end
sudo_psql_and_log(vm, '--command="create table foo (foo int)" postgres', 'psql_grant_tester') end
sudo_psql_and_log(vm, '--command="drop table foo" postgres', 'psql_grant_tester') describe 'postgresql_psql' do
end it 'should run some SQL when the unless query returns no rows' do
test_class = 'class {"postgresql_tests::system_default::test_ruby_psql": command => "SELECT 1", unless => "SELECT 1 WHERE 1=2" }'
# Run once to get all packages set up
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
# Check for exit code 2
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}' ; [ $? == 2 ]")
end
it 'should not run SQL when the unless query returns rows' do
test_class = 'class {"postgresql_tests::system_default::test_ruby_psql": command => "SELECT * FROM pg_datbase limit 1", unless => "SELECT 1 WHERE 1=1" }'
# Run once to get all packages set up
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
# Check for exit code 0
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
end
end
describe 'postgresql::user' do
it 'should idempotently create a user who can log in' do
test_class = 'class {"postgresql_tests::system_default::test_user": user => "postgresql_test_user", password => "postgresql_test_password" }'
# Run once to check for crashes
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
# Run again to check for idempotence
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
# Check that the user can log in
sudo_psql_and_log(vm, '--command="select datname from pg_database limit 1" postgres', 'postgresql_test_user')
end
end
describe 'postgresql::grant' do
it 'should grant access so a user can create in a database' do
test_class = 'class {"postgresql_tests::system_default::test_grant_create": db => "postgres", user => "psql_grant_tester", password => "psql_grant_pw" }'
# Run once to check for crashes
sudo_and_log(vm, "puppet apply -e '#{test_class}'")
# Run again to check for idempotence
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
# Check that the user can create a table in the database
sudo_psql_and_log(vm, '--command="create table foo (foo int)" postgres', 'psql_grant_tester')
sudo_psql_and_log(vm, '--command="drop table foo" postgres', 'psql_grant_tester')
end end
end end