########################################################################
#
# Not really a test, but a preparation policy for running other package
# tests. It sets up the required files for accessing our local package
# repository.
# It MUST run before the other tests in the unsafe packages directory.
#
########################################################################
body common control
{
  inputs => {
    "../../../../dcs.sub.cf",
    "../../../../../../$(sys.local_libdir)/files.cf",
    "../../../../../../$(sys.local_libdir)/commands.cf",
    "../../../packages-info.cf.sub",
    "../../../meta_skip.cf.sub",
  };
  bundlesequence => { default("$(this.promise_filename)") };
}

bundle agent test
{
  vars:
    "bundles"
      slist => {
        "repositories",
        "signing_keys",
        "apt_config",
        "dpkg_multiarch",
        "update",
      };

  methods:
    "any" usebundle => "$(bundles)";
}

body contain useshell
{
  useshell => "useshell";
}

bundle agent repositories
{
  vars:
    suse|sles::
      "repo_file" string => "/etc/zypp/repos.d/test-repository.repo";
      "repos" slist => { "rpm_repo" };

    redhat::
      "repo_file" string => "/etc/yum.repos.d/test-repository.repo";
      "repos" slist => { "rpm_repo" };

    debian::
      "repo_file" string => "/etc/apt/sources.list.d/testdebian.list";
      "repos" slist => { "deb_repo1", "deb_repo2" };

  files:
    "/test-repos/$(repos)"
      copy_from => local_cp("$(p.resources)/$(repos)"),
      depth_search => recurse("inf");

    "$(repo_file)"
      create => "true",
      edit_line => repository,
      edit_defaults => edit_defaults,
      classes => if_successful("repo_ok");

    # Remove original sources so that we don't hammer the distro servers.
    debian::
      "/etc/apt/sources.list" delete => tidy;

      "/etc/apt/sources.list.d"
        delete => tidy,
        depth_search => recurse("inf"),
        file_select => not_test_source;

    redhat::
      "/etc/yum.repos.d"
        delete => tidy,
        depth_search => recurse("inf"),
        file_select => not_test_source;

    suse|sles::
      "/etc/zypp/repos.d"
        delete => tidy,
        depth_search => recurse("inf"),
        file_select => not_test_source;
}

body file_select not_test_source
{
  file_result => "!path_name";
  path_name => { "$(repositories.repo_file)" };
}

bundle edit_line repository
{
  insert_lines:
    redhat|suse|sles::
      "[test-repository]";
      "name=Test repository";
      "baseurl=file:///test-repos/rpm_repo";
      "enabled=1";
      "gpgcheck=0";

    debian::
      "deb file:///test-repos/deb_repo1 package1 main";
      "deb file:///test-repos/deb_repo2 package2 main";
}

body edit_defaults edit_defaults
{
  edit_backup => "false";
  empty_file_before_editing => "true";
}

bundle agent signing_keys
{
  classes:
    !debian::
      "signing_keys_ok"
        expression => "any",
        scope => "namespace";

  vars:
    debian::
      "gpg" string => "gpg --homedir .gnupg-temp --batch --no-tty";

  commands:
    debian::
      # re-sign repo
      "mkdir -m 700 .gnupg-temp && \
       $(gpg) --import $(p.resources)/gpg/secring.gpg && \
       rm /test-repos/deb_repo1/dists/package1/Release.gpg /test-repos/deb_repo2/dists/package2/Release.gpg && \
       $(gpg) --detach-sign --armor --output /test-repos/deb_repo1/dists/package1/Release.gpg /test-repos/deb_repo1/dists/package1/Release && \
       $(gpg) --detach-sign --armor --output /test-repos/deb_repo2/dists/package2/Release.gpg /test-repos/deb_repo2/dists/package2/Release && \
       $(gpg) --export --output /etc/apt/trusted.gpg.d/cfengine-test-repo.gpg && \
       chmod 0644 /etc/apt/trusted.gpg.d/cfengine-test-repo.gpg && \
       rm -rf .gnupg-temp"
        contain => useshell,
        classes => if_successful("signing_keys_ok");
}

bundle agent apt_config
{
  classes:
    !(ubuntu_10|debian_6|ubuntu_24|ubuntu_26)::
      "apt_config_ok"
        expression => "any",
        scope => "namespace";

  files:
    ubuntu_24|ubuntu_26::
      "/etc/apt/apt.conf.d/accept-older-pubkeys"
        comment => "key in 17_packages/resources/gpg use rsa1024 which is not supported on Ubuntu-24 and later.",
        create => "true",
        content => 'APT::Key::Assert-Pubkey-Algo ">=rsa1024";',
        classes => if_successful("apt_config_ok");

    ubuntu_10|debian_6::
      # Work around bug: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=715494
      # Apt cache does not behave correctly if installing more than one package
      # per second.
      "/etc/apt/apt.conf.d/nocache"
        create => "true",
        content => 'Dir::Cache::pkgcache "";',
        classes => if_successful("apt_config_ok");
}

bundle agent dpkg_multiarch
{
  vars:
    debian.64_bit::
      "foreign_archs"
        string => execresult(
          "/usr/bin/dpkg --print-foreign-architectures", "noshell"
        );

  classes:
    debian.64_bit::
      "no_multiarch" not => strcmp($(foreign_archs), "i386");

  commands:
    debian.64_bit.no_multiarch::
      "/usr/bin/dpkg --add-architecture i386";
}

bundle agent update
{
  commands:
    suse|sles::
      "zypper ref"
        contain => useshell,
        classes => if_successful("update_ok");

    redhat::
      "yum makecache"
        contain => useshell,
        classes => if_successful("update_ok");

    debian::
      "apt-get update"
        contain => useshell,
        classes => if_successful("update_ok");
}

body classes if_successful(x)
{
  promise_repaired => { "$(x)" };
  promise_kept => { "$(x)" };
}

bundle agent check
{
  classes:
    "everything_ok"
      and => { "repo_ok", "signing_keys_ok", "apt_config_ok", "update_ok" };

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

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