######################################################
#
#  Basic test of promise module using the Python library and an slist attribute
#
#####################################################
body common control
{
        inputs => { "../default.cf.sub" };
        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";

  files:
      "$(this.promise_dirname)/cfengine.py"
      copy_from => local_cp("$(this.promise_dirname)/../../../modules/promises/cfengine.py");
}

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

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

bundle agent test
{
  meta:
      "description" -> { "CFE-3444" }
        string => "Test a simple promise module which uses the Python library and an slist attribute";

  vars:
      "lines" slist => { "hello", "from", "modules" };

  multiline_insert:
      "$(G.testfile)"
        lines => @(lines);
}
@endif

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

bundle agent check
{
  classes:
      "ok"
        expression => strcmp(join("$(const.n)", "test.lines"), readfile("$(G.testfile)")),
        if => fileexists("$(G.testfile)");

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