#######################################################
#
# Acceptance test for issue #4070
# Ensure that cfengine creates a
# file that has the same name as a pre-existing
# directory without consuming 100% cpu and crashing
# This test does what it says in 4070 - it creates
# a directory e.g. /tmp/backup/my_dir
# It then causes cfengine to create a file with an
# identical name i.e. /tmp/backup/my_dir
# The names above are simplified so as not to cause
# confusion.
#
#######################################################

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


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

bundle agent init
{

      #######################################
      #Clean up test directory before testing
      #######################################

  files:
      "$(G.testdir)/."
      depth_search => recurse("inf"),
      delete => tidy,
      file_select => all,
      classes => if_ok("dir_purged");

  commands:
    dir_purged::
      "$(G.mkdir) -p"
      args => "$(G.testdir)/backup/_tmp_TEST_cfengine_test";
}

bundle edit_line test_insert(str)
{
  insert_lines:
      "$(sys.date)";
}

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

bundle agent test
{
  files:
    agent_ran::
      "$(G.testdir)/test"
      edit_line => test_insert("inserted text"),
      classes => if_ok("file_edited");

  commands:
      "$(sys.cf_agent)"
      args => "-f $(this.promise_filename).sub -K",
      classes => if_ok("agent_ran");

    file_edited::
      "$(sys.cf_agent)"
      args => "-f $(this.promise_filename).sub -K -D SECOND_RUN",
      classes => classes_generic("agent_ran_test"),
      contain => test_timeout;

}

body contain test_timeout
{
      exec_timeout => "30";
}

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

bundle agent check
{

  classes:
      "ok" expression => fileexists("$(G.testdir)/SECOND_RUN");

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

}
