foreman_salt 10.2.0 → 10.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,122 @@
1
+ require 'test_plugin_helper'
2
+
3
+ module ForemanSalt
4
+ class HostsControllerExtensionsTest < ActionController::TestCase
5
+ tests ::HostsController
6
+
7
+ describe "setting salt master proxy on multiple hosts" do
8
+ before do
9
+ setup_user "edit"
10
+ as_admin do
11
+ @hosts = FactoryBot.create_list(:host, 2)
12
+ @proxy = FactoryBot.create(:smart_proxy, :with_salt_feature)
13
+ end
14
+ end
15
+
16
+ test 'user without edit permission should not be able to change salt master' do
17
+ @request.env['HTTP_REFERER'] = hosts_path
18
+
19
+ params = { :host_ids => @hosts.map(&:id),
20
+ :proxy => { :proxy_id => '' } }
21
+
22
+ post :update_multiple_salt_master, params: params,
23
+ session: set_session_user.merge(:user => users(:one).id)
24
+ assert_response :forbidden
25
+ end
26
+
27
+ test "should change the salt master proxy" do
28
+ @request.env['HTTP_REFERER'] = hosts_path
29
+
30
+ params = { :host_ids => @hosts.map(&:id),
31
+ :proxy => { :proxy_id => @proxy.id } }
32
+
33
+ post :update_multiple_salt_master, params: params,
34
+ session: set_session_user.merge(:user => users(:admin).id)
35
+
36
+ assert_empty flash[:error]
37
+
38
+ @hosts.each do |host|
39
+ as_admin do
40
+ assert_equal @proxy, host.reload.salt_proxy
41
+ end
42
+ end
43
+ end
44
+
45
+ test "should clear the salt master proxy of multiple hosts" do
46
+ @request.env['HTTP_REFERER'] = hosts_path
47
+
48
+ params = { :host_ids => @hosts.map(&:id),
49
+ :proxy => { :proxy_id => '' } }
50
+
51
+ post :update_multiple_salt_master, params: params,
52
+ session: set_session_user.merge(:user => users(:admin).id)
53
+
54
+ assert_empty flash[:error]
55
+
56
+ @hosts.each do |host|
57
+ as_admin do
58
+ assert_nil host.reload.salt_proxy
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "setting salt environment on multiple hosts" do
65
+ before do
66
+ setup_user "edit"
67
+ as_admin do
68
+ @hosts = FactoryBot.create_list(:host, 2)
69
+ @proxy = FactoryBot.create(:smart_proxy, :with_salt_feature)
70
+ @salt_environment = FactoryBot.create :salt_environment
71
+ end
72
+ end
73
+
74
+ test 'user without edit permission should not be able to change salt environment' do
75
+ @request.env['HTTP_REFERER'] = hosts_path
76
+
77
+ params = { :host_ids => @hosts.map(&:id),
78
+ :salt_environment => { :id => @salt_environment.id } }
79
+
80
+ post :update_multiple_salt_environment, params: params,
81
+ session: set_session_user.merge(:user => users(:one).id)
82
+ assert_response :forbidden
83
+ end
84
+
85
+ test "should change the salt environment" do
86
+ @request.env['HTTP_REFERER'] = hosts_path
87
+
88
+ params = { :host_ids => @hosts.map(&:id),
89
+ :salt_environment => { :id => @salt_environment.id } }
90
+
91
+ post :update_multiple_salt_environment, params: params,
92
+ session: set_session_user.merge(:user => users(:admin).id)
93
+
94
+ assert_empty flash[:error]
95
+
96
+ @hosts.each do |host|
97
+ as_admin do
98
+ assert_equal @salt_environment, host.reload.salt_environment
99
+ end
100
+ end
101
+ end
102
+
103
+ test "should clear the salt environment of multiple hosts" do
104
+ @request.env['HTTP_REFERER'] = hosts_path
105
+
106
+ params = { :host_ids => @hosts.map(&:id),
107
+ :salt_environment => { :id => '' } }
108
+
109
+ post :update_multiple_salt_environment, params: params,
110
+ session: set_session_user.merge(:user => users(:admin).id)
111
+
112
+ assert_empty flash[:error]
113
+
114
+ @hosts.each do |host|
115
+ as_admin do
116
+ assert_nil host.reload.salt_environment
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,82 @@
1
+ require 'test_plugin_helper'
2
+ require 'integration_test_helper'
3
+
4
+ module ForemanSalt
5
+ class HostJSTest < IntegrationTestWithJavascript
6
+ def index_modal
7
+ page.find('#confirmation-modal')
8
+ end
9
+
10
+ def multiple_actions_div
11
+ page.find('#submit_multiple')
12
+ end
13
+
14
+ setup do
15
+ as_admin do
16
+ proxy = FactoryBot.create(:smart_proxy, :with_salt_feature)
17
+ salt_environment = FactoryBot.create(:salt_environment)
18
+ @host = FactoryBot.create(:host, :salt_proxy => proxy, :salt_environment => salt_environment)
19
+ end
20
+ end
21
+
22
+ describe "hosts index salt multiple actions" do
23
+ test 'change salt master action' do
24
+ visit hosts_path
25
+ page.find('#check_all').trigger('click')
26
+
27
+ # Ensure and wait for all hosts to be checked, and that no unchecked hosts remain
28
+ assert page.has_no_selector?('input.host_select_boxes:not(:checked)')
29
+
30
+ # Dropdown visible?
31
+ assert multiple_actions_div.find('.dropdown-toggle').visible?
32
+ multiple_actions_div.find('.dropdown-toggle').click
33
+ assert multiple_actions_div.find('ul').visible?
34
+
35
+ # Hosts are added to cookie
36
+ host_ids_on_cookie = JSON.parse(CGI.unescape(page.driver.cookies['_ForemanSelectedhosts'].value))
37
+ assert(host_ids_on_cookie.include?(@host.id))
38
+
39
+ within('#submit_multiple') do
40
+ click_on('Change Salt Master')
41
+ end
42
+
43
+ assert index_modal.visible?, "Modal window was shown"
44
+ page.find('#proxy_proxy_id').find("option[value='#{@host.salt_proxy.id}']").select_option
45
+
46
+ # remove hosts cookie on submit
47
+ index_modal.find('.btn-primary').click
48
+ assert_current_path hosts_path
49
+ assert_empty(page.driver.cookies['_ForemanSelectedhosts'])
50
+ end
51
+
52
+ test 'change salt environment action' do
53
+ visit hosts_path
54
+ page.find('#check_all').trigger('click')
55
+
56
+ # Ensure and wait for all hosts to be checked, and that no unchecked hosts remain
57
+ assert page.has_no_selector?('input.host_select_boxes:not(:checked)')
58
+
59
+ # Dropdown visible?
60
+ assert multiple_actions_div.find('.dropdown-toggle').visible?
61
+ multiple_actions_div.find('.dropdown-toggle').click
62
+ assert multiple_actions_div.find('ul').visible?
63
+
64
+ # Hosts are added to cookie
65
+ host_ids_on_cookie = JSON.parse(CGI.unescape(page.driver.cookies['_ForemanSelectedhosts'].value))
66
+ assert(host_ids_on_cookie.include?(@host.id))
67
+
68
+ within('#submit_multiple') do
69
+ click_on('Change Salt Environment')
70
+ end
71
+
72
+ assert index_modal.visible?, "Modal window was shown"
73
+ page.find('#salt_environment_id').find("option[value='#{@host.salt_environment.id}']").select_option
74
+
75
+ # remove hosts cookie on submit
76
+ index_modal.find('.btn-primary').click
77
+ assert_current_path hosts_path
78
+ assert_empty(page.driver.cookies['_ForemanSelectedhosts'])
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,18 @@
1
+ {
2
+ "saltclient713.example.com": {
3
+ "file_|-/etc/motd_|-/etc/motd_|-managed": {
4
+ "comment": "The file /etc/motd is set to be changed",
5
+ "pchanges": {
6
+ "diff": "--- \n+++ \n@@ -8,7 +8,7 @@\n Kernel: 4.12.14-23-default\n Memory: 1986 MB\n \n-The server is managed using a configuration management system (saltstack.org).\n+This server is managed using a configuration management system (saltstack.org).\n Changes made to this box directly will likely be over-written by SALT. Instead\n modify server configuration via the configuration management git repository.\n ------------------------------------------------------------------------------\n"
7
+ },
8
+ "name": "/etc/motd",
9
+ "start_time": "19:15:44.510229",
10
+ "result": null,
11
+ "duration": 175.936,
12
+ "__run_num__": 0,
13
+ "__sls__": "motd",
14
+ "changes": {},
15
+ "__id__": "/etc/motd"
16
+ }
17
+ }
18
+ }
@@ -21,20 +21,26 @@ module ForemanSalt
21
21
 
