######################################################
#
#  Basic test of promise module using the shell library and producing classes
#
#####################################################
body common control
{
    inputs => { "../default.sub.cf" };
    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-with-classes.sh";
}

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

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

  classes:
      "promise_repaired"
        expression => canonify("cp_repaired"),
        scope => "namespace";
}
@endif

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

bundle agent check
{
  classes:
      "file_ok"
        expression => strcmp("$(init.test_string)", readfile("$(G.testfile).dst")),
        if => fileexists("$(G.testfile).dst");

      "ok" expression => "file_ok.promise_repaired";

  reports:
    DEBUG.file_ok::
      "file_ok";
    DEBUG.promise_repaired::
      "promise_repaired";

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