foreman_salt 1.1.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +0 -3
  3. data/app/controllers/foreman_salt/api/v2/base_controller.rb +13 -0
  4. data/app/controllers/foreman_salt/api/v2/jobs_controller.rb +9 -17
  5. data/app/controllers/foreman_salt/api/v2/salt_autosign_controller.rb +64 -0
  6. data/app/controllers/foreman_salt/api/v2/salt_environments_controller.rb +46 -0
  7. data/app/controllers/foreman_salt/api/v2/salt_keys_controller.rb +79 -0
  8. data/app/controllers/foreman_salt/api/v2/salt_minions_controller.rb +44 -0
  9. data/app/controllers/foreman_salt/api/v2/salt_states_controller.rb +46 -0
  10. data/app/controllers/foreman_salt/application_controller.rb +0 -1
  11. data/app/controllers/foreman_salt/concerns/hosts_controller_extensions.rb +0 -41
  12. data/app/controllers/foreman_salt/minions_controller.rb +54 -0
  13. data/app/controllers/foreman_salt/salt_autosign_controller.rb +8 -9
  14. data/app/controllers/foreman_salt/salt_environments_controller.rb +1 -1
  15. data/app/controllers/foreman_salt/salt_keys_controller.rb +10 -11
  16. data/app/controllers/foreman_salt/salt_modules_controller.rb +1 -1
  17. data/app/helpers/concerns/foreman_salt/hosts_helper_extensions.rb +10 -10
  18. data/app/helpers/concerns/foreman_salt/smart_proxies_helper_extensions.rb +2 -2
  19. data/app/helpers/foreman_salt/salt_keys_helper.rb +2 -3
  20. data/app/lib/actions/foreman_salt/report_import.rb +1 -2
  21. data/app/lib/proxy_api/salt.rb +1 -1
  22. data/app/models/foreman_salt/concerns/host_managed_extensions.rb +4 -4
  23. data/app/models/foreman_salt/concerns/hostgroup_extensions.rb +2 -2
  24. data/app/models/foreman_salt/concerns/orchestration/salt.rb +1 -1
  25. data/app/models/foreman_salt/fact_name.rb +0 -1
  26. data/app/models/foreman_salt/salt_environment.rb +2 -0
  27. data/app/models/foreman_salt/salt_module.rb +3 -1
  28. data/app/models/setting/salt.rb +12 -0
  29. data/app/overrides/salt_modules_selector.rb +12 -12
  30. data/app/services/foreman_salt/fact_importer.rb +10 -7
  31. data/app/services/foreman_salt/fact_parser.rb +100 -0
  32. data/app/services/foreman_salt/report_importer.rb +17 -14
  33. data/app/services/foreman_salt/smart_proxies/salt_keys.rb +26 -29
  34. data/app/views/foreman_salt/api/v2/salt_autosign/base.json.rabl +3 -0
  35. data/app/views/foreman_salt/api/v2/salt_autosign/create.json.rabl +3 -0
  36. data/app/views/foreman_salt/api/v2/salt_autosign/index.json.rabl +3 -0
  37. data/app/views/foreman_salt/api/v2/salt_autosign/main.json.rabl +3 -0
  38. data/app/views/foreman_salt/api/v2/salt_environments/base.json.rabl +3 -0
  39. data/app/views/foreman_salt/api/v2/salt_environments/create.json.rabl +3 -0
  40. data/app/views/foreman_salt/api/v2/salt_environments/index.json.rabl +3 -0
  41. data/app/views/foreman_salt/api/v2/salt_environments/main.json.rabl +3 -0
  42. data/app/views/foreman_salt/api/v2/salt_environments/show.json.rabl +3 -0
  43. data/app/views/foreman_salt/api/v2/salt_keys/base.json.rabl +3 -0
  44. data/app/views/foreman_salt/api/v2/salt_keys/index.json.rabl +3 -0
  45. data/app/views/foreman_salt/api/v2/salt_keys/main.json.rabl +3 -0
  46. data/app/views/foreman_salt/api/v2/salt_keys/update.json.rabl +3 -0
  47. data/app/views/foreman_salt/api/v2/salt_minions/base.json.rabl +3 -0
  48. data/app/views/foreman_salt/api/v2/salt_minions/index.json.rabl +3 -0
  49. data/app/views/foreman_salt/api/v2/salt_minions/main.json.rabl +3 -0
  50. data/app/views/foreman_salt/api/v2/salt_minions/show.json.rabl +8 -0
  51. data/app/views/foreman_salt/api/v2/salt_states/base.json.rabl +3 -0
  52. data/app/views/foreman_salt/api/v2/salt_states/create.json.rabl +3 -0
  53. data/app/views/foreman_salt/api/v2/salt_states/index.json.rabl +3 -0
  54. data/app/views/foreman_salt/api/v2/salt_states/main.json.rabl +3 -0
  55. data/app/views/foreman_salt/api/v2/salt_states/show.json.rabl +3 -0
  56. data/config/routes.rb +25 -12
  57. data/db/migrate/20140813081913_add_salt_proxy_to_host_and_host_group.rb +0 -1
  58. data/db/migrate/20140817210214_create_salt_modules.rb +1 -1
  59. data/db/migrate/20150220122707_fix_incorrect_report_metrics.rb +22 -0
  60. data/lib/foreman_salt/engine.rb +114 -57
  61. data/lib/foreman_salt/version.rb +1 -1
  62. data/lib/tasks/foreman_salt_tasks.rake +2 -2
  63. data/test/functional/api/v2/salt_autosign_controller_test.rb +27 -0
  64. data/test/functional/api/v2/salt_environments_controller_test.rb +33 -0
  65. data/test/functional/api/v2/salt_keys_controller_test.rb +29 -0
  66. data/test/functional/api/v2/salt_states_controller_test.rb +33 -0
  67. data/test/functional/minions_controller_test.rb +18 -0
  68. data/test/integration/salt_environment_test.rb +0 -1
  69. data/test/integration/salt_keys_test.rb +4 -6
  70. data/test/integration/salt_module_test.rb +0 -1
  71. data/test/unit/grains_centos.json +4 -1
  72. data/test/unit/grains_importer_test.rb +39 -13
  73. data/test/unit/salt_keys_test.rb +4 -6
  74. metadata +44 -5
  75. data/app/controllers/foreman_salt/concerns/smart_proxy_auth_extensions.rb +0 -24
  76. data/test/functional/hosts_controller_test.rb +0 -16
