#######################################################
#
# Test cf-serverd related promises, mtime simple copy localhost
#
#######################################################

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

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

bundle agent init
{
}

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

bundle agent test
{
  files:
      "$(G.testdir)/destfile_classic" copy_from => copy_src_file("classic");
      "$(G.testdir)/destfile_latest"  copy_from => copy_src_file("latest");
}

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

body copy_from copy_src_file(protocol_version)
{
      source      => "$(G.testdir)/source_file";
      servers     => { "127.0.0.1" };
      compare     => "mtime";
      copy_backup => "false";
      protocol_version => "$(protocol_version)";

      portnumber => "9876"; # localhost_open

      #encrypt     => "true";
      #verify      => "true";
      #purge       => "false";
      #type_check  => "true";
      #force_ipv4  => "true";
      trustkey => "true";
}

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

bundle agent check
{
  vars:

    # Real file size
    "size1" string => filestat( "$(G.testdir)/destfile_classic", size ),
            if =>   fileexists( "$(G.testdir)/destfile_classic");
    "size2" string => filestat( "$(G.testdir)/destfile_latest", size ),
            if =>   fileexists( "$(G.testdir)/destfile_latest");

    # Disk allocated space
    "alloc_size1" string =>
        execresult( "expr    `du -k $(G.testdir)/destfile_classic | cut -f1`    '*' 1024", useshell),
            if => fileexists("$(G.testdir)/destfile_classic");
    "alloc_size2" string =>
        execresult( "expr    `du -k $(G.testdir)/destfile_latest  | cut -f1`    '*' 1024", useshell),
            if => fileexists("$(G.testdir)/destfile_latest");

  classes:
      "dummy" expression => regextract("(.*)\.sub", $(this.promise_filename), "fn");

      "is_sparse1" expression => islessthan( "$(alloc_size1)", "$(size1)" );
      "is_sparse2" expression => islessthan( "$(alloc_size2)", "$(size2)" );

  methods:
      "any" usebundle => dcs_if_diff_expected(
                             "$(G.testdir)/source_file", "$(G.testdir)/destfile_classic",
                             "no", "same_classic", "differ_classic");
      "any" usebundle => dcs_if_diff_expected(
                             "$(G.testdir)/source_file", "$(G.testdir)/destfile_latest",
                             "no", "same_latest", "differ_latest");

  reports:

    DEBUG.!is_sparse1::
      "ERROR destfile_classic is NOT sparse; size1=$(size1) alloc_size1=$(alloc_size1)";
    DEBUG.!is_sparse2::
      "ERROR destfile_latest  is NOT sparse; size2=$(size2) alloc_size2=$(alloc_size2)";
    same_classic.same_latest.is_sparse1.is_sparse2::
      "$(fn[1]) Pass";
    !same_classic|!same_latest|!is_sparse1|!is_sparse2::
      "$(fn[1]) FAIL";
}
