From d1ec4fd9fc26a7c45ceadbcb47788abd95163f93 Mon Sep 17 00:00:00 2001 From: archanaserver Date: Tue, 9 Jan 2024 14:30:53 +0530 Subject: [PATCH] Fix Minitest/EmptyLineBeforeAssertionMethods cop --- .rubocop.yml | 3 +++ .../api/v2/salt_autosign_controller_test.rb | 3 +++ .../api/v2/salt_environments_controller_test.rb | 3 +++ .../api/v2/salt_keys_controller_test.rb | 3 +++ .../api/v2/salt_states_controller_test.rb | 8 ++++++++ .../api/v2/salt_variables_controller_test.rb | 3 +++ test/functional/hosts_controller_test.rb | 2 ++ test/functional/minions_controller_test.rb | 14 ++++++++++++++ test/integration/hosts_js_test.rb | 6 ++++++ test/integration/salt_autosign_test.rb | 4 ++++ test/integration/salt_environment_test.rb | 3 +++ test/integration/salt_keys_test.rb | 6 ++++++ test/integration/salt_module_test.rb | 4 ++++ test/integration/salt_variable_test.rb | 3 +++ test/unit/grains_importer_test.rb | 2 ++ test/unit/host_extensions_test.rb | 15 +++++++++++++++ test/unit/hostgroup_extensions_test.rb | 8 ++++++++ test/unit/report_importer_test.rb | 8 ++++++++ test/unit/salt_modules_test.rb | 2 ++ test/unit/salt_variables_test.rb | 4 ++++ 20 files changed, 104 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index f77a008d..510c0219 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -25,3 +25,6 @@ Metrics/BlockLength: - 'config/*routes.rb' - 'lib/foreman_salt/plugin.rb' - 'test/**/*.rb' + +Minitest/EmptyLineBeforeAssertionMethods: + Enabled: true diff --git a/test/functional/api/v2/salt_autosign_controller_test.rb b/test/functional/api/v2/salt_autosign_controller_test.rb index f56cbb05..0c90a679 100644 --- a/test/functional/api/v2/salt_autosign_controller_test.rb +++ b/test/functional/api/v2/salt_autosign_controller_test.rb @@ -13,18 +13,21 @@ class SaltAutosignControllerTest < ActionController::TestCase test 'should get index' do get :index, params: { smart_proxy_id: @proxy.id } + assert_response :success end test 'should create autosign' do ProxyAPI::Salt.any_instance.expects(:autosign_create).once.returns(true) post :create, params: { smart_proxy_id: @proxy.id, record: 'unicorn.example.com' } + assert_response :success end test 'should delete autosign' do ProxyAPI::Salt.any_instance.expects(:autosign_remove).once.returns(true) delete :destroy, params: { smart_proxy_id: @proxy.id, record: 'unicorn.example.com' } + assert_response :success end end diff --git a/test/functional/api/v2/salt_environments_controller_test.rb b/test/functional/api/v2/salt_environments_controller_test.rb index 6ceb94be..27477784 100644 --- a/test/functional/api/v2/salt_environments_controller_test.rb +++ b/test/functional/api/v2/salt_environments_controller_test.rb @@ -6,6 +6,7 @@ module V2 class SaltEnvironmentsControllerTest < ActionController::TestCase test 'should get index' do get :index + assert_response :success assert_template 'api/v2/salt_environments/index' end @@ -13,12 +14,14 @@ class SaltEnvironmentsControllerTest < ActionController::TestCase test 'should show environment' do environment = ForemanSalt::SaltEnvironment.create(name: 'foo') get :show, params: { id: environment.id } + assert_response :success assert_template 'api/v2/salt_environments/show' end test 'should create environment' do post :create, params: { environment: { name: 'unicorn' } } + assert_response :success assert ForemanSalt::SaltEnvironment.find_by(name: 'unicorn') assert_template 'api/v2/salt_environments/create' diff --git a/test/functional/api/v2/salt_keys_controller_test.rb b/test/functional/api/v2/salt_keys_controller_test.rb index 3f5d73f5..a6009816 100644 --- a/test/functional/api/v2/salt_keys_controller_test.rb +++ b/test/functional/api/v2/salt_keys_controller_test.rb @@ -13,18 +13,21 @@ class SaltKeysControllerTest < ActionController::TestCase test 'should get index' do get :index, params: { smart_proxy_id: @proxy.id } + assert_response :success end test 'should update keys' do ProxyAPI::Salt.any_instance.expects(:key_accept).once.returns(true) put :update, params: { smart_proxy_id: @proxy.id, name: 'saltstack.example.com', state: 'accepted' } + assert_response :success end test 'should delete keys' do ProxyAPI::Salt.any_instance.expects(:key_delete).once.returns(true) delete :destroy, params: { smart_proxy_id: @proxy.id, name: 'saltstack.example.com' } + assert_response :success end end diff --git a/test/functional/api/v2/salt_states_controller_test.rb b/test/functional/api/v2/salt_states_controller_test.rb index 021f21cb..44e9531d 100644 --- a/test/functional/api/v2/salt_states_controller_test.rb +++ b/test/functional/api/v2/salt_states_controller_test.rb @@ -6,6 +6,7 @@ module V2 class SaltStatesControllerTest < ActionController::TestCase test 'should get index' do get :index + assert_response :success assert_template 'api/v2/salt_states/index' end @@ -13,12 +14,14 @@ class SaltStatesControllerTest < ActionController::TestCase test 'should show state' do state = ForemanSalt::SaltModule.create(name: 'foo.bar.baz') get :show, params: { id: state.id } + assert_response :success assert_template 'api/v2/salt_states/show' end test 'should create state' do post :create, params: { state: { name: 'unicorn' } } + assert_response :success assert ForemanSalt::SaltModule.find_by(name: 'unicorn') assert_template 'api/v2/salt_states/create' @@ -48,12 +51,14 @@ class SaltStatesControllerTest < ActionController::TestCase @states.each do |env, states| environment = ::ForemanSalt::SaltEnvironment.find_by(name: env) + assert_empty environment.salt_modules.map(&:name) - states end end test 'should import only from a given environment' do post :import, params: { smart_proxy_id: @proxy.id, salt_environments: ['env2'] } + assert_response :success assert_not ::ForemanSalt::SaltEnvironment.where(name: 'env1').first assert ::ForemanSalt::SaltEnvironment.where(name: 'env2').first @@ -64,6 +69,7 @@ class SaltStatesControllerTest < ActionController::TestCase state = FactoryBot.create :salt_module, salt_environments: [env] post :import, params: { smart_proxy_id: @proxy.id, actions: ['add'] } + assert_response :success assert ::ForemanSalt::SaltModule.where(id: state).first assert ::ForemanSalt::SaltModule.where(name: 'state1').first @@ -72,6 +78,7 @@ class SaltStatesControllerTest < ActionController::TestCase test 'should limit actions to remove' do state = FactoryBot.create :salt_module post :import, params: { smart_proxy_id: @proxy.id, actions: ['remove'] } + assert_response :success assert_not ::ForemanSalt::SaltModule.where(id: state).first assert_not ::ForemanSalt::SaltModule.where(name: 'state1').first @@ -79,6 +86,7 @@ class SaltStatesControllerTest < ActionController::TestCase test 'dryrun should do nothing' do post :import, params: { smart_proxy_id: @proxy.id, dryrun: true } + assert_response :success assert_not ::ForemanSalt::SaltModule.all.any? assert_not ::ForemanSalt::SaltEnvironment.all.any? diff --git a/test/functional/api/v2/salt_variables_controller_test.rb b/test/functional/api/v2/salt_variables_controller_test.rb index 97cb7575..384a8487 100644 --- a/test/functional/api/v2/salt_variables_controller_test.rb +++ b/test/functional/api/v2/salt_variables_controller_test.rb @@ -14,12 +14,14 @@ class SaltVariablesControllerTest < ActionController::TestCase test 'should get index' do get :index, session: set_session_user response = JSON.parse(@response.body) + assert_not_empty response['results'] assert_response :success end test 'should destroy' do delete :destroy, params: { id: @variable.id }, session: set_session_user + assert_response :ok assert_not SaltVariable.exists?(@variable.id) end @@ -27,6 +29,7 @@ class SaltVariablesControllerTest < ActionController::TestCase test 'should create' do params = { key: 'test name', salt_state_id: FactoryBot.create(:salt_module).id } post :create, params: params, session: set_session_user + assert_response :success end end diff --git a/test/functional/hosts_controller_test.rb b/test/functional/hosts_controller_test.rb index fb116e38..65e72eb5 100644 --- a/test/functional/hosts_controller_test.rb +++ b/test/functional/hosts_controller_test.rb @@ -21,6 +21,7 @@ class HostsControllerExtensionsTest < ActionController::TestCase post :update_multiple_salt_master, params: params, session: set_session_user.merge(user: users(:one).id) + assert_response :forbidden end @@ -79,6 +80,7 @@ class HostsControllerExtensionsTest < ActionController::TestCase post :update_multiple_salt_environment, params: params, session: set_session_user.merge(user: users(:one).id) + assert_response :forbidden end diff --git a/test/functional/minions_controller_test.rb b/test/functional/minions_controller_test.rb index 1322e11a..5d13eccc 100644 --- a/test/functional/minions_controller_test.rb +++ b/test/functional/minions_controller_test.rb @@ -18,9 +18,11 @@ class MinionsControllerTest < ActionController::TestCase test 'salt smart proxy should get salt external node' do get :node, params: { id: @host, format: 'yml' } + assert_response :success res = YAML.safe_load(@response.body) + assert_equal('different', res['parameters']['parameter1']) end @@ -28,9 +30,11 @@ class MinionsControllerTest < ActionController::TestCase Setting['salt_namespace_pillars'] = true get :node, params: { id: @host, format: 'yml' } + assert_response :success res = YAML.safe_load(@response.body) + assert_equal('different', res['parameters']['foreman']['parameter1']) end @@ -40,9 +44,11 @@ class MinionsControllerTest < ActionController::TestCase @host.salt_modules << var.salt_module get :node, params: { id: @host, format: 'yml' } + assert_response :success res = YAML.safe_load(@response.body) + assert_equal res['parameters'][var.key], var.value end @@ -52,9 +58,11 @@ class MinionsControllerTest < ActionController::TestCase @host.salt_modules << var.salt_module get :node, params: { id: @host, format: 'yml' } + assert_response :success res = YAML.safe_load(@response.body) + assert_equal res['parameters']['parameter1'], var.value end @@ -70,9 +78,11 @@ class MinionsControllerTest < ActionController::TestCase @host.salt_modules << var.salt_module get :node, params: { id: @host, format: 'yml' } + assert_response :success res = YAML.safe_load(@response.body) + assert_equal res['parameters']['parameter1'], value2.value end @@ -87,9 +97,11 @@ class MinionsControllerTest < ActionController::TestCase @host2.salt_modules << var.salt_module get :node, params: { id: @host2, format: 'yml' } + assert_response :success res = YAML.safe_load(@response.body) + assert_equal res['parameters']['parameter1'], value2.value end @@ -104,9 +116,11 @@ class MinionsControllerTest < ActionController::TestCase var.salt_module.salt_environments << @host2.salt_environment @host2.salt_modules << var.salt_module get :node, params: { id: @host2, format: 'yml' } + assert_response :success res = YAML.safe_load(@response.body) + assert_equal res['parameters']['parameter1'], var.value end end diff --git a/test/integration/hosts_js_test.rb b/test/integration/hosts_js_test.rb index 57af93d3..9819dc4e 100644 --- a/test/integration/hosts_js_test.rb +++ b/test/integration/hosts_js_test.rb @@ -31,10 +31,12 @@ def multiple_actions_div # Dropdown visible? assert multiple_actions_div.find('.dropdown-toggle').visible? multiple_actions_div.find('.dropdown-toggle').click + assert multiple_actions_div.find('ul').visible? # Hosts are added to cookie host_ids_on_cookie = JSON.parse(CGI.unescape(get_me_the_cookie('_ForemanSelectedhosts')&.fetch(:value))) + assert_includes(host_ids_on_cookie, @host.id) within('#submit_multiple') do @@ -46,6 +48,7 @@ def multiple_actions_div # remove hosts cookie on submit index_modal.find('.btn-primary').click + assert_current_path hosts_path assert_empty(get_me_the_cookie('_ForemanSelectedhosts')) end @@ -60,10 +63,12 @@ def multiple_actions_div # Dropdown visible? assert multiple_actions_div.find('.dropdown-toggle').visible? multiple_actions_div.find('.dropdown-toggle').click + assert multiple_actions_div.find('ul').visible? # Hosts are added to cookie host_ids_on_cookie = JSON.parse(CGI.unescape(get_me_the_cookie('_ForemanSelectedhosts')&.fetch(:value))) + assert_includes(host_ids_on_cookie, @host.id) within('#submit_multiple') do @@ -75,6 +80,7 @@ def multiple_actions_div # remove hosts cookie on submit index_modal.find('.btn-primary').click + assert_current_path hosts_path assert_empty(get_me_the_cookie('_ForemanSelectedhosts')) end diff --git a/test/integration/salt_autosign_test.rb b/test/integration/salt_autosign_test.rb index 753a8c50..e0d38f2d 100644 --- a/test/integration/salt_autosign_test.rb +++ b/test/integration/salt_autosign_test.rb @@ -17,19 +17,23 @@ class SaltKeysTest < ActionDispatch::IntegrationTest test 'smart proxy details has autosign link' do visit smart_proxy_path(@proxy) + assert page.has_link? 'Salt Autosign' click_link 'Salt Autosign' + assert page.has_title?("Autosign entries for #{@proxy.hostname}"), 'Page title does not appear' end test 'index page' do visit smart_proxy_salt_autosign_index_path(smart_proxy_id: @proxy.id) + assert find_link('Keys').visible?, 'Keys is not visible' assert has_title?("Autosign entries for #{@proxy.hostname}"), 'Page title does not appear' end test 'has list of autosign' do visit smart_proxy_salt_autosign_index_path(smart_proxy_id: @proxy.id) + assert has_content?('foo.example.com'), 'Missing autosign entry on index page' end end diff --git a/test/integration/salt_environment_test.rb b/test/integration/salt_environment_test.rb index 34cad89f..693a1e67 100644 --- a/test/integration/salt_environment_test.rb +++ b/test/integration/salt_environment_test.rb @@ -5,12 +5,14 @@ module ForemanSalt class SaltEnvironmentTest < IntegrationTestWithJavascript test 'index page' do FactoryBot.create_list :salt_environment, 5 + assert_index_page(salt_environments_path, 'Salt Environment', 'New Salt Environment') end test 'create new page' do assert_new_button(salt_environments_path, 'New Salt Environment', new_salt_environment_path) fill_in 'foreman_salt_salt_environment_name', with: 'common' + assert_submit_button(salt_environments_path) assert page.has_link? 'common' end @@ -20,6 +22,7 @@ class SaltEnvironmentTest < IntegrationTestWithJavascript visit salt_environments_path click_link salt_environment.name fill_in 'foreman_salt_salt_environment_name', with: 'some_other_name' + assert_submit_button(salt_environments_path) assert page.has_link? 'some_other_name' end diff --git a/test/integration/salt_keys_test.rb b/test/integration/salt_keys_test.rb index b30a283c..a309703f 100644 --- a/test/integration/salt_keys_test.rb +++ b/test/integration/salt_keys_test.rb @@ -20,29 +20,35 @@ class SaltKeysTest < ActionDispatch::IntegrationTest test 'smart proxy details has keys link' do visit smart_proxy_path(@proxy) + assert page.has_link? 'Salt Keys' click_link 'Salt Keys' + assert page.has_title?("Salt Keys on #{@proxy}"), 'Page title does not appear' end test 'index page' do visit smart_proxy_salt_keys_path(smart_proxy_id: @proxy.id) + assert has_title?("Salt Keys on #{@proxy}"), 'Page title does not appear' end test 'has list of keys' do visit smart_proxy_salt_keys_path(smart_proxy_id: @proxy.id) + assert has_content?('saltclient01.example.com'), 'Missing key on index page' assert has_content?('98:c2:63:c1:57:59:bc:bd:f1:ef:5a:38:b2:e9:71:c1'), 'Missing fingerprint on index page' end test 'has accept link' do ProxyAPI::Salt.any_instance.stubs(:key_accept).returns(true) + assert_row_button(smart_proxy_salt_keys_path(smart_proxy_id: @proxy.id), 'saltclient01.example.com', 'Accept', dropdown: true) end test 'has reject link' do ProxyAPI::Salt.any_instance.stubs(:key_reject).returns(true) + assert_row_button(smart_proxy_salt_keys_path(smart_proxy_id: @proxy.id), 'saltclient01.example.com', 'Reject', dropdown: true) end end diff --git a/test/integration/salt_module_test.rb b/test/integration/salt_module_test.rb index ac20f967..e5fd9e80 100644 --- a/test/integration/salt_module_test.rb +++ b/test/integration/salt_module_test.rb @@ -14,12 +14,14 @@ class SaltModuleTest < IntegrationTestWithJavascript test 'index page' do FactoryBot.create_list :salt_module, 5 + assert_index_page(salt_modules_path, 'Salt State', 'New Salt State') end test 'create new page' do assert_new_button(salt_modules_path, 'New Salt State', new_salt_module_path) fill_in 'foreman_salt_salt_module_name', with: 'common' + assert_submit_button(salt_modules_path) assert page.has_link? 'common' end @@ -29,6 +31,7 @@ class SaltModuleTest < IntegrationTestWithJavascript visit salt_modules_path click_link salt_module.name fill_in :foreman_salt_salt_module_name, with: 'some_other_name' + assert_submit_button(salt_modules_path) assert page.has_link? 'some_other_name' end @@ -50,6 +53,7 @@ class SaltModuleTest < IntegrationTestWithJavascript all('input.state_check').each { |checkbox| check(checkbox[:id]) } click_button 'Update' + assert page.has_link? 'state1' end end diff --git a/test/integration/salt_variable_test.rb b/test/integration/salt_variable_test.rb index ae92bc86..a8851844 100644 --- a/test/integration/salt_variable_test.rb +++ b/test/integration/salt_variable_test.rb @@ -9,14 +9,17 @@ class SaltVariableTest < IntegrationTestWithJavascript test 'index page' do FactoryBot.create_list :salt_variable, 5 + assert_index_page(salt_variables_path, 'Salt Variable', 'New Salt Variable') end test 'create new page' do state = FactoryBot.create :salt_module + assert_new_button(salt_variables_path, 'New Salt Variable', new_salt_variable_path) fill_in 'foreman_salt_salt_variable_key', with: 'mykey' select2(state.name, from: 'foreman_salt_salt_variable_salt_module_id') + assert_submit_button(salt_variables_path) assert page.has_link? 'mykey' end diff --git a/test/unit/grains_importer_test.rb b/test/unit/grains_importer_test.rb index 71cdc242..12a8fbdf 100644 --- a/test/unit/grains_importer_test.rb +++ b/test/unit/grains_importer_test.rb @@ -28,6 +28,7 @@ class GrainsImporterTest < ActiveSupport::TestCase test 'nested facts have valid parents' do parent = ::FactName.find_by(name: 'cpu_flags') children = @imported_host.fact_values.with_fact_parent_id(parent) + assert_not_empty children assert_empty children.map(&:fact_name).reject { |fact| fact.name =~ /\Acpu_flags#{FactName::SEPARATOR}[0-9]+/ } end @@ -59,6 +60,7 @@ class GrainsImporterTest < ActiveSupport::TestCase test 'imported host has additional interface' do nic = @imported_host.interfaces.find_by(identifier: 'eth1') + assert_equal('de:ad:be:ef:07:13', nic.mac) assert_equal('1.2.3.4', nic.ip) end diff --git a/test/unit/host_extensions_test.rb b/test/unit/host_extensions_test.rb index fc24f2f1..3d6ccdc4 100644 --- a/test/unit/host_extensions_test.rb +++ b/test/unit/host_extensions_test.rb @@ -10,18 +10,21 @@ class HostExtensionsTest < ActiveSupport::TestCase test 'host has a salt smart proxy' do host = FactoryBot.create :host host.salt_proxy = @proxy + assert host.salt_proxy.has_feature? 'Salt' end test 'smart_proxy_ids returns salt smart proxy' do host = FactoryBot.create :host host.salt_proxy = @proxy + assert_includes host.smart_proxy_ids, host.salt_proxy_id end test 'host params includes salt_master' do host = FactoryBot.create :host host.salt_proxy = @proxy + assert host.params.key? 'salt_master' assert_equal host.params['salt_master'], host.salt_master end @@ -31,6 +34,7 @@ class HostExtensionsTest < ActiveSupport::TestCase hostgroup.salt_proxy = @proxy host = FactoryBot.create :host, hostgroup: hostgroup host.set_hostgroup_defaults + assert_equal host.salt_proxy, hostgroup.salt_proxy end @@ -51,8 +55,10 @@ class HostExtensionsTest < ActiveSupport::TestCase test '#configuration? considers salt' do host = FactoryBot.build(:host) + assert_not host.configuration? host.salt_proxy = @proxy + assert host.configuration? end @@ -70,8 +76,10 @@ class HostExtensionsTest < ActiveSupport::TestCase autosign_key = 'asdfasdfasfasdf' @host.expects(:generate_provisioning_key).returns(autosign_key) @host.build = true + assert @host.save! @host.clear_host_parameters_cache! + assert_equal autosign_key, @host.salt_autosign_key end end @@ -84,6 +92,7 @@ class HostExtensionsTest < ActiveSupport::TestCase test 'host returns empty hash when deriving salt grains with default autosign' do expected_hash = {} + assert_equal expected_hash, @host.instance_eval { derive_salt_grains } end @@ -91,28 +100,33 @@ class HostExtensionsTest < ActiveSupport::TestCase autosign_key = 'asdfasdfasfasdf' expected_hash = { @host.autosign_grain_name => autosign_key } @host.salt_autosign_key = autosign_key + assert_equal expected_hash, @host.instance_eval { derive_salt_grains(use_autosign: true) } end test 'host returns empty hash when deriving salt grains without any given' do expected_hash = {} + assert_equal expected_hash, @host.instance_eval { derive_salt_grains(use_autosign: true) } end test 'host returns empty hash when deriving salt grains without autosign' do expected_hash = {} + assert_equal expected_hash, @host.instance_eval { derive_salt_grains(use_autosign: false) } end test 'host returns host param grains when deriving salt grains' do expected_hash = { "Some key": 'Some value', "Another key": 'An extraordinary value' } @host.host_params[@host.host_params_grains_name] = expected_hash + assert_equal expected_hash, @host.instance_eval { derive_salt_grains(use_autosign: false) } end test 'host returns only host param grains when deriving salt grains' do expected_hash = { "Some key": 'Some value', "Another key": 'An extraordinary value' } @host.host_params[@host.host_params_grains_name] = expected_hash + assert_equal expected_hash, @host.instance_eval { derive_salt_grains(use_autosign: true) } end @@ -123,6 +137,7 @@ class HostExtensionsTest < ActiveSupport::TestCase expected_hash = host_param_grains.merge(@host.autosign_grain_name => autosign_key) @host.salt_autosign_key = autosign_key @host.host_params[@host.host_params_grains_name] = host_param_grains + assert_equal expected_hash, @host.instance_eval { derive_salt_grains(use_autosign: true) } end end diff --git a/test/unit/hostgroup_extensions_test.rb b/test/unit/hostgroup_extensions_test.rb index 1f0a5cf9..43ffcdbb 100644 --- a/test/unit/hostgroup_extensions_test.rb +++ b/test/unit/hostgroup_extensions_test.rb @@ -10,12 +10,14 @@ class HostgroupExtensionsTest < ActiveSupport::TestCase test 'host group has a salt smart proxy' do hostgroup = FactoryBot.create :hostgroup hostgroup.salt_proxy = @proxy + assert_includes hostgroup.salt_proxy.features.map(&:name), 'Salt' end test 'nested host group inherits salt modules from parent' do parent = FactoryBot.create :hostgroup, :with_salt_modules child = FactoryBot.create :hostgroup, parent: parent + assert_equal [], parent.all_salt_modules - child.all_salt_modules end @@ -23,6 +25,7 @@ class HostgroupExtensionsTest < ActiveSupport::TestCase parent = FactoryBot.create :hostgroup child_one = FactoryBot.create :hostgroup, parent: parent, salt_proxy: @proxy child_two = FactoryBot.create :hostgroup, parent: child_one + assert_not_nil parent assert_not_nil child_one assert_not_nil child_two @@ -36,6 +39,7 @@ class HostgroupExtensionsTest < ActiveSupport::TestCase parent = FactoryBot.create :hostgroup child_one = FactoryBot.create :hostgroup, parent: parent, salt_environment: environment child_two = FactoryBot.create :hostgroup, parent: child_one + assert_not_nil parent assert_not_nil child_one assert_not_nil child_two @@ -50,12 +54,14 @@ class HostgroupExtensionsTest < ActiveSupport::TestCase child = FactoryBot.create :hostgroup, :with_salt_modules, salt_environment: environment, parent: parent total = parent.salt_modules.count + child.salt_modules.count + assert_equal total, child.all_salt_modules.count end test 'child doesnt get modules from outside its environment' do parent = FactoryBot.create :hostgroup, :with_salt_modules child = FactoryBot.create :hostgroup, :with_salt_modules, parent: parent + assert_equal child.salt_modules.count, child.all_salt_modules.count end @@ -63,6 +69,7 @@ class HostgroupExtensionsTest < ActiveSupport::TestCase parent = FactoryBot.create :hostgroup, :with_salt_modules child_one = FactoryBot.create :hostgroup, parent: parent child_two = FactoryBot.create :hostgroup, parent: child_one + assert_empty parent.all_salt_modules - child_two.all_salt_modules end @@ -70,6 +77,7 @@ class HostgroupExtensionsTest < ActiveSupport::TestCase parent = FactoryBot.create :hostgroup child_one = FactoryBot.create :hostgroup, parent: parent child_two = FactoryBot.create :hostgroup, :with_salt_modules, parent: child_one + assert child_two.all_salt_modules.any? end end diff --git a/test/unit/report_importer_test.rb b/test/unit/report_importer_test.rb index d1876e4f..60a174a8 100644 --- a/test/unit/report_importer_test.rb +++ b/test/unit/report_importer_test.rb @@ -16,18 +16,21 @@ class ReportImporterTest < ActiveSupport::TestCase test 'importing report creates a host' do assert_not Host.find_by(name: @host) ForemanSalt::ReportImporter.import(@report) + assert Host.find_by(name: @host) end test 'importing report updates host status' do HostStatus::ConfigurationStatus.any_instance.stubs(:relevant?).returns(true) ForemanSalt::ReportImporter.import(@report) + assert Host.find_by(name: @host).get_status(HostStatus::ConfigurationStatus).error? end test 'importing report has correct status' do ForemanSalt::ReportImporter.import(@report) status = Host.find_by(name: @host).reports.last.status + assert_equal(9, status['applied']) assert_equal(3, status['failed']) end @@ -35,6 +38,7 @@ class ReportImporterTest < ActiveSupport::TestCase test 'report has salt origin and expected content' do ForemanSalt::ReportImporter.import(@report) report = Host.find_by(name: @host).reports.last + assert_equal 'Salt', report.origin assert_equal 'pkg_|-postfix_|-postfix_|-installed', report.logs.first.source.value assert_equal 'Package postfix is already installed.', report.logs.first.message.value @@ -44,6 +48,7 @@ class ReportImporterTest < ActiveSupport::TestCase ForemanSalt::ReportImporter.import(@report_pchanges) report = Host.find_by(name: @host).reports.last status = report.status + assert_equal 'Salt', report.origin assert_equal 'file_|-/etc/motd_|-/etc/motd_|-managed', report.logs.first.source.value assert_equal(1, status['pending']) @@ -51,8 +56,10 @@ class ReportImporterTest < ActiveSupport::TestCase test 'import returns Array of reports including host and its name' do reports = ForemanSalt::ReportImporter.import(@report) + assert_kind_of Array, reports first = reports.first + assert_equal 'Salt', first.origin assert_equal @host, first.host.name end @@ -60,6 +67,7 @@ class ReportImporterTest < ActiveSupport::TestCase test 'importing report with unhandled highstate' do HostStatus::ConfigurationStatus.any_instance.stubs(:relevant?).returns(true) ForemanSalt::ReportImporter.import(@report_unhandled) + assert Host.find_by(name: @host).get_status(HostStatus::ConfigurationStatus).error? end end diff --git a/test/unit/salt_modules_test.rb b/test/unit/salt_modules_test.rb index fe1ee30e..8efd4095 100644 --- a/test/unit/salt_modules_test.rb +++ b/test/unit/salt_modules_test.rb @@ -7,11 +7,13 @@ class SaltModulesTest < ActiveSupport::TestCase test 'salt module has a valid name' do salt_module = ForemanSalt::SaltModule.new(name: 'foo.bar.baz') + assert_valid salt_module end test 'salt module has invalid name' do salt_module = ForemanSalt::SaltModule.new(name: '&bad$name') + refute_valid salt_module, :name, /alphanumeric/ end end diff --git a/test/unit/salt_variables_test.rb b/test/unit/salt_variables_test.rb index 6337ef17..d6519083 100644 --- a/test/unit/salt_variables_test.rb +++ b/test/unit/salt_variables_test.rb @@ -8,6 +8,7 @@ class SaltVariablesTest < ActiveSupport::TestCase test 'salt variable has a salt module' do salt_variable = ForemanSalt::SaltVariable.new(key: 'awesome_key', salt_module_id: @state.id) + assert_valid salt_variable assert salt_variable.salt? assert_equal @state.id, salt_variable.salt_module.id @@ -15,8 +16,10 @@ class SaltVariablesTest < ActiveSupport::TestCase test 'salt variable is referencing a LookupValue' do salt_variable = ForemanSalt::SaltVariable.new(key: 'awesome_key', salt_module_id: @state.id) + assert salt_variable.lookup_values.count.zero? LookupValue.create(value: '[1.2.3.4,2.3.4.5]', match: 'domain = mydomain.net', lookup_key: salt_variable) + assert_equal(1, salt_variable.lookup_values.count) end @@ -27,6 +30,7 @@ class SaltVariablesTest < ActiveSupport::TestCase default_value: "{\r\n \"bat\": \"man\"\r\n}\r\n", override: true) salt_variable.save + assert salt_variable.default_value.is_a?(Hash) end end