######################################################
#
#  Test custom bodies with custom promise types
#
#####################################################
body common control
{
  inputs => { "../default.sub.cf" };
  bundlesequence => { default("$(this.promise_filename)") };
  version => "1.0";
}

#######################################################
bundle common version_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.20.0)
    "custom_promises_supports_bodies" expression => "any";
@endif
}

bundle agent init
{
  meta:
    "test_skip_unsupported"
      string => "!custom_promises_supports_bodies|!python_version_compatible_with_cfengine_library";

  files:
    "$(G.testfile)" 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.20.0)
promise agent bodies
{
  interpreter => "/usr/bin/python3";
  path => "$(this.promise_dirname)/bodies.py";
}

body example_body parent(a, b)
{
  alice => "$(a)";
  bob => "$(b)";
}

body example_body child
{
  bob => "3";
  charlie => "4";
  inherit_from => parent(1, 2);
}

bundle agent test
{
  meta:
    "description" -> { "CFE-3574" }
      string => "Test custom bodies with custom promise types";

  bodies:
    "$(G.testfile)" example_body => child;
}
@endif
#######################################################
bundle agent check
{
  vars:
    "expected"
      string => '{"example_body": {"alice": "1", "bob": "3", "charlie": "4"}}';

    "found" string => readfile("$(G.testfile)");

  classes:
    "ok" expression => strcmp("$(expected)", "$(found)");

  reports:
    DEBUG::
      "Expected: '$(expected)'";
      "Found: '$(found)'";

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

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