##############################################################################
#
# Test that agent reports the immutable bit in the error message when it
# cannot write to an immutable file (content attribute, no override).
#
# The whole scenario (create the file, set it immutable, then fail to write it)
# runs in a self-contained sub policy: its setup happens in its own agent
# process, so it does not depend on this test's evaluation order, and its
# non-zero exit does not fail this test. We only assert on the captured error
# output. Teardown clears the immutable bit natively via body fsattrs.
#
##############################################################################
body common control
{
  inputs => { "../../default.sub.cf" };
  bundlesequence => { default("$(this.promise_filename)") };
  version => "1.0";
}

bundle agent global
{
  vars:
    "testfile" string => "/tmp/14_immutable.txt";
}

body fsattrs clear_immutable
{
  immutable => "false";
}

bundle agent test
{
  meta:
    "description" -> { "ENT-14297" }
      string => "Test that the error message names the immutable bit when agent cannot write to an immutable file";

    "test_skip_unsupported" string => "hpux|aix|solaris|windows";
}

bundle agent check
{
  vars:
    "run"
      data => execresult_as_data(
        "$(sys.cf_agent) -KI -f $(with)", "noshell", "both"
      ),
      with => regex_replace("$(this.promise_filename)", "\.cf$", ".sub.cf", "");

  classes:
    "ok"
      expression => regcmp(
        ".*Cannot open file.*the immutable bit is set.*", "$(run[output])"
      );

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

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

    DEBUG::
      "$(this.bundle): agent exited '$(run[exit_code])', output was:";
      "$(run[output])";
}

bundle agent destroy
{
  files:
    "$(global.testfile)"
      delete => tidy,
      fsattrs => clear_immutable;
}
