#######################################################
#
# Test deletion a named versioned package
#
#######################################################
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
{
  vars:
    "package_name" string => "$(p.name[3])";

    debian::
      "package_arch" string => "$(debian_knowledge.default_arch)";
      "package_version" string => "$(p.version[1])";

    redhat::
      "package_arch" string => "x86_64";
      "package_version" string => "$(p.version[1])";

  packages:
    debian::
      "$(package_name)"
        package_policy => "addupdate",
        package_select => "==",
        package_version => "$(package_version)",
        package_architectures => { "$(package_arch)" },
        package_method => dpkg_version("$(p.resources)/file_repo");

    redhat::
      "$(package_name)"
        package_policy => "addupdate",
        package_select => "==",
        package_version => "$(package_version)",
        package_architectures => { "$(package_arch)" },
        package_method => rpm_version("$(p.resources)/rpm_repo");
}

#######################################################
bundle agent test
{
  vars:
    "package_name" string => "$(init.package_name)";
    "package_arch" string => "$(init.package_arch)";
    "package_version" string => "$(init.package_version)";

  packages:
    debian::
      "$(package_name)"
        package_policy => "delete",
        package_select => "==",
        package_version => "$(package_version)",
        package_architectures => { "$(package_arch)" },
        package_method => apt_get,
        classes => test_set_class("pass", "fail");

    redhat::
      "$(package_name)"
        package_policy => "delete",
        package_select => "==",
        package_version => "$(package_version)",
        package_architectures => { "$(package_arch)" },
        package_method => yum_rpm,
        classes => test_set_class("pass", "fail");
}

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

#######################################################
bundle agent check
{
  classes:
    debian::
      "not_has_pkg"
        not => returnszero(
          "dpkg -l | grep ' $(test.package_name) ' > /dev/null", "useshell"
        );

    redhat::
      "not_has_pkg"
        not => returnszero(
          "/bin/rpm -q $(test.package_name) > /dev/null", "useshell"
        );

    any::
      "not_has_file" not => fileexists("$(p.file[3][1])");
      "ok" expression => "pass.!fail.not_has_pkg.not_has_file";

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

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

body classes succesfully_executed(class)
{
  kept_returncodes => { "0" };
  promise_kept => { "$(class)" };
}
