foreman_ansible 6.0.2 → 6.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/ansible_roles_controller.rb +12 -8
  3. data/app/controllers/ansible_variables_controller.rb +4 -0
  4. data/app/controllers/api/v2/ansible_inventories_controller.rb +2 -2
  5. data/app/controllers/api/v2/ansible_roles_controller.rb +32 -11
  6. data/app/controllers/api/v2/ansible_variables_controller.rb +6 -4
  7. data/app/controllers/foreman_ansible/concerns/import_controller_helper.rb +3 -3
  8. data/app/helpers/foreman_ansible/ansible_reports_helper.rb +10 -5
  9. data/app/helpers/foreman_ansible/ansible_roles_data_preparations.rb +95 -0
  10. data/app/helpers/foreman_ansible/ansible_roles_helper.rb +20 -1
  11. data/app/jobs/sync_roles_and_variables.rb +20 -0
  12. data/app/models/concerns/foreman_ansible/host_managed_extensions.rb +10 -7
  13. data/app/models/concerns/foreman_ansible/hostgroup_extensions.rb +20 -5
  14. data/app/models/foreman_ansible/ansible_provider.rb +2 -1
  15. data/app/models/host_ansible_role.rb +1 -0
  16. data/app/models/hostgroup_ansible_role.rb +1 -0
  17. data/app/models/setting/ansible.rb +9 -0
  18. data/app/services/foreman_ansible/ansible_report_importer.rb +4 -0
  19. data/app/services/foreman_ansible/ansible_report_scanner.rb +15 -1
  20. data/app/services/foreman_ansible/api_roles_importer.rb +23 -11
  21. data/app/services/foreman_ansible/fact_parser.rb +21 -1
  22. data/app/services/foreman_ansible/import_roles_and_variables_error_notification.rb +31 -0
  23. data/app/services/foreman_ansible/import_roles_and_variables_success_notification.rb +26 -0
  24. data/app/services/foreman_ansible/inventory_creator.rb +3 -7
  25. data/app/services/foreman_ansible/operating_system_parser.rb +8 -2
  26. data/app/services/foreman_ansible/roles_importer.rb +3 -2
  27. data/app/services/foreman_ansible/ui_roles_importer.rb +2 -2
  28. data/app/services/foreman_ansible/variables_importer.rb +36 -1
  29. data/app/views/ansible_roles/import.html.erb +10 -50
  30. data/app/views/ansible_variables/index.html.erb +1 -2
  31. data/app/views/api/v2/ansible_roles/sync.json.rabl +5 -0
  32. data/app/views/foreman_ansible/ansible_roles/_select_tab_content.html.erb +8 -17
  33. data/app/views/foreman_ansible/config_reports/_ansible.html.erb +13 -6
  34. data/app/views/foreman_ansible/job_templates/ansible_collections_-_install_from_galaxy.erb +28 -0
  35. data/app/views/foreman_ansible/job_templates/ansible_roles_-_install_from_galaxy.erb +1 -1
  36. data/app/views/foreman_ansible/job_templates/capsule_upgrade_-_ansible_default.erb +2 -3
  37. data/app/views/foreman_ansible/job_templates/convert_to_rhel.erb +44 -0
  38. data/config/routes.rb +5 -4
  39. data/db/migrate/20210120150019_add_position_to_ansible_role.rb +27 -0
  40. data/db/seeds.d/100_common_parameters.rb +7 -0
  41. data/db/seeds.d/90_notification_blueprints.rb +19 -1
  42. data/lib/foreman_ansible/engine.rb +1 -0
  43. data/lib/foreman_ansible/register.rb +5 -5
  44. data/lib/foreman_ansible/version.rb +1 -1
  45. data/locale/ca/LC_MESSAGES/foreman_ansible.mo +0 -0
  46. data/locale/ca/foreman_ansible.po +1 -1
  47. data/locale/cs_CZ/LC_MESSAGES/foreman_ansible.mo +0 -0
  48. data/locale/cs_CZ/foreman_ansible.po +1 -1
  49. data/locale/de/LC_MESSAGES/foreman_ansible.mo +0 -0
  50. data/locale/de/foreman_ansible.po +1 -1
  51. data/locale/en/LC_MESSAGES/foreman_ansible.mo +0 -0
  52. data/locale/en/foreman_ansible.po +1 -1
  53. data/locale/en_GB/LC_MESSAGES/foreman_ansible.mo +0 -0
  54. data/locale/en_GB/foreman_ansible.po +1 -1
  55. data/locale/es/LC_MESSAGES/foreman_ansible.mo +0 -0
  56. data/locale/es/foreman_ansible.po +1 -1
  57. data/locale/fr/LC_MESSAGES/foreman_ansible.mo +0 -0
  58. data/locale/fr/foreman_ansible.po +1 -1
  59. data/locale/gl/LC_MESSAGES/foreman_ansible.mo +0 -0
  60. data/locale/gl/foreman_ansible.po +1 -1
  61. data/locale/it/LC_MESSAGES/foreman_ansible.mo +0 -0
  62. data/locale/it/foreman_ansible.po +1 -1
  63. data/locale/ja/LC_MESSAGES/foreman_ansible.mo +0 -0
  64. data/locale/ja/foreman_ansible.po +1 -1
  65. data/locale/ko/LC_MESSAGES/foreman_ansible.mo +0 -0
  66. data/locale/ko/foreman_ansible.po +1 -1
  67. data/locale/nl_NL/LC_MESSAGES/foreman_ansible.mo +0 -0
  68. data/locale/nl_NL/foreman_ansible.po +1 -1
  69. data/locale/pl/LC_MESSAGES/foreman_ansible.mo +0 -0
  70. data/locale/pl/foreman_ansible.po +1 -1
  71. data/locale/pt_BR/LC_MESSAGES/foreman_ansible.mo +0 -0
  72. data/locale/pt_BR/foreman_ansible.po +1 -1
  73. data/locale/ru/LC_MESSAGES/foreman_ansible.mo +0 -0
  74. data/locale/ru/foreman_ansible.po +1 -1
  75. data/locale/sv_SE/LC_MESSAGES/foreman_ansible.mo +0 -0
  76. data/locale/sv_SE/foreman_ansible.po +1 -1
  77. data/locale/zh_CN/LC_MESSAGES/foreman_ansible.mo +0 -0
  78. data/locale/zh_CN/foreman_ansible.po +1 -1
  79. data/locale/zh_TW/LC_MESSAGES/foreman_ansible.mo +0 -0
  80. data/locale/zh_TW/foreman_ansible.po +1 -1
  81. data/package.json +11 -42
  82. data/test/factories/ansible_roles.rb +12 -0
  83. data/test/factories/host_ansible_enhancements.rb +23 -0
  84. data/test/foreman_ansible/helpers/ansible_roles_helper_test.rb +45 -0
  85. data/test/functional/ansible_variables_controller_test.rb +0 -15
  86. data/test/functional/api/v2/ansible_roles_controller_test.rb +23 -0
  87. data/test/functional/api/v2/hostgroups_controller_test.rb +5 -4
  88. data/test/functional/api/v2/hosts_controller_test.rb +9 -8
  89. data/test/functional/hosts_controller_test.rb +10 -6
  90. data/test/unit/concerns/host_managed_extensions_test.rb +17 -3
  91. data/test/unit/concerns/hostgroup_extensions_test.rb +13 -7
  92. data/test/unit/host_ansible_role_test.rb +2 -1
  93. data/test/unit/hostgroup_ansible_role_test.rb +15 -1
  94. data/test/unit/ignore_roles_test.rb +43 -0
  95. data/test/unit/import_roles_and_variables.rb +60 -0
  96. data/test/unit/services/ansible_variables_importer_test.rb +13 -0
  97. data/test/unit/services/api_roles_importer_test.rb +22 -5
  98. data/test/unit/services/fact_parser_test.rb +16 -0
  99. data/test/unit/services/inventory_creator_test.rb +15 -7
  100. data/test/unit/services/ui_roles_importer_test.rb +2 -2
  101. data/webpack/__mocks__/foremanReact/common/helpers.js +13 -0
  102. data/webpack/__mocks__/foremanReact/components/common/forms/OrderableSelect/helpers.js +5 -0
  103. data/webpack/__mocks__/foremanReact/redux/API.js +7 -0
  104. data/webpack/components/AnsibleRolesAndVariables/AnsibleRolesAndVariables.js +178 -0
  105. data/webpack/components/AnsibleRolesAndVariables/AnsibleRolesAndVariables.scss +9 -0
  106. data/webpack/components/AnsibleRolesAndVariables/AnsibleRolesAndVariablesActions.js +39 -0
  107. data/webpack/components/AnsibleRolesAndVariables/AnsibleRolesAndVariablesConstants.js +4 -0
  108. data/webpack/components/AnsibleRolesAndVariables/AnsibleRolesAndVariablesHelpers.js +11 -0
  109. data/webpack/components/AnsibleRolesAndVariables/__test__/AnsibleRolesAndVariablesHelpers.test.js +50 -0
  110. data/webpack/components/AnsibleRolesAndVariables/__test__/AnsibleRolesAndVariablesImport.test.js +39 -0
  111. data/webpack/components/AnsibleRolesAndVariables/__test__/__snapshots__/AnsibleRolesAndVariablesImport.test.js.snap +177 -0
  112. data/webpack/components/AnsibleRolesAndVariables/index.js +31 -0
  113. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcher.js +25 -27
  114. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcher.scss +1 -1
  115. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherActions.js +20 -14
  116. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherConstants.js +1 -2
  117. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherReducer.js +13 -8
  118. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js +8 -28
  119. data/webpack/components/AnsibleRolesSwitcher/__fixtures__/ansibleRolesSwitcherReducer.fixtures.js +1 -0
  120. data/webpack/components/AnsibleRolesSwitcher/__tests__/AnsibleRolesSwitcher.test.js +6 -4
  121. data/webpack/components/AnsibleRolesSwitcher/__tests__/AnsibleRolesSwitcherReducer.test.js +1 -9
  122. data/webpack/components/AnsibleRolesSwitcher/__tests__/AnsibleRolesSwitcherSelectors.test.js +3 -16
  123. data/webpack/components/AnsibleRolesSwitcher/__tests__/__snapshots__/AnsibleRolesSwitcher.test.js.snap +4 -8
  124. data/webpack/components/AnsibleRolesSwitcher/__tests__/__snapshots__/AnsibleRolesSwitcherReducer.test.js.snap +8 -97
  125. data/webpack/components/AnsibleRolesSwitcher/__tests__/__snapshots__/AnsibleRolesSwitcherSelectors.test.js.snap +5 -30
  126. data/webpack/components/AnsibleRolesSwitcher/components/AnsiblePermissionDenied.test.js +1 -1
  127. data/webpack/components/AnsibleRolesSwitcher/components/AnsibleRole.js +5 -2
  128. data/webpack/components/AnsibleRolesSwitcher/components/AnsibleRole.test.js +1 -1
  129. data/webpack/components/AnsibleRolesSwitcher/components/AnsibleRoleInputs.js +43 -0
  130. data/webpack/components/AnsibleRolesSwitcher/components/AnsibleRoleInputs.test.js +19 -0
  131. data/webpack/components/AnsibleRolesSwitcher/components/AssignedRolesList.js +63 -51
  132. data/webpack/components/AnsibleRolesSwitcher/components/AssignedRolesList.test.js +4 -1
  133. data/webpack/components/AnsibleRolesSwitcher/components/AvailableRolesList.test.js +1 -1
  134. data/webpack/components/AnsibleRolesSwitcher/components/OrderedRolesTooltip.js +23 -0
  135. data/webpack/components/AnsibleRolesSwitcher/components/__snapshots__/AnsibleRoleInputs.test.js.snap +51 -0
  136. data/webpack/components/AnsibleRolesSwitcher/components/__snapshots__/AssignedRolesList.test.js.snap +28 -28
  137. data/webpack/components/AnsibleRolesSwitcher/index.js +4 -8
  138. data/webpack/index.js +6 -13
  139. metadata +84 -39
  140. data/app/views/ansible_variables/import.html.erb +0 -56
  141. data/webpack/test_setup.js +0 -12
