Skip to content

Commit

Permalink
Fixes #16013 - Fix tests from core
Browse files Browse the repository at this point in the history
Includes fixes to remove attr_accessible in favor of strong_params, and
also rubocop fixes. Also a few fixes for mysql/postgres in fact importer
  • Loading branch information
dLobatog committed Aug 9, 2016
1 parent 537415c commit 8641a6d
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Expand Up @@ -9,4 +9,4 @@ Style/DotPosition:
EnforcedStyle: 'trailing'

Rails/ActionFilter:
EnforcedStyle: filter
EnforcedStyle: action
Expand Up @@ -7,8 +7,6 @@ module HostManagedExtensions
has_many :host_ansible_roles, :foreign_key => :host_id
has_many :ansible_roles, :through => :host_ansible_roles,
:dependent => :destroy

attr_accessible :ansible_role_ids, :ansible_roles
end
end
end
2 changes: 0 additions & 2 deletions app/models/host_ansible_role.rb
@@ -1,8 +1,6 @@
# Join model that hosts the connection between hosts and ansible_roles
class HostAnsibleRole < ActiveRecord::Base
audited :associated_with => :host, :allow_mass_assignment => true
attr_accessible :host_id, :host, :ansible_role_id, :ansible_role

belongs_to_host
belongs_to :ansible_role

Expand Down
11 changes: 10 additions & 1 deletion lib/foreman_ansible/engine.rb
Expand Up @@ -13,7 +13,8 @@ class Engine < ::Rails::Engine
config.autoload_paths += Dir["#{config.root}/app/services"]
config.autoload_paths += Dir["#{config.root}/app/views"]

initializer 'foreman_ansible.register_gettext', :after => :load_config_initializers do
initializer 'foreman_ansible.register_gettext',
:after => :load_config_initializers do
locale_dir = File.join(File.expand_path('../../..', __FILE__), 'locale')
locale_domain = 'foreman_ansible'

Expand All @@ -24,6 +25,14 @@ class Engine < ::Rails::Engine
Foreman::Plugin.register :foreman_ansible do
# We need ActiveJob, only available post-1.12 because of Rails 4.2
requires_foreman '>= 1.12'

security_block :ansible do
permission :play_roles,
{ :hosts => [:play_roles, :multiple_play_roles] },
:resource_type => 'Host::Managed'
end

parameter_filter Host::Managed, :ansible_role_ids, :ansible_roles
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/factories/ansible_roles.rb
@@ -1,5 +1,5 @@
FactoryGirl.define do
factory :ansible_role, :class => ForemanAnsible::AnsibleRole do
sequence(:name) { |n| "ansible_role_#{n}"}
sequence(:name) { |n| "ansible_role_#{n}" }
end
end
9 changes: 5 additions & 4 deletions test/functional/hosts_controller_test.rb
Expand Up @@ -12,22 +12,23 @@ class HostsControllerExtensionsTest < ActionController::TestCase
:managed => false,
:ansible_role_ids => [@role.id] }
post :create, { :host => host },
set_session_user
set_session_user
assert_redirected_to host_url(assigns('host'))
assert assigns('host').ansible_roles, [@role]
end

test 'update a host with ansible roles' do
host = FactoryGirl.create(:host, :managed => false)
post :update, { :id => host.id, :ansible_role_ids => [@role.id] },
set_session_user
post :update, { :id => host.id,
:host => { :ansible_role_ids => [@role.id] } },
set_session_user
assert_redirected_to host_url(assigns('host'))
assert assigns('host').ansible_roles, [@role]
end

test 'delete a host with ansible roles' do
host = FactoryGirl.create(:host, :managed => false,
:ansible_roles => [@role])
:ansible_roles => [@role])
assert_include @role.hosts, host
delete :destroy, { :id => host.id }, set_session_user
assert_redirected_to hosts_url
Expand Down
5 changes: 3 additions & 2 deletions test/test_plugin_helper.rb
@@ -1,8 +1,9 @@
require 'test_helper'

def sample_facts_file
File.read(File.join(
ForemanAnsible::Engine.root, 'test', 'fixtures', 'sample_facts.json'))
File.read(
File.join(
ForemanAnsible::Engine.root, 'test', 'fixtures', 'sample_facts.json'))
end

def facts_json
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ansible_role_test.rb
@@ -1,4 +1,4 @@
require 'test_plugin_helper'
require 'test_plugin_helper'

class AnsibleRoleTest < ActiveSupport::TestCase
should have_many(:host_ansible_roles)
Expand Down
18 changes: 9 additions & 9 deletions test/unit/fact_importer_test.rb
Expand Up @@ -3,21 +3,21 @@
module ForemanAnsible
class FactImporterTest < ActiveSupport::TestCase
setup do
@host = FactoryGirl.build_stubbed(:host)
@host = FactoryGirl.build(:host)
end

test 'add new facts adds all fact names in the fixture' do
@fact_importer = FactImporter.new(@host, facts_json)
facts_to_be_added = FactSparser.sparse(facts_json[:ansible_facts]).keys +
FactSparser.unsparse(facts_json[:ansible_facts]).keys
FactSparser.unsparse(facts_json[:ansible_facts]).keys
@fact_importer.send(:add_new_facts)
assert (facts_to_be_added - FactName.all.map(&:name)).empty?
assert((facts_to_be_added - FactName.all.map(&:name)).empty?)
end

test 'missing_facts returns facts we do not have in the database' do
@fact_importer = FactImporter.new(@host, facts_json)
@fact_importer.expects(:db_facts).returns('ansible_cmdline' => 'fakevalue')
refute @fact_importer.send(:missing_facts).include?('ansible_cmdline')
@fact_importer.expects(:db_facts).returns('ansible_cmd' => 'fakevalue')
refute @fact_importer.send(:missing_facts).include?('ansible_cmd')
end

describe '#add_fact_value' do
Expand All @@ -36,12 +36,12 @@ class FactImporterTest < ActiveSupport::TestCase
@fact_importer.counters[:added] = 0
assert_difference('@host.fact_values.count', 1) do
@fact_importer.send(:add_fact_value, 'missing_value', missing_fact)
@host.save
# We have to save the host in order to ensure @host.fact_values.count
# resolves properly (otherwise) :add_fact_value just won't save the
# relation
end
end
end

test 'add_fact_value works for hosts that have not been created yet' do
end
end
end

0 comments on commit 8641a6d

Please sign in to comment.