Commit 59e766e8 authored by Antonio Terceiro's avatar Antonio Terceiro

Merge branch 'ruby-code-style'

Apply patches by Bhavy Singh, and modify .rubocop.yml in one go

Merge requests: !61 !68 !70 !71 !72 !73 !74
parents 60666317 81edbf9f
Pipeline #63764 canceled with stage
This diff is collapsed.
......@@ -7,7 +7,7 @@ status_dir = ARGV[0]
output_file = ARGV[1]
data = Dir.glob(File.join(status_dir, '[0-9]*/[0-9]*/[0-9]*/*.json')).sort.map do |f|
File.open(f) { |s| JSON.load(s) }
File.open(f) { |s| JSON.parse(s.read) }
end
File.open(output_file, 'w') do |f|
......
......@@ -57,7 +57,7 @@ describe Debci::API do
end
it 'gets a key based on client certificate' do
post '/api/v1/getkey', {}, { 'SSL_CLIENT_S_DN_CN' => 'foo@bar.com' }
post '/api/v1/getkey', {}, 'SSL_CLIENT_S_DN_CN' => 'foo@bar.com'
expect(last_response.status).to eq(201)
key = Debci::Key.find_by(user: 'foo@bar.com')
......@@ -69,11 +69,9 @@ describe Debci::API do
expect(last_response.status).to eq(200)
expect(last_response.content_type).to match('text/html')
end
end
context 'receiving test requests' do
before do
key = Debci::Key.create!(user: 'theuser').key
......@@ -81,11 +79,10 @@ describe Debci::API do
end
context 'for a single test' do
it 'accepts a valid request' do
expect_any_instance_of(Debci::Job).to receive(:enqueue)
post '/api/v1/test/%s/%s/mypackage' % [suite, arch]
post format('/api/v1/test/%<suite>s/%<arch>s/mypackage', suite: suite, arch: arch)
job = Debci::Job.last
expect(job.package).to eq('mypackage')
......@@ -98,42 +95,40 @@ describe Debci::API do
it 'enqueues with priority' do
expect_any_instance_of(Debci::Job).to receive(:enqueue).with(Integer)
post '/api/v1/test/%s/%s/mypackage' % [suite, arch]
post format('/api/v1/test/%<suite>s/%<arch>s/mypackage', suite: suite, arch: arch)
end
it 'rejects blacklisted package' do
allow_any_instance_of(Debci::Blacklist).to receive(:include?).with('mypackage').and_return(true)
post '/api/v1/test/%s/%s/mypackage' % [suite, arch]
post format('/api/v1/test/%<suite>s/%<arch>s/mypackage', suite: suite, arch: arch)
expect(last_response.status).to eq(400)
end
it 'rejects invalid package names' do
jobs = Debci::Job.count
post '/api/v1/test/%s/%s/foo=bar' % [suite, arch]
post format('/api/v1/test/%<suite>s/%<arch>s/foo=bar', suite: suite, arch: arch)
expect(last_response.status).to eq(400)
expect(Debci::Job.count).to eq(jobs)
end
it 'rejects unknown arch' do
post '/api/v1/test/%s/%s/mypackage' % [suite, 'xyz']
post format('/api/v1/test/%<suite>s/%<arch>s/mypackage', suite: suite, arch: 'xyz')
expect(last_response.status).to eq(400)
end
it 'rejects unknown suite' do
post '/api/v1/test/%s/%s/mypackage' % ['nonexistingsuite', arch]
post format('/api/v1/test/%<suite>s/%<arch>s/mypackage', suite: 'nonexistingsuite', arch: arch)
expect(last_response.status).to eq(400)
end
end
context 'for test a batch' do
it 'accepts a valid request' do
allow_any_instance_of(Debci::Job).to receive(:enqueue)
post '/api/v1/test/%s/%s' % [suite, arch], tests: '[{"package": "package1"}, {"package": "package2"}]'
post format('/api/v1/test/%<suite>s/%<arch>s', suite: suite, arch: arch), tests: '[{"package": "package1"}, {"package": "package2"}]'
['package1', 'package2'].each do |pkg|
%w[package1 package2].each do |pkg|
job = Debci::Job.where(package: pkg).last
expect(job.suite).to eq(suite)
expect(job.arch).to eq(arch)
......@@ -145,13 +140,13 @@ describe Debci::API do
it 'rejects unknown arch' do
expect_any_instance_of(Debci::Job).to_not receive(:enqueue)
post '/api/v1/test/%s/%s' % [suite, 'xyz'], tests: '[{"package": "package1"}, {"package": "package2"}]'
post format('/api/v1/test/%<suite>s/%<arch>s', suite: suite, arch: 'xyz'), tests: '[{"package": "package1"}, {"package": "package2"}]'
expect(last_response.status).to eq(400)
end
it 'rejects unknown suite' do
expect_any_instance_of(Debci::Job).to_not receive(:enqueue)
post '/api/v1/test/%s/%s' % ['nonexistingsuite', arch], tests: '[{"package": "package1"}, {"package": "package2"}]'
post format('/api/v1/test/%<suite>s/%<arch>s', suite: 'nonexistingsuite', arch: arch), tests: '[{"package": "package1"}, {"package": "package2"}]'
expect(last_response.status).to eq(400)
end
......@@ -161,7 +156,7 @@ describe Debci::API do
expect_any_instance_of(Debci::Job).to receive(:enqueue).once
post '/api/v1/test/%s/%s' % [suite, arch], tests: '[{"package": "package1"}, {"package": "package2"}]'
post format('/api/v1/test/%<suite>s/%<arch>s', suite: suite, arch: arch), tests: '[{"package": "package1"}, {"package": "package2"}]'
expect(last_response.status).to eq(201)
job1 = Debci::Job.find_by(package: 'package1', suite: suite, arch: arch)
......@@ -172,7 +167,7 @@ describe Debci::API do
end
it 'marks invalid package names as failed right away' do
post '/api/v1/test/%s/%s' % [suite, arch], tests: '[{"package": "package1"}, {"package": "foo=package2"}]'
post format('/api/v1/test/%<suite>s/%<arch>s', suite: suite, arch: arch), tests: '[{"package": "package1"}, {"package": "foo=package2"}]'
expect(last_response.status).to eq(201)
job1 = Debci::Job.find_by(package: 'package1', suite: suite, arch: arch)
......@@ -184,7 +179,7 @@ describe Debci::API do
it 'handles invalid JSON gracefully' do
expect_any_instance_of(Debci::Job).to_not receive(:enqueue)
post '/api/v1/test/%s/%s' % [suite, arch], tests: 'invalid json'
post format('/api/v1/test/%<suite>s/%<arch>s', suite: suite, arch: arch), tests: 'invalid json'
expect(last_response.status).to eq(400)
end
......@@ -193,7 +188,7 @@ describe Debci::API do
it 'handles trigger and pin' do
expect_any_instance_of(Debci::Job).to receive(:enqueue)
post '/api/v1/test/%s/%s' % [suite, arch], tests: File.read(test_file)
post format('/api/v1/test/%<suite>s/%<arch>s', suite: suite, arch: arch), tests: File.read(test_file)
expect(last_response.status).to eq(201)
job = Debci::Job.find_by(suite: suite, arch: arch, package: 'package1')
......@@ -203,16 +198,14 @@ describe Debci::API do
it 'handles trigger and pin as a file upload' do
expect_any_instance_of(Debci::Job).to receive(:enqueue)
post '/api/v1/test/%s/%s' % [suite, arch], tests: Rack::Test::UploadedFile.new(test_file, "application/json")
post format('/api/v1/test/%<suite>s/%<arch>s', suite: suite, arch: arch), tests: Rack::Test::UploadedFile.new(test_file, 'application/json')
expect(last_response.status).to eq(201)
job = Debci::Job.find_by(suite: suite, arch: arch, package: 'package1')
expect(job.trigger).to eq('foo/1.0')
expect(job.pin_packages).to eq([['src:foo', 'unstable']])
end
end
end
context 'validating package names' do
......@@ -243,14 +236,13 @@ describe Debci::API do
end
context 'retriggers' do
it 'rejects non-authenticated requests' do
post '/api/v1/retry/1'
expect(last_response.status).to eq(403)
end
it 'displays a user friendly page to authenticated users' do
get '/api/v1/retry/1', {}, { 'SSL_CLIENT_S_DN_CN' => 'foo@bar.com' }
get '/api/v1/retry/1', {}, 'SSL_CLIENT_S_DN_CN' => 'foo@bar.com'
expect(last_response.status).to eq(200)
expect(last_response.content_type).to match('text/html')
end
......@@ -272,7 +264,7 @@ describe Debci::API do
arch: arch,
requestor: user,
trigger: trigger,
pin_packages: pin_packages,
pin_packages: pin_packages
)
job_org = Debci::Job.last
......@@ -305,13 +297,13 @@ describe Debci::API do
arch: arch,
requestor: user,
trigger: trigger,
pin_packages: pin_packages,
pin_packages: pin_packages
)
job_org = Debci::Job.last
# Here we are going to retrigger it
post "/api/v1/retry/#{job_org.run_id}", {}, { 'SSL_CLIENT_S_DN_CN' => 'foo@bar.com' }
post "/api/v1/retry/#{job_org.run_id}", {}, 'SSL_CLIENT_S_DN_CN' => 'foo@bar.com'
expect(last_response.status).to eq(201)
......@@ -327,5 +319,4 @@ describe Debci::API do
expect(last_response.status).to eq(400)
end
end
end
require 'spec_helper'
require 'debci/data'
RSpec.shared_context "export/import" do
RSpec.shared_context 'export/import' do
let(:job_data) do
{
'run_id': '9999',
......@@ -9,7 +9,7 @@ RSpec.shared_context "export/import" do
'suite': 'unstable',
'arch': 'amd64',
'version': '12.3.1-1',
'status': 'pass',
'status': 'pass'
}
end
......@@ -24,7 +24,7 @@ RSpec.shared_context "export/import" do
let(:output_tarball) { File.join(@tmpdir, 'export.tar') }
let(:exporter) { Debci::Data::Export.new(output_tarball) }
let(:exported_files) { contents = `tar taf #{output_tarball}`.split }
let(:exported_files) { `tar taf #{output_tarball}`.split }
let(:input_tarball) { File.join(@tmpdir, 'import.tar') }
let(:importer) { Debci::Data::Import.new(input_tarball) }
......@@ -45,16 +45,16 @@ RSpec.shared_context "export/import" do
exporter.add('rake')
exporter.save
end
def cleanup!
FileUtils.rm_rf(Dir[File.join(@tmpdir, '**/*')].reject { |f| f == output_tarball })
FileUtils.mv(output_tarball, input_tarball)
Debci::Job.delete_all
end
end
describe Debci::Data::Export do
include_context "export/import"
include_context 'export/import'
before(:each) do
export!
......@@ -79,8 +79,7 @@ describe Debci::Data::Export do
end
describe Debci::Data::Import do
include_context "export/import"
include_context 'export/import'
before(:each) do
export!
......@@ -99,7 +98,7 @@ describe Debci::Data::Import do
it 'creates job in database' do
expect(Debci::Job.count).to eq(1)
job = Debci::Job.first
job_data.reject { |k,v| k == :run_id }.each do |k,v|
job_data.reject { |k, _v| k == :run_id }.each do |k, v|
expect(job.send(k)).to eq(v)
end
end
......@@ -108,6 +107,6 @@ describe Debci::Data::Import do
job = Debci::Job.first
pkgdir = File.join(@tmpdir, 'packages/unstable/amd64/r/rake')
logs = Dir.chdir(pkgdir) { Dir['*.log'] }
expect(logs).to include('%d.log' % job.run_id)
expect(logs).to include(format('%<id>d.log', id: job.run_id))
end
end
......@@ -3,14 +3,19 @@ require 'debci/graph'
require 'fileutils'
require 'json'
describe Debci::Graph do
def generate_data_element(date, pass, fail, tmpfail, total)
{ 'date' => date, 'pass' => pass, 'fail' => fail, 'tmpfail' => tmpfail, 'total' => total }
end
describe Debci::Graph do
before(:all) do
@datadir = Dir.mktmpdir
initial_date = Time.parse('2014-08-10 12:12:30 UTC')
final_date = Time.parse('2014-08-15 01:30:15 UTC')
data_element1 = generate_data_element(initial_date, 100, 200, 20, 320)
data_element2 = generate_data_element(final_date, 200, 150, 20, 370)
mkdir_p 'status/unstable/amd64'
history 'status/unstable/amd64', [{'date' => '2014-08-10 12:12:30 UTC', 'pass' => 100, 'fail' => 200, 'tmpfail' => 20, 'total' => 320},
{'date' => '2014-08-15 01:30:15 UTC', 'pass' => 200, 'fail' => 150, 'tmpfail' => 20, 'total' => 370}]
history 'status/unstable/amd64', [data_element1, data_element2]
end
after(:all) do
......@@ -18,7 +23,7 @@ describe Debci::Graph do
end
def mkdir_p(path)
FileUtils.mkdir_p(File.join@datadir, path)
FileUtils.mkdir_p(File.join(@datadir, path))
end
def history(path, data)
......@@ -37,12 +42,13 @@ describe Debci::Graph do
it 'reduces history do 101 entries' do
initial_date = Time.parse('2014-08-10 12:12:30 UTC')
data = (0..150).map do |i|
{ 'date' => initial_date + 3600*24*i, 'pass' => 100, 'fail' => 200, 'tmpfail' => 20, 'total' => 320 }
generate_data_element(initial_date + 3600 * 24 * i, 100, 200, 20, 320)
end
history 'status/unstable/amd64', data
expect(graph.entries.size).to eq(101)
expect(Time.parse(graph.entries.first['date'])).to eq(initial_date)
expect(Time.parse(graph.entries.last['date'])).to eq(initial_date + 150*24*3600)
days = 150 * 24 * 3600
expect(Time.parse(graph.entries.last['date'])).to eq(initial_date + days)
end
end
......@@ -3,7 +3,6 @@ require 'spec_helper'
require 'debci/job'
describe Debci::Job do
it 'sets created_at' do
job = Debci::Job.create
expect(job.created_at).to_not be_nil
......@@ -28,7 +27,6 @@ describe Debci::Job do
expect(Debci::Job.pending).to eq([job0, job1])
end
it 'escapes trigger' do
job = Debci::Job.new(trigger: 'foo bar')
expect(job.get_enqueue_parameters).to_not include('trigger:foo bar')
......@@ -39,16 +37,16 @@ describe Debci::Job do
'áéíóú',
'`cat /etc/passwd`',
'$(cat /etc/passwd)',
"a\nb",
"a\nb"
].each do |invalid|
it('escapes \"%s" in trigger' % invalid) do
it(format('escapes \"%<invalid>s" in trigger', invalid: invalid)) do
job = Debci::Job.new(trigger: invalid)
expect(job.get_enqueue_parameters).to_not include(invalid)
end
end
let(:suite) { 'unstable' }
let(:arch) { 'amd64'}
let(:arch) { 'amd64' }
it 'imports status file' do
job = Debci::Job.create(suite: suite, arch: arch)
......@@ -81,5 +79,4 @@ describe Debci::Job do
expect(job.message).to eq('bla bla bla')
expect(job.previous_status).to eq('fail')
end
end
......@@ -2,7 +2,6 @@ require 'debci/package'
require 'debci/status'
describe Debci::Package do
let(:repository) { double }
let(:package) do
......@@ -10,13 +9,13 @@ describe Debci::Package do
end
it 'queries repository for architectures' do
expect(repository).to receive(:architectures_for).with(package).and_return(['amd64', 'i386'])
expect(package.architectures).to eq(['amd64', 'i386'])
expect(repository).to receive(:architectures_for).with(package).and_return(%w[amd64 i386])
expect(package.architectures).to eq(%w[amd64 i386])
end
it 'queries repository for suites' do
expect(repository).to receive(:suites_for).with(package).and_return(['unstable', 'experimental'])
expect(package.suites).to eq(['unstable', 'experimental'])
expect(repository).to receive(:suites_for).with(package).and_return(%w[unstable experimental])
expect(package.suites).to eq(%w[unstable experimental])
end
it 'queries repository for status' do
......@@ -83,5 +82,4 @@ describe Debci::Package do
allow(Debci.blacklist).to receive(:include?).with(pkg).and_return(true)
expect(pkg).to be_blacklisted
end
end
......@@ -6,7 +6,6 @@ require 'fileutils'
require 'json'
describe Debci::Repository do
before(:all) do
@datadir = Dir.mktmpdir
mkdir_p 'packages/unstable/amd64/r/rake'
......@@ -17,16 +16,16 @@ describe Debci::Repository do
mkdir_p 'packages/unstable/amd64.old'
history 'packages/testing/amd64/r/rake', [
{'status' => 'fail', 'date' => '2014-08-01 11:11:12'},
{'status' => 'pass', 'date' => '2014-07-07 12:12:15'},
{ 'status' => 'fail', 'date' => '2014-08-01 11:11:12' },
{ 'status' => 'pass', 'date' => '2014-07-07 12:12:15' }
]
history 'packages/unstable/amd64/r/rake', [
{'status' => 'fail', 'date' => '2014-08-01 11:11:12'},
{'status' => 'pass', 'date' => '2014-07-07 12:12:15'},
{'status' => 'tmpfail', 'date' => '2014-03-01 14:15:30'},
{'status' => 'fail', 'date' => '2015-03-01 14:15:30'},
{'status' => 'pass', 'date' => '2016-03-01 14:15:30'}
{ 'status' => 'fail', 'date' => '2014-08-01 11:11:12' },
{ 'status' => 'pass', 'date' => '2014-07-07 12:12:15' },
{ 'status' => 'tmpfail', 'date' => '2014-03-01 14:15:30' },
{ 'status' => 'fail', 'date' => '2015-03-01 14:15:30' },
{ 'status' => 'pass', 'date' => '2016-03-01 14:15:30' }
]
mkdir_p 'packages/unstable/amd64/r/rake-compiler'
......@@ -41,8 +40,8 @@ describe Debci::Repository do
mkdir_p 'packages/testing/amd64/r/ruby-ffi'
mkdir_p 'packages/testing/i386/r/ruby-ffi'
latest_status 'packages/unstable/amd64/r/ruby-ffi', {'status' => 'tmpfail',
'previous_status' => 'pass' }
latest_status 'packages/unstable/amd64/r/ruby-ffi', 'status' => 'tmpfail',
'previous_status' => 'pass'
mkdir_p 'packages/unstable/amd64/r/rubygems-integration'
mkdir_p 'packages/unstable/i386/r/rubygems-integration'
......@@ -50,38 +49,32 @@ describe Debci::Repository do
mkdir_p 'packages/testing/i386/r/rubygems-integration'
# platform specific issue
latest_status 'packages/unstable/amd64/r/rubygems-integration', {
'status' => 'pass',
'previous_status' => 'pass',
}
latest_status 'packages/unstable/i386/r/rubygems-integration', {
'status' => 'fail',
'previous_status' => 'fail',
}
latest_status 'packages/unstable/amd64/r/rubygems-integration',
'status' => 'pass',
'previous_status' => 'pass'
latest_status 'packages/unstable/i386/r/rubygems-integration',
'status' => 'fail',
'previous_status' => 'fail'
# NOT a platform specific issue - tmpfail should be ignored
mkdir_p 'packages/unstable/amd64/r/racc'
mkdir_p 'packages/unstable/i386/r/racc'
latest_status 'packages/unstable/amd64/r/racc', {
'status' => 'pass',
'previous_status' => 'pass',
}
latest_status 'packages/unstable/i386/r/racc', {
'status' => 'tmpfail',
'previous_status' => 'pass',
}
latest_status 'packages/unstable/amd64/r/racc',
'status' => 'pass',
'previous_status' => 'pass'
latest_status 'packages/unstable/i386/r/racc',
'status' => 'tmpfail',
'previous_status' => 'pass'
mkdir_p 'packages/unstable/amd64/s/slowpackage'
latest_status 'packages/unstable/amd64/s/slowpackage', {
'status' => 'pass',
'duration_seconds' => 5000,
}
latest_status 'packages/unstable/amd64/s/slowpackage',
'status' => 'pass',
'duration_seconds' => 5000
mkdir_p 'packages/unstable/amd64/n/newsworthypacakge'
latest_status 'packages/unstable/amd64/n/newsworthypacakge', {
'status' => 'pass',
'previous_status' => 'fail',
}
latest_status 'packages/unstable/amd64/n/newsworthypacakge',
'status' => 'pass',
'previous_status' => 'fail'
end
after(:all) do
......@@ -89,10 +82,10 @@ describe Debci::Repository do
end
def mkdir_p(path)
FileUtils.mkdir_p(File.join@datadir, path)
FileUtils.mkdir_p(File.join(@datadir, path))
end
def get_run_id
def fetch_run_id
@run_id ||= 0
@run_id += 1
end
......@@ -100,7 +93,7 @@ describe Debci::Repository do
def history(path, data)
previous_status = nil
data.each do |entry|
entry['run_id'] = get_run_id
entry['run_id'] = fetch_run_id
entry['previous_status'] = previous_status if previous_status
previous_status = entry['status']
end
......@@ -111,7 +104,7 @@ describe Debci::Repository do
def latest_status(path, data)
package = File.basename(path)
run_id = get_run_id
run_id = fetch_run_id
File.open(File.join(@datadir, path, 'latest.json'), 'w') do |f|
f.write(JSON.dump({ 'package' => package, 'run_id' => run_id }.merge(data)))
end
......@@ -120,11 +113,11 @@ describe Debci::Repository do
let(:repository) { Debci::Repository.new(@datadir) }
it 'knows about architectures' do
expect(repository.architectures).to eq(['amd64', 'i386'])
expect(repository.architectures).to eq(%w[amd64 i386])
end
it 'knows about suites' do
expect(repository.suites).to eq(['testing', 'unstable'])
expect(repository.suites).to eq(%w[testing unstable])
end
it 'knows about prefixes' do
......@@ -148,7 +141,7 @@ describe Debci::Repository do
end
it 'raises an exception when package is not found' do
expect(lambda { repository.find_package('doesnotexist') }).to raise_error(Debci::Repository::PackageNotFound)
expect(-> { repository.find_package('doesnotexist') }).to raise_error(Debci::Repository::PackageNotFound)
end
it 'searches for packages with exact match' do
......@@ -165,8 +158,8 @@ describe Debci::Repository do
expect(statuses.first.length).to eq(2) # 2 architectures
statuses.flatten.each do |s|
expect(s).to be_a(Debci::Status)
expect(['unstable', 'testing']).to include(s.suite)
expect(['amd64', 'i386']).to include(s.architecture)
expect(%w[unstable testing]).to include(s.suite)
expect(%w[amd64 i386]).to include(s.architecture)
end
end
......@@ -175,8 +168,8 @@ describe Debci::Repository do
expect(statuses.length).to eq(3)
statuses.each do |s|
expect(s).to be_a(Debci::Status)
expect(['unstable', 'testing']).to include(s.suite)
expect(['amd64', 'i386']).to include(s.architecture)
expect(%w[unstable testing]).to include(s.suite)
expect(%w[amd64 i386]).to include(s.architecture)
end
end
......@@ -258,8 +251,8 @@ describe Debci::Repository do
expect(slow).to be_a(Array)
expect(slow.length).to be >= 1
status = slow.find { |status| status.package == 'slowpackage' }
expect(status).to be_a(Debci::Status)
slow_status = slow.find { |status| status.package == 'slowpackage' }
expect(slow_status).to be_a(Debci::Status)
end
it 'knows about platform-specific issues' do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment