#######################################################
#
# Test this.promiser on files promises 
#
#######################################################
body common control
{
  inputs => { "../default.sub.cf" };
  bundlesequence => { default("$(this.promise_filename)") };
  version => "1.0";
}

body classes promiser0_generic
{
  promise_repaired => { "$(this.promiser)_repaired", "$(this.promiser)_0_ok" };
  promise_kept => { "$(this.promiser)_kept", "$(this.promiser)_0_ok" };
}

body classes promiser1_generic
{
  promise_repaired => { "$(this.promiser)_repaired", "$(this.promiser)_1_ok" };
  promise_kept => { "$(this.promiser)_kept", "$(this.promiser)_1_ok" };
}

body classes promiser2_generic
{
  promise_repaired => { "$(this.promiser)_repaired", "$(this.promiser)_2_ok" };
  promise_kept => { "$(this.promiser)_kept", "$(this.promiser)_2_ok" };
}

body classes promiser3_generic
{
  promise_repaired => { "$(this.promiser)_repaired", "$(this.promiser)_3_ok" };
  promise_kept => { "$(this.promiser)_kept", "$(this.promiser)_3_ok" };
}

body classes promiser4_generic
{
  promise_repaired => { "$(this.promiser)_repaired", "$(this.promiser)_4_ok" };
  promise_kept => { "$(this.promiser)_kept", "$(this.promiser)_4_ok" };
}

bundle agent init
{
  vars:
    "files" slist => { "aa", "ab", "ac", "ba", "bb", "bc" };

  files:
    "$(G.testdir)/copy_me_source" create => "true";
}

#######################################################
bundle agent test
{
  meta:
    "test_soft_fail"
      string => "windows",
      meta => { "ENT-10257" };

  commands:
    "$(G.true)" classes => promiser0_generic;

  files:
    # 6 promisers
    "$(G.testdir)/$(init.files)"
      create => "true",
      classes => promiser1_generic;

    # 3 promisers
    "$(G.testdir)/a.*"
      perms => m("666"),
      classes => promiser2_generic;

    # 2 promisers - 1 file inside the directory and the directory itself (as the
    # real promiser)
    "$(G.testdir)"
      file_select => by_exec_cmd,
      perms => m("666"),
      classes => promiser2_generic,
      depth_search => test_recurse;

    # 6 promisers - 6 files inside the directory and the directory itself (as the
    # real promiser)
    "$(G.testdir)"
      file_select => test_plain,
      depth_search => test_recurse,
      delete => tidyfiles,
      classes => promiser3_generic;

    "$(G.testdir)/copy_me" copy_from => local_cp("$(this.promiser)_source");

  methods:
    "template_test" usebundle => template_test;
}

bundle agent template_test
{
  vars:
    "testdir" string => "$(G.testdir)/template_test";
    "cfe_files" slist => { "cfe_file1", "cfe_file2" };
    "mustache_files" slist => { "mustache_file1", "mustache_file2" };

  files:
    # create empty files so that we can test pattern and file_select cases
    "$(testdir)/." create => "true";
    "$(testdir)/$(cfe_files)" create => "true";
    "$(testdir)/$(mustache_files)" create => "true";

    "$(testdir)/$(cfe_files).cf-template"
      create => "true",
      edit_line => insert_lines("Just a cfengine line for $(this.promiser).");

    "$(testdir)/$(mustache_files).mustache"
      create => "true",
      edit_line => insert_lines("Just a mustache line for $(this.promiser).");

    "$(testdir)/cfe_file[1,2]"
      perms => m("666"),
      template_method => "cfengine",
      edit_template => "$(this.promiser).cf-template",
      classes => promiser4_generic;

    "$(testdir)/mustache_.*"
      file_select => mustache_files,
      perms => m("666"),
      template_method => "mustache",
      edit_template => "$(this.promiser).mustache",
      classes => promiser4_generic;
}

body file_select mustache_files
{
  leaf_name => { "@(template_test.mustache_files)" };
  file_result => "leaf_name";
}

body delete tidyfiles
{
  dirlinks => "delete";
  rmdirs => "true";
}

body file_select test_plain
{
  leaf_name => { "[a,b][a,b,c]" };
  file_types => { "plain" };
  file_result => "file_types.leaf_name";
}

body file_select by_exec_cmd
# Redmine #3530
{
  leaf_name => { "ba" };
  exec_program => "$(G.ls) $(this.promiser)";
  file_result => "leaf_name.exec_program";
}

body depth_search test_recurse
{
  depth => "inf";
}

#######################################################
bundle agent check
{
  vars:
    "ok_promisers" slist => classesmatching(".*_ok");
    "ok_0_count" int => countclassesmatching(".*_0_ok");
    "ok_1_count" int => countclassesmatching(".*_1_ok");
    "ok_2_count" int => countclassesmatching(".*_2_ok");
    "ok_3_count" int => countclassesmatching(".*_3_ok");
    "ok_4_count" int => countclassesmatching(".*_4_ok");

    "ok_pattern_promiser_count"
      int => countclassesmatching(".*__[01234]_ok"),
      comment => "the patterns should be cannonified and classes for them should be defined";

  classes:
    "ok_expand"
      expression => none("__this_promiser_[0,1,2,3]_ok", ok_promisers);

    "ok0" expression => strcmp("$(ok_0_count)", 1);
    "ok1" expression => strcmp("$(ok_1_count)", 6);
    "ok2" expression => strcmp("$(ok_2_count)", 6);
    "ok3" expression => strcmp("$(ok_3_count)", 7);
    "ok4" expression => strcmp("$(ok_4_count)", 6);
    "ok5" expression => fileexists("$(G.testdir)/copy_me");
    "ok_pattern" expression => strcmp("$(ok_pattern_promiser_count)", 3);

    "ok"
      and => {
        "ok_expand", "ok0", "ok1", "ok2", "ok3", "ok4", "ok5", "ok_pattern"
      };

  reports:
    DEBUG::
      "$(ok_promisers)";
      "0: $(ok_0_count) (expected 1)";
      "1: $(ok_1_count) (expected 6)";
      "2: $(ok_2_count) (expected 6)";
      "3: $(ok_3_count) (expected 7)";
      "4: $(ok_4_count) (expected 6)";
      "pattern promiser classes: $(ok_pattern_promiser_count) (expected 3)";

    DEBUG.!ok5::
      "5: file not copied";

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

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

### PROJECT_ID: core
### CATEGORY_ID: 27
