|
| 1 | +require 'spec_helper_acceptance' |
| 2 | +require 'ERB' |
| 3 | + |
| 4 | +%w(2012 2014).each do |version| |
| 5 | + host = find_only_one("sql#{version}") |
| 6 | + describe "sqlserver::login #{version}", :node => host do |
| 7 | + |
| 8 | + # An ERB template for a sqlserver::login |
| 9 | + manifest = <<-MANIFEST |
| 10 | + sqlserver::config{'MSSQLSERVER': |
| 11 | + admin_pass => '<%= SQL_ADMIN_PASS %>', |
| 12 | + admin_user => '<%= SQL_ADMIN_USER %>', |
| 13 | + } |
| 14 | + sqlserver::login{'<%= login_name %>': |
| 15 | + ensure => <%= defined?(ensure_value) ? ensure_value : 'present' %>, |
| 16 | + password => '<%= defined?(password) ? password : SQL_ADMIN_PASS %>', |
| 17 | + default_database => '<%= defined?(default_database) ? default_database : 'master' %>', |
| 18 | + default_language => '<%= defined?(default_language) ? default_language : 'us_english'%>', disabled => <%= defined?(disabled) ? disabled : false %>, |
| 19 | + } |
| 20 | + MANIFEST |
| 21 | + no_results_error = 'Expected 1 rows but observed 0' |
| 22 | + |
| 23 | + context 'create a sql login' do |
| 24 | + |
| 25 | + login_name = 'Gumby' |
| 26 | + |
| 27 | + after(:each) do |
| 28 | + opts = {:query => "DROP LOGIN #{login_name}", :no_rows_expected => true} |
| 29 | + |
| 30 | + # delete the login |
| 31 | + run_sql_query(host, opts) do |r| |
| 32 | + expect(r.stdout).not_to match(/Error/) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + it "should create a login" do |
| 37 | + name_query = "SELECT name FROM sys.server_principals WHERE name = '#{login_name}'" |
| 38 | + pp = ERB.new(manifest).result(binding) |
| 39 | + |
| 40 | + apply_manifest_on(host, pp) do |r| |
| 41 | + expect(r.stdout).not_to match(/Error/) |
| 42 | + end |
| 43 | + |
| 44 | + run_sql_query(host, {:query => name_query}) do |r| |
| 45 | + expect(r.stdout).not_to match(/Error/) |
| 46 | + expect(r.stdout).to match(/#{login_name}/) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + end |
| 51 | + |
| 52 | + context 'delete a login' do |
| 53 | + |
| 54 | + login_name = 'Pokey' |
| 55 | + |
| 56 | + before(:all) do |
| 57 | + pp = ERB.new(manifest).result(binding) |
| 58 | + |
| 59 | + # create the login needed for test |
| 60 | + apply_manifest_on(host, pp) do |r| |
| 61 | + expect(r.stdout).not_to match(/Error/) |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + it 'removes an existing login' do |
| 66 | + ensure_value = 'absent' |
| 67 | + pp = ERB.new(manifest).result(binding) |
| 68 | + |
| 69 | + apply_manifest_on(host, pp) do |r| |
| 70 | + expect(r.stdout).not_to match(/Error/) |
| 71 | + expect{run_sql_query(host, {:query => query})}.to raise_error(RuntimeError, no_results_error) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + end |
| 76 | + |
| 77 | + context 'mutate a login' do |
| 78 | + |
| 79 | + login_name = 'Denali' |
| 80 | + db_fixture = 'professor_kapp' |
| 81 | + |
| 82 | + before(:all) do |
| 83 | + pp = ERB.new(manifest).result(binding) |
| 84 | + |
| 85 | + # create the login to test |
| 86 | + apply_manifest_on(host, pp) do |r| |
| 87 | + expect(r.stdout).not_to match(/Error/) |
| 88 | + end |
| 89 | + |
| 90 | + # create a DB to use as a fixture |
| 91 | + opts = {:query => "CREATE DATABASE #{db_fixture}", :no_rows_expected => true} |
| 92 | + run_sql_query(host, opts) do |r| |
| 93 | + expect(r.stdout).not_to match(/Error/) |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + after(:all) do |
| 98 | + login_opts = {:query => "DROP LOGIN #{login_name}", :no_rows_expected => true} |
| 99 | + db_opts = {:query => "DROP DATABASE #{db_fixture}", :no_rows_expected => true} |
| 100 | + |
| 101 | + # delete the login |
| 102 | + run_sql_query(host, login_opts) do |r| |
| 103 | + expect(r.stdout).not_to match(/Error/) |
| 104 | + end |
| 105 | + |
| 106 | + # cleanup fixture DB |
| 107 | + run_sql_query(host, db_opts) do |r| |
| 108 | + expect(r.stdout).not_to match(/Error/) |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + it 'disables the login' do |
| 113 | + disabled_query = "SELECT SP.is_disabled FROM sys.server_principals AS SP WHERE name = '#{login_name}'" |
| 114 | + disabled = true |
| 115 | + pp = ERB.new(manifest).result(binding) |
| 116 | + |
| 117 | + apply_manifest_on(host, pp) do |r| |
| 118 | + expect(r.stdout).not_to match(/Error/) |
| 119 | + end |
| 120 | + |
| 121 | + run_sql_query(host, {:query => disabled_query}) do |r| |
| 122 | + expect(r.stdout).not_to match(/Error/) |
| 123 | + expect(r.stdout).to match(/^1/) |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + it 'changes the default database' do |
| 128 | + default_db_query = "SELECT SP.default_database_name FROM sys.server_principals AS SP WHERE name = '#{login_name}'" |
| 129 | + default_database = db_fixture |
| 130 | + pp = ERB.new(manifest).result(binding) |
| 131 | + |
| 132 | + apply_manifest_on(host, pp) do |r| |
| 133 | + expect(r.stdout).not_to match(/Error/) |
| 134 | + end |
| 135 | + |
| 136 | + run_sql_query(host, {:query => default_db_query}) do |r| |
| 137 | + expect(r.stdout).not_to match(/Error/) |
| 138 | + expect(r.stdout).to match(/#{db_fixture}/) |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + it 'changes the default language' do |
| 143 | + language_query = "SELECT SP.default_language_name FROM sys.server_principals AS SP WHERE name = '#{login_name}'" |
| 144 | + default_language = 'german' |
| 145 | + pp = ERB.new(manifest).result(binding) |
| 146 | + |
| 147 | + apply_manifest_on(host, pp) do |r| |
| 148 | + expect(r.stdout).not_to match(/Error/) |
| 149 | + end |
| 150 | + |
| 151 | + run_sql_query(host, {:query => language_query}) do |r| |
| 152 | + expect(r.stdout).not_to match(/Error/) |
| 153 | + expect(r.stdout).to match(/#{default_language}/) |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + end |
| 158 | + end |
| 159 | +end |
0 commit comments