@@ -1,7 +1,7 @@
1
1
  class CreateSaltModules < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :salt_modules do |t|
4
- t.string :name, :default => '', :null => false
4
+ t.string :name, :default => '', :null => false
5
5
  t.timestamps
6
6
  end
7
7
 
@@ -0,0 +1,22 @@
1
+ class FixIncorrectReportMetrics < ActiveRecord::Migration
2
+ def up
3
+ Report.all.each do |report|
4
+ next unless report.metrics && report.metrics['time']
5
+ metrics = report.metrics.dup
6
+
7
+ report.metrics['time'].each do |resource, time|
8
+ metrics['time'][resource] = if time.is_a? String
9
+ Float(time.delete(' ms')) rescue nil
10
+ else
11
+ time
12
+ end
13
+ end
14
+
15
+ report.update_attributes(:metrics => metrics) if metrics != report.metrics
16
+ end
17
+ end
18
+
19
+ def down
20
+ # Nothing
21
+ end
22
+ end
@@ -2,7 +2,6 @@ require 'deface'
2
2
 
3
3
  module ForemanSalt
4
4
  class Engine < ::Rails::Engine
5
-
6
5
  config.autoload_paths += Dir["#{config.root}/app/controllers/concerns"]
7
6
  config.autoload_paths += Dir["#{config.root}/app/helpers/concerns"]
8
7
  config.autoload_paths += Dir["#{config.root}/app/models/concerns"]
@@ -11,82 +10,145 @@ module ForemanSalt
11
10
  config.autoload_paths += Dir["#{config.root}/app/lib"]
12
11
 
13
12
  if defined? ForemanTasks
14
- initializer 'foreman_salt.require_dynflow', :before => 'foreman_tasks.initialize_dynflow' do |app|
13
+ initializer 'foreman_salt.require_dynflow', :before => 'foreman_tasks.initialize_dynflow' do |_app|
15
14
  ForemanTasks.dynflow.require!
16
15
  end
17
16
  end
18
17
 
