From 27ba6d1fb2c57fcb39573ee8c2de156dc6fa1a25 Mon Sep 17 00:00:00 2001 From: Glenn Sarti Date: Mon, 19 Jun 2017 11:38:38 -0700 Subject: [PATCH] (MODULES-2386) Using as_sysadmin_accounts without AS feature should error Previously if the as_sysadmin_accounts parameter was set however the required AS feature was not in the feature list, then the SQL installation would proceed and not install AS. This commit changes the behaviour of the provider to fail the resource if the as_sysadmin_accounts is set in the sql_instance resource without the required AS feature in the feature list on creation. --- .../provider/sqlserver_instance/mssql.rb | 4 ++ .../provider/sqlserver_instance_spec.rb | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/lib/puppet/provider/sqlserver_instance/mssql.rb b/lib/puppet/provider/sqlserver_instance/mssql.rb index 78afe74b..c2f48223 100644 --- a/lib/puppet/provider/sqlserver_instance/mssql.rb +++ b/lib/puppet/provider/sqlserver_instance/mssql.rb @@ -71,6 +71,10 @@ def create warn "Uninstalling all features for instance #{@resource[:name]} because an empty array was passed, please use ensure absent instead." destroy else + unless @resource[:as_sysadmin_accounts].nil? || @resource[:features].include?('AS') + 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.') + end + instance_version = PuppetX::Sqlserver::ServerHelper.sql_version_from_install_source(@resource[:source]) Puppet.debug("Installation source detected as version #{instance_version}") unless instance_version.nil? diff --git a/spec/unit/puppet/provider/sqlserver_instance_spec.rb b/spec/unit/puppet/provider/sqlserver_instance_spec.rb index 7cb995ec..09ef7a34 100644 --- a/spec/unit/puppet/provider/sqlserver_instance_spec.rb +++ b/spec/unit/puppet/provider/sqlserver_instance_spec.rb @@ -90,6 +90,45 @@ def stub_uninstall(args, installed_features, exit_code = 0) } end + shared_examples 'create_failure' do |exit_code, error_matcher| + it { + execute_args = args.merge(munged_values) + @resource = Puppet::Type::Sqlserver_instance.new(args) + @provider = provider_class.new(@resource) + + stub_powershell_call(subject) + stub_source_which_call args[:source] + + cmd_args = ["#{execute_args[:source]}/setup.exe", + "/ACTION=install", + '/Q', + '/IACCEPTSQLSERVERLICENSETERMS', + "/INSTANCENAME=#{execute_args[:name]}", + "/FEATURES=#{execute_args[:features].join(',')}",] + (execute_args.keys - %w( ensure loglevel features name source sql_sysadmin_accounts sql_security_mode install_switches).map(&:to_sym)).sort.collect do |key| + cmd_args << "/#{key.to_s.gsub(/_/, '').upcase}=\"#{@resource[key]}\"" + end + if execute_args[:sql_security_mode] + cmd_args << "/SECURITYMODE=SQL" + end + + # wrap each arg in doublequotes + admin_args = execute_args[:sql_sysadmin_accounts].map { |a| "\"#{a}\"" } + # prepend first arg only with CLI switch + admin_args[0] = "/SQLSYSADMINACCOUNTS=" + admin_args[0] + cmd_args += admin_args + + additional_install_switches.each do |switch| + cmd_args << switch + end + + @provider.stubs(:warn).with(anything).times(0) + + result = Puppet::Util::Execution::ProcessOutput.new('', exit_code || 0) + Puppet::Util::Execution.stubs(:execute).with(cmd_args.compact, failonfail: false).returns(result) + expect{ @provider.create }.to raise_error(error_matcher) + } + end shared_examples 'destroy' do |exit_code, warning_matcher| it { @@ -127,6 +166,18 @@ def stub_uninstall(args, installed_features, exit_code = 0) end end + describe 'it should raise error if as_sysadmin_accounts is specified without AS feature' do + it_behaves_like 'create_failure', 1, /as_sysadmin_accounts was specified however the AS feature was not included/i do + args = get_basic_args + args[:features] = ['SQLEngine'] + args[:as_sysadmin_accounts] = 'username' + + let(:args) { args } + munged = {:features => Array.new(args[:features])} + let(:munged_values) { munged } + end + end + describe 'it should raise warning on install when 1641 exit code returned' do it_behaves_like 'create', 1641, /reboot initiated/i do args = get_basic_args