@@ -13,7 +13,7 @@
13
13
  #
14
14
  msgid ""
15
15
  msgstr ""
16
- "Project-Id-Version: foreman_ansible 5.0.1\n"
16
+ "Project-Id-Version: foreman_ansible 3.0.2\n"
17
17
  "Report-Msgid-Bugs-To: \n"
18
18
  "PO-Revision-Date: 2018-11-14 21:36+0000\n"
19
19
  "Last-Translator: dschutterop <daniel@schutterop.nl>, 2019\n"
@@ -9,7 +9,7 @@
9
9
  #
10
10
  msgid ""
11
11
  msgstr ""
12
- "Project-Id-Version: foreman_ansible 5.0.1\n"
12
+ "Project-Id-Version: foreman_ansible 3.0.2\n"
13
13
  "Report-Msgid-Bugs-To: \n"
14
14
  "PO-Revision-Date: 2018-11-14 21:36+0000\n"
15
15
  "Last-Translator: Michał Foryt <michal.foryt@gmail.com>, 2019\n"
@@ -5,7 +5,7 @@
5
5
  #
6
6
  msgid ""
7
7
  msgstr ""
8
- "Project-Id-Version: foreman_ansible 5.0.1\n"
8
+ "Project-Id-Version: foreman_ansible 3.0.2\n"
9
9
  "Report-Msgid-Bugs-To: \n"
