我有一台包含 serverspec 测试的测试机器。这是一台 ubuntu 机器,我想使用 WinRM 对 Windows 机器运行测试。
执行测试时它立即失败,看起来它正在尝试在本地运行测试而不是针对远程主机。
我现在已经在 spec_helper.rb 中硬编码了主机名,但我会发布 Rakefile 以确保完整性
耙文件:
require 'rake'
require 'rspec/core/rake_task'
hosts = %w(
我的主机名)
task :spec => 'spec:all'
task :default => :spec
namespace :spec do
task :all => hosts.map {|h| 'spec:' + h.split('.')[0] }
hosts.each do |host|
short_name = host.split('.')[0]
role = short_name.match(/[^0-9]+/)[0]
desc "Run serverspec to #{host}"
RSpec::Core::RakeTask.new(short_name) do |t|
ENV['TARGET_HOST'] = host
t.pattern = "spec/base/*_spec.rb"
end
end
end
Spec_helper.rb
require 'serverspec'
require 'winrm'
set :backend, :winrm
user = "Packer"
pass = "Drj33l1n9"
endpoint = "http://hostname:5985/wsman"
opts = {
user: user,
password: pass,
endpoint: endpoint,
operation_timeout: 30,
}
winrm = WinRM::Connection.new ( opts)
Specinfra.configuration.winrm = winrm
Sample_spec.rb
require 'spec_helper'
set :os, :family => 'windows'
describe file('c:/windows') do
it { should be_directory }
end
describe file('c:/temp/testfile.txt') do
it { should be_file }
end
describe service('Schedule') do
it { should be_installed }
it { should be_enabled }
it { should be_running }
it { should have_start_mode("Automatic") }
end
describe port(139) do
it { should be_listening }
end
describe user('Administrator') do
it { should exist }
it { should belong_to_group('Administrators')}
end
这是失败的输出
/usr/bin/ruby2.3 -I/var/lib/gems/2.3.0/gems/rspec-support-3.5.0/lib:/var/lib/gems/2.3.0/gems/rspec-core-3.5.4/lib /var/lib/gems/2.3.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/base/\*_spec.rb
File "c:/windows"
should be directory (FAILED - 1)
File "c:/temp/testfile.txt"
should be file (FAILED - 2)