Separate tests into one operation per test

This commit is contained in:
Lars Kanis 2018-02-12 21:47:10 +01:00
parent 0632dae5eb
commit d9cb63ed98
No known key found for this signature in database
GPG Key ID: 20362776599898C0
2 changed files with 58 additions and 31 deletions

View File

@ -27,4 +27,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "minitest", "~> 5.0" spec.add_development_dependency "minitest", "~> 5.0"
spec.add_development_dependency "bundler", "~> 1.16" spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest-hooks", "~> 1.4"
end end

View File

@ -1,10 +1,13 @@
require "minitest/autorun" require "minitest/autorun"
require 'minitest/hooks/test'
require "pg_ldap_sync" require "pg_ldap_sync"
require 'yaml' require 'yaml'
require 'fileutils' require 'fileutils'
require_relative 'ldap_server' require_relative 'ldap_server'
class TestPgLdapSync < Minitest::Test class TestPgLdapSync < Minitest::Test
include Minitest::Hooks
def log_and_run( *cmd ) def log_and_run( *cmd )
puts cmd.join(' ') puts cmd.join(' ')
system( *cmd ) system( *cmd )
@ -12,8 +15,7 @@ class TestPgLdapSync < Minitest::Test
end end
def start_ldap_server def start_ldap_server
yaml_fname = File.join(File.dirname(__FILE__), "fixtures/ldapdb.yaml") @directory = [{}]
@directory = File.open(yaml_fname){|f| YAML::load(f.read) }
# Listen for incoming LDAP connections. For each one, create a Connection # Listen for incoming LDAP connections. For each one, create a Connection
# object, which will invoke a HashOperation object for each request. # object, which will invoke a HashOperation object for each request.
@ -26,7 +28,7 @@ class TestPgLdapSync < Minitest::Test
# :ssl_cert_file => "cert.pem", # :ssl_cert_file => "cert.pem",
# :ssl_on_connect => true, # :ssl_on_connect => true,
:operation_class => HashOperation, :operation_class => HashOperation,
:operation_args => [@directory] :operation_args => @directory
) )
@ldap_server.run_tcpserver @ldap_server.run_tcpserver
end end
@ -44,23 +46,29 @@ class TestPgLdapSync < Minitest::Test
log_and_run 'initdb', '-D', 'temp/pg_data', '--no-locale' log_and_run 'initdb', '-D', 'temp/pg_data', '--no-locale'
end end
log_and_run 'pg_ctl', '-w', '-o', "-k.", '-D', 'temp/pg_data', 'start' log_and_run 'pg_ctl', '-w', '-o', "-k.", '-D', 'temp/pg_data', 'start'
log_and_run 'psql', '-e', '-c', "DROP ROLE IF EXISTS fred, wilma, \"Flintstones\", \"Wilmas\", \"All Users\"", 'postgres'
end end
def stop_pg_server def stop_pg_server
log_and_run 'pg_ctl', '-w', '-o', "-k.", '-D', 'temp/pg_data', 'stop' log_and_run 'pg_ctl', '-w', '-o', "-k.", '-D', 'temp/pg_data', 'stop'
end end
def setup def before_all
super
ENV['LC_MESSAGES'] = 'C'
start_ldap_server start_ldap_server
start_pg_server start_pg_server
end end
def teardown def after_all
super
stop_ldap_server stop_ldap_server
stop_pg_server stop_pg_server
end end
def setup
log_and_run 'psql', '-e', '-c', "DROP ROLE IF EXISTS fred, wilma, \"Flintstones\", \"Wilmas\", \"All Users\"", 'postgres'
end
def psqlre(*args) def psqlre(*args)
/^\s*#{args[0]}[ |]*#{args[1]}[ |\{"]*#{args[2..-1].join('[", ]+')}["\}\s]*$/ /^\s*#{args[0]}[ |]*#{args[1]}[ |\{"]*#{args[2..-1].join('[", ]+')}["\}\s]*$/
end end
@ -75,42 +83,60 @@ class TestPgLdapSync < Minitest::Test
return text return text
end end
def test_sanity def load_ldap_fixture(fname)
PgLdapSync::Application.run(%w[-c test/fixtures/config-ldapdb.yaml -vv]) yaml_fname = File.join(File.dirname(__FILE__), "fixtures/#{fname}.yaml")
@directory[0] = File.open(yaml_fname){|f| YAML::load(f.read) }
end
ENV['LC_MESSAGES'] = 'C' def sync_with_config(config="config-ldapdb")
psql_du = exec_psql_du PgLdapSync::Application.run(["-c", "test/fixtures/#{config}.yaml", "-vv"])
end
def sync_to_fixture(fixture: "ldapdb", config: "config-ldapdb")
load_ldap_fixture(fixture)
sync_with_config(config)
end
def sync_change
sync_to_fixture
yield(@directory)
sync_with_config
exec_psql_du
end
def test_base_users_groups_memberships
psql_du = sync_change{}
assert_match(psqlre('All Users','Cannot login'), psql_du) assert_match(psqlre('All Users','Cannot login'), psql_du)
assert_match(psqlre('Flintstones','Cannot login'), psql_du) assert_match(psqlre('Flintstones','Cannot login'), psql_du)
assert_match(psqlre('Wilmas','Cannot login','All Users'), psql_du) assert_match(psqlre('Wilmas','Cannot login','All Users'), psql_du)
assert_match(psqlre('fred','','All Users','Flintstones'), psql_du) assert_match(psqlre('fred','','All Users','Flintstones'), psql_du)
assert_match(psqlre('wilma','','Flintstones','Wilmas'), psql_du) assert_match(psqlre('wilma','','Flintstones','Wilmas'), psql_du)
end
def test_add_membership
psql_du = sync_change do |dir|
# add 'Fred' to 'Wilmas'
@directory[0]['cn=Wilmas,dc=example,dc=com']['member'] << 'cn=Fred Flintstone,dc=example,dc=com'
end
assert_match(psqlre('fred','','All Users','Flintstones', 'Wilmas'), psql_du)
end
def test_revoke_membership
psql_du = sync_change do |dir|
# revoke membership of 'wilma' to 'Flintstones' # revoke membership of 'wilma' to 'Flintstones'
@directory['cn=Flintstones,dc=example,dc=com']['member'].pop dir[0]['cn=Flintstones,dc=example,dc=com']['member'].pop
end
PgLdapSync::Application.run(%w[-c test/fixtures/config-ldapdb.yaml -vv])
psql_du = exec_psql_du
assert_match(psqlre('All Users','Cannot login'), psql_du)
assert_match(psqlre('Flintstones','Cannot login'), psql_du)
assert_match(psqlre('Wilmas','Cannot login','All Users'), psql_du)
assert_match(psqlre('fred','','All Users','Flintstones'), psql_du)
assert_match(psqlre('wilma','','Wilmas'), psql_du) assert_match(psqlre('wilma','','Wilmas'), psql_du)
end
def test_rename_role
psql_du = sync_change do |dir|
# rename role 'wilma' # rename role 'wilma'
@directory['cn=Wilma Flintstone,dc=example,dc=com']['sAMAccountName'] = ['Wilma Flintstone'] dir[0]['cn=Wilma Flintstone,dc=example,dc=com']['sAMAccountName'] = ['Wilma Flintstone']
# re-add 'Wilma' to 'Flintstones' end
@directory['cn=Flintstones,dc=example,dc=com']['member'] << 'cn=Wilma Flintstone,dc=example,dc=com'
PgLdapSync::Application.run(%w[-c test/fixtures/config-ldapdb.yaml -vv])
psql_du = exec_psql_du
assert_match(psqlre('All Users','Cannot login'), psql_du)
assert_match(psqlre('Flintstones','Cannot login'), psql_du)
assert_match(psqlre('Wilmas','Cannot login','All Users'), psql_du)
assert_match(psqlre('fred','','All Users','Flintstones'), psql_du)
refute_match(/wilma/, psql_du) refute_match(/wilma/, psql_du)
assert_match(psqlre('Wilma Flintstone','','Flintstones','Wilmas'), psql_du) assert_match(psqlre('Wilma Flintstone','','Flintstones','Wilmas'), psql_du)
end end