19
- # Add any db migrations
18
+ initializer 'foreman_salt.load_default_settings', :before => :load_config_initializers do
19
+ require_dependency File.expand_path('../../../app/models/setting/salt.rb', __FILE__) if (Setting.table_exists? rescue(false))
20
+ end
21
+
20
22
  initializer 'foreman_salt.load_app_instance_data' do |app|
21
23
  app.config.paths['db/migrate'] += ForemanSalt::Engine.paths['db/migrate'].existent
22
24
  end
23
25
 
24
- initializer 'foreman_salt.register_plugin', :after=> :finisher_hook do |app|
26
+ initializer 'foreman_salt.apipie' do
27
+ Apipie.configuration.api_controllers_matcher << "#{ForemanSalt::Engine.root}/app/controllers/foreman_salt/api/v2/*.rb"
28
+ Apipie.configuration.checksum_path += ['/salt/api/']
29
+ end
30
+
31
+ initializer 'foreman_salt.register_plugin', :after => :finisher_hook do |_app|
25
32
  Foreman::Plugin.register :foreman_salt do
26
- requires_foreman '>= 1.7'
33
+ requires_foreman '>= 1.8'
27
34
 
35
+ ## Menus
28
36
  menu :top_menu, :salt_environments,
29
- :url_hash => {:controller => :'foreman_salt/salt_environments', :action => :index },
30
- :caption => 'Environments',
31
- :parent => :configure_menu,
32
- :after => :common_parameters
37
+ :url_hash => { :controller => :'foreman_salt/salt_environments', :action => :index },
38
+ :caption => 'Environments',
39
+ :parent => :configure_menu,
40
+ :after => :common_parameters
33
41
 
34
42
  menu :top_menu, :salt_modules,
35
- :url_hash => {:controller => :'foreman_salt/salt_modules', :action => :index },
36
- :caption => 'States',
37
- :parent => :configure_menu,
38
- :after => :common_parameters
43
+ :url_hash => { :controller => :'foreman_salt/salt_modules', :action => :index },
44
+ :caption => 'States',
45
+ :parent => :configure_menu,
46
+ :after => :common_parameters
39
47
 
40
48
  divider :top_menu, :parent => :configure_menu,
