Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Commit

Permalink
Optimizations & improvements to errata module (rev 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
movitto committed Apr 17, 2014
1 parent 65d5fb2 commit e335c70
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 33 deletions.
56 changes: 38 additions & 18 deletions lib/polisher/errata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
9 changes: 6 additions & 3 deletions lib/polisher/version_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions spec/errata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e335c70

Please sign in to comment.