##############################################################
#
# Test reworked in CFE-3644 to be more dynamic
#
##############################################################

body common control
{
  bundlesequence => { "test", "check" };
}

bundle agent test
{
  meta:
    any::
      "decription" -> { "CFE-3569" }
        string => "Test for expected values of variable sys.os_version_major";

  vars:
    # Platforms to test
    any::
      "platforms"
        slist => { "debian", "ubuntu", "redhat", "rhel", "centos", "fedora",
                   "aix", "hpux", "suse", "opensuse", "opensuse_leap", "sles",
                   "solaris", "sunos", "windows", "freebsd", "macos" };


    # Regex matching current platforms OS-class with version numbers
    !solaris&!sunos::
      "class_regex"
        string => format("^(%s)_[0-9]+$", join("|", "platforms"));
    solaris|sunos::
      "class_regex"
        string => format("^(%s)_[0-9]+_[0-9]+$", join("|", "platforms"));

    # Regex to extract major version number from OS-class
    # Edge cases:
    #  - On Solaris/SunOS major version comes second
    #    E.g. Solaris 11 has class "solaris_5_11"
    any::
      "extract_regex"
        string => ifelse("solaris|sunos", "^[a-z]+_[0-9]+_([0-9]+$)",
                         "opensuse_leap", "^[a-z_]+_([0-9]+)$",
                                          "^[a-z]+_([0-9]+)$");

    # Find OS-class with version numbers using regex
    any::
      "os_class"
        string => nth(classesmatching("$(class_regex)"), "0");

    # Get extracted major version number
    any::
      "expected"
        string => nth("version_number", "1");

  classes:
    any::
      "regextract_success"
        expression => regextract("$(extract_regex)", "$(os_class)", "version_number");
}

bundle agent check
{
  vars:
    any::
      "defined_classes"
        slist => classesmatching(".*");

  classes:
    any::
      "passed"
        expression => strcmp("$(test.expected)", "$(sys.os_version_major)");

  reports:
    DEBUG::
      "Version number extracted from class: $(test.os_class)";
      "Defined classes: $(defined_classes)";
      "$(this.promise_filename) Expected: $(test.expected)";
      "$(this.promise_filename) Found: $(sys.os_version_major)";
    passed::
      "$(this.promise_filename) Pass";
    !passed::
      "$(this.promise_filename) FAIL";
}