41
- :caption => 'Salt',
42
- :after => :common_parameters
43
-
44
- security_block :hosts do |map|
45
- permission :saltrun_hosts, {:hosts => [:saltrun]}, :resource_type => 'Host'
46
- permission :view_hosts, {:hosts => [:salt_external_node]}, :resource_type => 'Host'
47
- end
48
-
49
- security_block :salt_environments do |map|
50
- permission :create_salt_environments, {:'foreman_salt/salt_environments' => [:new, :create]}, :resource_type => 'ForemanSalt::SaltEnvironment'
51
- permission :view_salt_environments, {:'foreman_salt/salt_environments' => [:index, :show, :auto_complete_search]}, :resource_type => 'ForemanSalt::SaltEnvironment'
52
- permission :edit_salt_environments, {:'foreman_salt/salt_environments' => [:update, :edit]},:resource_type => 'ForemanSalt::SaltEnvironment'
53
- permission :destroy_salt_environments, {:'foreman_salt/salt_environments' => [:destroy]}, :resource_type => 'ForemanSalt::SaltEnvironment'
54
- end
55
-
56
- security_block :salt_modules do |map|
57
- permission :create_salt_modules, {:'foreman_salt/salt_modules' => [:new, :create]}, :resource_type => 'ForemanSalt::SaltModule'
58
- permission :view_salt_modules, {:'foreman_salt/salt_modules' => [:index, :show, :auto_complete_search]}, :resource_type => 'ForemanSalt::SaltModule'
59
- permission :edit_salt_modules, {:'foreman_salt/salt_modules' => [:update, :edit]},:resource_type => 'ForemanSalt::SaltModule'
60
- permission :destroy_salt_modules, {:'foreman_salt/salt_modules' => [:destroy]}, :resource_type => 'ForemanSalt::SaltModule'
61
- end
62
-
63
- security_block :salt_keys do |map|
64
- permission :view_smart_proxies_salt_keys, {:'foreman_salt/salt_keys' => [:index]}, :resource_type => 'SmartProxy'
65
- permission :destroy_smart_proxies_salt_keys, {:'foreman_salt/salt_keys' => [:destroy]},:resource_type => 'SmartProxy'
66
- permission :edit_smart_proxies_salt_keys, {:'foreman_salt/salt_keys' => [:accept, :reject]}, :resource_type => 'SmartProxy'
67
- end
68
-
69
- security_block :salt_autosign do |map|
70
- permission :destroy_smart_proxies_salt_autosign, {:'foreman_salt/salt_autosign' => [:destroy]}, :resource_type => 'SmartProxy'
71
- permission :create_smart_proxies_salt_autosign, {:'foreman_salt/salt_autosign' => [:new, :create]}, :resource_type => 'SmartProxy'
72
- permission :view_smart_proxies_salt_autosign, {:'foreman_salt/salt_autosign' => [:index]}, :resource_type => 'SmartProxy'
73
- end
74
-
75
- security_block :api do |map|
76
- permission :create_reports, {:'foreman_salt/api/v2/jobs' => [:upload]}, :resource_type => 'Report'
49
+ :caption => 'Salt',
50
+ :after => :common_parameters
51
+
52
+ # Permissions
53
+ security_block :foreman_salt do |_map|
54
+ permission :destroy_smart_proxies_salt_autosign,
55
+ { :'foreman_salt/salt_autosign' => [:destroy],
56
+ :'foreman_salt/api/v2/salt_autosign' => [:destroy] },
57
+ :resource_type => 'SmartProxy'
58
+
59
+ permission :create_smart_proxies_salt_autosign,
60
+ { :'foreman_salt/salt_autosign' => [:new, :create],
61
+ :'foreman_salt/api/v2/salt_autosign' => [:create] },
62
+ :resource_type => 'SmartProxy'
63
+
64
+ permission :view_smart_proxies_salt_autosign,
65
+ { :'foreman_salt/salt_autosign' => [:index],
66
+ :'foreman_salt/api/v2/salt_autosign' => [:index] },
67
+ :resource_type => 'SmartProxy'
68
+
69
+ permission :create_salt_environments,
70
+ { :'foreman_salt/salt_environments' => [:new, :create],
71
+ :'foreman_salt/api/v2/salt_environments' => [:create] },
72
+ :resource_type => 'ForemanSalt::SaltEnvironment'
73
+
74
+ permission :view_salt_environments,
75
+ { :'foreman_salt/salt_environments' => [:index, :show, :auto_complete_search],
76
+ :'foreman_salt/api/v2/salt_environments' => [:index, :show] },
77
+ :resource_type => 'ForemanSalt::SaltEnvironment'
78
+
79
+ permission :edit_salt_environments,
80
+ { :'foreman_salt/salt_environments' => [:update, :edit] },
81
+ :resource_type => 'ForemanSalt::SaltEnvironment'
82
+
83
+ permission :destroy_salt_environments,
84
+ { :'foreman_salt/salt_environments' => [:destroy],
85
+ :'foreman_salt/api/v2/salt_environments' => [:destroy] },
86
+ :resource_type => 'ForemanSalt::SaltEnvironment'
87
+
88
+ permission :create_reports,
89
+ { :'foreman_salt/api/v2/jobs' => [:upload] },
90
+ :resource_type => 'Report'
91
+
92
+ permission :saltrun_hosts,
93
+ { :'foreman_salt/minions' => [:run] },
94
+ :resource_type => 'Host'
95
+
96
+ permission :edit_hosts,
97
+ { :'foreman_salt/api/v2/salt_minions' => [:update] },
98
+ :resource_type => 'Host'
99
+
100
+ permission :view_hosts,
101
+ { :'foreman_salt/minions' => [:node],
102
+ :'foreman_salt/api/v2/salt_minions' => [:index, :show] },
103
+ :resource_type => 'Host'
104
+
105
+ permission :view_smart_proxies_salt_keys,
106
+ { :'foreman_salt/salt_keys' => [:index],
107
+ :'foreman_salt/api/v2/salt_keys' => [:index] },
108
+ :resource_type => 'SmartProxy'
109
+
110
+ permission :destroy_smart_proxies_salt_keys,
111
+ { :'foreman_salt/salt_keys' => [:destroy],
112
+ :'foreman_salt/api/v2/salt_keys' => [:destroy] },
113
+ :resource_type => 'SmartProxy'
114
+
115
+ permission :edit_smart_proxies_salt_keys,
116
+ { :'foreman_salt/salt_keys' => [:accept, :reject],
117
+ :'foreman_salt/api/v2/salt_keys' => [:update] },
118
+ :resource_type => 'SmartProxy'
119
+
120
+ permission :create_salt_modules,
121
+ { :'foreman_salt/salt_modules' => [:new, :create],
122
+ :'foreman_salt/api/v2/salt_states' => [:create] },
123
+ :resource_type => 'ForemanSalt::SaltModule'
124
+
125
+ permission :view_salt_modules,
126
+ { :'foreman_salt/salt_modules' => [:index, :show, :auto_complete_search],
127
+ :'foreman_salt/api/v2/salt_states' => [:index, :show] },
128
+ :resource_type => 'ForemanSalt::SaltModule'
129
+
130
+ permission :edit_salt_modules,
131
+ { :'foreman_salt/salt_modules' => [:update, :edit] },
132
+ :resource_type => 'ForemanSalt::SaltModule'
133
+
134
+ permission :destroy_salt_modules,
135
+ { :'foreman_salt/salt_modules' => [:destroy],
136
+ :'foreman_salt/api/v2/salt_states' => [:destroy] },
137
+ :resource_type => 'ForemanSalt::SaltModule'
77
138
  end