22
22
  test 'child host group inherits salt proxy from child parent' do
23
23
  parent = FactoryBot.create :hostgroup
24
- child_one = FactoryBot.create :hostgroup, :parent => parent
25
- child_one.salt_proxy = @proxy
26
- child_one.reload
24
+ child_one = FactoryBot.create :hostgroup, :parent => parent, :salt_proxy => @proxy
27
25
  child_two = FactoryBot.create :hostgroup, :parent => child_one
26
+ assert_not_nil parent
27
+ assert_not_nil child_one
28
+ assert_not_nil child_two
29
+ assert_not_nil child_one.salt_proxy
30
+ assert_not_nil child_two.salt_proxy
28
31
  assert_equal child_two.salt_proxy, child_one.salt_proxy
29
32
  end
30
33
 
31
34
  test 'child host group inherits salt environment from child parent' do
32
35
  environment = FactoryBot.create :salt_environment
33
36
  parent = FactoryBot.create :hostgroup
34
- child_one = FactoryBot.create :hostgroup, :parent => parent
35
- child_one.salt_environment = environment
36
- child_one.reload
37
+ child_one = FactoryBot.create :hostgroup, :parent => parent, :salt_environment => environment
37
38
  child_two = FactoryBot.create :hostgroup, :parent => child_one
