Skip to content
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

(MODULES-2392) Automated Tests for sqlserver::config #140

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
142 changes: 142 additions & 0 deletions spec/acceptance/sqlserver_config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
require 'spec_helper_acceptance'
require 'securerandom'
require 'erb'

host = find_only_one("sql_host")

# Get instance name
INST_NAME = ("MSSQL" + SecureRandom.hex(4)).upcase

# Get database name
DB_NAME = ("DB" + SecureRandom.hex(4)).upcase

describe "sqlserver::config test", :node => host do
version = host['sql_version'].to_s

def ensure_sqlserver_instance(host, ensure_val = 'present')
create_new_instance= <<-MANIFEST
sqlserver_instance{'#{INST_NAME}':
ensure => '#{ensure_val}',
source => 'H:',
features => [ 'SQL' ],
sql_sysadmin_accounts => ['Administrator'],
security_mode => 'SQL',
sa_pwd => 'Pupp3t1@',
}
MANIFEST

apply_manifest_on(host, create_new_instance) do |r|
expect(r.stderr).not_to match(/Error/i)
end
end

context "can create sqlserver::config" do

before(:all) do
# Create new instance
ensure_sqlserver_instance(host)

# get credentials for new config
@admin_user = "admin" + SecureRandom.hex(2)
@admin_pass = 'Pupp3t1@'

# get database user
@db_user = "dbuser" + SecureRandom.hex(2)
end

after(:all) do
# remove the newly created instance
ensure_sqlserver_instance(host, 'absent')
end

it "Create New Admin Login:" do
create_new_login = <<-MANIFEST
sqlserver::config{'#{INST_NAME}':
instance_name => '#{INST_NAME}',
admin_user => 'sa',
admin_pass => 'Pupp3t1@',
}
sqlserver::login{'#{@admin_user}':
instance => '#{INST_NAME}',
login_type => 'SQL_LOGIN',
login => '#{@admin_user}',
password => '#{@admin_pass}',
svrroles => {'sysadmin' => 1},
}
MANIFEST
apply_manifest_on(host, create_new_login) do |r|
expect(r.stderr).not_to match(/Error/i)
end
end

it "Validate New Config WITH using instance_name in sqlserver::config" do
pp = <<-MANIFEST
sqlserver::config{'#{INST_NAME}':
admin_user => '#{@admin_user}',
admin_pass => '#{@admin_pass}',
instance_name => '#{INST_NAME}',
}
sqlserver::database{'#{DB_NAME}':
instance => '#{INST_NAME}',
}
MANIFEST
apply_manifest_on(host, pp) do |r|
expect(r.stderr).not_to match(/Error/i)
end
end

it "Validate new login and database actualy created" do
hostname = host.hostname
query = "USE #{DB_NAME};"

output = run_sql_query(host, {:query => query, :server => hostname, :instance => INST_NAME, :sql_admin_user => @admin_user, :sql_admin_pass => @admin_pass})
expect(output).to match(/Changed database context to '#{Regexp.new(DB_NAME)}'/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reasoning for having a Regexp within a Regexp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regexp within Regex is the only way I know to get it work, if not declare like that, it won't pass the variable #{DB_NAME} into the expression

end

it "Validate New Config WITHOUT using instance_name in sqlserver::config" do
pp = <<-MANIFEST
sqlserver::config{'#{INST_NAME}':
admin_user => '#{@admin_user}',
admin_pass => '#{@admin_pass}',
}
sqlserver::database{'#{DB_NAME}':
instance => '#{INST_NAME}',
}
MANIFEST
apply_manifest_on(host, pp) do |r|
expect(r.stderr).not_to match(/Error/i)
end
end

it "Negative test: sqlserver::config without admin_user" do
pp = <<-MANIFEST
sqlserver::config{'#{INST_NAME}':
admin_pass => '#{@admin_pass}',
instance_name => '#{INST_NAME}',
}
sqlserver::database{'#{DB_NAME}':
instance => '#{INST_NAME}',
}
MANIFEST
apply_manifest_on(host, pp, {:acceptable_exit_codes => [0,1]}) do |r|
expect(r.stderr).to match(/Error: Must pass admin_user to Sqlserver/)

end
end

it "Negative test: sqlserver::config without admin_pass" do
pp = <<-MANIFEST
sqlserver::config{'#{INST_NAME}':
admin_user => '#{@admin_user}',
instance_name => '#{INST_NAME}',
}
sqlserver::database{'#{DB_NAME}':
instance => '#{INST_NAME}',
}
MANIFEST
apply_manifest_on(host, pp, {:acceptable_exit_codes => [0,1]}) do |r|
expect(r.stderr).to match(/Error: Must pass admin_pass to Sqlserver/)
end
end
end
end
38 changes: 12 additions & 26 deletions spec/sql_testing_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,21 @@ def install_sqlserver(host, opts = {})
apply_manifest_on(host, pp)
end

def run_sql_query(host, opts = {}, &block)
# runs an arbitrary SQL command
opts[:expected_row_count] ||= 1
def run_sql_query(host, opts = {})
query = opts[:query]
server = opts[:server]
instance = opts[:instance]
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")
tmpfile.gsub!("/", "\\")
sqlcmd_query = <<-sql_query
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|

unless opts[:expected_row_count] == 0
# 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
case block.arity
when 0
yield self
else
yield result
end

powershell = <<-EOS
$Env:Path +=\";C:\\Program Files\\Microsoft SQL Server\\Client SDK\\ODBC\\110\\Tools\\Binn;C:\\Program Files\\Microsoft SQL Server\\110\\Tools\\Binn\\"
sqlcmd.exe -S #{server}\\#{instance} -U #{sql_admin_user} -P #{sql_admin_pass} -Q \"#{query}\"
EOS
create_remote_file(host,"tmp.ps1", powershell)

on(host, "powershell -NonInteractive -NoLogo -File \"C:\\cygwin64\\home\\Administrator\\tmp.ps1\"") do |r|
return r.stdout
end
end

Expand Down