78
139
 
140
+ ## Roles
79
141
  role 'Salt admin', [:saltrun_hosts, :create_salt_modules, :view_salt_modules, :edit_salt_modules, :destroy_salt_modules,
80
142
  :view_smart_proxies_salt_keys, :destroy_smart_proxies_salt_keys, :edit_smart_proxies_salt_keys,
81
143
  :create_smart_proxies_salt_autosign, :view_smart_proxies_salt_autosign, :destroy_smart_proxies_salt_autosign,
82
144
  :create_salt_environments, :view_salt_environments, :edit_salt_environments, :destroy_salt_environments]
83
-
84
145
  end
85
146
  end
86
147
 
87
148
  config.to_prepare do
88
149
  begin
89
150
  ::FactImporter.register_fact_importer(:foreman_salt, ForemanSalt::FactImporter)
151
+ ::FactParser.register_fact_importer(:foreman_salt, ForemanSalt::FactParser)
90
152
 
91
153
  # Helper Extensions
92
154
  ::HostsHelper.send :include, ForemanSalt::HostsHelperExtensions
@@ -99,13 +161,8 @@ module ForemanSalt
99
161
 
100
162
  # Controller Extensions
101
163
  ::UnattendedController.send :include, ForemanSalt::Concerns::UnattendedControllerExtensions
102
- ::HostsController.send :include, ForemanSalt::Concerns::HostsControllerExtensions
103
- ::HostsController.send :include, ForemanSalt::Concerns::SmartProxyAuthExtensions
104
- ::HostgroupsController.send :include, ForemanSalt::Concerns::HostgroupsControllerExtensions
105
-
106
- # API Extensions
107
- ::Api::V2::HostsController.send :include, ForemanSalt::Concerns::SmartProxyAuthExtensions
108
- ::Api::V2::ReportsController.send :include, ForemanSalt::Concerns::SmartProxyAuthExtensions
164
+ ::HostsController.send :include, ForemanSalt::Concerns::HostsControllerExtensions
165
+ ::HostgroupsController.send :include, ForemanSalt::Concerns::HostgroupsControllerExtensions
109
166
  rescue => e
110
167
  puts "ForemanSalt: skipping engine hook (#{e})"
111
168
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanSalt
2
- VERSION = '1.1.1'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -8,7 +8,7 @@ namespace :foreman_salt do
8
8
  "#{ForemanSalt::Engine.root}/test/**/*.rb"]
9
9
  end
10
10
  rescue
11
- puts "Rubocop not loaded."
11
+ puts 'Rubocop not loaded.'
12
12
  end
13
13
 
14
14
  Rake::Task['rubocop_salt'].invoke
@@ -19,7 +19,7 @@ namespace :test do
19
19
  desc 'Test ForemanSalt'
20
20
  Rake::TestTask.new(:foreman_salt) do |t|
21
21
  test_dir = File.join(File.dirname(__FILE__), '../..', 'test')
22
- t.libs << ['test',test_dir]
22
+ t.libs << ['test', test_dir]
23
23
  t.pattern = "#{test_dir}/**/*_test.rb"
24
24
  t.verbose = true
25
25
  end
