Skip to content

Commit e52e9f0

Browse files
authored
Merge pull request #220 from glennsarti/modules-2386-fix-as-sysadmin
(MODULES-2386) Using as_sysadmin_accounts without AS feature should error
2 parents e18fb51 + 27ba6d1 commit e52e9f0

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lib/puppet/provider/sqlserver_instance/mssql.rb

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def create
7171
warn "Uninstalling all features for instance #{@resource[:name]} because an empty array was passed, please use ensure absent instead."
7272
destroy
7373
else
74+
unless @resource[:as_sysadmin_accounts].nil? || @resource[:features].include?('AS')
75+
fail('The parameter as_sysadmin_accounts was specified however the AS feature was not included in the installed features. Either remove the as_sysadmin_accounts parameter or add AS as a feature to the instance.')
76+
end
77+
7478
instance_version = PuppetX::Sqlserver::ServerHelper.sql_version_from_install_source(@resource[:source])
7579
Puppet.debug("Installation source detected as version #{instance_version}") unless instance_version.nil?
7680

spec/unit/puppet/provider/sqlserver_instance_spec.rb

+51
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,45 @@ def stub_uninstall(args, installed_features, exit_code = 0)
9090
}
9191
end
9292

93+
shared_examples 'create_failure' do |exit_code, error_matcher|
94+
it {
95+
execute_args = args.merge(munged_values)
96+
@resource = Puppet::Type::Sqlserver_instance.new(args)
97+
@provider = provider_class.new(@resource)
98+
99+
stub_powershell_call(subject)
100+
stub_source_which_call args[:source]
101+
102+
cmd_args = ["#{execute_args[:source]}/setup.exe",
103+
"/ACTION=install",
104+
'/Q',
105+
'/IACCEPTSQLSERVERLICENSETERMS',
106+
"/INSTANCENAME=#{execute_args[:name]}",
107+
"/FEATURES=#{execute_args[:features].join(',')}",]
108+
(execute_args.keys - %w( ensure loglevel features name source sql_sysadmin_accounts sql_security_mode install_switches).map(&:to_sym)).sort.collect do |key|
109+
cmd_args << "/#{key.to_s.gsub(/_/, '').upcase}=\"#{@resource[key]}\""
110+
end
111+
if execute_args[:sql_security_mode]
112+
cmd_args << "/SECURITYMODE=SQL"
113+
end
114+
115+
# wrap each arg in doublequotes
116+
admin_args = execute_args[:sql_sysadmin_accounts].map { |a| "\"#{a}\"" }
117+
# prepend first arg only with CLI switch
118+
admin_args[0] = "/SQLSYSADMINACCOUNTS=" + admin_args[0]
119+
cmd_args += admin_args
120+
121+
additional_install_switches.each do |switch|
122+
cmd_args << switch
123+
end
124+
125+
@provider.stubs(:warn).with(anything).times(0)
126+
127+
result = Puppet::Util::Execution::ProcessOutput.new('', exit_code || 0)
128+
Puppet::Util::Execution.stubs(:execute).with(cmd_args.compact, failonfail: false).returns(result)
129+
expect{ @provider.create }.to raise_error(error_matcher)
130+
}
131+
end
93132

94133
shared_examples 'destroy' do |exit_code, warning_matcher|
95134
it {
@@ -127,6 +166,18 @@ def stub_uninstall(args, installed_features, exit_code = 0)
127166
end
128167
end
129168

169+
describe 'it should raise error if as_sysadmin_accounts is specified without AS feature' do
170+
it_behaves_like 'create_failure', 1, /as_sysadmin_accounts was specified however the AS feature was not included/i do
171+
args = get_basic_args
172+
args[:features] = ['SQLEngine']
173+
args[:as_sysadmin_accounts] = 'username'
174+
175+
let(:args) { args }
176+
munged = {:features => Array.new(args[:features])}
177+
let(:munged_values) { munged }
178+
end
179+
end
180+
130181
describe 'it should raise warning on install when 1641 exit code returned' do
131182
it_behaves_like 'create', 1641, /reboot initiated/i do
132183
args = get_basic_args

0 commit comments

Comments
 (0)