##############################################################################
#
# Test that agent can override the immutable bit when using rename attribute
#
##############################################################################

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

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

body fsattrs set_immutable
{
  immutable => "true";
}

bundle agent init
{
  files:
    "$(global.testfile)"
      create => "true";

  commands:
    "chattr +i $(global.testfile)"
      contain => in_shell;
}

body rename nuke
{
  disable => "true";
  disable_suffix => ".nuked";
}

bundle agent test
{
  meta:
    "description" -> { "CFE-1840", "ENT-10961" }
      string => "Test that agent can override the immutable bit on a file while using the rename attribute";

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

  files:
    "$(global.testfile)"
      fsattrs => set_immutable,
      rename => nuke;
}

bundle agent check
{
  classes:
    "original_exists"
      expression => fileexists("$(global.testfile)");
    "nuked_exists"
      expression => fileexists("$(global.testfile).nuked");
    "ok"
      expression => "!original_exists&nuked_exists";

  reports:
    ok::
      "$(this.promise_filename) Pass";
    !ok::
      "$(this.promise_filename) FAIL";
    any::
      "Expected file '$(global.testfile)' to not exists $(with)"
        with => ifelse("!original_exists", "and it does not", "but it does");
      "Expected file '$(global.testfile).nuked' to exists $(with)"
        with => ifelse("nuked_exists", "and it does", "but it does not");
}

bundle agent destroy
{
  commands:
    "chattr -i $(global.testfile)"
      contain => in_shell,
      handle => "orig is mutable",
      if => fileexists("$(global.testfile)");
    "chattr -i $(global.testfile).nuked"
      contain => in_shell,
      handle => "nuked is mutable",
      if => fileexists("$(global.testfile).nuked");

  files:
    "$(global.testfile)"
      delete => tidy,
      depends_on => { "orig is mutable" };
    "$(global.testfile).nuked"
      delete => tidy,
      depends_on => { "nuked is mutable" };
}