@@ -0,0 +1,27 @@
1
+ require 'test_plugin_helper'
2
+
3
+ module ForemanSalt
4
+ class Api::V2::SaltAutosignControllerTest < ActionController::TestCase
5
+ setup do
6
+ @proxy = FactoryGirl.create(:smart_proxy, :with_salt_feature)
7
+ ProxyAPI::Salt.any_instance.stubs(:autosign_list).returns((%w(foo bar baz)))
8
+ end
9
+
10
+ test 'should get index' do
11
+ get :index, :smart_proxy_id => @proxy.id
12
+ assert_response :success
13
+ end
14
+
15
+ test 'should create autosign' do
16
+ ProxyAPI::Salt.any_instance.expects(:autosign_create).once.returns(true)
17
+ post :create, :smart_proxy_id => @proxy.id, :record => 'unicorn.example.com'
18
+ assert_response :success
19
+ end
20
+
21
+ test 'should delete autosign' do
22
+ ProxyAPI::Salt.any_instance.expects(:autosign_remove).once.returns(true)
23
+ delete :destroy, :smart_proxy_id => @proxy.id, :record => 'unicorn.example.com'
24
+ assert_response :success
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_plugin_helper'
2
+
3
+ module ForemanSalt
4
+ class Api::V2::SaltEnvironmentsControllerTest < ActionController::TestCase
5
+ test 'should get index' do
6
+ get :index, {}
7
+ assert_response :success
8
+ assert_template 'api/v2/salt_environments/index'
9
+ end
10
+
11
+ test 'should show environment' do
12
+ environment = ForemanSalt::SaltEnvironment.create(:name => 'foo')
13
+ get :show, :id => environment.id
14
+ assert_response :success
15
+ assert_template 'api/v2/salt_environments/show'
16
+ end
17
+
18
+ test 'should create environment' do
19
+ post :create, :environment => { :name => 'unicorn' }
20
+ assert_response :success
21
+ assert ForemanSalt::SaltEnvironment.find_by_name('unicorn')
22
+ assert_template 'api/v2/salt_environments/create'
23
+ end
24
+
25
+ test 'should delete environment' do
26
+ environment = ForemanSalt::SaltEnvironment.create(:name => 'foo.bar.baz')
27
+ assert_difference('ForemanSalt::SaltEnvironment.count', -1) do
28
+ delete :destroy, :id => environment.id
29
+ end
30
+ assert_response :success
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_plugin_helper'
2
+
3
+ module ForemanSalt
4
+ class Api::V2::SaltKeysControllerTest < ActionController::TestCase
5
+ setup do
6
+ @proxy = FactoryGirl.create(:smart_proxy, :with_salt_feature)
7
+
8
+ ProxyAPI::Salt.any_instance.stubs(:key_list).returns('saltstack.example.com' => { 'state' => 'unaccepted',
9
+ 'fingerprint' => '98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c0' })
10
+ end
11
+
12
+ test 'should get index' do
13
+ get :index, :smart_proxy_id => @proxy.id
14
+ assert_response :success
15
+ end
16
+
17
+ test 'should update keys' do
18
+ ProxyAPI::Salt.any_instance.expects(:key_accept).once.returns(true)
19
+ put :update, :smart_proxy_id => @proxy.id, :name => 'saltstack.example.com', :key => { :state => 'accepted' }
20
+ assert_response :success
21
+ end
22
+
23
+ test 'should delete keys' do
24
+ ProxyAPI::Salt.any_instance.expects(:key_delete).once.returns(true)
25
+ delete :destroy, :smart_proxy_id => @proxy.id, :name => 'saltstack.example.com'
26
+ assert_response :success
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_plugin_helper'
2
+
3
+ module ForemanSalt
4
+ class Api::V2::SaltStatesControllerTest < ActionController::TestCase
5
+ test 'should get index' do
6
+ get :index, {}
7
+ assert_response :success
8
+ assert_template 'api/v2/salt_states/index'
9
+ end
10
+
11
+ test 'should show state' do
12
+ state = ForemanSalt::SaltModule.create(:name => 'foo.bar.baz')
13
+ get :show, :id => state.id
14
+ assert_response :success
15
+ assert_template 'api/v2/salt_states/show'
16
+ end
17
+
18
+ test 'should create state' do
19
+ post :create, :state => { :name => 'unicorn' }
20
+ assert_response :success
21
+ assert ForemanSalt::SaltModule.find_by_name('unicorn')
22
+ assert_template 'api/v2/salt_states/create'
23
+ end
24
+
25
+ test 'should delete state' do
26
+ state = ForemanSalt::SaltModule.create(:name => 'foo.bar.baz')
27
+ assert_difference('ForemanSalt::SaltModule.count', -1) do
28
+ delete :destroy, :id => state.id
29
+ end
30
+ assert_response :success
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_plugin_helper'
2
+
3
+ module ForemanSalt
4
+ class MinionsControllerTest < ActionController::TestCase
5
+ test 'salt smart proxy should get salt external node' do
6
+ User.current = nil
7
+ Setting[:restrict_registered_smart_proxies] = true
8
+ Setting[:require_ssl_smart_proxies] = false
9
+
10
+ proxy = FactoryGirl.create :smart_proxy, :with_salt_feature
11
+ Resolv.any_instance.stubs(:getnames).returns([proxy.to_s])
12
+
13
+ host = FactoryGirl.create :host
14
+ get :node, :id => host, :format => 'yml'
15
+ assert_response :success
16
+ end
17
+ end
18
+ end
@@ -2,7 +2,6 @@ require 'test_plugin_helper'
2
2
 
