bundle common test_meta
{
  vars:
      "description" string => "Test that a package which is installed gets removed";
      "story_id" string => "5514";
      "covers" string => "operational_repaired";
}

#######################################################

body common control
{
    inputs => {
                "../../../dcs.sub.cf",
                "../../../../../$(sys.local_libdir)/stdlib.cf",
                "../../../../../$(sys.local_libdir)/packages.cf",
                "../../packages-info.cf.sub",
              };
    bundlesequence  => { default("$(this.promise_filename)") };
    version => "1.0";
    cache_system_functions => "no";
}



#######################################################

bundle agent init
{
  meta:
      "test_skip_needs_work" string => "!redhat.!debian",
        meta => { "redmine5866" };
      # RedHat 4 RPM has a bug which corrupts the RPM DB during our tests, so it is untestable.
      "test_skip_unsupported" string => "redhat_4|centos_4";

    vars:
        "package_name" slist => { "$(p.name[3])" };

    packages:


        debian::

           "$(package_name)"
            package_policy => "add",
            package_method => apt_get;

        redhat|suse|sles::

            "$(package_name)"
            package_policy => "add",
            package_method => yum_rpm;
}

#######################################################

bundle agent test
{
    vars:
        "package_name" slist => { "$(init.package_name)" };

    packages:

        debian::
            "$(package_name)"
            package_policy => "delete",
            classes => test_set_class("pass","fail");

    redhat|suse|sles::
        "$(package_name)"
        package_policy => "delete",
        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|suse|sles::
            "not_has_pkg" not => returnszero("/bin/rpm -q $(test.package_name) > /dev/null", "useshell");

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

    reports:
        "$(p.file[3][$(p.latest_version)])";
        ok::
            "$(this.promise_filename) Pass";
        !ok::
            "$(this.promise_filename) FAIL";
}

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