Merge pull request #559 from chef/chris-rock/vbscript

Add `vbscript` resource
This commit is contained in:
Dominik Richter 2016-03-20 10:54:35 +01:00
commit 1d7c110917
6 changed files with 110 additions and 4 deletions

View file

@ -96,6 +96,7 @@ require 'resources/service'
require 'resources/shadow'
require 'resources/ssh_conf'
require 'resources/user'
require 'resources/vbscript'
require 'resources/windows_feature'
require 'resources/xinetd'
require 'resources/yum'

54
lib/resources/vbscript.rb Normal file
View file

@ -0,0 +1,54 @@
# encoding: utf-8
# author: Christoph Hartmann
# author: Dominik Richter
module Inspec::Resources
# This resource allows users to run vbscript on windows machines. We decided
# not to use scriptcontrol, due to the fact that it works on 32 bit systems only:
# $script = new-object -comobject MSScriptControl.ScriptControl
# $script.language = "vbscript"
# $script.ExecuteStatement($Cmd)
#
# For that reason, we call csript.exe directy with the script. Vbscript is
# embedded in Powershell to ease the file transfer and reuse powershell
# encodedCommand since train does not allow file upload yet.
#
# We run cscript with /nologo option to get the expected output only with the
# version information.
#
# Since Windows does not delete tmp files automatically, we remove the VBScript
# after we executed it
# @see https://msdn.microsoft.com/en-us/library/aa364991.aspx
class VBScript < PowershellScript
name 'vbscript'
desc ''
example "
script = <<-EOH
# you vbscript
EOH
describe vbscript(script) do
its('stdout') { should eq 'output' }
end
"
def initialize(vbscript)
return skip_resource 'The `vbscript` resource is not supported on your OS yet.' unless inspec.os.windows?
cmd = <<-EOH
$vbscript = @"
#{vbscript}
"@
$filename = [System.IO.Path]::GetTempFileName() + ".vbs"
New-Item $filename -type file -force -value $vbscript | Out-Null
cscript.exe /nologo $filename
Remove-Item $filename | Out-Null
EOH
super(cmd)
end
def to_s
'Windows VBScript'
end
end
end

View file

@ -3,11 +3,29 @@ script = <<-EOH
Write-Output 'hello'
EOH
# Write-Output comes with a newline
describe powershell(script) do
its('stdout') { should eq 'hello' }
its('stdout') { should eq "hello\r\n" }
its('stderr') { should eq '' }
end
# legacy test
# legacy test with `script` resource
describe script(script) do
its('stdout') { should eq 'hello' }
its('stdout') { should eq "hello\r\n" }
its('stderr') { should eq '' }
end
# -NoNewLine only works in powershell 5
# @see https://blogs.technet.microsoft.com/heyscriptingguy/2015/08/07/the-powershell-5-nonewline-parameter/
describe powershell("'hello' | Write-Host -NoNewLine") do
its('stdout') { should eq 'hello' }
its('stderr') { should eq '' }
end
# test stderr
describe powershell("Write-Error \"error\"") do
its('stdout') { should eq '' }
# this is an xml error for now, if the script is run via WinRM
# @see https://github.com/WinRb/WinRM/issues/106
# its('stderr') { should eq 'error' }
end

View file

@ -0,0 +1,15 @@
# encoding: utf-8
# script that may have multiple lines
vbscript = <<-EOH
WScript.Echo "hello"
EOH
describe vbscript(vbscript) do
its('stdout') { should eq "hello\r\n" }
end
# ensure that we do not require a newline
describe vbscript("Wscript.Stdout.Write \"hello\"") do
its('stdout') { should eq 'hello' }
end

View file

@ -5,7 +5,7 @@
require 'helper'
require 'inspec/resource'
describe 'Inspec::Resources::Script' do
describe 'Inspec::Resources::Powershell' do
ps1_script = <<-EOH
# call help for get command

View file

@ -0,0 +1,18 @@
# encoding: utf-8
# author: Christoph Hartmann
# author: Dominik Richter
require 'helper'
require 'inspec/resource'
describe 'Inspec::Resources::VbScript' do
vb_script = <<-EOH
WScript.Echo "hello vbscript"
EOH
it 'check if `vbscript` for windows is properly generated ' do
resource = MockLoader.new(:windows).load_resource('vbscript', vb_script)
_(resource.command).must_equal 'powershell -encodedCommand JABQAHIAbwBnAHIAZQBzAHMAUAByAGUAZgBlAHIAZQBuAGMAZQA9ACcAUwBpAGwAZQBuAHQAbAB5AEMAbwBuAHQAaQBuAHUAZQAnADsAJAB2AGIAcwBjAHIAaQBwAHQAIAA9ACAAQAAiAAoAIAAgACAAIABXAFMAYwByAGkAcAB0AC4ARQBjAGgAbwAgACIAaABlAGwAbABvACAAdgBiAHMAYwByAGkAcAB0ACIACgAKACIAQAAKACQAZgBpAGwAZQBuAGEAbQBlACAAPQAgAFsAUwB5AHMAdABlAG0ALgBJAE8ALgBQAGEAdABoAF0AOgA6AEcAZQB0AFQAZQBtAHAARgBpAGwAZQBOAGEAbQBlACgAKQAgACsAIAAiAC4AdgBiAHMAIgAKAE4AZQB3AC0ASQB0AGUAbQAgACQAZgBpAGwAZQBuAGEAbQBlACAALQB0AHkAcABlACAAZgBpAGwAZQAgAC0AZgBvAHIAYwBlACAALQB2AGEAbAB1AGUAIAAkAHYAYgBzAGMAcgBpAHAAdAAgAHwAIABPAHUAdAAtAE4AdQBsAGwACgBjAHMAYwByAGkAcAB0AC4AZQB4AGUAIAAvAG4AbwBsAG8AZwBvACAAJABmAGkAbABlAG4AYQBtAGUACgBSAGUAbQBvAHYAZQAtAEkAdABlAG0AIAAkAGYAaQBsAGUAbgBhAG0AZQAgAHwAIABPAHUAdAAtAE4AdQBsAGwACgA='
end
end