Skip to content

(MODULES-2453) Create Automated Tests for sqlserver_tsql #141

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions spec/acceptance/sqlserver_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def ensure_sqlserver_instance(host, ensure_val = 'present')
end
end

context "can create sqlserver::config" do
context "Testing sqlserver::config", {:testrail => ['89070', '89071', '89072', '89073']} do

before(:all) do
# Create new instance
Expand Down Expand Up @@ -87,10 +87,10 @@ def ensure_sqlserver_instance(host, ensure_val = 'present')

it "Validate new login and database actualy created" do
hostname = host.hostname
query = "USE #{DB_NAME};"
query = "USE #{DB_NAME}; SELECT * from master..sysdatabases WHERE name = '#{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)}'/)
run_sql_query(host, {:query => query, :server => hostname, :instance => INST_NAME, \
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the '' character is necessary here. Ruby ignores whitespace. I would recommend this.

run_sql_query_opts = {
  :query => "USE #{DB_NAME}; SELECT * from master..sysdatabases WHERE name = '#{DB_NAME}'",
  :sql_admin_user => @admin_user,
  :sql_admin_pass => @admin_pass,
  :expected_row_count => 1,
} 
run_sql_query(host, run_sql_query_opts)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @zreichert , it looks much cleaner with your recommendation. Modified and pushed

:sql_admin_user => @admin_user, :sql_admin_pass => @admin_pass, :expected_row_count => 1})
end

it "Validate New Config WITHOUT using instance_name in sqlserver::config" do
Expand Down Expand Up @@ -120,7 +120,6 @@ def ensure_sqlserver_instance(host, ensure_val = 'present')
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

Expand Down
175 changes: 175 additions & 0 deletions spec/acceptance/sqlserver_tsql_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
require 'spec_helper_acceptance'
require 'securerandom'
require 'erb'

host = find_only_one("sql_host")

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

#database user:
DB_LOGIN_USER = "loginuser" + SecureRandom.hex(2)

describe "sqlserver_tsql test", :node => host do

def ensure_sqlserver_database(host, ensure_val = 'present')
pp = <<-MANIFEST
sqlserver::config{'MSSQLSERVER':
admin_user => 'sa',
admin_pass => 'Pupp3t1@',
}
sqlserver::database{'#{DB_NAME}':
instance => 'MSSQLSERVER',
}
MANIFEST

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

context "Test sqlserver_tsql", {:testrail => ['89024', '89025', '89026', '89068', '89069']} do

before(:all) do
# Create new database
@table_name = 'Tables_' + SecureRandom.hex(3)
@query = "USE #{DB_NAME}; SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '#{@table_name}';"

ensure_sqlserver_database(host)
end

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

it "Run a simple tsql command via sqlserver_tsql:" do
pp = <<-MANIFEST
sqlserver::config{'MSSQLSERVER':
instance_name => 'MSSQLSERVER',
admin_user => 'sa',
admin_pass => 'Pupp3t1@',
}
sqlserver_tsql{'testsqlserver_tsql':
instance => 'MSSQLSERVER',
database => '#{DB_NAME}',
command => "CREATE TABLE #{@table_name} (id INT, name VARCHAR(20), email VARCHAR(20));",
}
MANIFEST
apply_manifest_on(host, pp) do |r|
expect(r.stderr).not_to match(/Error/i)
end

puts "validate the result of tsql command and table #{@table_name} should be created:"
run_sql_query_opts = {
:query => @query,
:sql_admin_user => @admin_user,
:sql_admin_pass => @admin_pass,
:expected_row_count => 1,
}
run_sql_query(host, run_sql_query_opts)
end

it "Run sqlserver_tsql WITH onlyif is true:" do
#Initilize a new table name:
@table_name = 'Table_' + SecureRandom.hex(3)
@query = "USE #{DB_NAME}; SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '#{@table_name}';"
pp = <<-MANIFEST
sqlserver::config{'MSSQLSERVER':
instance_name => 'MSSQLSERVER',
admin_user => 'sa',
admin_pass => 'Pupp3t1@',
}
sqlserver_tsql{'testsqlserver_tsql':
instance => 'MSSQLSERVER',
database => '#{DB_NAME}',
command => "CREATE TABLE #{@table_name} (id INT, name VARCHAR(20), email VARCHAR(20));",
onlyif => "IF (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES) < 10000"
}
MANIFEST

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

