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

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

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

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

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

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

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

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

  vars:
    "gzip_path"
      string => ifelse(
        isexecutable("/bin/gzip"),
        "/bin/gzip",
        "/usr/bin/gzip"
      );

  files:
    "$(global.testfile)"
      fsattrs => set_immutable,
      transformer => "$(gzip_path) $(this.promiser)";

}

bundle agent check
{
  classes:
    "original_exists"
      expression => fileexists("$(global.testfile)");
    "transformed_exists"
      expression => fileexists("$(global.testfile).gz");
    "ok"
      expression => "!original_exists&transformed_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).gz' to exists $(with)"
        with => ifelse("transformed_exists", "and it does", "but it does not");
}

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

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