#######################################################
#
# Test packagesmatching() and that the env prefix is stripped
#
#######################################################

body common control
{
      inputs => { "../../default.sub.cf" };
      bundlesequence  => { default("$(this.promise_filename)") };
      version => "1.0";
}

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

bundle agent init
{
  packages:
      "cfe-no-such-package"
      package_policy => "add",
      package_method => mock1;

      "cfe-no-such-package"
      package_policy => "add",
      package_method => mock2;

      # the parsing of env X="y z" fails

      # "cfe-no-such-package"
      # package_policy => "add",
      # package_method => mock3;

      "cfe-no-such-package"
      package_policy => "add",
      package_method => mock4;
}

body package_method mock1
{
      package_changes => "individual";

      package_list_command => "$(G.printf) 'bfoobar-1-besm6'";
      package_list_name_regex => "^([^-]+)";
      package_list_version_regex => "^[^-]+-([^-]+)";
      package_list_arch_regex => "^[^-]+-[^-]+-([\S]+)";

      package_installed_regex => ".*";

      package_add_command => "$(G.false)";
      package_update_command => "$(G.false)";
      package_delete_command => "$(G.false)";
      package_verify_command => "$(G.false)";
}

body package_method mock2
{
      package_changes => "individual";

      package_list_command => "/usr/bin/env $(G.printf) 'bfoobar-1-besm6'";
      package_list_name_regex => "^([^-]+)";
      package_list_version_regex => "^[^-]+-([^-]+)";
      package_list_arch_regex => "^[^-]+-[^-]+-([\S]+)";

      package_installed_regex => ".*";

      package_add_command => "$(G.false)";
      package_update_command => "$(G.false)";
      package_delete_command => "$(G.false)";
      package_verify_command => "$(G.false)";
}

body package_method mock3
{
      package_changes => "individual";

      package_list_command => '/usr/bin/env X="y z" $(G.printf) "bfoobar-1-besm6"';
      package_list_name_regex => "^([^-]+)";
      package_list_version_regex => "^[^-]+-([^-]+)";
      package_list_arch_regex => "^[^-]+-[^-]+-([\S]+)";

      package_installed_regex => ".*";

      package_add_command => "$(G.false)";
      package_update_command => "$(G.false)";
      package_delete_command => "$(G.false)";
      package_verify_command => "$(G.false)";
}

body package_method mock4
{
      package_changes => "individual";

      package_list_command => "/usr/bin/env X=Y $(G.printf) 'bfoobar-1-besm6'";
      package_list_name_regex => "^([^-]+)";
      package_list_version_regex => "^[^-]+-([^-]+)";
      package_list_arch_regex => "^[^-]+-[^-]+-([\S]+)";

      package_installed_regex => ".*";

      package_add_command => "$(G.false)";
      package_update_command => "$(G.false)";
      package_delete_command => "$(G.false)";
      package_verify_command => "$(G.false)";
}

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

bundle agent test
{
  meta:
      "test_suppress_fail" string => "windows",
        meta => { "redmine4741", "ENT-10422" };

  vars:
      "all_packages" data => packagesmatching(".*", ".*", ".*", ".*");
      "all_string" string => format("packages = %S", all_packages);
}

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

bundle agent check
{
  vars:
      "e" string => '{"arch":"besm6","method":"printf","name":"bfoobar","version":"1"}';
  methods:
      "" usebundle => dcs_check_strcmp($(test.all_string),
                                       'packages = [$(e)]',
                                       $(this.promise_filename),
                                       "no");
}