10
10
  "PO-Revision-Date: 2018-03-12 12:24-0400\n"
11
11
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
@@ -5,7 +5,7 @@
5
5
  #
6
6
  msgid ""
7
7
  msgstr ""
8
- "Project-Id-Version: foreman_ansible 5.0.1\n"
8
+ "Project-Id-Version: foreman_ansible 3.0.2\n"
9
9
  "Report-Msgid-Bugs-To: \n"
10
10
  "PO-Revision-Date: 2018-03-12 12:24-0400\n"
11
11
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
@@ -10,7 +10,7 @@
10
10
  #
11
11
  msgid ""
12
12
  msgstr ""
13
- "Project-Id-Version: foreman_ansible 5.0.1\n"
13
+ "Project-Id-Version: foreman_ansible 3.0.2\n"
14
14
  "Report-Msgid-Bugs-To: \n"
15
15
  "PO-Revision-Date: 2018-11-14 21:36+0000\n"
16
16
  "Last-Translator: johnny.westerlund <johnny.westerlund@gmail.com>, 2019\n"
@@ -5,7 +5,7 @@
5
5
  #
6
6
  msgid ""
7
7
  msgstr ""
8
- "Project-Id-Version: foreman_ansible 5.0.1\n"
8
+ "Project-Id-Version: foreman_ansible 3.0.2\n"
9
9
  "Report-Msgid-Bugs-To: \n"
