From e335c700ae0a6d41c5cba7bf7b30837e1e34b9c9 Mon Sep 17 00:00:00 2001 From: Mo Morsi Date: Thu, 17 Apr 2014 10:25:45 -0400 Subject: [PATCH] Optimizations & improvements to errata module (rev 1) --- lib/polisher/errata.rb | 56 ++++++++++++++++++++++----------- lib/polisher/version_checker.rb | 9 ++++-- spec/errata_spec.rb | 33 ++++++++++++------- 3 files changed, 65 insertions(+), 33 deletions(-) diff --git a/lib/polisher/errata.rb b/lib/polisher/errata.rb index 0e5d4bb..2d9e798 100644 --- a/lib/polisher/errata.rb +++ b/lib/polisher/errata.rb @@ -6,30 +6,50 @@ require 'json' require 'curb' +require 'polisher/core' + module Polisher class Errata - # Initialize/return singleton curl handle to - # query errata and set url - def self.client(url) + extend ConfHelpers + + conf_attr :advisory_url, '' + conf_attr :package_prefix, 'rubygem-' + + # Initialize/return singleton curl handle to query errata + def self.client @curl ||= begin - c = Curl::Easy.new - c.ssl_verify_peer = false - c.ssl_verify_host = false - c.http_auth_types = :negotiate - c.userpwd = ':' + curl = Curl::Easy.new + curl.ssl_verify_peer = false + curl.ssl_verify_host = false + curl.http_auth_types = :negotiate + curl.userpwd = ':' + curl end + end - @curl.url = url - @curl + def self.clear! + @cached_url = nil + @cached_builds = nil + self end - def self.versions_for(advisory_url, name, &bl) - result = self.client("#{advisory_url}/builds").get - versions = - JSON.parse(result.body_str).collect { |tag, builds| - ErrataBuild.builds_matching(builds, name) - }.flatten + def self.builds + @cached_url ||= advisory_url + @cached_builds ||= nil + + if @cached_url != advisory_url || @cached_builds.nil? + client.url = "#{advisory_url}/builds" + @cached_builds = client.get + @cached_builds = JSON.parse(client.body_str) + end + + @cached_builds + end + def self.versions_for(name, &bl) + versions = builds.collect do |tag, builds| + ErrataBuild.builds_matching(builds, name) + end.flatten bl.call(:errata, name, versions) unless(bl.nil?) versions end @@ -44,12 +64,12 @@ def self.builds_matching(builds, name) def self.build_matches?(build, name) pkg,meta = *build.flatten - pkg =~ /^rubygem-#{name}-([^-]*)-.*$/ + pkg =~ /^#{Errata.package_prefix}#{name}-([^-]*)-.*$/ end def self.build_version(build, name) pkg,meta = *build.flatten - pkg.split('-')[2] + pkg.gsub(Errata.package_prefix, '').split('-')[1] end end end diff --git a/lib/polisher/version_checker.rb b/lib/polisher/version_checker.rb index d5454fd..a3f83a4 100644 --- a/lib/polisher/version_checker.rb +++ b/lib/polisher/version_checker.rb @@ -12,7 +12,8 @@ class VersionChecker FEDORA_TARGET = :fedora GIT_TARGET = :git YUM_TARGET = :yum - BODHI_TARGET = :bodhi # fedora dispatches to bodhi to not enabled by default + BODHI_TARGET = :bodhi # fedora dispatches to bodhi so not enabled by default + ERRATA_TARGET = :errata # not enabled by default ALL_TARGETS = [GEM_TARGET, KOJI_TARGET, FEDORA_TARGET, GIT_TARGET, YUM_TARGET] @@ -67,8 +68,10 @@ def self.versions_for(name, &bl) versions.merge! :bodhi => Bodhi.versions_for(name, &bl) end - #bodhi_version = Bodhi.versions_for(name, &bl) - #errata_version = Errata.version_for('url?', name, &bl) + if should_check?(ERRATA_TARGET) + require 'polisher/errata' + versions.merge! :errata => Errata.versions_for(name, &bl) + end versions end diff --git a/spec/errata_spec.rb b/spec/errata_spec.rb index 85779ba..67fe101 100644 --- a/spec/errata_spec.rb +++ b/spec/errata_spec.rb @@ -9,34 +9,43 @@ module Polisher describe Errata do describe "#versions_for" do before(:each) do - @result = double(Curl::Easy.new) - @result.should_receive(:body_str).and_return({'tag' => [['rubygem-rails-1.0.0-1']]}.to_json) + @result = {'tag' => [['rubygem-rails-1.0.0-1']]}.to_json + @orig_url = described_class.advisory_url + end + + after(:each) do + described_class.advisory_url @orig_url + described_class.clear! end it "uses curl to retreive updates" do client = Curl::Easy.new - described_class.should_receive(:client).with('http://errata.url/builds').and_return(client) - client.should_receive(:get).and_return(@result) + described_class.should_receive(:client).at_least(:once).and_return(client) + client.should_receive(:url=).with('http://errata.url/builds') + client.should_receive(:get) + client.should_receive(:body_str).and_return(@result) - described_class.versions_for('http://errata.url', 'rails') + described_class.advisory_url 'http://errata.url' + described_class.versions_for('rails') end it "returns versions" do client = Curl::Easy.new - described_class.should_receive(:client).and_return(client) - client.should_receive(:get).and_return(@result) - - described_class.versions_for('http://errata.url', 'rails').should == ['1.0.0'] + described_class.should_receive(:client).at_least(:once).and_return(client) + client.stub(:get) + client.should_receive(:body_str).and_return(@result) + described_class.versions_for('rails').should == ['1.0.0'] end it "invokes block with versions" do client = Curl::Easy.new - described_class.should_receive(:client).and_return(client) - client.should_receive(:get).and_return(@result) + described_class.should_receive(:client).at_least(:once).and_return(client) + client.stub(:get) + client.should_receive(:body_str).and_return(@result) cb = proc {} cb.should_receive(:call).with(:errata, 'rails', ['1.0.0']) - described_class.versions_for('http://errata.url', 'rails', &cb) + described_class.versions_for('rails', &cb) end end end # describe Koji