#354:added specs for autostart build_lists

This commit is contained in:
Vokhmin Alexey V 2014-02-18 01:02:48 +04:00
parent 5bebb4d5a8
commit ea8bb0b42b
11 changed files with 101 additions and 22 deletions

View File

@ -4,6 +4,7 @@ gem 'rails', '3.2.16' #, git: 'git://github.com/rails/rails.git'
gem 'redhillonrails_core', git: 'git://github.com/rosa-abf/redhillonrails_core.git', branch: 'rails31' # '~> 2.0.0.pre' # deprecated
gem 'pg', '~> 0.14.0'
gem 'activerecord-postgres-hstore'
gem 'devise', '~> 2.2.3'
gem 'omniauth'

View File

@ -56,6 +56,10 @@ GEM
activesupport (= 3.2.16)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activerecord-postgres-hstore (0.7.7)
activerecord (>= 3.1)
pg-hstore (>= 1.1.5)
rake
activeresource (3.2.16)
activemodel (= 3.2.16)
activesupport (= 3.2.16)
@ -272,6 +276,7 @@ GEM
cocaine (~> 0.5.3)
mime-types
pg (0.14.1)
pg-hstore (1.2.0)
polyglot (0.3.3)
posix-spawn (0.3.8)
puma (2.7.1)
@ -459,6 +464,7 @@ PLATFORMS
DEPENDENCIES
RedCloth
activerecord-postgres-hstore
airbrake (~> 3.1.2)
ancestry (~> 1.3.0)
angular-i18n (= 0.1.2)

View File

@ -49,11 +49,10 @@ class Project < ActiveRecord::Base
attr_accessible :name, :description, :visibility, :srpm, :is_package, :default_branch,
:has_issues, :has_wiki, :maintainer_id, :publish_i686_into_x86_64,
:url, :srpms_list, :mass_import, :add_to_repository_id, :architecture_dependent
:url, :srpms_list, :mass_import, :add_to_repository_id, :architecture_dependent,
:autostart_status
attr_readonly :owner_id, :owner_type
serialize :default_platforms, Array
scope :recent, order("lower(#{table_name}.name) ASC")
scope :search_order, order("CHAR_LENGTH(#{table_name}.name) ASC")
scope :search, lambda {|q|
@ -172,7 +171,7 @@ class Project < ActiveRecord::Base
# Select main and project platform repository(contrib, non-free and etc)
# If main does not exist, will connect only project platform repository
# If project platform repository is main, only main will be connect
main_rep_id = build_for_platform.repositories.find_by_name(%w(main base)).try(:id)
main_rep_id = build_for_platform.repositories.main.first.try(:id)
include_repos = ([main_rep_id] << (save_to_platform.main? ? repository_id : nil)).compact.uniq
project_version = project_version_for save_to_platform, build_for_platform
@ -301,26 +300,29 @@ class Project < ActiveRecord::Base
class << self
Modules::Models::Autostart::HUMAN_AUTOSTART_STATUSES.each do |autostart_status, human_autostart_status|
define_method "autostart_build_lists_#{human_autostart_status}" do
autostart_iso_builds autostart_status
autostart_build_lists autostart_status
end
end
end
def self.autostart_build_lists(autostart_status)
Project.where(autostart_status: autostart_status).find_each do |p|
Platform.where(id: p.default_platforms).each do |platform|
platform.platform_arch_settings.pluck(:arch_id).each do |arch_id|
build_list = build_lists.build do |bl|
p.project_to_repositories.autostart_enabled.includes(repository: :platform).each do |p_to_r|
repository = p_to_r.repository
platform = repository.platform
platform.platform_arch_settings.by_default.pluck(:arch_id).each do |arch_id|
build_list = p.build_lists.build do |bl|
bl.save_to_platform = platform
bl.build_for_platform = platform
bl.update_type = 'newpackage'
bl.arch_id = arch_id
bl.project_version = project_version_for(platform, platform)
bl.user = p.owner.is_a?(Group) ? p.owner.owner : p.owner
bl.auto_publish = true # TODO: ???
bl.save_to_repository_id = p.repositories.where(platform_id: platform).first
bl.project_version = p.project_version_for(platform, platform)
bl.user = User.find(p_to_r.user_id)
bl.auto_publish = p_to_r.auto_publish == 'true'
bl.save_to_repository = repository
bl.include_repos = [repository.id, platform.repositories.main.first.try(:id)].uniq.compact
end
build_list.save
build_list.save!
end
end
end

View File

@ -1,13 +1,22 @@
class ProjectToRepository < ActiveRecord::Base
AUTOSTART_OPTIONS = %w(auto_publish user_id enabled)
belongs_to :project
belongs_to :repository
delegate :path, to: :project
scope :autostart_enabled, lambda { where("autostart_options -> 'enabled' = 'true'") }
after_destroy lambda { project.destroy_project_from_repository(repository) }, unless: lambda {Thread.current[:skip]}
validate :one_project_in_platform_repositories, on: :create
serialize :autostart_options, ActiveRecord::Coders::Hstore
AUTOSTART_OPTIONS.each do |field|
store_accessor :autostart_options, field
end
protected
def one_project_in_platform_repositories

View File

@ -21,7 +21,8 @@ class Repository < ActiveRecord::Base
validates :description, presence: true
validates :name, uniqueness: {scope: :platform_id, case_sensitive: false}, presence: true, format: {with: /\A[a-z0-9_\-]+\z/}
scope :recent, order("#{table_name}.name ASC")
scope :recent, order("#{table_name}.name ASC")
scope :main, lambda { where(name: %w(main base)) }
before_destroy :detele_directory

View File

@ -1,6 +0,0 @@
class AddAutostartStatusToProjects < ActiveRecord::Migration
def change
add_column :projects, :autostart_status, :integer
add_column :projects, :default_platforms, :text
end
end

View File

@ -0,0 +1,9 @@
class SetupHstore < ActiveRecord::Migration
def self.up
execute "CREATE EXTENSION IF NOT EXISTS hstore"
end
def self.down
execute "DROP EXTENSION IF EXISTS hstore"
end
end

View File

@ -0,0 +1,6 @@
class AddAutostartStatusToProjects < ActiveRecord::Migration
def change
add_column :projects, :autostart_status, :integer
add_column :project_to_repositories, :autostart_options, :hstore#, default: '', null: false
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20140216203553) do
ActiveRecord::Schema.define(:version => 20140217192640) do
create_table "activity_feeds", :force => true do |t|
t.integer "user_id", :null => false
@ -416,6 +416,7 @@ ActiveRecord::Schema.define(:version => 20140216203553) do
t.integer "repository_id"
t.datetime "created_at"
t.datetime "updated_at"
t.hstore "autostart_options"
end
add_index "project_to_repositories", ["repository_id", "project_id"], :name => "index_project_to_repositories_on_repository_id_and_project_id", :unique => true
@ -442,7 +443,6 @@ ActiveRecord::Schema.define(:version => 20140216203553) do
t.string "owner_uname", :null => false
t.boolean "architecture_dependent", :default => false, :null => false
t.integer "autostart_status"
t.text "default_platforms"
end
add_index "projects", ["owner_id", "name", "owner_type"], :name => "index_projects_on_name_and_owner_id_and_owner_type", :unique => true, :case_sensitive => false

