|
| 1 | +require 'spec_helper_acceptance' |
| 2 | +require 'securerandom' |
| 3 | +require 'erb' |
| 4 | + |
| 5 | +host = find_only_one("sql_host") |
| 6 | + |
| 7 | +# Get instance name |
| 8 | +INST_NAME = ("MSSQL" + SecureRandom.hex(4)).upcase |
| 9 | + |
| 10 | +# Get database name |
| 11 | +DB_NAME = ("DB" + SecureRandom.hex(4)).upcase |
| 12 | + |
| 13 | +describe "sqlserver::config test", :node => host do |
| 14 | + version = host['sql_version'].to_s |
| 15 | + |
| 16 | + def ensure_sqlserver_instance(host, ensure_val = 'present') |
| 17 | + create_new_instance= <<-MANIFEST |
| 18 | + sqlserver_instance{'#{INST_NAME}': |
| 19 | + ensure => '#{ensure_val}', |
| 20 | + source => 'H:', |
| 21 | + features => [ 'SQL' ], |
| 22 | + sql_sysadmin_accounts => ['Administrator'], |
| 23 | + security_mode => 'SQL', |
| 24 | + sa_pwd => 'Pupp3t1@', |
| 25 | + } |
| 26 | + MANIFEST |
| 27 | + |
| 28 | + apply_manifest_on(host, create_new_instance) do |r| |
| 29 | + expect(r.stderr).not_to match(/Error/i) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + context "can create sqlserver::config" do |
| 34 | + |
| 35 | + before(:all) do |
| 36 | + # Create new instance |
| 37 | + ensure_sqlserver_instance(host) |
| 38 | + |
| 39 | + # get credentials for new config |
| 40 | + @admin_user = "admin" + SecureRandom.hex(2) |
| 41 | + @admin_pass = 'Pupp3t1@' |
| 42 | + |
| 43 | + # get database user |
| 44 | + @db_user = "dbuser" + SecureRandom.hex(2) |
| 45 | + end |
| 46 | + |
| 47 | + after(:all) do |
| 48 | + # remove the newly created instance |
| 49 | + ensure_sqlserver_instance(host, 'absent') |
| 50 | + end |
| 51 | + |
| 52 | + it "Create New Admin Login:" do |
| 53 | + create_new_login = <<-MANIFEST |
| 54 | + sqlserver::config{'#{INST_NAME}': |
| 55 | + instance_name => '#{INST_NAME}', |
| 56 | + admin_user => 'sa', |
| 57 | + admin_pass => 'Pupp3t1@', |
| 58 | + } |
| 59 | + sqlserver::login{'#{@admin_user}': |
| 60 | + instance => '#{INST_NAME}', |
| 61 | + login_type => 'SQL_LOGIN', |
| 62 | + login => '#{@admin_user}', |
| 63 | + password => '#{@admin_pass}', |
| 64 | + svrroles => {'sysadmin' => 1}, |
| 65 | + } |
| 66 | + MANIFEST |
| 67 | + apply_manifest_on(host, create_new_login) do |r| |
| 68 | + expect(r.stderr).not_to match(/Error/i) |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + it "Validate New Config WITH using instance_name in sqlserver::config" do |
| 73 | + pp = <<-MANIFEST |
| 74 | + sqlserver::config{'#{INST_NAME}': |
| 75 | + admin_user => '#{@admin_user}', |
| 76 | + admin_pass => '#{@admin_pass}', |
| 77 | + instance_name => '#{INST_NAME}', |
| 78 | + } |
| 79 | + sqlserver::database{'#{DB_NAME}': |
| 80 | + instance => '#{INST_NAME}', |
| 81 | + } |
| 82 | + MANIFEST |
| 83 | + apply_manifest_on(host, pp) do |r| |
| 84 | + expect(r.stderr).not_to match(/Error/i) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + it "Validate new login and database actualy created" do |
| 89 | + hostname = host.hostname |
| 90 | + query = "USE #{DB_NAME};" |
| 91 | + |
| 92 | + output = run_sql_query(host, {:query => query, :server => hostname, :instance => INST_NAME, :sql_admin_user => @admin_user, :sql_admin_pass => @admin_pass}) |
| 93 | + expect(output).to match(/Changed database context to '#{Regexp.new(DB_NAME)}'/) |
| 94 | + end |
| 95 | + |
| 96 | + it "Validate New Config WITHOUT using instance_name in sqlserver::config" do |
| 97 | + pp = <<-MANIFEST |
| 98 | + sqlserver::config{'#{INST_NAME}': |
| 99 | + admin_user => '#{@admin_user}', |
| 100 | + admin_pass => '#{@admin_pass}', |
| 101 | + } |
| 102 | + sqlserver::database{'#{DB_NAME}': |
| 103 | + instance => '#{INST_NAME}', |
| 104 | + } |
| 105 | + MANIFEST |
| 106 | + apply_manifest_on(host, pp) do |r| |
| 107 | + expect(r.stderr).not_to match(/Error/i) |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + it "Negative test: sqlserver::config without admin_user" do |
| 112 | + pp = <<-MANIFEST |
| 113 | + sqlserver::config{'#{INST_NAME}': |
| 114 | + admin_pass => '#{@admin_pass}', |
| 115 | + instance_name => '#{INST_NAME}', |
| 116 | + } |
| 117 | + sqlserver::database{'#{DB_NAME}': |
| 118 | + instance => '#{INST_NAME}', |
| 119 | + } |
| 120 | + MANIFEST |
| 121 | + apply_manifest_on(host, pp, {:acceptable_exit_codes => [0,1]}) do |r| |
| 122 | + expect(r.stderr).to match(/Error: Must pass admin_user to Sqlserver/) |
| 123 | + |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + it "Negative test: sqlserver::config without admin_pass" do |
| 128 | + pp = <<-MANIFEST |
| 129 | + sqlserver::config{'#{INST_NAME}': |
| 130 | + admin_user => '#{@admin_user}', |
| 131 | + instance_name => '#{INST_NAME}', |
| 132 | + } |
| 133 | + sqlserver::database{'#{DB_NAME}': |
| 134 | + instance => '#{INST_NAME}', |
| 135 | + } |
| 136 | + MANIFEST |
| 137 | + apply_manifest_on(host, pp, {:acceptable_exit_codes => [0,1]}) do |r| |
| 138 | + expect(r.stderr).to match(/Error: Must pass admin_pass to Sqlserver/) |
| 139 | + end |
| 140 | + end |
| 141 | + end |
| 142 | +end |
0 commit comments