Skip to content

Test/release/fm 2405 sqlserver login #88

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

Closed
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
160 changes: 160 additions & 0 deletions spec/acceptance/sqlserver_login_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
require 'spec_helper_acceptance'
require 'ERB'

%w(2012 2014).each do |version|
host = find_only_one("sql#{version}")
describe "sqlserver::login #{version}", :node => host do

# An ERB template for a sqlserver::login
manifest = <<-MANIFEST
sqlserver::config{'MSSQLSERVER':
admin_pass => '<%= SQL_ADMIN_PASS %>',
admin_user => '<%= SQL_ADMIN_USER %>',
}
sqlserver::login{'<%= login_name %>':
ensure => <%= defined?(ensure_value) ? ensure_value : 'present' %>,
password => '<%= defined?(password) ? password : SQL_ADMIN_PASS %>',
default_database => '<%= defined?(default_database) ? default_database : 'master' %>',
default_language => '<%= defined?(default_language) ? default_language : 'us_english'%>', disabled => <%= defined?(disabled) ? disabled : false %>,
}
MANIFEST
no_results_error = 'Expected 1 rows but observed 0'

context 'create a sql login' do

login_name = 'Gumby'

after(:each) do
opts = {:query => "DROP LOGIN #{login_name}", :no_rows_expected => true}

# delete the login
run_sql_query(host, opts) do |r|
expect(r.stdout).not_to match(/Error/)
end
end

it "should create a login" do
name_query = "SELECT name FROM sys.server_principals WHERE name = '#{login_name}'"
pp = ERB.new(manifest).result(binding)

apply_manifest_on(host, pp) do |r|
expect(r.stdout).not_to match(/Error/)
end

run_sql_query(host, {:query => name_query}) do |r|
expect(r.stdout).not_to match(/Error/)
expect(r.stdout).to match(/#{login_name}/)
end
end

end

context 'delete a login' do

login_name = 'Pokey'

before(:all) do
pp = ERB.new(manifest).result(binding)

# create the login needed for test
apply_manifest_on(host, pp) do |r|
expect(r.stdout).not_to match(/Error/)
end
end

it 'removes an existing login' do
ensure_value = 'absent'
pp = ERB.new(manifest).result(binding)
name_query = "SELECT name FROM sys.server_principals WHERE name = '#{login_name}'"

apply_manifest_on(host, pp) do |r|
expect(r.stdout).not_to match(/Error/)
expect{run_sql_query(host, {:query => name_query})}.to raise_error(RuntimeError, no_results_error)
end
end

end

context 'mutate a login' do

login_name = 'Denali'
db_fixture = 'professor_kapp'

before(:all) do
pp = ERB.new(manifest).result(binding)

# create the login to test
apply_manifest_on(host, pp) do |r|
expect(r.stdout).not_to match(/Error/)
end

# create a DB to use as a fixture
opts = {:query => "CREATE DATABASE #{db_fixture}", :no_rows_expected => true}
run_sql_query(host, opts) do |r|
expect(r.stdout).not_to match(/Error/)
end
end

after(:all) do
login_opts = {:query => "DROP LOGIN #{login_name}", :no_rows_expected => true}
db_opts = {:query => "DROP DATABASE #{db_fixture}", :no_rows_expected => true}

# delete the login
run_sql_query(host, login_opts) do |r|
expect(r.stdout).not_to match(/Error/)
end

# cleanup fixture DB
run_sql_query(host, db_opts) do |r|
expect(r.stdout).not_to match(/Error/)
end
end

it 'disables the login' do
disabled_query = "SELECT SP.is_disabled FROM sys.server_principals AS SP WHERE name = '#{login_name}'"
disabled = true
pp = ERB.new(manifest).result(binding)

apply_manifest_on(host, pp) do |r|
expect(r.stdout).not_to match(/Error/)
end

run_sql_query(host, {:query => disabled_query}) do |r|
expect(r.stdout).not_to match(/Error/)
expect(r.stdout).to match(/^1/)
end
end

it 'changes the default database' do
default_db_query = "SELECT SP.default_database_name FROM sys.server_principals AS SP WHERE name = '#{login_name}'"
default_database = db_fixture
pp = ERB.new(manifest).result(binding)

apply_manifest_on(host, pp) do |r|
expect(r.stdout).not_to match(/Error/)
end

run_sql_query(host, {:query => default_db_query}) do |r|
expect(r.stdout).not_to match(/Error/)
expect(r.stdout).to match(/#{db_fixture}/)
end
end

it 'changes the default language' do
language_query = "SELECT SP.default_language_name FROM sys.server_principals AS SP WHERE name = '#{login_name}'"
default_language = 'german'
pp = ERB.new(manifest).result(binding)

apply_manifest_on(host, pp) do |r|
expect(r.stdout).not_to match(/Error/)
end

run_sql_query(host, {:query => language_query}) do |r|
expect(r.stdout).not_to match(/Error/)
expect(r.stdout).to match(/#{default_language}/)
end
end

end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
QA_RESOURCE_ROOT = "http://int-resources.ops.puppetlabs.net/QA_resources/microsoft_sql/iso/"
SQL_2014_ISO = "SQLServer2014-x64-ENU.iso"
SQL_2012_ISO = "SQLServer2012SP1-FullSlipstream-ENU-x64.iso"
SQL_ADMIN_USER = 'sa'
SQL_ADMIN_PASS = 'Pupp3t1@'

RSpec.configure do |c|
# Readable test descriptions
Expand Down
27 changes: 19 additions & 8 deletions spec/sql_testing_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ def install_sqlserver(host, opts = {})
apply_manifest_on(host, pp)
end

def run_sql_query(host, opts = {})
def run_sql_query(host, opts = {}, &block)
# runs an arbitrary SQL command
opts[:expected_row_count] ||= 1
query = opts[:query]
sql_admin_pass = opts[:sql_admin_pass]
sql_admin_user = opts[:sql_admin_user]
sql_admin_pass = opts[:sql_admin_pass] ||= SQL_ADMIN_PASS
sql_admin_user = opts[:sql_admin_user] ||= SQL_ADMIN_USER
environment_path = '/cygdrive/c/Program Files/Microsoft SQL Server/Client SDK/ODBC/110/Tools/Binn:/cygdrive/c/Program Files/Microsoft SQL Server/110/Tools/Binn'
tmpfile = host.tmpfile('should_contain_query.sql')
create_remote_file(host, tmpfile, query + "\n")
Expand All @@ -57,11 +57,22 @@ def run_sql_query(host, opts = {})
sqlcmd.exe -U #{sql_admin_user} -P #{sql_admin_pass} -h-1 -W -s "|" -i \"#{tmpfile}\"
sql_query
on(host, sqlcmd_query, :environment => {"PATH" => environment_path}) do |result|
match = /(\d*) rows affected/.match(result.stdout)
raise 'Could not match number of rows for SQL query' unless match
rows_observed = match[1]
error_message = "Expected #{opts[:expected_row_count]} rows but observed #{rows_observed}"
raise error_message unless opts[:expected_row_count] == rows_observed.to_i
unless opts[:no_rows_expected]
# match an expeted row count
match = /(\d*) rows affected/.match(result.stdout)
raise 'Could not match number of rows for SQL query' unless match
rows_observed = match[1]
error_message = "Expected #{opts[:expected_row_count]} rows but observed #{rows_observed}"
raise error_message unless opts[:expected_row_count] == rows_observed.to_i
end
if block_given?
case block.arity
when 0
yield self
else
yield result
end
end
end
end

Expand Down