3
3
  module ForemanSalt
4
4
  class SaltEnvironmentTest < ActionDispatch::IntegrationTest
5
-
6
5
  test 'index page' do
7
6
  FactoryGirl.create_list :salt_environment, 50
8
7
  assert_index_page(salt_environments_path, 'Salt Environment', 'New Salt Environment')
@@ -6,12 +6,10 @@ module ForemanSalt
6
6
  @proxy = FactoryGirl.create :smart_proxy, :with_salt_feature
7
7
 
8
8
  ::ProxyAPI::Salt.any_instance.stubs(:key_list).returns(
9
- {
10
- 'saltstack.example.com' => {'state'=>'accepted', 'fingerprint'=>'98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c0'},
11
- 'saltclient01.example.com'=> {'state'=>'unaccepted', 'fingerprint'=>'98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c1'},
12
- 'saltclient02.example.com'=> {'state'=>'unaccepted', 'fingerprint'=>'98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c2'},
13
- 'saltclient03.example.com' => {'state'=>'rejected', 'fingerprint'=>'98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c3'}
14
- }
9
+ 'saltstack.example.com' => { 'state' => 'accepted', 'fingerprint' => '98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c0' },
10
+ 'saltclient01.example.com' => { 'state' => 'unaccepted', 'fingerprint' => '98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c1' },
11
+ 'saltclient02.example.com' => { 'state' => 'unaccepted', 'fingerprint' => '98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c2' },
12
+ 'saltclient03.example.com' => { 'state' => 'rejected', 'fingerprint' => '98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c3' }
15
13
  )
16
14
  end
17
15
 
@@ -2,7 +2,6 @@ require 'test_plugin_helper'
2
2
 
3
3
  module ForemanSalt
4
4
  class SaltModuleTest < ActionDispatch::IntegrationTest
5
-
6
5
  test 'index page' do
7
6
  FactoryGirl.create_list :salt_module, 50
8
7
  assert_index_page(salt_modules_path, 'Salt State', 'New Salt State')
@@ -102,7 +102,10 @@
102
102
  "os_family": "RedHat",
103
103
  "operatingsystemrelease": "6.5",
104
104
  "gpus::0::vendor": "unknown",
105
- "lsb_distrib_codename": "Final"
105
+ "lsb_distrib_codename": "Final",
106
+ "fqdn_ip4::0": "10.7.13.141",
107
+ "ip_interfaces::eth1::0": "1.2.3.4",
108
+ "hwaddr_interfaces::eth1": "DE:AD:BE:EF:07:13"
106
109
  },
107
110
  "name": "saltclient01.example.com"
108
111
  }
@@ -6,33 +6,59 @@ module ForemanSalt
6
6
  User.current = User.find_by_login 'admin'
7
7
  Setting[:create_new_host_when_facts_are_uploaded] = true
8
8
 
9
- # I don't even know, the plug-in successfully registers the importer
10
- # in development, and even on the Rails test console but not here
11
- # in the test itself...
12
- ::FactImporter.stubs(:importer_for).returns(ForemanSalt::FactImporter)
9
+ Operatingsystem.where(:name => 'CentOS', :major => '6', :minor => '5').delete_all
13
10
 
