#######################################################
#
# Test multiple promise_repaired and cancel_repaired (contrived example)
# Insert lines to a file, verify that insert promise is repaired
#
# Present in both 00_basics/03_bodies and 10_files/05_classes
#
#######################################################

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

bundle agent init
{
  vars:
      "body" string =>
      "BEGIN
    One potato
    Two potato
    Three potatoes
    Four
END";

  files:
      "$(G.testfile)"
      create => "true",
      edit_line => init_insert("$(body)"),
      edit_defaults => init_empty,
      classes => all_classes;
}

bundle edit_line init_insert(str)
{
  insert_lines:
      "$(str)";
}

body edit_defaults init_empty
{
      empty_file_before_editing => "true";
}

# These set/clear global classes, so they can be tested in another bundle
body classes all_classes
{
      promise_repaired => { "promise_repaired", "p2_repaired" };
      cancel_repaired => { "cancel_repaired", "cancel_kept", "cancel_notkept" };
}

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

bundle agent test
{
  vars:
      "dummy" string => "dummy";
}

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

bundle agent check
{
      # These variables determine the conditions being tested
  vars:
      "expect[promise_kept]" string	=> "";
      "expect[promise_repaired]" string	=> "ON";
      "expect[p2_repaired]" string	=> "ON";
      "expect[repair_failed]" string	=> "";
      "expect[repair_denied]" string	=> "";
      "expect[repair_timeout]" string	=> "";
      "expect[cancel_kept]" string	=> "";
      "expect[cancel_repaired]" string	=> "";
      "expect[cancel_notkept]" string	=> "";

      # Everything from here down is "boilerplate" to these 1xx.cf tests
      "lookfor" slist => {
			   "promise_kept", "promise_repaired", "p2_repaired",
			   "repair_failed", "repair_denied",
			   "repair_timeout", "cancel_kept", "cancel_repaired", "cancel_notkept",
      };

  classes:
      "p1_$(lookfor)" and => {
			       strcmp("$(expect[$(lookfor)])", "ON"),
			       "$(lookfor)",
      };
      "p2_$(lookfor)" and => {
			       strcmp("$(expect[$(lookfor)])", ""),
			       "!$(lookfor)",
      };
      "pass_$(lookfor)" xor => { "p1_$(lookfor)", "p2_$(lookfor)" };
      
      "f1_$(lookfor)" and => {
			       strcmp("$(expect[$(lookfor)])", "ON"),
			       "!$(lookfor)",
      };
      "f2_$(lookfor)" and => {
			       strcmp("$(expect[$(lookfor)])", ""),
			       "$(lookfor)",
      };
      "f3_$(lookfor)" not => isvariable("expect[$(lookfor)]");
      "fail_$(lookfor)" or => {"f1_$(lookfor)", "f2_$(lookfor)", "f3_$(lookfor)"};

      "oops" expression => classmatch("fail.*");
      "ok" and => { classmatch("pass.*"), "!oops" };

  reports:
    DEBUG::
      "ok: class '$(lookfor)' was set (should be)"
      if => "p1_$(lookfor)";
      "ok: class '$(lookfor)' was not set (should not be)"
      if => "p2_$(lookfor)";
      "ERROR: class '$(lookfor)' WAS NOT set (should be)"
      if => "f1_$(lookfor)";
      "ERROR: class '$(lookfor)' was set (should NOT be)"
      if => "f2_$(lookfor)";
      "ERROR: missing variable expect['$(lookfor)']"
      if => "f3_$(lookfor)";
    ok::
      "$(this.promise_filename) Pass";
    !ok::
      "$(this.promise_filename) FAIL";
}
### PROJECT_ID: core
### CATEGORY_ID: 27
