From 1dbff12baedf3e8fca2ce3adcc4dfdbf2dd8fcdf Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Thu, 15 Nov 2012 14:47:42 -0800 Subject: [PATCH 1/4] try running functional tests first ...should help avoid pollution of global state from unit tests --- ci/jenkins_run_tests.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/jenkins_run_tests.bat b/ci/jenkins_run_tests.bat index 7d3a2d7c0df..9c8fbbf3776 100644 --- a/ci/jenkins_run_tests.bat +++ b/ci/jenkins_run_tests.bat @@ -2,4 +2,4 @@ set PATH=C:\Ruby192\bin;%PATH% ruby -v call bundle install --binstubs --path vendor/bundle || ( call rm Gemfile.lock && call bundle install --binstubs --path vendor/bundle ) -ruby bin\rspec -r rspec_junit_formatter -f RspecJunitFormatter -o test.xml -f documentation spec/unit spec/functional +ruby bin\rspec -r rspec_junit_formatter -f RspecJunitFormatter -o test.xml -f documentation spec/functional spec/unit From 9b39d2ca14733fe082fcd6e05d6c928219726d07 Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Thu, 15 Nov 2012 16:16:36 -0800 Subject: [PATCH 2/4] replace thin w/ webrick for func. test server --- Gemfile | 6 --- spec/functional/knife/cookbook_delete_spec.rb | 2 - spec/functional/knife/exec_spec.rb | 2 - spec/functional/knife/ssh_spec.rb | 1 - spec/functional/resource/remote_file_spec.rb | 1 - spec/functional/tiny_server_spec.rb | 9 +++-- spec/tiny_server.rb | 39 ++++++++++++++----- 7 files changed, 34 insertions(+), 26 deletions(-) diff --git a/Gemfile b/Gemfile index a1d35569e19..4a84958e39b 100644 --- a/Gemfile +++ b/Gemfile @@ -7,12 +7,6 @@ gem "ronn" group(:development, :test) do gem 'rack' - gem 'thin' - - # Eventmachine 1.0.0 is causing functional test failures on Solaris - # 9 SPARC. Pinning em to 0.12.10 solves this issue until we can - # replace thin with webrat or some other alternative. - gem 'eventmachine', '0.12.10', :platforms => :ruby gem 'ruby-shadow', :platforms => :ruby unless RUBY_PLATFORM.downcase.match(/(darwin|freebsd)/) # gem 'awesome_print' diff --git a/spec/functional/knife/cookbook_delete_spec.rb b/spec/functional/knife/cookbook_delete_spec.rb index 54081263f0d..ef38cb2e1f2 100644 --- a/spec/functional/knife/cookbook_delete_spec.rb +++ b/spec/functional/knife/cookbook_delete_spec.rb @@ -23,8 +23,6 @@ before(:all) do @original_config = Chef::Config.hash_dup - Thin::Logging.silent = true - @server = TinyServer::Manager.new @server.start end diff --git a/spec/functional/knife/exec_spec.rb b/spec/functional/knife/exec_spec.rb index fa4a448fbb7..114a30d7916 100644 --- a/spec/functional/knife/exec_spec.rb +++ b/spec/functional/knife/exec_spec.rb @@ -23,8 +23,6 @@ before(:all) do @original_config = Chef::Config.hash_dup - Thin::Logging.silent = false - @server = TinyServer::Manager.new#(:debug => true) @server.start end diff --git a/spec/functional/knife/ssh_spec.rb b/spec/functional/knife/ssh_spec.rb index 8f87e53bf7f..696fd58c4d8 100644 --- a/spec/functional/knife/ssh_spec.rb +++ b/spec/functional/knife/ssh_spec.rb @@ -24,7 +24,6 @@ before(:all) do @original_config = Chef::Config.hash_dup Chef::Knife::Ssh.load_deps - Thin::Logging.silent = true @server = TinyServer::Manager.new @server.start end diff --git a/spec/functional/resource/remote_file_spec.rb b/spec/functional/resource/remote_file_spec.rb index e695e8feaea..2ebeecd3d1a 100644 --- a/spec/functional/resource/remote_file_spec.rb +++ b/spec/functional/resource/remote_file_spec.rb @@ -40,7 +40,6 @@ def create_resource end before(:all) do - Thin::Logging.silent = false @server = TinyServer::Manager.new @server.start @api = TinyServer::API.instance diff --git a/spec/functional/tiny_server_spec.rb b/spec/functional/tiny_server_spec.rb index 0cfef4305ff..68ab9e7294d 100644 --- a/spec/functional/tiny_server_spec.rb +++ b/spec/functional/tiny_server_spec.rb @@ -37,15 +37,16 @@ it "creates a route for a GET request" do @api.get('/foo/bar', 200, 'hello foobar') - response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => '/foo/bar') - response.should == [200, {'Content-Type' => 'application/json'}, 'hello foobar'] + # WEBrick gives you the full URI with host, Thin only gave the part after scheme+host+port + response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => 'http://localhost:1974/foo/bar') + response.should == [200, {'Content-Type' => 'application/json'}, [ 'hello foobar' ]] end it "creates a route for a request with a block" do block_called = false @api.get('/bar/baz', 200) { block_called = true; 'hello barbaz' } - response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => '/bar/baz') - response.should == [200, {'Content-Type' => 'application/json'}, 'hello barbaz'] + response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => 'http://localhost:1974/bar/baz') + response.should == [200, {'Content-Type' => 'application/json'}, [ 'hello barbaz' ]] block_called.should be_true end diff --git a/spec/tiny_server.rb b/spec/tiny_server.rb index ae8518097b8..ba75cd57c1a 100644 --- a/spec/tiny_server.rb +++ b/spec/tiny_server.rb @@ -17,11 +17,13 @@ # require 'rubygems' +require 'webrick' require 'rack' -require 'thin' +#require 'thin' require 'singleton' require 'chef/json_compat' require 'open-uri' +require 'chef/config' module TinyServer @@ -29,30 +31,42 @@ class Server < Rack::Server attr_writer :app - def self.run(options=nil, &block) + def self.setup(options=nil, &block) tiny_app = new(options) app_code = Rack::Builder.new(&block).to_app tiny_app.app = app_code - tiny_app.start + tiny_app + end + + def shutdown + server.shutdown end end class Manager - DEFAULT_OPTIONS = {:server => 'thin', :Port => 9000, :Host => 'localhost', :environment => :none} + # 5 == debug, 3 == warning + LOGGER = WEBrick::Log.new(STDOUT, 3) + DEFAULT_OPTIONS = { + :server => 'webrick', + :Port => 9000, + :Host => 'localhost', + :environment => :none, + :Logger => LOGGER, + :AccessLog => [] # Remove this option to enable the access log when debugging. + } def initialize(options=nil) @options = options ? DEFAULT_OPTIONS.merge(options) : DEFAULT_OPTIONS @creator = caller.first - - Thin::Logging.silent = !@options[:debug] end def start @server_thread = Thread.new do - @server = Server.run(@options) do + @server = Server.setup(@options) do run API.instance end + @server.start end block_until_started end @@ -63,7 +77,10 @@ def url def block_until_started 200.times do - return true if started? + if started? + raise "ivar weirdness" if @server.nil? + return true + end end raise "TinyServer failed to boot :/" end @@ -84,6 +101,7 @@ def started? def stop # yes, this is terrible. + @server.shutdown @server_thread.kill @server_thread.join @server_thread = nil @@ -132,7 +150,7 @@ def call(env) debug_info = {:message => "no data matches the request for #{env['REQUEST_URI']}", :available_routes => @routes, :request => env} # Uncomment me for glorious debugging - #pp :not_found => debug_info + # pp :not_found => debug_info [404, {'Content-Type' => 'application/json'}, debug_info.to_json] end end @@ -152,6 +170,7 @@ def initialize(path_spec, response) end def matches_request?(uri) + uri = URI.parse(uri).request_uri @path_spec === uri end @@ -171,7 +190,7 @@ def initialize(response_code=200,data=nil, &block) def call data = @data || @block.call - [@response_code, HEADERS, data] + [@response_code, HEADERS, Array(data)] end def to_s From d068ae1842ee1690db21c92654d51e0959cda8b4 Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Fri, 16 Nov 2012 16:28:49 -0800 Subject: [PATCH 3/4] fixes binmode issues on windows --- spec/functional/resource/cookbook_file_spec.rb | 8 +++++++- spec/functional/resource/remote_file_spec.rb | 12 ++++++++++-- spec/spec_helper.rb | 1 + spec/support/chef_helpers.rb | 13 +++++++++++++ spec/support/shared/functional/file_resource.rb | 15 +++++++++++---- .../shared/functional/securable_resource.rb | 4 ++++ spec/unit/chef_fs/diff_spec.rb | 2 +- spec/unit/provider/file_spec.rb | 6 ++++-- 8 files changed, 51 insertions(+), 10 deletions(-) diff --git a/spec/functional/resource/cookbook_file_spec.rb b/spec/functional/resource/cookbook_file_spec.rb index 89efccc0642..b2bee5d95b0 100644 --- a/spec/functional/resource/cookbook_file_spec.rb +++ b/spec/functional/resource/cookbook_file_spec.rb @@ -24,7 +24,13 @@ let(:file_base) { 'cookbook_file_spec' } let(:source) { 'java.response' } let(:cookbook_name) { 'java' } - let(:expected_content) { IO.read(File.join(CHEF_SPEC_DATA, 'cookbooks', 'java', 'files', 'default', 'java.response')) } + let(:expected_content) do + content = File.open(File.join(CHEF_SPEC_DATA, 'cookbooks', 'java', 'files', 'default', 'java.response'), "rb") do |f| + f.read + end + content.force_encoding(Encoding::BINARY) if content.respond_to?(:force_encoding) + content + end def create_resource # set up cookbook collection for this run to use, based on our diff --git a/spec/functional/resource/remote_file_spec.rb b/spec/functional/resource/remote_file_spec.rb index 2ebeecd3d1a..998255e720e 100644 --- a/spec/functional/resource/remote_file_spec.rb +++ b/spec/functional/resource/remote_file_spec.rb @@ -24,7 +24,13 @@ let(:file_base) { "remote_file_spec" } let(:source) { 'http://localhost:9000/nyan_cat.png' } - let(:expected_content) { IO.read(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png')) } + let(:expected_content) do + content = File.open(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png'), "rb") do |f| + f.read + end + content.force_encoding(Encoding::BINARY) if content.respond_to?(:force_encoding) + content + end def create_resource node = Chef::Node.new @@ -45,7 +51,9 @@ def create_resource @api = TinyServer::API.instance @api.clear @api.get("/nyan_cat.png", 200) { - IO.read(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png')) + File.open(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png'), "rb") do |f| + f.read + end } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1d101022acd..521cc41a30c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -72,6 +72,7 @@ module Shell config.filter_run_excluding :ruby_19_only => true unless ruby_19? config.filter_run_excluding :requires_root => true unless ENV['USER'] == 'root' config.filter_run_excluding :requires_unprivileged_user => true if ENV['USER'] == 'root' + config.filter_run_excluding :uses_diff => true unless has_diff? config.run_all_when_everything_filtered = true config.treat_symbols_as_metadata_keys_with_true_values = true diff --git a/spec/support/chef_helpers.rb b/spec/support/chef_helpers.rb index 77f5fc76695..77cbe5b5cbe 100644 --- a/spec/support/chef_helpers.rb +++ b/spec/support/chef_helpers.rb @@ -50,3 +50,16 @@ def make_tmpname(prefix_suffix, n) path << "-#{n}" if n path << suffix end + +# NOTE: +# This is a temporary fix to get tests passing on systems that have no `diff` +# until we can replace shelling out to `diff` with ruby diff-lcs +def has_diff? + begin + diff_cmd = Mixlib::ShellOut.new("diff -v") + diff_cmd.run_command + true + rescue Errno::ENOENT + false + end +end diff --git a/spec/support/shared/functional/file_resource.rb b/spec/support/shared/functional/file_resource.rb index fcadc2bd10d..b025dc10b5f 100644 --- a/spec/support/shared/functional/file_resource.rb +++ b/spec/support/shared/functional/file_resource.rb @@ -146,6 +146,13 @@ # note the stripping of the drive letter from the tmpdir on windows let(:backup_glob) { File.join(CHEF_SPEC_BACKUP_PATH, Dir.tmpdir.sub(/^([A-Za-z]:)/, ""), "#{file_base}*") } + def binread(file) + content = File.open(file, "rb") do |f| + f.read + end + content.force_encoding(Encoding::BINARY) if "".respond_to?(:force_encoding) + end + context "when the target file does not exist" do before do # Assert starting state is expected @@ -162,7 +169,7 @@ end it "creates the file with the correct content when the :create action is run" do - IO.read(path).should == expected_content + binread(path).should == expected_content end it "is marked as updated by last action" do @@ -176,7 +183,7 @@ end it "creates the file with the correct content" do - IO.read(path).should == expected_content + binread(path).should == expected_content end it "is marked as updated by last action" do @@ -211,7 +218,7 @@ def denied_acl(sid, expected_perms) context "when the target file has the wrong content" do before(:each) do - File.open(path, "w") { |f| f.print "This is so wrong!!!" } + File.open(path, "wb") { |f| f.print "This is so wrong!!!" } now = Time.now.to_i File.utime(now - 9000, now - 9000, path) @@ -238,7 +245,7 @@ def denied_acl(sid, expected_perms) context "when the target file has the correct content" do before(:each) do - File.open(path, "w") { |f| f.print expected_content } + File.open(path, "wb") { |f| f.print expected_content } now = Time.now.to_i File.utime(now - 9000, now - 9000, path) diff --git a/spec/support/shared/functional/securable_resource.rb b/spec/support/shared/functional/securable_resource.rb index aca1a6914d4..240b65b2254 100644 --- a/spec/support/shared/functional/securable_resource.rb +++ b/spec/support/shared/functional/securable_resource.rb @@ -29,12 +29,16 @@ before :each do File.chown(Etc.getpwnam('nobody').uid, 1337, path) File.chmod(0776, path) + now = Time.now.to_i + File.utime(now - 9000, now - 9000, path) end end context "without root", :requires_unprivileged_user do before :each do File.chmod(0776, path) + now = Time.now.to_i + File.utime(now - 9000, now - 9000, path) end end end diff --git a/spec/unit/chef_fs/diff_spec.rb b/spec/unit/chef_fs/diff_spec.rb index 19d87052ad6..ecd5803e154 100644 --- a/spec/unit/chef_fs/diff_spec.rb +++ b/spec/unit/chef_fs/diff_spec.rb @@ -28,7 +28,7 @@ def remove_date(diff) diff.gsub(/([+-]{3}.*)\t.*/, '\1 DATE') end -describe 'diff' do +describe 'diff', :uses_diff => true do include FileSystemSupport context 'with two filesystems with all types of difference' do diff --git a/spec/unit/provider/file_spec.rb b/spec/unit/provider/file_spec.rb index 9f5ad3a8f8f..23352a5124e 100644 --- a/spec/unit/provider/file_spec.rb +++ b/spec/unit/provider/file_spec.rb @@ -342,10 +342,12 @@ end it "should call action create if the does not file exist" do - @resource.path("/tmp/non_existant_file") + @resource.path("/tmp/example-dir/non_existant_file") @provider = Chef::Provider::File.new(@resource, @run_context) @provider.should_receive(:diff_current_from_content).and_return("") ::File.stub!(:exists?).with(@resource.path).and_return(false) + ::File.stub!(:directory?).with("/tmp/example-dir/non_existant_file").and_return(false) + ::File.stub!(:directory?).with("/tmp/example-dir").and_return(true) @provider.stub!(:update_new_file_state) io = StringIO.new File.should_receive(:open).with(@provider.new_resource.path, "w+").and_yield(io) @@ -355,7 +357,7 @@ end end - describe "when a diff is requested" do + describe "when a diff is requested", :uses_diff => true do before(:each) do @original_config = Chef::Config.hash_dup From 51a06a4586e9dee31cb8d68ece22e52d14db3919 Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Mon, 19 Nov 2012 12:55:55 -0800 Subject: [PATCH 4/4] remove deprecated `has_rdoc` --- chef.gemspec | 1 - 1 file changed, 1 deletion(-) diff --git a/chef.gemspec b/chef.gemspec index 6a392f8abef..fcf5d2b8ecd 100644 --- a/chef.gemspec +++ b/chef.gemspec @@ -5,7 +5,6 @@ Gem::Specification.new do |s| s.name = 'chef' s.version = Chef::VERSION s.platform = Gem::Platform::RUBY - s.has_rdoc = true s.extra_rdoc_files = ["README.md", "CONTRIBUTING.md", "LICENSE" ] s.summary = "A systems integration framework, built to bring the benefits of configuration management to your entire infrastructure." s.description = s.summary