10
10
  "PO-Revision-Date: 2018-03-12 12:24-0400\n"
11
11
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
@@ -5,7 +5,7 @@
5
5
  #
6
6
  msgid ""
7
7
  msgstr ""
8
- "Project-Id-Version: foreman_ansible 5.0.1\n"
8
+ "Project-Id-Version: foreman_ansible 3.0.2\n"
9
9
  "Report-Msgid-Bugs-To: \n"
10
10
  "PO-Revision-Date: 2018-03-12 12:24-0400\n"
11
11
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
data/package.json CHANGED
@@ -7,33 +7,24 @@
7
7
  "test": "test"
8
8
  },
9
9
  "peerDependencies": {
10
- "@theforeman/vendor": ">= 3.3.2"
10
+ "@theforeman/vendor": ">= 6.0.0"
11
11
  },
12
12
  "dependencies": {
13
13
  "react-json-tree": "^0.11.0"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@babel/core": "^7.7.0",
17
- "@theforeman/builder": "^4.0.2",
18
- "@theforeman/vendor-dev": "^3.3.2",
19
- "babel-eslint": "^10.0.0",
20
- "babel-jest": "^24.9.0",
21
- "enzyme": "^3.7.0",
22
- "enzyme-adapter-react-16": "^1.7.0",
23
- "enzyme-to-json": "^3.3.5",
24
- "eslint": "^6.0.0",
25
- "eslint-plugin-patternfly-react": "^0.2.1",
26
- "identity-obj-proxy": "^3.0.0",
27
- "jest": "^24.9.0",
28
- "prettier": "^1.19.0",
29
- "react-redux": "^5.0.7",
30
- "react-redux-test-utils": "^0.1.1",
31
- "redux": "^3.7.2",
32
- "redux-thunk": "^2.3.0"
17
+ "@theforeman/builder": "^6.0.0",
18
+ "@theforeman/eslint-plugin-foreman": "6.0.0",
19
+ "@theforeman/test": "^6.0.0",
20
+ "@theforeman/vendor-dev": "^6.0.0",
21
+ "babel-eslint": "^10.0.3",
22
+ "eslint": "^6.7.2",
23
+ "prettier": "^1.13.5"
33
24
  },
