######################################################
#
#  Basic test of promise module using the Python library
#
#####################################################
body common control
{
  inputs => { "../default.sub.cf" };
  bundlesequence => { default("$(this.promise_filename)") };
  version => "1.0";
}

#######################################################
bundle common python_check
{
  classes:
    "python_version_compatible_with_cfengine_library"
      expression => returnszero(
        "/usr/bin/python3 -c 'import sys; assert sys.version_info >= (3,6)'",
        "useshell"
      );
@if minimum_version(3.17)
    "cfengine_supports_custom_promises" expression => "any";
@endif
}

bundle agent init
{
  meta:
    "test_skip_unsupported"
      string => "!python_version_compatible_with_cfengine_library|!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.py"
      copy_from => local_cp(
        "$(this.promise_dirname)/../../../modules/promises/cfengine.py"
      );
}

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

#######################################################
@if minimum_version(3.17)
promise agent copy
{
  interpreter => "/usr/bin/python3";
  path => "$(this.promise_dirname)/copy_promises.py";
}

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

  copy:
    "$(G.testfile).dst" from => "$(G.testfile).src";
}
@endif
#######################################################
bundle agent check
{
  classes:
    "ok"
      expression => strcmp(
        "$(init.test_string)", readfile("$(G.testfile).dst")
      ),
      if => fileexists("$(G.testfile).dst");

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

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