bundle common test_meta
{
  vars:
      "description" string => "Test that multiple packages which are installed get removed";
      "story_id" string => "5514";
      "covers" string => "operational_repaired";
}

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

body common control
{
    inputs => {
                "../../../dcs.cf.sub",
                "../../../../../$(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])","$(p.name[1])","$(p.name[2])"};

    packages:

        debian::

            "$(package_name)"
            package_policy => "add",
            package_architectures => { "$(p.arch)" },
            package_method => apt_get;

        redhat|suse|sles::

            "$(package_name)"
            package_policy => "add",
            package_architectures => { "$(p.arch)" },
            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_$(package_name)","fail_$(package_name)");

        redhat|suse|sles::

            "$(package_name)"
            package_policy => "delete",
            classes => test_set_class("pass_$(package_name)","fail_$(package_name)");

}



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_$(test.package_name)" not => returnszero("dpkg -l | grep ' $(test.package_name) ' > /dev/null", "useshell");

        redhat|suse|sles::

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

        any::
            "not_has_file_test_package_charlie" not => fileexists("$(p.file[3][$(p.latest_version)])");
            "not_has_file_test_package_alpha" not => fileexists("$(p.file[1][$(p.latest_version)])");
            "not_has_file_test_package_beta" not => fileexists("$(p.file[2][$(p.latest_version)])");
            # Would be nice to test the pass_* and fail_* classes here, but CFEngine
            # doesn't set the classes properly when installing multiple new packages.
            "charlie_ok" and => { #"pass_test_package_charlie", "!fail_test_package_charlie",
                               "not_has_pkg_test_package_charlie", "not_has_file_test_package_charlie" };
            "alpha_ok" and => { #"pass_test_package_alpha", "!fail_test_package_alpha",
                                "not_has_pkg_test_package_alpha", "not_has_file_test_package_alpha" };
            "beta_ok" and => { #"pass_test_package_beta", "!fail_test_package_beta",
                               "not_has_pkg_test_package_beta", "not_has_file_test_package_beta" };

            "ok" and => { "charlie_ok", "alpha_ok", "beta_ok" };

    reports:

        ok::

            "$(this.promise_filename) Pass";

        !ok::

            "$(this.promise_filename) FAIL";
}

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

