diff --git a/lib/puppet/forge/repository.rb b/lib/puppet/forge/repository.rb index 2528bd350..8ddf44b6d 100644 --- a/lib/puppet/forge/repository.rb +++ b/lib/puppet/forge/repository.rb @@ -1,109 +1,116 @@ require 'net/http' require 'digest/sha1' require 'uri' class Puppet::Forge # = Repository # # This class is a file for accessing remote repositories with modules. class Repository attr_reader :uri, :cache # Instantiate a new repository instance rooted at the +url+. # The agent will report +consumer_version+ in the User-Agent to # the repository. def initialize(url, consumer_version) @uri = url.is_a?(::URI) ? url : ::URI.parse(url.sub(/^(?!https?:\/\/)/, 'http://')) @cache = Cache.new(self) @consumer_version = consumer_version end # Read HTTP proxy configurationm from Puppet's config file, or the # http_proxy environment variable. def http_proxy_env proxy_env = ENV["http_proxy"] || ENV["HTTP_PROXY"] || nil begin return URI.parse(proxy_env) if proxy_env rescue URI::InvalidURIError return nil end return nil end def http_proxy_host env = http_proxy_env if env and env.host then return env.host end if Puppet.settings[:http_proxy_host] == 'none' return nil end return Puppet.settings[:http_proxy_host] end def http_proxy_port env = http_proxy_env if env and env.port then return env.port end return Puppet.settings[:http_proxy_port] end # Return a Net::HTTPResponse read for this +request_path+. def make_http_request(request_path) request = Net::HTTP::Get.new(request_path, { "User-Agent" => user_agent }) if ! @uri.user.nil? && ! @uri.password.nil? request.basic_auth(@uri.user, @uri.password) end return read_response(request) end # Return a Net::HTTPResponse read from this HTTPRequest +request+. def read_response(request) begin Net::HTTP::Proxy( http_proxy_host, http_proxy_port ).start(@uri.host, @uri.port) do |http| http.request(request) end rescue Errno::ECONNREFUSED, SocketError msg = "Error: Could not connect to #{@uri}\n" msg << " There was a network communications problem\n" msg << " Check your network connection and try again\n" Puppet.err msg exit(1) end end # Return the local file name containing the data downloaded from the # repository at +release+ (e.g. "myuser-mymodule"). def retrieve(release) return cache.retrieve(@uri + release) end # Return the URI string for this repository. def to_s return @uri.to_s end # Return the cache key for this repository, this a hashed string based on # the URI. def cache_key return @cache_key ||= [ @uri.to_s.gsub(/[^[:alnum:]]+/, '_').sub(/_$/, ''), Digest::SHA1.hexdigest(@uri.to_s) ].join('-') end def user_agent - "#{@consumer_version} Puppet/#{Puppet.version} (#{Facter.value(:operatingsystem)} #{Facter.value(:operatingsystemrelease)}) Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE}; #{RUBY_PLATFORM})" + "#{@consumer_version} Puppet/#{Puppet.version} (#{Facter.value(:operatingsystem)} #{Facter.value(:operatingsystemrelease)}) #{ruby_version}" end private :user_agent + + def ruby_version + # the patchlevel is not available in ruby 1.8.5 + patch = defined?(RUBY_PATCHLEVEL) ? "-p#{RUBY_PATCHLEVEL}" : "" + "Ruby/#{RUBY_VERSION}#{patch} (#{RUBY_RELEASE_DATE}; #{RUBY_PLATFORM})" + end + private :ruby_version end end diff --git a/spec/unit/forge/repository_spec.rb b/spec/unit/forge/repository_spec.rb index 78416883e..064a24fd3 100644 --- a/spec/unit/forge/repository_spec.rb +++ b/spec/unit/forge/repository_spec.rb @@ -1,88 +1,88 @@ require 'spec_helper' require 'net/http' require 'puppet/forge/repository' require 'puppet/forge/cache' describe Puppet::Forge::Repository do let(:consumer_version) { "Test/1.0" } let(:repository) { Puppet::Forge::Repository.new('http://fake.com', consumer_version) } it "retrieve accesses the cache" do uri = URI.parse('http://some.url.com') repository.cache.expects(:retrieve).with(uri) repository.retrieve(uri) end describe 'http_proxy support' do after :each do ENV["http_proxy"] = nil end it "supports environment variable for port and host" do ENV["http_proxy"] = "http://test.com:8011" repository.http_proxy_host.should == "test.com" repository.http_proxy_port.should == 8011 end it "supports puppet configuration for port and host" do ENV["http_proxy"] = nil proxy_settings_of('test.com', 7456) repository.http_proxy_port.should == 7456 repository.http_proxy_host.should == "test.com" end it "uses environment variable before puppet settings" do ENV["http_proxy"] = "http://test1.com:8011" proxy_settings_of('test2.com', 7456) repository.http_proxy_host.should == "test1.com" repository.http_proxy_port.should == 8011 end end describe "making a request" do before :each do proxy_settings_of("proxy", 1234) end it "returns the result object from the request" do result = "the http response" performs_an_http_request result do |http| http.expects(:request).with(responds_with(:path, "the_path")) end repository.make_http_request("the_path").should == result end it "sets the user agent for the request" do performs_an_http_request do |http| http.expects(:request).with() do |request| puppet_version = /Puppet\/\d+\..*/ os_info = /\(.*\)/ - ruby_version = /Ruby\/\d+\.\d+\.\d+-p\d+ \(\d{4}-\d{2}-\d{2}; .*\)/ + ruby_version = /Ruby\/\d+\.\d+\.\d+(-p\d+)? \(\d{4}-\d{2}-\d{2}; .*\)/ request["User-Agent"] =~ /^#{consumer_version} #{puppet_version} #{os_info} #{ruby_version}/ end end repository.make_http_request("the_path") end def performs_an_http_request(result = nil, &block) http = mock("http client") yield http proxy = mock("http proxy") proxy.expects(:start).with("fake.com", 80).yields(http).returns(result) Net::HTTP.expects(:Proxy).with("proxy", 1234).returns(proxy) end end def proxy_settings_of(host, port) Puppet.settings.stubs(:[]).with(:http_proxy_host).returns(host) Puppet.settings.stubs(:[]).with(:http_proxy_port).returns(port) end end