foreman_ansible 1.5.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +1 -1
  3. data/app/controllers/foreman_ansible/api/v2/hostgroups_controller_extensions.rb +6 -21
  4. data/app/controllers/foreman_ansible/api/v2/hosts_controller_extensions.rb +5 -18
  5. data/app/controllers/foreman_ansible/concerns/hostgroups_controller_extensions.rb +12 -5
  6. data/app/controllers/foreman_ansible/concerns/hosts_controller_extensions.rb +20 -16
  7. data/app/controllers/foreman_ansible/concerns/job_invocation_helper.rb +17 -0
  8. data/app/helpers/foreman_ansible/ansible_plugin_helper.rb +1 -5
  9. data/app/helpers/foreman_ansible/ansible_reports_helper.rb +2 -1
  10. data/app/helpers/foreman_ansible/hosts_helper_extensions.rb +19 -18
  11. data/app/lib/actions/foreman_ansible/helpers/host_common.rb +1 -5
  12. data/app/models/ansible_role.rb +5 -0
  13. data/app/models/concerns/foreman_ansible/has_many_ansible_roles.rb +1 -3
  14. data/app/models/concerns/foreman_ansible/host_managed_extensions.rb +9 -0
  15. data/app/models/concerns/foreman_ansible/hostgroup_extensions.rb +0 -4
  16. data/app/models/foreman_ansible/ansible_provider.rb +24 -0
  17. data/app/models/setting/ansible.rb +4 -1
  18. data/app/services/foreman_ansible/fact_parser.rb +4 -2
  19. data/app/services/foreman_ansible/inventory_creator.rb +7 -2
  20. data/app/services/foreman_ansible/proxy_selector.rb +6 -0
  21. data/app/services/foreman_ansible/ui_roles_importer.rb +2 -0
  22. data/app/views/ansible_roles/index.html.erb +1 -2
  23. data/app/views/foreman_ansible/ansible_roles/_hostgroup_ansible_roles_button.erb +8 -7
  24. data/app/views/foreman_ansible/job_templates/ansible_roles.erb +17 -0
  25. data/config/routes.rb +1 -0
  26. data/db/migrate/20160705082036_create_ansible_role.rb +1 -1
  27. data/db/migrate/20160706074540_create_join_table_hosts_ansible_roles.rb +1 -1
  28. data/db/migrate/20160707195442_create_host_ansible_roles.rb +1 -1
  29. data/db/migrate/20160729094457_add_columns_to_ansible_role.rb +1 -1
  30. data/db/migrate/20160802153302_create_join_table_hostgroup_ansible_roles.rb +2 -1
  31. data/db/migrate/20160805094233_add_primary_key_hostgroup_ansible_roles.rb +1 -1
  32. data/db/migrate/20161122154057_automatically_set_role_timestamps.rb +1 -1
  33. data/db/seeds.d/75_job_templates.rb +20 -0
  34. data/lib/foreman_ansible/engine.rb +11 -53
  35. data/lib/foreman_ansible/register.rb +52 -0
  36. data/lib/foreman_ansible/remote_execution.rb +22 -0
  37. data/lib/foreman_ansible/version.rb +1 -1
  38. data/test/factories/ansible_proxy.rb +1 -1
  39. data/test/factories/ansible_roles.rb +1 -1
  40. data/test/functional/ansible_roles_controller_test.rb +4 -2
  41. data/test/functional/api/v2/ansible_roles_controller_test.rb +5 -3
  42. data/test/functional/api/v2/hostgroups_controller_test.rb +17 -25
  43. data/test/functional/api/v2/hosts_controller_test.rb +16 -24
  44. data/test/functional/hosts_controller_test.rb +33 -29
  45. data/test/test_plugin_helper.rb +12 -2
  46. data/test/unit/actions/run_ansible_job_test.rb +0 -0
  47. data/test/unit/actions/run_proxy_ansible_command_test.rb +0 -0
  48. data/test/unit/concerns/host_managed_extensions_test.rb +7 -7
  49. data/test/unit/concerns/hostgroup_extensions_test.rb +6 -6
  50. data/test/unit/helpers/foreman_ansible/ansible_reports_helper_test.rb +2 -2
  51. data/test/unit/host_ansible_role_test.rb +2 -2
  52. data/test/unit/hostgroup_ansible_role_test.rb +2 -2
  53. data/test/unit/lib/foreman_ansible_core/roles_reader_test.rb +4 -5
  54. data/test/unit/services/fact_importer_test.rb +3 -5
  55. data/test/unit/services/fact_parser_test.rb +0 -2
  56. data/test/unit/services/fact_sparser_test.rb +0 -2
  57. data/test/unit/services/inventory_creator_test.rb +1 -1
  58. data/test/unit/services/proxy_selector_test.rb +4 -4
  59. data/test/unit/services/roles_importer_test.rb +2 -2
  60. data/test/unit/services/structured_fact_importer_test.rb +1 -1
  61. data/test/unit/services/ui_roles_importer_test.rb +1 -1
  62. metadata +44 -31
  63. data/app/lib/actions/foreman_ansible/play_host_roles.rb +0 -42
  64. data/app/lib/actions/foreman_ansible/play_hostgroup_roles.rb +0 -56
  65. data/app/lib/actions/foreman_ansible/play_hosts_roles.rb +0 -26
  66. data/test/fixtures/ansible_permissions.yml +0 -11
  67. data/test/support/fixture_support.rb +0 -29
  68. data/test/support/foreman_tasks/task.rb +0 -48
  69. data/test/support/foreman_test_helper_additions.rb +0 -22
