Skip to content

Commit 78077b9

Browse files
committed
Merge pull request #140 from phongdly/MODULES-2392/Create_Automated_Test_sqlserver-config
(MODULES-2392) Automated Tests for sqlserver::config
2 parents b157d3e + 938850c commit 78077b9

File tree

2 files changed

+154
-26
lines changed

2 files changed

+154
-26
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
require 'spec_helper_acceptance'
2+
require 'securerandom'
3+
require 'erb'
4+
5+
host = find_only_one("sql_host")
6+
7+
# Get instance name
8+
INST_NAME = ("MSSQL" + SecureRandom.hex(4)).upcase
9+
10+
# Get database name
11+
DB_NAME = ("DB" + SecureRandom.hex(4)).upcase
12+
13+
describe "sqlserver::config test", :node => host do
14+
version = host['sql_version'].to_s
15+
16+
def ensure_sqlserver_instance(host, ensure_val = 'present')
17+
create_new_instance= <<-MANIFEST
18+
sqlserver_instance{'#{INST_NAME}':
19+
ensure => '#{ensure_val}',
20+
source => 'H:',
21+
features => [ 'SQL' ],
22+
sql_sysadmin_accounts => ['Administrator'],
23+
security_mode => 'SQL',
24+
sa_pwd => 'Pupp3t1@',
25+
}
26+
MANIFEST
27+
28+
apply_manifest_on(host, create_new_instance) do |r|
29+
expect(r.stderr).not_to match(/Error/i)
30+
end
31+
end
32+
33+
context "can create sqlserver::config" do
34+
35+
before(:all) do
36+
# Create new instance
37+
ensure_sqlserver_instance(host)
38+
39+
# get credentials for new config
40+
@admin_user = "admin" + SecureRandom.hex(2)
41+
@admin_pass = 'Pupp3t1@'
42+
43+
# get database user
44+
@db_user = "dbuser" + SecureRandom.hex(2)
45+
end
46+
47+
after(:all) do
48+
# remove the newly created instance
49+
ensure_sqlserver_instance(host, 'absent')
50+
end
51+
52+
it "Create New Admin Login:" do
53+
create_new_login = <<-MANIFEST
54+
sqlserver::config{'#{INST_NAME}':
55+
instance_name => '#{INST_NAME}',
56+
admin_user => 'sa',
57+
admin_pass => 'Pupp3t1@',
58+
}
59+
sqlserver::login{'#{@admin_user}':
60+
instance => '#{INST_NAME}',
61+
login_type => 'SQL_LOGIN',
62+
login => '#{@admin_user}',
63+
password => '#{@admin_pass}',
64+
svrroles => {'sysadmin' => 1},
65+
}
66+
MANIFEST
67+
apply_manifest_on(host, create_new_login) do |r|
68+
expect(r.stderr).not_to match(/Error/i)
69+
end
70+
end
71+
72+
it "Validate New Config WITH using instance_name in sqlserver::config" do
73+
pp = <<-MANIFEST
74+
sqlserver::config{'#{INST_NAME}':
75+
admin_user => '#{@admin_user}',
76+
admin_pass => '#{@admin_pass}',
77+
instance_name => '#{INST_NAME}',
78+
}
79+
sqlserver::database{'#{DB_NAME}':
80+
instance => '#{INST_NAME}',
81+
}
82+
MANIFEST
83+
apply_manifest_on(host, pp) do |r|
84+
expect(r.stderr).not_to match(/Error/i)
85+
end
86+
end
87+
88+
it "Validate new login and database actualy created" do
89+
hostname = host.hostname
90+
query = "USE #{DB_NAME};"
91+
92+
output = run_sql_query(host, {:query => query, :server => hostname, :instance => INST_NAME, :sql_admin_user => @admin_user, :sql_admin_pass => @admin_pass})
93+
expect(output).to match(/Changed database context to '#{Regexp.new(DB_NAME)}'/)
94+
end
95+
96+
it "Validate New Config WITHOUT using instance_name in sqlserver::config" do
97+
pp = <<-MANIFEST
98+
sqlserver::config{'#{INST_NAME}':
99+
admin_user => '#{@admin_user}',
100+
admin_pass => '#{@admin_pass}',
101+
}
102+
sqlserver::database{'#{DB_NAME}':
103+
instance => '#{INST_NAME}',
104+
}
105+
MANIFEST
106+
apply_manifest_on(host, pp) do |r|
107+
expect(r.stderr).not_to match(/Error/i)
108+
end
109+
end
110+
111+
it "Negative test: sqlserver::config without admin_user" do
112+
pp = <<-MANIFEST
113+
sqlserver::config{'#{INST_NAME}':
114+
admin_pass => '#{@admin_pass}',
115+
instance_name => '#{INST_NAME}',
116+
}
117+
sqlserver::database{'#{DB_NAME}':
118+
instance => '#{INST_NAME}',
119+
}
120+
MANIFEST
121+
apply_manifest_on(host, pp, {:acceptable_exit_codes => [0,1]}) do |r|
122+
expect(r.stderr).to match(/Error: Must pass admin_user to Sqlserver/)
123+
124+
end
125+
end
126+
127+
it "Negative test: sqlserver::config without admin_pass" do
128+
pp = <<-MANIFEST
129+
sqlserver::config{'#{INST_NAME}':
130+
admin_user => '#{@admin_user}',
131+
instance_name => '#{INST_NAME}',
132+
}
133+
sqlserver::database{'#{DB_NAME}':
134+
instance => '#{INST_NAME}',
135+
}
136+
MANIFEST
137+
apply_manifest_on(host, pp, {:acceptable_exit_codes => [0,1]}) do |r|
138+
expect(r.stderr).to match(/Error: Must pass admin_pass to Sqlserver/)
139+
end
140+
end
141+
end
142+
end

