######################################################
#
#  Test that one promise module can be used with two different promise types
#
#####################################################
body common control
{
    inputs => { "../default.cf.sub" };
    bundlesequence  => { default("$(this.promise_filename)") };
    version => "1.0";
}

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

bundle common version_check
{
@if minimum_version(3.17)
  classes:
    "cfengine_supports_custom_promises" expression => "any";
@endif
}

bundle agent init
{
  meta:
      "test_skip_unsupported" string => "!cfengine_supports_custom_promises";

  vars:
    "test_string"
      string => "hello, modules";

   files:
    "$(G.testfile).src"
      create => "true",
      edit_line => insert_lines("$(test_string)");

    "$(G.testfile).dst"
      delete => init_delete;

    "$(this.promise_dirname)/cfengine.sh"
      copy_from => local_cp("$(this.promise_dirname)/../../../modules/promises/cfengine.sh");

}

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

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

@if minimum_version(3.17)
promise agent cp
{
    interpreter => "/bin/bash";
    path => "$(this.promise_dirname)/cp.sh";
}

promise agent cpa
{
    interpreter => "/bin/bash";
    path => "$(this.promise_dirname)/cp.sh";
}

bundle agent test
{
  meta:
    "description" -> { "CFE-3575" }
      string => "Test a simple promise module which uses library";

  cp:
    "$(G.testfile).dst"
      from => "$(G.testfile).src";

  cpa:
    "$(G.testfile)_cpa.dst"
      from => "$(G.testfile).src";
}
@endif

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

bundle agent check
{
  vars:
      "src_mtime" string => filestat("$(G.testfile).src", "mtime");
      "dst_mtime" string => filestat("$(G.testfile)_cpa.dst", "mtime");

  classes:
      "cp_ok"
        expression => strcmp("$(init.test_string)", readfile("$(G.testfile).dst")),
        if => fileexists("$(G.testfile).dst");

      "cpa_content_ok"
        expression => strcmp("$(init.test_string)", readfile("$(G.testfile)_cpa.dst")),
        if => fileexists("$(G.testfile)_cpa.dst");

      "cpa_mtime_ok"
        expression => strcmp("$(src_mtime)", "$(dst_mtime)"),
        if => fileexists("$(G.testfile)_cpa.dst");

      "ok" and => { "cp_ok", "cpa_content_ok", "cpa_mtime_ok" };

  reports:
    DEBUG.!cpa_mtime_ok::
      "mtimes: $(src_mtime) | $(dst_mtime)";
    ok::
      "$(this.promise_filename) Pass";
    !ok::
      "$(this.promise_filename) FAIL";
}