@@ -16,13 +16,18 @@ module ForemanAnsible
16
16
  # more advanced cases). Therefore we have only the 'all' group
17
17
  # with all hosts.
18
18
  def to_hash
19
- { 'all' => { 'hosts' => hosts.map(&:fqdn) },
19
+ hosts = @hosts.map do |h|
20
+ RemoteExecutionProvider.find_ip_or_hostname(h)
21
+ end
22
+ { 'all' => { 'hosts' => hosts },
20
23
  '_meta' => { 'hostvars' => hosts_vars } }
21
24
  end
22
25
 
23
26
  def hosts_vars
24
27
  hosts.reduce({}) do |hash, host|
25
- hash.update(host.fqdn => host_vars(host))
28
+ hash.update(
29
+ RemoteExecutionProvider.find_ip_or_hostname(host) => host_vars(host)
30
+ )
26
31
  end
27
32
  end
28
33
 
@@ -3,6 +3,12 @@ module ForemanAnsible
3
3
  class ProxySelector < ::ForemanTasks::ProxySelector
4
4
  def available_proxies(host)
5
5
  proxies = {}
6
+ if host.execution_interface && host.execution_interface.subnet
7
+ proxies[:subnet] = host.
8
+ execution_interface.
9
+ subnet.
10
+ remote_execution_proxies.with_features(provider)
11
+ end
6
12
  proxies[:fallback] = host.smart_proxies.with_features('Ansible')
7
13
  proxies[:global] = proxy_scope(host).authorized.with_features('Ansible')
8
14
  proxies
@@ -11,6 +11,7 @@ module ForemanAnsible
11
11
  delete_old_roles changes['obsolete'] if changes['obsolete']
12
12
  end
13
13
 
14
+ # rubocop:disable Performance/HashEachMethods
14
15
  def create_new_roles(changes)
15
16
  changes.values.each do |new_role|
16
17
  ::AnsibleRole.create(JSON.parse(new_role))
@@ -22,5 +23,6 @@ module ForemanAnsible
22
23
  ::AnsibleRole.find(JSON.parse(old_role)['id']).destroy
23
24
  end
24
25
  end
26
+ # rubocop:enable Performance/HashEachMethods
25
27
  end
26
28
  end
@@ -1,7 +1,6 @@
1
1
  <% title _("Ansible Roles") %>
2
2
 
3
- <% title_actions ansible_proxy_import(hash_for_import_ansible_roles_path),
4
- documentation_button('#4.1ImportingRoles', :root_url => ansible_doc_url) %>
3
+ <% title_actions ansible_proxy_import(hash_for_import_ansible_roles_path) %>
5
4
 
6
5
  <table class="<%= table_css_classes 'table-fixed' %>">
7
6
  <thead>
@@ -1,13 +1,14 @@
1
1
  <%=
2
- play_roles = if hostgroup.all_ansible_roles.empty?
3
- "<a title='No Roles assigned' href=\"#\">#{_('Play Roles')}</a>".html_safe
4
- else
5
- display_link_if_authorized(_('Play Roles'), hash_for_play_roles_hostgroup_path(:id => hostgroup), :'data-no-turbolink' => true)
6
- end
7
-
2
+ if hostgroup.all_ansible_roles.empty?
8
3
  action_buttons(
9
4
  display_link_if_authorized(_('Nest'), hash_for_nest_hostgroup_path(:id => hostgroup)),
10
5
  display_link_if_authorized(_('Clone'), hash_for_clone_hostgroup_path(:id => hostgroup)),
11
- play_roles,
12
6
  display_delete_if_authorized(hash_for_hostgroup_path(:id => hostgroup).merge(:auth_object => hostgroup, :authorizer => authorizer), :data => { :confirm => warning_message(hostgroup) }))
7
+ else
8
+ action_buttons(
9
+ display_link_if_authorized(_('Nest'), hash_for_nest_hostgroup_path(:id => hostgroup)),
10
+ display_link_if_authorized(_('Clone'), hash_for_clone_hostgroup_path(:id => hostgroup)),
11
+ display_link_if_authorized(_('Play Roles'), hash_for_play_roles_hostgroup_path(:id => hostgroup), :'data-no-turbolink' => true),
12
+ display_delete_if_authorized(hash_for_hostgroup_path(:id => hostgroup).merge(:auth_object => hostgroup, :authorizer => authorizer), :data => { :confirm => warning_message(hostgroup) }))
13
+ end
13
14
  %>
@@ -0,0 +1,17 @@
1
+ <%#
2
+ kind: job_template
3
+ name: Ansible Roles - Ansible Default
4
+ job_category: Ansible Playbook
5
+ description_format: 'Run Ansible roles for host'
6
+ feature: ansible_run_host
7
+ provider_type: Ansible
8
+ %>
9
+
10
+ ---
11
+ - hosts: <%= @host.name %>
12
+ tasks:
13
+ - debug: msg=These are the parameters "{{ foreman_params }}"
14
+ roles:
15
+ <% if @host.all_ansible_roles.present? -%>
16
+ - <%= @host.all_ansible_roles.map { |role| role.name.strip }.join("\n - ") %>
17
+ <% end -%>
@@ -61,3 +61,4 @@ Rails.application.routes.draw do
61
61
  end
62
62
  end
63
63
  end
64
+ # rubocop:enable BlockLength
@@ -1,5 +1,5 @@
1
1
  # Creates simple model of Ansible Role
2
- class CreateAnsibleRole < ActiveRecord::Migration
2
+ class CreateAnsibleRole < ActiveRecord::Migration[4.2]
3
3
  def change
4
4
  create_table :ansible_roles do |t|
5
5
  t.string :name, :null => false, :limit => 255, :unique => true
@@ -1,5 +1,5 @@
1
1
  # Defines the relation between Host and AnsibleRole
2
- class CreateJoinTableHostsAnsibleRoles < ActiveRecord::Migration
2
+ class CreateJoinTableHostsAnsibleRoles < ActiveRecord::Migration[4.2]
3
3
  def change
4
4
  create_join_table :ansible_roles, :hosts do |t|
5
5
  t.index [:host_id, :ansible_role_id]
@@ -1,5 +1,5 @@
1
1
  # Renames join table between Host and Ansible Roles
2
- class CreateHostAnsibleRoles < ActiveRecord::Migration
2
+ class CreateHostAnsibleRoles < ActiveRecord::Migration[4.2]
3
3
  def change
4
4
  rename_table :ansible_roles_hosts, :host_ansible_roles
5
5
  add_column :host_ansible_roles, :id, :primary_key
@@ -1,5 +1,5 @@
1
1
  # to keep track of when the roles were imported
2
- class AddColumnsToAnsibleRole < ActiveRecord::Migration
2
+ class AddColumnsToAnsibleRole < ActiveRecord::Migration[4.2]
3
3
  def up
4
4
  add_column :ansible_roles, :created_at, :datetime, :default => Time.now.utc
5
5
  add_column :ansible_roles, :updated_at, :datetime, :default => Time.now.utc
@@ -1,10 +1,11 @@
1
1
  # Defines the relation between Hostgroup and AnsibleRole
2
2
  # rubocop:disable Metrics/LineLength
3
- class CreateJoinTableHostgroupAnsibleRoles < ActiveRecord::Migration
3
+ class CreateJoinTableHostgroupAnsibleRoles < ActiveRecord::Migration[4.2]
4
4
  def change
5
5
  create_join_table :hostgroup, :ansible_roles, :table_name => 'hostgroup_ansible_roles' do |t|
6
6
  t.index [:hostgroup_id, :ansible_role_id], :name => 'index_ansible_roles_hostgroup_on_hostgroup_id_and_role_id'
7
7
  t.index [:ansible_role_id, :hostgroup_id], :name => 'index_ansible_roles_hostgroup_on_role_id_and_hostgroup_id'
8
+ # rubocop:enable Metrics/LineLength
8
9
  end
9
10
  end
10
11
  end
@@ -1,5 +1,5 @@
1
1
  # Adds primary key to join table between Hostgroup and Ansible Role
2
- class AddPrimaryKeyHostgroupAnsibleRoles < ActiveRecord::Migration
2
+ class AddPrimaryKeyHostgroupAnsibleRoles < ActiveRecord::Migration[4.2]
3
3
  def change
4
4
  add_column :hostgroup_ansible_roles, :id, :primary_key
5
5
  end
@@ -1,5 +1,5 @@
1
1
  # Creation and updates timestamps for Ansible Roles
2
- class AutomaticallySetRoleTimestamps < ActiveRecord::Migration
2
+ class AutomaticallySetRoleTimestamps < ActiveRecord::Migration[4.2]
3
3
  def up
4
4
  change_column :ansible_roles, :created_at, :datetime, :null => true,
5
5
  :default => nil
@@ -0,0 +1,20 @@
1
+ User.as_anonymous_admin do
2
+ if Rails.env.test? || File.basename($0) == 'rake'
3
+ # If this file tries to import a template with a REX feature in a SeedsTest,
4
+ # it will fail - the REX feature isn't registered on SeedsTest because
5
+ # DatabaseCleaner truncates the db before every test.
6
+ # During db:seed, we also want to know the feature is registered before
7
+ # seeding the template
8
+ ForemanAnsible::Engine.register_rex_feature
9
+ end
10
+ JobTemplate.without_auditing do
11
+ Dir[File.join("#{ForemanAnsible::Engine.root}/app/views/foreman_ansible/"\
12
+ 'job_templates/**/*.erb')].each do |template|
13
+ sync = !Rails.env.test? && Setting[:remote_execution_sync_templates]
14
+ JobTemplate.import_raw!(File.read(template),
15
+ :default => true,
16
+ :locked => true,
17
+ :update => sync)
18
+ end
19
+ end
20
+ end
@@ -2,10 +2,10 @@ require 'deface'
2
2
  require 'fast_gettext'
3
3
  require 'gettext_i18n_rails'
4
4
  require 'foreman_ansible_core'
5
+ require 'foreman_ansible/remote_execution'
5
6
 
6
7
  module ForemanAnsible
7
8
  # This engine connects ForemanAnsible with Foreman core
8
- # rubocop:disable ClassLength
9
9
  class Engine < ::Rails::Engine
10
10
  engine_name 'foreman_ansible'
11
11
 
@@ -31,58 +31,8 @@ module ForemanAnsible
31
31
  Foreman::Gettext::Support.add_text_domain locale_domain, locale_dir
32
32
  end
33
33
 
34
- # rubocop:disable BlockLength
35
34
  initializer 'foreman_ansible.register_plugin', :before => :finisher_hook do
36
- Foreman::Plugin.register :foreman_ansible do
37
- requires_foreman '>= 1.15'
38
-
39
- security_block :foreman_ansible do
40
- permission :play_roles_on_host,
41
- { :hosts => [:play_roles, :multiple_play_roles],
42
- :'api/v2/hosts' => [:play_roles,
43
- :multiple_play_roles] },
44
- :resource_type => 'Host'
45
- permission :play_roles_on_hostgroup,
46
- { :hostgroups => [:play_roles],
47
- :'api/v2/hostgroups' => [:play_roles,
48
- :multiple_play_roles] },
49
- :resource_type => 'Hostgroup'
50
- permission :view_ansible_roles,
51
- { :ansible_roles => [:index],
52
- :'api/v2/ansible_roles' => [:index, :show] },
53
- :resource_type => 'AnsibleRole'
54
- permission :destroy_ansible_roles,
55
- { :ansible_roles => [:destroy],
56
- :'api/v2/ansible_roles' => [:destroy, :obsolete] },
57
- :resource_type => 'AnsibleRole'
58
- permission :import_ansible_roles,
59
- { :ansible_roles => [:import, :confirm_import],
60
- :'api/v2/ansible_roles' => [:import] },
61
- :resource_type => 'AnsibleRole'
62
- end
63
-
64
- role 'Ansible Roles Manager',
65
- [:play_roles_on_host, :play_roles_on_hostgroup,
66
- :view_ansible_roles, :destroy_ansible_roles,
67
- :import_ansible_roles]
68
-
69
- add_all_permissions_to_default_roles
70
-
71
- role_assignment_params = { :ansible_role_ids => [],
72
- :ansible_roles => [] }
73
- parameter_filter Host::Managed, role_assignment_params
74
- parameter_filter Hostgroup, role_assignment_params
75
-
76
- divider :top_menu, :caption => N_('Ansible'), :parent => :configure_menu
77
- menu :top_menu, :ansible_roles,
78
- :caption => N_('Roles'),
79
- :url_hash => { :controller => :ansible_roles, :action => :index },
80
- :parent => :configure_menu
81
-
82
- apipie_documented_controllers [
83
- "#{ForemanAnsible::Engine.root}/app/controllers/api/v2/*.rb"
84
- ]
85
- end
35
+ require 'foreman_ansible/register'
86
36
  end
87
37
 
88
38
  initializer('foreman_ansible.require_dynflow',
@@ -117,6 +67,7 @@ module ForemanAnsible
117
67
  Apipie.configuration.checksum_path += ['/foreman_ansible/api/']
118
68
  end
119
69
 
70
+ # rubocop:disable BlockLength
120
71
  config.to_prepare do
121
72
  begin
122
73
  foreman_version = ::Foreman::Version.new
@@ -146,9 +97,16 @@ module ForemanAnsible
146
97
  ::Api::V2::HostgroupsController.send(
147
98
  :include, ForemanAnsible::Api::V2::HostgroupsControllerExtensions
148
99
  )
149
- rescue => e
100
+ rescue StandardError => e
150
101
  Rails.logger.warn "Foreman Ansible: skipping engine hook (#{e})"
151
102
  end
152
103
  end
104
+ # rubocop:enable BlockLength
105
+
106
+ rake_tasks do
107
+ Rake::Task['db:seed'].enhance do
108
+ ForemanAnsible::Engine.load_seed
109
+ end
110
+ end
153
111
  end
154
112
  end
@@ -0,0 +1,52 @@
1
+ # rubocop:disable BlockLength
2
+ Foreman::Plugin.register :foreman_ansible do
3
+ requires_foreman '>= 1.16'
4
+
5
+ security_block :foreman_ansible do
6
+ permission :play_roles_on_host,
7
+ { :hosts => [:play_roles, :multiple_play_roles],
8
+ :'api/v2/hosts' => [:play_roles,
9
+ :multiple_play_roles] },
10
+ :resource_type => 'Host'
11
+ permission :play_roles_on_hostgroup,
12
+ { :hostgroups => [:play_roles],
13
+ :'api/v2/hostgroups' => [:play_roles,
14
+ :multiple_play_roles] },
15
+ :resource_type => 'Hostgroup'
16
+ permission :view_ansible_roles,
17
+ { :ansible_roles => [:index],
18
+ :'api/v2/ansible_roles' => [:index, :show] },
19
+ :resource_type => 'AnsibleRole'
20
+ permission :destroy_ansible_roles,
21
+ { :ansible_roles => [:destroy],
22
+ :'api/v2/ansible_roles' => [:destroy, :obsolete] },
23
+ :resource_type => 'AnsibleRole'
24
+ permission :import_ansible_roles,
25
+ { :ansible_roles => [:import, :confirm_import],
26
+ :'api/v2/ansible_roles' => [:import] },
27
+ :resource_type => 'AnsibleRole'
28
+ end
29
+
30
+ role 'Ansible Roles Manager',
31
+ [:play_roles_on_host, :play_roles_on_hostgroup,
32
+ :view_ansible_roles, :destroy_ansible_roles,
33
+ :import_ansible_roles]
34
+
35
+ add_all_permissions_to_default_roles
36
+
37
+ role_assignment_params = { :ansible_role_ids => [],
38
+ :ansible_roles => [] }
39
+ parameter_filter Host::Managed, role_assignment_params
40
+ parameter_filter Hostgroup, role_assignment_params
41
+
42
+ divider :top_menu, :caption => N_('Ansible'), :parent => :configure_menu
43
+ menu :top_menu, :ansible_roles,
44
+ :caption => N_('Roles'),
45
+ :url_hash => { :controller => :ansible_roles, :action => :index },
46
+ :parent => :configure_menu
47
+
48
+ apipie_documented_controllers [
49
+ "#{ForemanAnsible::Engine.root}/app/controllers/api/v2/*.rb"
50
+ ]
51
+ end
52
+ # rubocop:enable BlockLength
@@ -0,0 +1,22 @@
1
+ require 'foreman_remote_execution'
2
+
3
+ module ForemanAnsible
4
+ # Dependencies related with the remote execution plugin
5
+ class Engine < ::Rails::Engine
6
+ config.to_prepare do
7
+ RemoteExecutionProvider.register(
8
+ :Ansible,
9
+ ForemanAnsible::AnsibleProvider
10
+ )
11
+ end
12
+
13
+ def self.register_rex_feature
14
+ RemoteExecutionFeature.register(
15
+ :ansible_run_host,
16
+ N_('Ansible: Run host roles'),
17
+ :description => N_('Runs an Ansible playbook which contains all'\
18
+ ' the roles defined for a host')
19
+ )
20
+ end
21
+ end
22
+ end
@@ -2,5 +2,5 @@
2
2
  # This way other parts of Foreman can just call ForemanAnsible::VERSION
3
3
  # and detect what version the plugin is running.
4
4
  module ForemanAnsible
5
- VERSION = '1.5.2'.freeze
5
+ VERSION = '2.0.0'.freeze
6
6
  end
@@ -1,4 +1,4 @@
1
- FactoryGirl.modify do
1
+ FactoryBot.modify do
2
2
  factory :smart_proxy do
3
3
  trait :with_ansible do
4
4
  features { [::Feature.find_or_create_by(:name => 'Ansible')] }
@@ -1,4 +1,4 @@
1
- FactoryGirl.define do
1
+ FactoryBot.define do
2
2
  factory :ansible_role do
3
3
  sequence(:name) { |n| "ansible_role_#{n}" }
4
4
  end
@@ -2,14 +2,16 @@ require 'test_plugin_helper'
2
2
  # functional tests for AnsibleRolesController
3
3
  class AnsibleRolesControllerTest < ActionController::TestCase
4
4
  setup do
5
- @role = FactoryGirl.create(:ansible_role)
5
+ @role = FactoryBot.create(:ansible_role)
6
6
  end
7
7
 
8
8
  basic_index_test
9
9
 
10
10
  test 'should destroy role' do
11
11
  assert_difference('AnsibleRole.count', -1) do
12
- delete :destroy, { :id => @role.id }, set_session_user
12
+ delete :destroy,
13
+ :params => { :id => @role.id },
14
+ :session => set_session_user
13
15
  end
14
16
  assert_redirected_to ansible_roles_url
15
17
  end
@@ -5,18 +5,20 @@ module Api
5
5
  # Tests for the controller to CRUD Ansible Roles
6
6
  class AnsibleRolesControllerTest < ActionController::TestCase
7
7
  setup do
8
- @role = FactoryGirl.create(:ansible_role)
8
+ @role = FactoryBot.create(:ansible_role)
9
9
  end
10
10
 
11
11
  test 'should get index' do
12
- get :index, {}, set_session_user
12
+ get :index, :session => set_session_user
13
13
  response = JSON.parse(@response.body)
14
14
  refute_empty response['results']
15
15
  assert_response :success
16
16
  end
17
17
 
18
18
  test 'should destroy' do
19
- delete :destroy, { :id => @role.id }, set_session_user
19
+ delete :destroy,
20
+ :params => { :id => @role.id },
21
+ :session => set_session_user
20
22
  assert_response :ok
21
23
  refute AnsibleRole.exists?(@role.id)
22
24
  end
@@ -5,46 +5,38 @@ module Api
5
5
  module V2
6
6
  # Tests for the extra methods to play roles on Hostgroup
7
7
  class HostgroupsControllerTest < ActionController::TestCase
8
- include ::Dynflow::Testing
9
-
10
8
  setup do
11
- @host1 = FactoryGirl.create(:host, :with_hostgroup)
12
- @host2 = FactoryGirl.create(:host, :with_hostgroup)
13
- end
14
-
15
- after do
16
- ::ForemanTasks::Task::DynflowTask.all.each do |task|
17
- task.destroy
18
- task.delete
19
- end
9
+ @host1 = FactoryBot.create(:host, :with_hostgroup)
10
+ @host2 = FactoryBot.create(:host, :with_hostgroup)
20
11
  end
21
12
 
22
13
  test 'should return an not_found due to non-existent host_id' do
23
- post :play_roles, :id => 'non-existent'
14
+ post :play_roles, :params => { :id => 'non-existent' }
24
15
  response = JSON.parse(@response.body)
25
16
  refute_empty response
26
17
  assert_response :not_found
27
18
  end
28
19
 
29
20
  test 'should trigger task on host group' do
30
- post :play_roles, :id => @host1.hostgroup.id
21
+ load File.join(ForemanAnsible::Engine.root,
22
+ '/db/seeds.d/75_job_templates.rb')
23
+ ::JobInvocationComposer.any_instance.expects(:trigger!).returns(true)
24
+ target = @host1
25
+ post :play_roles, :params => { :id => target.hostgroup.id }
31
26
  response = JSON.parse(@response.body)
32
-
33
- assert response['message']['foreman_tasks'].key?('id'),
34
- 'task id not contained in response'
35
- assert_equal response['message']['hostgroup']['name'],
36
- @host1.hostgroup.name,
37
- 'host group name not contained in response'
38
- assert_response :success
27
+ assert_job_invocation_is_ok(response, target.id)
39
28
  end
40
29
 
41
30
  test 'should trigger two host group tasks' do
42
- post :multiple_play_roles,
43
- :hostgroup_names => [@host1.hostgroup.name, @host2.hostgroup.name]
31
+ load File.join(ForemanAnsible::Engine.root,
32
+ '/db/seeds.d/75_job_templates.rb')
33
+ ::JobInvocationComposer.any_instance.expects(:trigger!).returns(true)
34
+ target = [@host1, @host2]
35
+ post :multiple_play_roles, :params => {
36
+ :hostgroup_ids => target.map(&:hostgroup_id)
37
+ }
44
38
  response = JSON.parse(@response.body)
45
-
46
- assert response['message'].length == 2, 'should trigger two tasks'
47
- assert_response :success
39
+ assert_job_invocation_is_ok(response, target.map(&:id))
48
40
  end
49
41
  end
50
42
  end