39
+ assert_not_nil parent
40
+ assert_not_nil child_one
41
+ assert_not_nil child_two
42
+ assert_not_nil child_one.salt_environment
43
+ assert_not_nil child_two.salt_environment
38
44
  assert_equal child_two.salt_environment, child_one.salt_environment
39
45
  end
40
46
 
@@ -7,6 +7,7 @@ module ForemanSalt
7
7
  Setting[:create_new_host_when_facts_are_uploaded] = true
8
8
 
9
9
  @report = JSON.parse(File.read(File.join(Engine.root, 'test', 'unit', 'highstate.json')))
10
+ @report_pchanges = JSON.parse(File.read(File.join(Engine.root, 'test', 'unit', 'highstate_pchanges.json')))
10
11
 
11
12
  @host = 'saltclient713.example.com'
12
13
  end
@@ -29,5 +30,22 @@ module ForemanSalt
29
30
  assert_equal status['applied'], 9
30
31
  assert_equal status['failed'], 3
31
32
  end
33
+
34
+ test 'report has salt origin and expected content' do
35
+ ForemanSalt::ReportImporter.import(@report)
36
+ report = Host.find_by_name(@host).reports.last
37
+ assert_equal 'Salt', report.origin
38
+ assert_equal 'pkg_|-postfix_|-postfix_|-installed', report.logs.first.source.value
39
+ assert_equal 'Package postfix is already installed.', report.logs.first.message.value
40
+ end
41
+
42
+ test 'report with pchanges has salt origin and expected content' do
43
+ ForemanSalt::ReportImporter.import(@report_pchanges)
44
+ report = Host.find_by_name(@host).reports.last
45
+ status = report.status
46
+ assert_equal 'Salt', report.origin
47
+ assert_equal 'file_|-/etc/motd_|-/etc/motd_|-managed', report.logs.first.source.value
48
+ assert_equal status['pending'], 1
49
+ end
32
50
  end
33
51
  end
