##############################################################################
#
# Test that agent can override the immutable bit when using edit_template
# attribute with mustache
#
##############################################################################

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

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

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

bundle agent init
{
  files:
    "$(global.testfile)"
      create => "true",
      content => "Hello olehermanse!";

  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 edit_template attribute with mustache";

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

  vars:
    "object"
      data => '{ "user": "larsewi" }';

  files:
    "$(global.testfile)"
      fsattrs => set_immutable,
      template_method => "inline_mustache",
      edit_template_string => "Hello {{{user}}}!",
      template_data => @(object);
}

bundle agent check
{
  vars:
    "expected"
      string => "Hello larsewi!";
    "actual"
      string => readfile("$(global.testfile)");

  classes:
    "ok"
      expression => strcmp("$(actual)", "$(expected)");

  reports:
    ok::
      "$(this.promise_filename) Pass";
    !ok::
      "$(this.promise_filename) FAIL";
    any::
      "Expecting '$(expected)', got '$(actual)'";
}

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

  files:
    "$(global.testfile)"
      delete => tidy,
      depends_on => { "is mutable" };
    "$(global.testfile).cf-before-edit"
      delete => tidy;
}
