######################################################
#
#  Basic test of promise module using the Python library and a data attribute
#
#####################################################
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";

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

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

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

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

  vars:
      "sample_data" data => parsejson('
{
  "key2": ["value3", "value4"]
}
');

  json_insert:
      "$(G.testfile)"
        json_data => parsejson('
{
  "key": ["value1", "value2"]
}
');
      "$(G.testfile)2"
        json_data => @(sample_data);
}
@endif

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

bundle agent check
{
  classes:
      "ok1"
        expression => strcmp('{"key": ["value1", "value2"]}', readfile("$(G.testfile)")),
        if => fileexists("$(G.testfile)");

      "ok2"
        expression => strcmp('{"key2": ["value3", "value4"]}', readfile("$(G.testfile)2")),
        if => fileexists("$(G.testfile)2");

      "ok" and => {"ok1", "ok2"};

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