spec/sql_testing_helpers.rb

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,21 @@ def install_sqlserver(host, opts = {})
4343
apply_manifest_on(host, pp)
4444
end
4545

46-
def run_sql_query(host, opts = {}, &block)
47-
# runs an arbitrary SQL command
48-
opts[:expected_row_count] ||= 1
46+
def run_sql_query(host, opts = {})
4947
query = opts[:query]
48+
server = opts[:server]
49+
instance = opts[:instance]
5050
sql_admin_pass = opts[:sql_admin_pass] ||= SQL_ADMIN_PASS
5151
sql_admin_user = opts[:sql_admin_user] ||= SQL_ADMIN_USER
52-
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'
53-
tmpfile = host.tmpfile('should_contain_query.sql')
54-
create_remote_file(host, tmpfile, query + "\n")
55-
tmpfile.gsub!("/", "\\")
56-
sqlcmd_query = <<-sql_query
57-
sqlcmd.exe -U #{sql_admin_user} -P #{sql_admin_pass} -h-1 -W -s "|" -i \"#{tmpfile}\"
58-
sql_query
59-
on(host, sqlcmd_query, :environment => {"PATH" => environment_path}) do |result|
60-
61-
unless opts[:expected_row_count] == 0
62-
# match an expeted row count
63-
match = /(\d*) rows affected/.match(result.stdout)
64-
raise 'Could not match number of rows for SQL query' unless match
65-
rows_observed = match[1]
66-
error_message = "Expected #{opts[:expected_row_count]} rows but observed #{rows_observed}"
67-
raise error_message unless opts[:expected_row_count] == rows_observed.to_i
68-
end
69-
case block.arity
70-
when 0
71-
yield self
72-
else
73-
yield result
74-
end
52+
53+
powershell = <<-EOS
54+
$Env:Path +=\";C:\\Program Files\\Microsoft SQL Server\\Client SDK\\ODBC\\110\\Tools\\Binn;C:\\Program Files\\Microsoft SQL Server\\110\\Tools\\Binn\\"
55+
sqlcmd.exe -S #{server}\\#{instance} -U #{sql_admin_user} -P #{sql_admin_pass} -Q \"#{query}\"
56+
EOS
57+
create_remote_file(host,"tmp.ps1", powershell)
58+
59+
on(host, "powershell -NonInteractive -NoLogo -File \"C:\\cygwin64\\home\\Administrator\\tmp.ps1\"") do |r|
60+
return r.stdout
7561
end
7662
end
7763

0 commit comments

Comments
 (0)