34
25
  "scripts": {
35
- "test": "node node_modules/.bin/jest webpack",
36
- "lint": "eslint ./webpack"
26
+ "test": "tfm-test --plugin",
27
+ "lint": "tfm-lint --plugin -d webpack"
37
28
  },
38
29
  "repository": {
39
30
  "type": "git",
@@ -44,27 +35,5 @@
44
35
  "bugs": {
45
36
  "url": "https://projects.theforeman.org/projects/ansible"
46
37
  },
47
- "homepage": "https://theforeman.org/plugins/foreman_ansible/",
48
- "jest": {
49
- "verbose": true,
50
- "moduleDirectories": [
51
- "node_modules/@theforeman/vendor-core/node_modules",
52
- "node_modules",
53
- "webpack"
54
- ],
55
- "transform": {
56
- "^.+\\.js$": "babel-jest"
57
- },
58
- "setupFiles": [
59
- "raf/polyfill",
60
- "./webpack/test_setup.js"
61
- ],
62
- "testPathIgnorePatterns": [
63
- "/node_modules/",
64
- "<rootDir>/foreman/"
65
- ],
66
- "moduleNameMapper": {
67
- "^.+\\.(css|scss)$": "identity-obj-proxy"
68
- }
69
- }
38
+ "homepage": "https://theforeman.org/plugins/foreman_ansible/"
70
39
  }
@@ -4,4 +4,16 @@ FactoryBot.define do
4
4
  factory :ansible_role do
5
5
  sequence(:name) { |n| "ansible_role_#{n}" }
6
6
  end
7
+
8
+ factory :host_ansible_role do
9
+ position { nil }
10
+ host
11
+ ansible_role
12
+ end
13
+
14
+ factory :hostgroup_ansible_role do
15
+ position { nil }
16
+ hostgroup
17
+ ansible_role
18
+ end
7
19
  end