@@ -0,0 +1,23 @@
1
+ require 'test_plugin_helper'
2
+
3
+ module ForemanSalt
4
+ class SaltFactImpoterTest < ActiveSupport::TestCase
5
+ def setup
6
+ @host = FactoryBot.build(:host)
7
+ end
8
+
9
+ test 'should have fact set' do
10
+ importer = FactImporter.new(@host, 'a' => 'b')
11
+ assert_equal({ 'a' => 'b' }, importer.send(:facts))
12
+ end
13
+
14
+ test 'should have Salt as origin' do
15
+ importer = FactImporter.new(@host, 'a' => 'b')
16
+ importer.stubs(:ensure_no_active_transaction).returns(true)
17
+ importer.import!
18
+ imported_fact = FactName.find_by_name('a')
19
+ assert_equal 'a', imported_fact.name
20
+ assert_equal 'foreman_salt/Salt', imported_fact.origin
21
+ end
22
+ end
23
+ end
@@ -2,22 +2,20 @@ require 'test_plugin_helper'
2
2
 
3
3
  module ForemanSalt
4
4
  class SaltFactsParserTest < ActiveSupport::TestCase
5
- attr_reader :importer
6
-
7
5
  def setup
8
6
  grains = JSON.parse(File.read(File.join(Engine.root, 'test', 'unit', 'grains_centos.json')))
9
- @importer = FactParser.new grains["facts"]
7
+ @facts_parser = FactParser.new grains["facts"]
10
8
  User.current = users :admin
11
9
  end
12
10
 
13
11
  test "should return list of interfaces" do
14
- assert importer.interfaces.present?
15
- assert_not_nil importer.suggested_primary_interface(FactoryBot.build(:host))
16
- assert importer.interfaces.key?(importer.suggested_primary_interface(FactoryBot.build(:host)).first)
12
+ assert @facts_parser.interfaces.present?
13
+ assert_not_nil @facts_parser.suggested_primary_interface(FactoryBot.build(:host))
14
+ assert @facts_parser.interfaces.key?(@facts_parser.suggested_primary_interface(FactoryBot.build(:host)).first)
17
15
  end
18
16
 
19
17
  test "should set operatingsystem correctly" do
20
- os = importer.operatingsystem
18
+ os = @facts_parser.operatingsystem
21
19
  assert os.present?
22
20
  assert_equal 'CentOS', os.name
23
21
  assert_equal '6', os.major
@@ -26,21 +24,21 @@ module ForemanSalt
26
24
  end
27
25
 
28
26
  test "should set domain correctly" do
29
- domain = importer.domain
27
+ domain = @facts_parser.domain
30
28
  assert domain.present?
31
29
  assert_equal 'example.com', domain.name
32
30
  end
33
31
 
34
32
  test "should set ip correctly" do
35
- assert_equal '10.7.13.141', importer.ip
33
+ assert_equal '10.7.13.141', @facts_parser.ip
36
34
  end
37
35
 
38
36
  test "should set primary_interface correctly" do
39
- assert_equal 'eth0', importer.primary_interface
37
+ assert_equal 'eth0', @facts_parser.primary_interface
40
38
  end
41
39
 
42
40
  test "should set mac correctly" do
43
- assert_equal '52:54:00:35:30:2a', importer.mac
41
+ assert_equal '52:54:00:35:30:2a', @facts_parser.mac
44
42
  end
45
43
  end
46
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_salt
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.2.0
4
+ version: 10.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Benjamin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-25 00:00:00.000000000 Z
11
+ date: 2019-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deface
@@ -48,6 +48,7 @@ files:
48
48
  - LICENSE
49
49
  - README.md
50
50
  - Rakefile
51
+ - app/assets/images/foreman_salt/Salt.png
51
52
  - app/assets/javascripts/foreman_salt/states.js
52
53
  - app/controllers/foreman_salt/api/v2/base_controller.rb
53
54
  - app/controllers/foreman_salt/api/v2/jobs_controller.rb
@@ -71,6 +72,7 @@ files:
71
72
  - app/helpers/concerns/foreman_salt/smart_proxies_helper_extensions.rb
72
73
  - app/helpers/foreman_salt/salt_keys_helper.rb
73
74
  - app/helpers/foreman_salt/salt_modules_helper.rb
