Sha256: f0ea679877d16dae67001655f48493428b06b3db4e74b8c53435ff4c56171ff3

Contents?: true

Size: 1.91 KB

Versions: 29

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require 'test_plugin_helper'

module ForemanAnsible
  # Test for the facts importer - only verify that given
  # a set of facts it's able to import them
  class FactImporterTest < ActiveSupport::TestCase
    setup do
      @host = FactoryBot.build(:host)
    end

    test 'add new facts adds all fact names in the fixture' do
      @fact_importer = FactImporter.new(@host, facts_json)
      facts_to_be_added = FactSparser.sparse(facts_json[:ansible_facts]).keys +
                          FactSparser.unsparse(facts_json[:ansible_facts]).keys
      @fact_importer.send(:add_new_facts)
      assert((facts_to_be_added - FactName.all.map(&:name)).empty?)
    end

    test 'missing_facts returns facts we do not have in the database' do
      @fact_importer = FactImporter.new(@host, facts_json)
      @fact_importer.expects(:host_fact_names).
        returns(['ansible_cmdline'])
      refute @fact_importer.send(:missing_facts).include?('ansible_cmdline')
    end

    describe '#add_fact_value' do
      setup do
        @fact_importer = FactImporter.new(@host, :ansible_facts => {})
      end
      test 'does not add existing facts' do
        existing_fact = FactoryBot.build(:fact_name)
        @fact_importer.expects(:missing_facts).returns([])
        assert_nil @fact_importer.send(:add_fact_value, nil, existing_fact)
      end

      test 'adds missing facts' do
        missing_fact = FactoryBot.build(:fact_name)
        @fact_importer.expects(:missing_facts).returns([missing_fact.name])
        @fact_importer.counters[:added] = 0
        assert_difference('@host.fact_values.count', 1) do
          @fact_importer.send(:add_fact_value, 'missing_value', missing_fact)
          @host.save
          # We have to save the host in order to ensure @host.fact_values.count
          # resolves properly (otherwise) :add_fact_value just won't save the
          # relation
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
foreman_ansible-6.3.4 test/unit/services/fact_importer_test.rb
foreman_ansible-6.3.3 test/unit/services/fact_importer_test.rb
foreman_ansible-6.3.2 test/unit/services/fact_importer_test.rb
foreman_ansible-6.3.1 test/unit/services/fact_importer_test.rb
foreman_ansible-6.4.1 test/unit/services/fact_importer_test.rb
foreman_ansible-6.4.0 test/unit/services/fact_importer_test.rb
foreman_ansible-6.3.0 test/unit/services/fact_importer_test.rb
foreman_ansible-6.2.0 test/unit/services/fact_importer_test.rb
foreman_ansible-6.1.1 test/unit/services/fact_importer_test.rb
foreman_ansible-6.0.2 test/unit/services/fact_importer_test.rb
foreman_ansible-6.1.0 test/unit/services/fact_importer_test.rb
foreman_ansible-6.0.1 test/unit/services/fact_importer_test.rb
foreman_ansible-5.1.3 test/unit/services/fact_importer_test.rb
foreman_ansible-6.0.0 test/unit/services/fact_importer_test.rb
foreman_ansible-5.1.2 test/unit/services/fact_importer_test.rb
foreman_ansible-5.1.1 test/unit/services/fact_importer_test.rb
foreman_ansible-5.1.0 test/unit/services/fact_importer_test.rb
foreman_ansible-4.0.3.5 test/unit/services/fact_importer_test.rb
foreman_ansible-4.0.3.4 test/unit/services/fact_importer_test.rb
foreman_ansible-4.0.3.3 test/unit/services/fact_importer_test.rb