puts "Validate #{@table_name} is successfully created:"
run_sql_query_opts = {
:query => @query,
:sql_admin_user => @admin_user,
:sql_admin_pass => @admin_pass,
:expected_row_count => 1,
}
run_sql_query(host, run_sql_query_opts)
end

it "Run sqlserver_tsql WITH onlyif is false:" do
#Initilize a new table name:
@table_name = 'Table_' + SecureRandom.hex(3)
@query = "USE #{DB_NAME}; SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '#{@table_name}';"
pp = <<-MANIFEST
sqlserver::config{'MSSQLSERVER':
instance_name => 'MSSQLSERVER',
admin_user => 'sa',
admin_pass => 'Pupp3t1@',
}
sqlserver_tsql{'testsqlserver_tsql':
instance => 'MSSQLSERVER',
database => '#{DB_NAME}',
command => "CREATE TABLE #{@table_name} (id INT, name VARCHAR(20), email VARCHAR(20));",
onlyif => "IF (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES) > 10000
THROW 5300, 'Too many tables', 10"
}
MANIFEST

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

puts "Validate #{@table_name} is NOT created:"
run_sql_query_opts = {
:query => @query,
:sql_admin_user => @admin_user,
:sql_admin_pass => @admin_pass,
:expected_row_count => 0,
}
run_sql_query(host, run_sql_query_opts)
end

it "Negative test: Run tsql with invalid command:" do
pp = <<-MANIFEST
sqlserver::config{'MSSQLSERVER':
instance_name => 'MSSQLSERVER',
admin_user => 'sa',
admin_pass => 'Pupp3t1@',
}
sqlserver_tsql{'testsqlserver_tsql':
instance => 'MSSQLSERVER',
database => '#{DB_NAME}',
command => "invalid-tsql-command",
}
MANIFEST
apply_manifest_on(host, pp, {:acceptable_exit_codes => [0,1]}) do |r|
expect(r.stderr).to match(/Error/i)
end
end

it "Negative test: Run tsql with non-existing database:" do
@table_name = 'Table_' + SecureRandom.hex(3)
pp = <<-MANIFEST
sqlserver::config{'MSSQLSERVER':
instance_name => 'MSSQLSERVER',
admin_user => 'sa',
admin_pass => 'Pupp3t1@',
}
sqlserver_tsql{'testsqlserver_tsql':
instance => 'MSSQLSERVER',
database => 'Non-Existing-Database',
command => "CREATE TABLE #{@table_name} (id INT, name VARCHAR(20), email VARCHAR(20));",
}
MANIFEST
apply_manifest_on(host, pp, {:acceptable_exit_codes => [0,1]}) do |r|
expect(r.stderr).to match(/Error/i)
end
end
end
end
22 changes: 20 additions & 2 deletions spec/sql_testing_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def install_sqlserver(host, opts = {})
apply_manifest_on(host, pp)
end

def run_sql_query(host, opts = {})
def run_sql_query(host, opts = {}, &block)
query = opts[:query]
server = opts[:server]
instance = opts[:instance]
Expand All @@ -54,10 +54,28 @@ def run_sql_query(host, opts = {})
$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
# sqlcmd has problem authenticate to sqlserver if the instance is the default one MSSQLSERVER
# Below is a work-around for it (remove "-S server\instance" from the connection string)
if (instance == nil || instance == "MSSQLSERVER")
powershell.gsub!("-S #{server}\\#{instance}", "")
end

create_remote_file(host,"tmp.ps1", powershell)

on(host, "powershell -NonInteractive -NoLogo -File \"C:\\cygwin64\\home\\Administrator\\tmp.ps1\"") do |r|
return r.stdout
match = /(\d*) rows affected/.match(r.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 r
end
end
end

Expand Down