@@ -0,0 +1,23 @@
1
+ FactoryBot.modify do
2
+ factory :host do
3
+ trait :with_ansible_roles do
4
+ transient do
5
+ roles_count { 2 }
6
+ end
7
+ after(:build) do |host, evaluator|
8
+ host.host_ansible_roles = FactoryBot.build_list(:host_ansible_role, evaluator.roles_count, host: host)
9
+ end
10
+ end
11
+ end
12
+
13
+ factory :hostgroup do
14
+ trait :with_ansible_roles do
15
+ transient do
16
+ roles_count { 2 }
17
+ end
18
+ after(:build) do |hostgroup, evaluator|
19
+ hostgroup.hostgroup_ansible_roles = FactoryBot.build_list(:hostgroup_ansible_role, evaluator.roles_count, hostgroup: hostgroup)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class AnsibleRolesHelperTest < ActiveSupport::TestCase
6
+ include ForemanAnsible::AnsibleRolesHelper
7
+
8
+ describe '#role_attributes_for_roles_switcher' do
9
+ let(:hg_with_roles) { FactoryBot.create(:hostgroup, :with_ansible_roles) }
10
+
11
+ context 'with hostgroup' do
12
+ let(:child) { FactoryBot.build(:hostgroup, :with_ansible_roles, parent: hg_with_roles) }
13
+
14
+ it 'show inherited and positioned own roles' do
15
+ role_attributes = role_attributes_for_roles_switcher(child)
16
+ expected_ids = hg_with_roles.ansible_roles.pluck(:id) + child.hostgroup_ansible_roles.map(&:ansible_role_id)
17
+ assert_equal(expected_ids, role_attributes.map { |ra| ra[:id] })
18
+ end
19
+
20
+ it 'includes inherited roles just once' do
21
+ child.host_ansible_roles << FactoryBot.build(:hostgroup_ansible_role, hostgroup: child, ansible_role: hg_with_roles.ansible_roles.first)
22
+ role_attributes = role_attributes_for_roles_switcher(child)
23
+ expected_ids = hg_with_roles.ansible_roles.pluck(:id) + child.hostgroup_ansible_roles.map(&:ansible_role_id)
24
+ assert_equal(expected_ids.uniq, role_attributes.map { |ra| ra[:id] })
25
+ end
26
+ end
27
+
28
+ context 'with host' do
29
+ let(:host_new) { FactoryBot.build(:host, :with_ansible_roles, hostgroup: hg_with_roles) }
30
+
31
+ it 'show inherited and positioned own roles' do
32
+ role_attributes = role_attributes_for_roles_switcher(host_new)
33
+ expected_ids = hg_with_roles.ansible_roles.pluck(:id) + host_new.host_ansible_roles.map(&:ansible_role_id)
34
+ assert_equal(expected_ids, role_attributes.map { |ra| ra[:id] })
35
+ end
36
+
37
+ it 'includes inherited roles just once' do
38
+ host_new.host_ansible_roles << FactoryBot.build(:host_ansible_role, host: host_new, ansible_role: hg_with_roles.ansible_roles.first)
39
+ role_attributes = role_attributes_for_roles_switcher(host_new)
40
+ expected_ids = hg_with_roles.ansible_roles.pluck(:id) + host_new.host_ansible_roles.map(&:ansible_role_id)
41
+ assert_equal(expected_ids.uniq, role_attributes.map { |ra| ra[:id] })
42
+ end
43
+ end
44
+ end
45
+ end
@@ -34,21 +34,6 @@ class AnsibleVariablesControllerTest < ActionController::TestCase
34
34
  assert_redirected_to ansible_variables_url
35
35
  end
36
36
 
37
- test 'should show import page' do
38
- ForemanAnsible::UiRolesImporter.any_instance.
39
- expects(:import_role_names).returns({})
40
-
41
- ForemanAnsible::VariablesImporter.any_instance.
42
- expects(:import_variable_names).returns({
43
- :obsolete => [@model]
44
- })
45
-
46
- get :import,
47
- :params => { :proxy => @proxy.id },
48
- :session => set_session_user
49
- assert_response :success
50
- end
51
-
52
37
  test 'should create ansible variable' do
53
38
  params = { :ansible_variable => { :key => 'great name', :ansible_role_id => FactoryBot.create(:ansible_role).id } }
54
39
  assert_difference('AnsibleVariable.count', 1) do
@@ -45,7 +45,30 @@ module Api
45
45
  }, :session => set_session_user
46
46
  assert_response :success
47
47
  end
48
+
48
49
  test 'should fetch' do
50
+ @controller = Api::V2::AnsibleRolesController.new
51
+ @controller.stubs(:prepare_ansible_import_rows).returns(
52
+ [{
53
+ :name => 'test_user.test_name',
54
+ :id => nil,
55
+ :role_action => 'Import Role',
56
+ :variables => 'Add: 7',
57
+ :hosts_count => '',
58
+ :hostgroup_count => '',
59
+ :kind => 'new'
60
+ },
61
+ {
62
+ :name => 'some_user.some_role',
63
+ :id => nil,
64
+ :role_action => 'Import Role',
65
+ :variables => 'Add: 3',
66
+ :hosts_count => '',
67
+ :hostgroup_count => '',
68
+ :kind => 'new'
69
+ }]
70
+ )
71
+
49
72
  get :fetch, :params => {
50
73
  :proxy_id => @proxy.id
51
74
  }, :session => set_session_user
@@ -9,6 +9,7 @@ module Api
9
9
  class HostgroupsControllerTest < ActionController::TestCase
10
10
  setup do
11
11
  @ansible_role1 = FactoryBot.create(:ansible_role)