75
+ - app/helpers/foreman_salt/salt_reports_helper.rb
74
76
  - app/lib/actions/foreman_salt/report_import.rb
75
77
  - app/lib/proxy_api/salt.rb
76
78
  - app/models/foreman_salt/concerns/host_managed_extensions.rb
@@ -127,6 +129,8 @@ files:
127
129
  - app/views/foreman_salt/salt_modules/import.html.erb
128
130
  - app/views/foreman_salt/salt_modules/index.html.erb
129
131
  - app/views/foreman_salt/salt_modules/new.html.erb
132
+ - app/views/hosts/select_multiple_salt_environment.html.erb
133
+ - app/views/hosts/select_multiple_salt_master.html.erb
130
134
  - config/routes.rb
131
135
  - db/migrate/20140813081913_add_salt_proxy_to_host_and_host_group.rb
132
136
  - db/migrate/20140817210214_create_salt_modules.rb
@@ -144,12 +148,20 @@ files:
144
148
  - lib/foreman_salt/plugin.rb
145
149
  - lib/foreman_salt/version.rb
146
150
  - lib/tasks/foreman_salt_tasks.rake
151
+ - locale/Makefile
152
+ - locale/action_names.rb
153
+ - locale/de/foreman_salt.edit.po
154
+ - locale/de/foreman_salt.po
155
+ - locale/en/foreman_salt.po
156
+ - locale/foreman_salt.pot
147
157
  - test/factories/foreman_salt_factories.rb
148
158
  - test/functional/api/v2/salt_autosign_controller_test.rb
149
159
  - test/functional/api/v2/salt_environments_controller_test.rb
150
160
  - test/functional/api/v2/salt_keys_controller_test.rb
151
161
  - test/functional/api/v2/salt_states_controller_test.rb
162
+ - test/functional/hosts_controller_test.rb
152
163
  - test/functional/minions_controller_test.rb
164
+ - test/integration/hosts_js_test.rb
153
165
  - test/integration/salt_autosign_test.rb
154
166
  - test/integration/salt_environment_test.rb
155
167
  - test/integration/salt_keys_test.rb
@@ -158,9 +170,11 @@ files:
158
170
  - test/unit/grains_centos.json
159
171
  - test/unit/grains_importer_test.rb
160
172
  - test/unit/highstate.json
173
+ - test/unit/highstate_pchanges.json
161
174
  - test/unit/host_extensions_test.rb
162
175
  - test/unit/hostgroup_extensions_test.rb
163
176
  - test/unit/report_importer_test.rb
177
+ - test/unit/salt_fact_importer_test.rb
164
178
  - test/unit/salt_fact_parser_test.rb
165
179
  - test/unit/salt_keys_test.rb
166
180
  - test/unit/salt_modules_test.rb
@@ -191,15 +205,18 @@ summary: Foreman Plug-in for Salt
191
205
  test_files:
192
206
  - test/integration/salt_environment_test.rb
193
207
  - test/integration/salt_module_test.rb
208
+ - test/integration/hosts_js_test.rb
194
209
  - test/integration/salt_autosign_test.rb
195
210
  - test/integration/salt_keys_test.rb
196
211
  - test/factories/foreman_salt_factories.rb
197
212
  - test/unit/grains_importer_test.rb
213
+ - test/unit/highstate_pchanges.json
198
214
  - test/unit/hostgroup_extensions_test.rb
199
215
  - test/unit/highstate.json
200
216
  - test/unit/salt_fact_parser_test.rb
201
217
  - test/unit/salt_modules_test.rb
202
218
  - test/unit/grains_centos.json
219
+ - test/unit/salt_fact_importer_test.rb
203
220
  - test/unit/report_importer_test.rb
204
221
  - test/unit/salt_keys_test.rb
205
222
  - test/unit/host_extensions_test.rb
@@ -209,3 +226,4 @@ test_files:
209
226
  - test/functional/api/v2/salt_states_controller_test.rb
210
227
  - test/functional/api/v2/salt_environments_controller_test.rb
211
228
  - test/functional/api/v2/salt_keys_controller_test.rb
229
+ - test/functional/hosts_controller_test.rb