View File

@ -0,0 +1,8 @@
FactoryGirl.define do
factory :platform_arch_setting do
association :platform, factory: :platform
association :arch, factory: :arch
default true
time_living 777
end
end

View File

@ -181,4 +181,47 @@ describe Project do
owner.projects.should have(2).items
end
context '#autostart_build_lists_once_a_*' do
let(:project) { FactoryGirl.create(:project_with_commit) }
let(:repository) { FactoryGirl.create(:repository) }
let(:user) { FactoryGirl.create(:user) }
before do
repository.add_member user
repository.projects << project
p_to_r = project.project_to_repositories.where(repository_id: repository).first
p_to_r.enabled = true
p_to_r.user_id = user.id
p_to_r.save
FactoryGirl.create(:platform_arch_setting, platform: repository.platform)
FactoryGirl.create(:platform_arch_setting, platform: repository.platform, default: false)
end
it { ProjectToRepository.autostart_enabled.should have(1).item }
it { repository.platform.platform_arch_settings.should have(2).item }
shared_examples_for 'autostart build_lists' do |once_a_12_hours, once_a_day, once_a_week|
it { lambda { Project.autostart_build_lists_once_a_12_hours }.should change{ BuildList.count }.by(once_a_12_hours) }
it { lambda { Project.autostart_build_lists_once_a_day }.should change{ BuildList.count }.by(once_a_day) }
it { lambda { Project.autostart_build_lists_once_a_week }.should change{ BuildList.count }.by(once_a_week) }
end
context 'once_a_12_hours' do
before { project.update_attributes(autostart_status: Modules::Models::Autostart::ONCE_A_12_HOURS) }
it_should_behave_like 'autostart build_lists', 1, 0, 0
end
context 'once_a_day' do
before { project.update_attributes(autostart_status: Modules::Models::Autostart::ONCE_A_DAY) }
it_should_behave_like 'autostart build_lists', 0, 1, 0
end
context 'once_a_day' do
before { project.update_attributes(autostart_status: Modules::Models::Autostart::ONCE_A_WEEK) }
it_should_behave_like 'autostart build_lists', 0, 0, 1
end
end
end