<commit_before># frozen_string_literal: true

module Facter
  module Acceptance
    module BaseFactUtils
      # return a hash of expected facts for os, processors, and kernel facts
      #
      def os_processors_and_kernel_expected_facts(agent)
        if agent['platform'] =~ /aix-/
          aix_expected_facts(agent)
        elsif agent['platform'] =~ /debian-/
          debian_expected_facts(agent)
        elsif agent['platform'] =~ /el-|centos-/
          el_expected_facts(agent)
        elsif agent['platform'] =~ /fedora-/
          fedora_expected_facts(agent)
        elsif agent['platform'] =~ /osx-/
          osx_expected_facts(agent)
        elsif agent['platform'] =~ /sles-/
          sles_expected_facts(agent)
        elsif agent['platform'] =~ /solaris-/
          solaris_expected_facts(agent)
        elsif agent['platform'] =~ /ubuntu-/
          ubuntu_expected_facts(agent)
        elsif agent['platform'] =~ /windows-/
          windows_expected_facts(agent)
        else
          fail_test("unknown agent type being tested #{agent['platform']}")
        end
      end

      # return the value from the facter json parsed set of results using the fact path separated using '.'s
      #
      def json_result_fact_by_key_path(results, fact)
        fact.split('.').each do |key|
          results = results[key]
        end
        results
      end

      # AIX
      def aix_expected_facts(agent)
        version = agent['platform'].match(/aix-(\d)\.(\d)/)
        kernel_major_version = if version.nil?
                                 /\d\d00/
                               else
                                 /#{version[1]}#{version[2]}00/
                               end
        kernel_release = /^#{kernel_major_version}-\d+-\d+-\d+/
        os_arch        = /[Pp]ower[Pp][Cc]/
        os_hardware    = /IBM/

        expected_facts = {
          'os.architecture' => os_arch,
          'os.family' => 'AIX',
          'os.hardware' => os_hardware,
          'os.name' => 'AIX',
          'os.release.full' => kernel_release,
          'os.release.major' => kernel_major_version,
          'processors.count' => /[1-9]+/,
          'processors.isa' => os_arch,
          'processors.models' => os_arch,
          'kernel' => 'AIX',
          'kernelrelease' => kernel_release,
          'kernelversion' => kernel_major_version,
          'kernelmajversion' => kernel_major_version
        }
        expected_facts
      end

      # Debian
      def debian_expected_facts(agent)
        version = agent['platform'].match(/debian-(\d{1,2})/)
        os_version = if version.nil?
                       /\d+/
                     else
                       /#{version[1]}/
                     end
        if agent['platform'] =~ /amd64/
          os_arch     = 'amd64'
          os_hardware = 'x86_64'
        else
          os_arch     = 'i386'
          os_hardware = 'i686'
        end

        expected_facts = {
          'os.architecture' => os_arch,
          'os.distro.codename' => /\w+/,
          'os.distro.description' => %r{Debian GNU/Linux #{os_version}(\.\d)?},
          'os.distro.id' => 'Debian',
          'os.distro.release.full' => /#{os_version}\.\d+/,
          'os.distro.release.major' => os_version,
          'os.distro.release.minor' => /\d/,
          'os.family' => 'Debian',
          'os.hardware' => os_hardware,
          'os.name' => 'Debian',
          'os.release.full' => /#{os_version}\.\d+/,
          'os.release.major' => os_version,
          'os.release.minor' => /\d/,
          'processors.count' => /[1-9]/,
          'processors.physicalcount' => /[1-9]/,
          'processors.isa' => /unknown|#{os_hardware}/,
          'processors.models' => /(Intel\(R\).*)|(AMD.*)/,
          'kernel' => 'Linux',
          'kernelrelease' => /\d+\.\d+\.\d+/,
          'kernelversion' => /\d+\.\d+/,
          'kernelmajversion' => /\d+\.\d+/
        }
        expected_facts
      end

      # el (RedHat, Centos)
      def el_expected_facts(agent)
        version = agent['platform'].match(/(el|centos)-(\d)/)
        os_version = if version.nil?
                       /\d+/
                     else
                       /#{version[2]}/
                     end
        release_string = on(agent, 'cat /etc/*-release').stdout.downcase
        case release_string
        when /amazon/
          os_name = 'Amazon'
          # This parses: VERSION_ID="2017.09"
          os_version = on(agent, 'grep VERSION_ID /etc/os-release | cut --delimiter=\" --fields=2 | cut --delimiter=. --fields=1').stdout.chomp
          os_distro_description = /Amazon Linux( AMI)? release (\d )?(\()?#{os_version}(\))?/
          os_distro_id = /^Amazon(AMI)?$/
          os_distro_release_full = /#{os_version}(\.\d+)?/
        when /centos/
          os_name = 'CentOS'
          os_distro_description = /CentOS( Linux)? release #{os_version}\.\d+(\.\d+)? \(\w+\)/
          os_distro_id = 'CentOS'
          os_distro_release_full = /#{os_version}\.\d+/
        else
          os_name = 'RedHat'
          os_distro_description = /Red Hat Enterprise Linux( Server)? release #{os_version}\.\d+ \(\w+\)/
          os_distro_id = /^RedHatEnterprise(Server)?$/
          os_distro_release_full = /#{os_version}\.\d+/
        end
        if agent['platform'] =~ /x86_64/
          os_arch                 = 'x86_64'
          os_hardware             = 'x86_64'
          processor_model_pattern = /(Intel\(R\).*)|(AMD.*)/
        elsif agent['platform'] =~ /aarch64/
          os_arch                 = 'aarch64'
          os_hardware             = 'aarch64'
          processor_model_pattern = // # aarch64 does not populate a model value in /proc/cpuinfo
        elsif agent['platform'] =~ /ppc64le/
          os_arch                 = 'ppc64le'
          os_hardware             = 'ppc64le'
          processor_model_pattern = /(POWER.*)/
        else
          os_arch                 = 'i386'
          os_hardware             = 'i686'
          processor_model_pattern = /(Intel\(R\).*)|(AMD.*)/
        end<commit_msg>(maint) Update acceptance OS detection for Alma and Rocky<commit_after># frozen_string_literal: true

module Facter
  module Acceptance
    module BaseFactUtils
      # return a hash of expected facts for os, processors, and kernel facts
      #
      def os_processors_and_kernel_expected_facts(agent)
        if agent['platform'] =~ /aix-/
          aix_expected_facts(agent)
        elsif agent['platform'] =~ /debian-/
          debian_expected_facts(agent)
        elsif agent['platform'] =~ /el-|centos-/
          el_expected_facts(agent)
        elsif agent['platform'] =~ /fedora-/
          fedora_expected_facts(agent)
        elsif agent['platform'] =~ /osx-/
          osx_expected_facts(agent)
        elsif agent['platform'] =~ /sles-/
          sles_expected_facts(agent)
        elsif agent['platform'] =~ /solaris-/
          solaris_expected_facts(agent)
        elsif agent['platform'] =~ /ubuntu-/
          ubuntu_expected_facts(agent)
        elsif agent['platform'] =~ /windows-/
          windows_expected_facts(agent)
        else
          fail_test("unknown agent type being tested #{agent['platform']}")
        end
      end

      # return the value from the facter json parsed set of results using the fact path separated using '.'s
      #
      def json_result_fact_by_key_path(results, fact)
        fact.split('.').each do |key|
          results = results[key]
        end
        results
      end

      # AIX
      def aix_expected_facts(agent)
        version = agent['platform'].match(/aix-(\d)\.(\d)/)
        kernel_major_version = if version.nil?
                                 /\d\d00/
                               else
                                 /#{version[1]}#{version[2]}00/
                               end
        kernel_release = /^#{kernel_major_version}-\d+-\d+-\d+/
        os_arch        = /[Pp]ower[Pp][Cc]/
        os_hardware    = /IBM/

        expected_facts = {
          'os.architecture' => os_arch,
          'os.family' => 'AIX',
          'os.hardware' => os_hardware,
          'os.name' => 'AIX',
          'os.release.full' => kernel_release,
          'os.release.major' => kernel_major_version,
          'processors.count' => /[1-9]+/,
          'processors.isa' => os_arch,
          'processors.models' => os_arch,
          'kernel' => 'AIX',
          'kernelrelease' => kernel_release,
          'kernelversion' => kernel_major_version,
          'kernelmajversion' => kernel_major_version
        }
        expected_facts
      end

      # Debian
      def debian_expected_facts(agent)
        version = agent['platform'].match(/debian-(\d{1,2})/)
        os_version = if version.nil?
                       /\d+/
                     else
                       /#{version[1]}/
                     end
        if agent['platform'] =~ /amd64/
          os_arch     = 'amd64'
          os_hardware = 'x86_64'
        else
          os_arch     = 'i386'
          os_hardware = 'i686'
        end

        expected_facts = {
          'os.architecture' => os_arch,
          'os.distro.codename' => /\w+/,
          'os.distro.description' => %r{Debian GNU/Linux #{os_version}(\.\d)?},
          'os.distro.id' => 'Debian',
          'os.distro.release.full' => /#{os_version}\.\d+/,
          'os.distro.release.major' => os_version,
          'os.distro.release.minor' => /\d/,
          'os.family' => 'Debian',
          'os.hardware' => os_hardware,
          'os.name' => 'Debian',
          'os.release.full' => /#{os_version}\.\d+/,
          'os.release.major' => os_version,
          'os.release.minor' => /\d/,
          'processors.count' => /[1-9]/,
          'processors.physicalcount' => /[1-9]/,
          'processors.isa' => /unknown|#{os_hardware}/,
          'processors.models' => /(Intel\(R\).*)|(AMD.*)/,
          'kernel' => 'Linux',
          'kernelrelease' => /\d+\.\d+\.\d+/,
          'kernelversion' => /\d+\.\d+/,
          'kernelmajversion' => /\d+\.\d+/
        }
        expected_facts
      end

      # el (RedHat, Centos)
      def el_expected_facts(agent)
        version = agent['platform'].match(/(el|centos)-(\d)/)
        os_version = if version.nil?
                       /\d+/
                     else
                       /#{version[2]}/
                     end
        release_string = on(agent, 'cat /etc/*-release').stdout.downcase
        case release_string
        when /almalinux/
          os_name = 'AlmaLinux'
          os_distro_description = /AlmaLinux release #{os_version}\.\d+ \(.+\)/
          os_distro_id = 'AlmaLinux'
          os_distro_release_full = /#{os_version}\.\d+/
        when /amazon/
          os_name = 'Amazon'
          # This parses: VERSION_ID="2017.09"
          os_version = on(agent, 'grep VERSION_ID /etc/os-release | cut --delimiter=\" --fields=2 | cut --delimiter=. --fields=1').stdout.chomp
          os_distro_description = /Amazon Linux( AMI)? release (\d )?(\()?#{os_version}(\))?/
          os_distro_id = /^Amazon(AMI)?$/
          os_distro_release_full = /#{os_version}(\.\d+)?/
        when /rocky/
          os_name = 'Rocky'
          os_distro_description = /Rocky Linux release #{os_version}\.\d+ \(.+\)/
          os_distro_id = 'Rocky'
          os_distro_release_full = /#{os_version}\.\d+/
        when /centos/
          os_name = 'CentOS'
          os_distro_description = /CentOS( Linux)? release #{os_version}\.\d+(\.\d+)? \(\w+\)/
          os_distro_id = 'CentOS'
          os_distro_release_full = /#{os_version}\.\d+/
        else
          os_name = 'RedHat'
          os_distro_description = /Red Hat Enterprise Linux( Server)? release #{os_version}\.\d+ \(\w+\)/
          os_distro_id = /^RedHatEnterprise(Server)?$/
          os_distro_release_full = /#{os_version}\.\d+/
        end
        if agent['platform'] =~ /x86_64/
          os_arch                 = 'x86_64'
          os_hardware             = 'x86_64'
          processor_model_pattern = /(Intel\(R\).*)|(AMD.*)/
        elsif agent['platform'] =~ /aarch64/
          os_arch                 = 'aarch64'
          os_hardware             = 'aarch64'
          processor_model_pattern = // # aarch64 does not populate a model value in /proc/cpuinfo
        elsif agent['platform'] =~ /ppc64le/
          os_arch                 = 'ppc64le'
          os_hardware             = 'ppc64le'
          processor_model_pattern = /(POWER.*)/
        else
          os_arch                 = 'i386'
          os_hardware             = 'i686'
          processor_model_pattern = /(Intel\(R\).*)|(AMD.*)/
        end
{'fraction_non_alphanumeric': 0.14901341752170483, 'fraction_numerical': 0.011049723756906077, 'mean_word_length': 1.5943898443898443, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1882926', 'n_tokens_mistral': 4171, 'n_tokens_neox': 3959, 'n_words': 710}