#######################################################
#
# Redmine#3184: Test editing of an immutable file
# Redmine#2100: Test chmod of an immutable file
#
#######################################################

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

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

bundle common chattr
{
  vars:
      "chattr" string => "/usr/bin/chattr";

  classes:
      "have_chattr" expression => fileexists($(chattr));
}

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

bundle agent test
{
  methods:
      "1" usebundle => test_setup($(G.testfile));
      "2" usebundle => test_edit($(G.testfile));
      "2" usebundle => test_chmod($(G.testfile));
      "3" usebundle => test_teardown($(G.testfile));
}

bundle agent test_setup(file)
{
  commands:
    have_chattr::
      "$(chattr.chattr)" args => "+i $(file)",
      classes => scoped_classes_generic("namespace", "chattr");
}

bundle agent test_edit(file)
{
  files:
    have_chattr::
      "$(file)"
      edit_line => test_edit_line(),
      classes => scoped_classes_generic("namespace", "test");
}

bundle agent test_chmod(file)
{
  files:
    have_chattr::
      "$(file)"
      perms => m("755"),
      classes => scoped_classes_generic("namespace", "chmod");
}

body perms m(mode)
{
      mode => "$(mode)";
}

bundle agent test_teardown(file)
{
  commands:
    have_chattr::
      "$(chattr.chattr)" args => "-i $(file)",
      classes => scoped_classes_generic("namespace", "chattr");
}

bundle edit_line test_edit_line()
{
  insert_lines:
      "blah";
}

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

bundle agent check
{
  classes:
      # we're OK if:
      # - we don't have chattr
      # - chattr failed to DTRT (e.g. we're not root)
      # - the test failed to edit
      # - the test denied the chmod (TODO: should it be "denied" or "failed"?
      "ok" expression => "!have_chattr|chattr_failed|(test_failed.!test_kept.!test_denied.!test_timeout.!test_repaired.!chmod_failed.!chmod_kept.chmod_denied.!chmod_timeout.!chmod_repaired)";

  methods:
      "report" usebundle => dcs_report_generic_classes("test");
      "report" usebundle => dcs_report_generic_classes("chmod");
      "report" usebundle => dcs_report_generic_classes("chattr");

  reports:
    DEBUG.!have_chattr::
      "Without $(chattr.chattr), the test will always pass";
    DEBUG.chattr_failed::
      "Since you can't run $(chattr.chattr) to set attributes, the test will always pass";
    ok::
      "$(this.promise_filename) Pass";
    !ok::
      "$(this.promise_filename) FAIL";
}