12
+ @ansible_role2 = FactoryBot.create(:ansible_role)
12
13
  @host1 = FactoryBot.create(:host, :with_hostgroup)
13
14
  @host2 = FactoryBot.create(:host, :with_hostgroup)
14
15
  @host3 = FactoryBot.create(:host, :with_hostgroup)
@@ -44,23 +45,23 @@ module Api
44
45
  end
45
46
 
46
47
  test 'should list ansible roles for a host group' do
47
- @host3.hostgroup.ansible_roles = [@ansible_role1]
48
+ FactoryBot.create(:hostgroup_ansible_role, hostgroup_id: @host3.hostgroup.id, ansible_role_id: @ansible_role1.id, position: 0)
48
49
  get :ansible_roles, :params => { :id => @host3.hostgroup.id }
49
50
  response = JSON.parse(@response.body)
50
51
  assert_equal @ansible_role1.id, response.first['id']
51
52
  end
52
53
 
53
- test 'should assign a role to a hostgroup' do
54
+ test 'should assign a role to a hostgroup with a correct ordering' do
54
55
  hostgroup = FactoryBot.create(:hostgroup,
55
56
  :ansible_role_ids => [])
56
57
  post :assign_ansible_roles,
57
58
  :params => {
58
59
  :id => hostgroup.id,
59
- :ansible_role_ids => [@ansible_role1.id]
60
+ :ansible_role_ids => [@ansible_role2.id, @ansible_role1.id]
60
61
  },
61
62
  :session => set_session_user
62
63
  assert_response :success
63
- assert assigns('hostgroup').ansible_roles, [@ansible_role1]
64
+ assert_equal assigns('hostgroup').ansible_roles, [@ansible_role2, @ansible_role1]
64
65
  end
65
66
  end
66
67
  end
@@ -53,9 +53,9 @@ module Api
53
53
  end
54
54
 
55
55
  test 'should update a host with ansible_role_ids param' do
56
- host = FactoryBot.create(:host,
57
- :managed => false,
58
- :ansible_role_ids => [@ansible_role1.id])
56
+ host = FactoryBot.create(:host, :managed => false)
57
+ FactoryBot.create(:host_ansible_role, :host_id => host.id, :ansible_role_id => @ansible_role1.id, :position => 0)
58
+
59
59
  post :update,
60
60
  :params => {
61
61
  :id => host.id,
@@ -63,28 +63,29 @@ module Api
63
63
  },
64
64
  :session => set_session_user
65
65
  assert_response :success
66
- assert assigns('host').ansible_roles, [@ansible_role2]
66
+ host.reload
67
+ assert_equal host.ansible_roles, [@ansible_role2]
67
68
  end
68
69
 
69
70
  test 'should list ansible roles for a host' do
70
- @host3.ansible_roles = [@ansible_role1]
71
+ FactoryBot.create(:host_ansible_role, :host_id => @host3.id, :ansible_role_id => @ansible_role1.id, :position => 0)
71
72
  get :ansible_roles, :params => { :id => @host3.id }
72
73
  response = JSON.parse(@response.body)
73
74
  assert_equal @ansible_role1.id, response.first['id']
74
75
  end
75
76
 
76
- test 'should assign a role to a host' do
77
+ test 'should assign a role to a host with a correct ordering' do
77
78
  host = FactoryBot.create(:host,
78
79
  :managed => false,
79
80
  :ansible_role_ids => [])
80
81
  post :assign_ansible_roles,
81
82
  :params => {
82
83
  :id => host.id,
83
- :ansible_role_ids => [@ansible_role1.id]
84
+ :ansible_role_ids => [@ansible_role2.id, @ansible_role1.id]
84
85
  },
85
86
  :session => set_session_user
86
87
  assert_response :success
87
- assert assigns('host').ansible_roles, [@ansible_role1]
88
+ assert_equal assigns('host').ansible_roles, [@ansible_role2, @ansible_role1]
88
89
  end
89
90
  end
90
91
  end
@@ -16,7 +16,9 @@ class HostsControllerExtensionsTest < ActionController::TestCase
16
16
  test 'create a host with ansible roles' do
