Skip to content

(FM-2791) Add database param to sqlserver_tsql #112

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
merged 1 commit into from
Jul 28, 2015
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
2 changes: 1 addition & 1 deletion lib/puppet/provider/sqlserver_tsql/mssql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def run(query)
debug("Running resource #{query} against #{resource[:instance]}")
config = get_config
config = get_config.merge({ :database => resource[:database] })
sqlconn = PuppetX::Sqlserver::SqlConnection.new

sqlconn.open_and_run_command(query, config)
Expand Down
7 changes: 7 additions & 0 deletions lib/puppet/type/sqlserver_tsql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def self.checks
end
end

newparam(:database) do
defaultto 'master'
validate do |value|
fail("Invalid database name #{value}") unless /^[[:word:]|#|@]+/.match(value)
end
end

desc 'SQL Query to run and only run if exits with non-zero'
newcheck(:onlyif, :parent => Puppet::Property::SqlserverTsql) do
#Runs in the event that our TSQL exits with anything other than 0
Expand Down
82 changes: 82 additions & 0 deletions spec/unit/puppet/provider/sqlserver__tsql_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'spec_helper'
require 'mocha'


RSpec.describe Puppet::Type.type(:sqlserver_tsql).provider(:mssql) do
subject { described_class }
let(:config) { {:admin_user => 'sa', :admin_pass => 'Pupp3t1@', :instance_name => 'MSSQLSERVER'} }

def stub_open_and_run(query, config)
sqlconn = mock()
sqlconn.expects(:open_and_run_command).with(gen_query(query), config)
PuppetX::Sqlserver::SqlConnection.expects(:new).returns(sqlconn)
end

def create_sqlserver_tsql(args)
@resource = Puppet::Type::Sqlserver_tsql.new(args)
@provider = subject.new(@resource)
end

def stub_get_instance_config(config)
@provider.expects(:get_config).returns(config)
end

def gen_query(query)
<<-PP
BEGIN TRY
#{query}
END TRY
BEGIN CATCH
DECLARE @msg as VARCHAR(max);
SELECT @msg = 'THROW CAUGHT: ' + ERROR_MESSAGE();
THROW 51000, @msg, 10
END CATCH
PP
end

context 'run_update' do
describe 'against non master database' do
it {
create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :instance => 'MSSQLSERVER', :database => 'myDb'})
stub_get_instance_config(config)
stub_open_and_run('whacka whacka', config.merge({:database => 'myDb'}))

@provider.run_update
}
end
describe 'against default database' do
it {
create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :instance => 'MSSQLSERVER'})
stub_get_instance_config(config)
stub_open_and_run('whacka whacka', config.merge({:database => 'master'}))

@provider.run_update
}
end
end
context 'run_check' do
describe 'against default database' do
it {
create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :onlyif => 'fozy wozy', :instance => 'MSSQLSERVER'})
stub_get_instance_config(config)
stub_open_and_run('fozy wozy', config.merge({:database => 'master'}))

@provider.run_check
}
end
describe 'against non master database' do
it {
create_sqlserver_tsql(
{:title => 'runme',
:command => 'whacka whacka',
:onlyif => 'fozy wozy',
:instance => 'MSSQLSERVER',
:database => 'myDb'})
stub_get_instance_config(config)
stub_open_and_run('fozy wozy', config.merge({:database => 'myDb'}))

@provider.run_check
}
end
end
end