|
| 1 | +require 'spec_helper' |
| 2 | +require 'mocha' |
| 3 | + |
| 4 | + |
| 5 | +RSpec.describe Puppet::Type.type(:sqlserver_tsql).provider(:mssql) do |
| 6 | + subject { described_class } |
| 7 | + let(:config) { {:admin_user => 'sa', :admin_pass => 'Pupp3t1@', :instance_name => 'MSSQLSERVER'} } |
| 8 | + |
| 9 | + def stub_open_and_run(query, config) |
| 10 | + sqlconn = mock() |
| 11 | + sqlconn.expects(:open_and_run_command).with(gen_query(query), config) |
| 12 | + PuppetX::Sqlserver::SqlConnection.expects(:new).returns(sqlconn) |
| 13 | + end |
| 14 | + |
| 15 | + def create_sqlserver_tsql(args) |
| 16 | + @resource = Puppet::Type::Sqlserver_tsql.new(args) |
| 17 | + @provider = subject.new(@resource) |
| 18 | + end |
| 19 | + |
| 20 | + def stub_get_instance_config(config) |
| 21 | + @provider.expects(:get_config).returns(config) |
| 22 | + end |
| 23 | + |
| 24 | + def gen_query(query) |
| 25 | + <<-PP |
| 26 | +BEGIN TRY |
| 27 | + #{query} |
| 28 | +END TRY |
| 29 | +BEGIN CATCH |
| 30 | + DECLARE @msg as VARCHAR(max); |
| 31 | + SELECT @msg = 'THROW CAUGHT: ' + ERROR_MESSAGE(); |
| 32 | + THROW 51000, @msg, 10 |
| 33 | +END CATCH |
| 34 | + PP |
| 35 | + end |
| 36 | + |
| 37 | + context 'run_update' do |
| 38 | + describe 'against non master database' do |
| 39 | + it { |
| 40 | + create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :instance => 'MSSQLSERVER', :database => 'myDb'}) |
| 41 | + stub_get_instance_config(config) |
| 42 | + stub_open_and_run('whacka whacka', config.merge({:database => 'myDb'})) |
| 43 | + |
| 44 | + @provider.run_update |
| 45 | + } |
| 46 | + end |
| 47 | + describe 'against default database' do |
| 48 | + it { |
| 49 | + create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :instance => 'MSSQLSERVER'}) |
| 50 | + stub_get_instance_config(config) |
| 51 | + stub_open_and_run('whacka whacka', config.merge({:database => 'master'})) |
| 52 | + |
| 53 | + @provider.run_update |
| 54 | + } |
| 55 | + end |
| 56 | + end |
| 57 | + context 'run_check' do |
| 58 | + describe 'against default database' do |
| 59 | + it { |
| 60 | + create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :onlyif => 'fozy wozy', :instance => 'MSSQLSERVER'}) |
| 61 | + stub_get_instance_config(config) |
| 62 | + stub_open_and_run('fozy wozy', config.merge({:database => 'master'})) |
| 63 | + |
| 64 | + @provider.run_check |
| 65 | + } |
| 66 | + end |
| 67 | + describe 'against non master database' do |
| 68 | + it { |
| 69 | + create_sqlserver_tsql( |
| 70 | + {:title => 'runme', |
| 71 | + :command => 'whacka whacka', |
| 72 | + :onlyif => 'fozy wozy', |
| 73 | + :instance => 'MSSQLSERVER', |
| 74 | + :database => 'myDb'}) |
| 75 | + stub_get_instance_config(config) |
| 76 | + stub_open_and_run('fozy wozy', config.merge({:database => 'myDb'})) |
| 77 | + |
| 78 | + @provider.run_check |
| 79 | + } |
| 80 | + end |
| 81 | + end |
| 82 | +end |
0 commit comments