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

#######################################################
bundle agent init
{
  vars:
    "script_location" string => dirname("$(this.promise_filename)");

  classes:
    "ok_to_run" not => regcmp("/var/cfengine|.*\.cf-agent", "$(sys.workdir)");

  files:
    ok_to_run::
      "$(sys.workdir)/modules/usemodule.cf.bat"
        create => "true",
        perms => usemodule_perms,
        edit_line => usemodule_lines;

      "$(sys.workdir)/modules/usemodule-neg.cf.bat"
        create => "true",
        perms => usemodule_perms,
        edit_line => usemodule_lines_neg;
}

bundle edit_line usemodule_lines_neg
{
  insert_lines:
    !windows::
      "#!/bin/sh";
      ". \"$(init.script_location)/usemodule.cf.bat\"";
      "exit 1";

  delete_lines:
    ".*";
}

bundle edit_line usemodule_lines
{
  insert_lines:
    windows::
      "@echo off";
      "\"$(init.script_location)\usemodule.cf.bat\"";

    !windows::
      "#!/bin/sh";
      ". \"$(init.script_location)/usemodule.cf.bat\"";

  delete_lines:
    ".*";
}

body perms usemodule_perms
{
  mode => "755";
}

#######################################################
bundle agent test
{
  classes:
    "ok_to_run" not => regcmp("/var/cfengine|.*\.cf-agent", "$(sys.workdir)");

  classes:
    ok_to_run::
      # Should be undefined by the following script.
      "undefined_class" expression => "any";
      "function_ok" expression => usemodule("usemodule.cf.bat", "");

      "function_neg_ok"
        comment => "Negative test. Class set if module returns non-zero",
        not => usemodule("usemodule-neg.cf.bat", "");

      "defined_variable"
        expression => strcmp("$(usemodule_cf_bat.defined_variable)", "yes");

      "ok"
        and => {
          "function_neg_ok",
          "function_ok",
          "defined_class",
          "!undefined_class",
          "defined_variable",
        };

  vars:
    ok::
      "ok" string => "yes";

  reports:
    !ok_to_run::
      "Must be run using testall script";

    ok_to_run.DEBUG.!function_ok::
      "usemodule did not execute correctly.";

    ok_to_run.DEBUG.!function_neg_ok::
      "usemodule returned non-zero, class function_neg_ok was not set, but should be set.";

    ok_to_run.DEBUG.!defined_class::
      "defined_class was not set, but should be.";

    ok_to_run.DEBUG.undefined_class::
      "undefined_class was set, but shouldn't be.";

    ok_to_run.DEBUG.!defined_variable::
      "defined_variable was not set, but should be.";

    ok_to_run.DEBUG.function_ok::
      "usemodule executed correctly.";

    ok_to_run.DEBUG.function_neg_ok::
      "usemodule returned non-zero and class function_neg_ok was set.";

    ok_to_run.DEBUG.defined_class::
      "defined_class was set.";

    ok_to_run.DEBUG.!undefined_class::
      "undefined_class was not set.";

    ok_to_run.DEBUG.defined_variable::
      "defined_variable was set.";
}

#######################################################
bundle agent check
{
  classes:
    "ok" expression => strcmp("$(test.ok)", "yes");

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

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