17
17
  host = { :name => 'foo',
18
18
  :managed => false,
19
- :ansible_role_ids => [@role.id] }
19
+ :host_ansible_roles_attributes => {
20
+ 0 => { :ansible_role_id => @role.id, :position => 0 }
21
+ } }
20
22
  post :create, :params => { :host => host }, :session => set_session_user
21
23
  assert_redirected_to host_url(assigns('host'))
22
24
  assert assigns('host').ansible_roles, [@role]
@@ -27,17 +29,19 @@ class HostsControllerExtensionsTest < ActionController::TestCase
27
29
  post :update,
28
30
  :params => {
29
31
  :id => host.id,
30
- :host => { :ansible_role_ids => [@role.id] }
32
+ :host => { :host_ansible_roles_attributes => {
33
+ 0 => { :ansible_role_id => @role.id, :position => 0 }
34
+ } }
31
35
  },
32
36
  :session => set_session_user
33
37
  assert_redirected_to host_url(assigns('host'))
34
- assert assigns('host').ansible_roles, [@role]
38
+ assert_equal assigns('host').ansible_roles, [@role]
35
39
  end
36
40
 
37
41
  test 'delete a host with ansible roles' do
38
- host = FactoryBot.create(:host,
39
- :managed => false,
40
- :ansible_roles => [@role])
42
+ host = FactoryBot.create(:host, :managed => false)
43
+ FactoryBot.create(:host_ansible_role, host_id: host.id, ansible_role_id: @role.id, position: 0)
44
+
41
45
  assert_include @role.hosts, host
42
46
  delete :destroy,
43
47
  :params => { :id => host.id },
@@ -9,8 +9,8 @@ class HostManagedExtensionsTest < ActiveSupport::TestCase
9
9
  @role2 = FactoryBot.create(:ansible_role)
10
10
  @role3 = FactoryBot.create(:ansible_role)
11
11
 
12
- @hostgroup_parent = FactoryBot.create(:hostgroup,
13
- :ansible_roles => [@role2])
12
+ @hostgroup_parent = FactoryBot.create(:hostgroup)
13
+ FactoryBot.create(:hostgroup_ansible_role, hostgroup_id: @hostgroup_parent.id, ansible_role_id: @role2.id)
14
14
  @hostgroup = FactoryBot.create(:hostgroup, :parent => @hostgroup_parent)
15
15
  @host = FactoryBot.build_stubbed(:host, :ansible_roles => [@role1])
16
16
  @another_host = FactoryBot.build_stubbed(:host, :ansible_roles => [@role3])
@@ -23,7 +23,7 @@ class HostManagedExtensionsTest < ActiveSupport::TestCase
23
23
 
24
24
  test 'returns assigned and inherited roles for host with a hostgroup' do
25
25
  @host.hostgroup = @hostgroup
26
- @host.all_ansible_roles.must_equal [@role1, @role2]
26
+ @host.all_ansible_roles.must_equal [@role2, @role1]
27
27
  end
28
28
  end
29
29
 
@@ -48,4 +48,18 @@ class HostManagedExtensionsTest < ActiveSupport::TestCase
48
48
  assert Host::Managed.import_host('192.168.128.27').new_record?
49
49
  end
50
50
  end
51
+
52
+ test 'should correctly order roles for host' do
53
+ host = FactoryBot.create(:host)
54
+ FactoryBot.create(:host_ansible_role, :ansible_role_id => @role1.id, :position => 1, :host_id => host.id)
55
+ FactoryBot.create(:host_ansible_role, :ansible_role_id => @role2.id, :position => 2, :host_id => host.id)
56
+ FactoryBot.create(:host_ansible_role, :ansible_role_id => @role3.id, :position => 0, :host_id => host.id)
57
+ host.ansible_roles.must_equal [@role3, @role1, @role2]
58
+ end
59
+
60
+ test 'should order hostgroup roles before host roles' do
61
+ host = FactoryBot.create(:host, :hostgroup => @hostgroup)
62
+ FactoryBot.create(:host_ansible_role, :ansible_role_id => @role1.id, :position => 0, :host_id => host.id)
63
+ host.all_ansible_roles.must_equal [@role2, @role1]
64
+ end
51
65
  end