14
11
  grains = JSON.parse(File.read(File.join(Engine.root, 'test', 'unit', 'grains_centos.json')))
15
- @host = grains['name']
16
- @facts = grains['facts']
12
+ host = grains['name']
13
+ facts = HashWithIndifferentAccess.new(grains['facts'])
14
+
15
+ (@imported_host, _) = ::Host::Managed.import_host_and_facts host, facts
17
16
  end
18
17
 
19
18
  test 'importing salt grains creates a host' do
20
- refute Host.find_by_name(@host)
21
- ::Host::Managed.import_host_and_facts @host, @facts
22
- assert Host.find_by_name(@host)
19
+ assert @imported_host
23
20
  end
24
21
 
25
22
  test 'grains are successfully imported for a host' do
26
- (host, _) = ::Host::Managed.import_host_and_facts @host, @facts
27
- assert_equal 'CentOS', host.facts_hash['operatingsystem']
23
+ assert_equal 'CentOS', @imported_host.facts_hash['operatingsystem']
28
24
  end
29
25
 
30
26
  test 'nested facts have valid parents' do
31
- (host, _) = ::Host::Managed.import_host_and_facts @host, @facts
32
27
  parent = ::FactName.find_by_name('cpu_flags')
33
- children = host.fact_values.with_fact_parent_id(parent)
28
+ children = @imported_host.fact_values.with_fact_parent_id(parent)
34
29
  assert_not_empty children
35
30
  assert_empty children.map(&:fact_name).reject { |fact| fact.name =~ /\Acpu_flags#{FactName::SEPARATOR}[0-9]+/ }
36
31
  end
32
+
33
+ # Parser
34
+ test 'imported host has operating system' do
35
+ assert_equal @imported_host.os.to_label, 'CentOS 6.5'
36
+ end
37
+
38
+ test 'imported host operating system has deduced family' do
39
+ assert_equal @imported_host.os.family, 'Redhat'
40
+ end
41
+
42
+ test 'imported host has hardware model' do
43
+ assert_equal @imported_host.model.name, 'KVM'
44
+ end
45
+
46
+ test 'imported host has architecture' do
47
+ assert_equal @imported_host.arch.name, 'x86_64'
48
+ end
49
+
50
+ test 'imported host has primary ip' do
51
+ assert_equal @imported_host.ip, '10.7.13.141'
52
+ end
53
+
54
+ test 'imported host has primary mac' do
55
+ assert_equal @imported_host.mac, '52:54:00:35:30:2a'
56
+ end
57
+
58
+ test 'imported host has additional interface' do
59
+ nic = @imported_host.interfaces.find_by_identifier('eth1')
60
+ assert_equal nic.mac, 'de:ad:be:ef:07:13'
61
+ assert_equal nic.ip, '1.2.3.4'
62
+ end
37
63
  end
38
64
  end
@@ -10,12 +10,10 @@ class SaltKeysTest < ActiveSupport::TestCase
10
10
  @proxy = OpenStruct.new(:id => 1, :url => 'http://dummy.example.com:9090')
11
11
 
12
12
  ProxyAPI::Salt.any_instance.stubs(:key_list).returns(
13
- {
14
- 'saltstack.example.com' => {'state'=>'accepted', 'fingerprint'=>'98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c0'},
15
- 'saltclient01.example.com' => {'state'=>'unaccepted', 'fingerprint'=>'98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c1'},
16
- 'saltclient02.example.com' => {'state'=>'unaccepted', 'fingerprint'=>'98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c2'},
17
- 'saltclient03.example.com' => {'state'=>'rejected', 'fingerprint'=>'98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c3'}
18
- }
13
+ 'saltstack.example.com' => { 'state' => 'accepted', 'fingerprint' => '98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c0' },
14
+ 'saltclient01.example.com' => { 'state' => 'unaccepted', 'fingerprint' => '98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c1' },
15
+ 'saltclient02.example.com' => { 'state' => 'unaccepted', 'fingerprint' => '98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c2' },
16
+ 'saltclient03.example.com' => { 'state' => 'rejected', 'fingerprint' => '98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c3' }
19
17
  )
20
18
  end
21
19