#######################################################
#
# Test that updating to a greater-than-installed version of the named
# package does not produce a "kept" outcome
#
#######################################################
body common control
{
  inputs => {
    "../../../default.sub.cf",
    "../../../../../lib/3.6/packages.cf",
    "../../packages-info.cf.sub",
  };
  bundlesequence => { default("$(this.promise_filename)") };
  version => "1.0";
  cache_system_functions => "no";
}

#######################################################
bundle agent init
{
  methods:
    "install_package"
      usebundle => install_package(
        "$(p.name[3])", "$(p.version[1])", "$(p.arch)", "dummy"
      );
}

#######################################################
bundle agent test
{
  vars:
    "package_name" string => "$(p.name[3])";
    "package_version" string => "$(p.version[1])";

  packages:
    debian::
      "$(package_name)"
        package_policy => "addupdate",
        package_select => ">",
        package_version => "$(package_version)",
        package_method => apt_get,
        classes => test_set_context_class("fail", "pass", "pass");

    redhat::
      "$(package_name)"
        package_policy => "addupdate",
        package_select => ">",
        package_version => "$(package_version)",
        package_method => yum_rpm,
        classes => test_set_context_class("fail", "pass", "pass");
}

body classes test_set_class(ok_class, notok_class)
{
  promise_kept => { "$(ok_class)" };
  promise_repaired => { "$(ok_class)" };
  repair_failed => { "$(notok_class)" };
}

body classes test_set_context_class(kept_class, repaired_class, failed_class)
{
  promise_kept => { "$(kept_class)" };
  promise_repaired => { "$(repaired_class)" };
  repair_failed => { "$(failed_class)" };
}

#######################################################
bundle agent check
{
  classes:
    redhat::
      "has_pkg"
        expression => returnszero(
          "/bin/rpm -q $(test.package_name) > /dev/null", "useshell"
        );

    debian::
      "has_pkg"
        expression => returnszero(
          "dpkg -l | grep ' $(test.package_name) ' > /dev/null", "useshell"
        );

    any::
      "has_file" expression => fileexists("$(p.file[3][$(p.latest_version)])");
      "ok" expression => "pass.!fail.has_pkg.has_file";

  reports:
    ok::
      "$(this.promise_filename) Pass";

    !ok::
      "$(this.promise_filename) FAIL";
}
