summaryrefslogtreecommitdiffstats
path: root/test/fileutils/fileasserts.rb
blob: d7845445550262647e3504c6fee777f0d096df8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# $Id$

module Test
  module Unit
    module Assertions   # redefine

      def _wrap_assertion
        yield
      end

      def assert_same_file(from, to)
        _wrap_assertion {
          assert_block("file #{from} != #{to}") {
            File.read(from) == File.read(to)
          }
        }
      end

      def assert_same_entry(from, to)
        a = File.stat(from)
        b = File.stat(to)
        assert_equal a.mode, b.mode, "mode #{a.mode} != #{b.mode}"
        #assert_equal a.atime, b.atime
        assert_equal_timestamp a.mtime, b.mtime, "mtime #{a.mtime} != #{b.mtime}"
        assert_equal a.uid, b.uid, "uid #{a.uid} != #{b.uid}"
        assert_equal a.gid, b.gid, "gid #{a.gid} != #{b.gid}"
      end

      def assert_file_exist(path)
        _wrap_assertion {
          assert_block("file not exist: #{path}") {
            File.exist?(path)
          }
        }
      end

      def assert_file_not_exist(path)
        _wrap_assertion {
          assert_block("file not exist: #{path}") {
            not File.exist?(path)
          }
        }
      end

      def assert_directory(path)
        _wrap_assertion {
          assert_block("is not directory: #{path}") {
            File.directory?(path)
          }
        }
      end

      def assert_symlink(path)
        _wrap_assertion {
          assert_block("is not a symlink: #{path}") {
            File.symlink?(path)
          }
        }
      end

      def assert_not_symlink(path)
        _wrap_assertion {
          assert_block("is a symlink: #{path}") {
            not File.symlink?(path)
          }
        }
      end

      def assert_equal_time(expected, actual, message=nil)
        _wrap_assertion {
	  expected_str = expected.to_s
	  actual_str = actual.to_s
	  if expected_str == actual_str
	    expected_str << " (nsec=#{expected.nsec})"
	    actual_str << " (nsec=#{actual.nsec})"
	  end
	  full_message = build_message(message, <<EOT)
<#{expected_str}> expected but was
<#{actual_str}>.
EOT
	  assert_block(full_message) { expected == actual }
        }
      end

      def assert_equal_timestamp(expected, actual, message=nil)
        _wrap_assertion {
	  expected_str = expected.to_s
	  actual_str = actual.to_s
	  if expected_str == actual_str
	    expected_str << " (nsec=#{expected.nsec})"
	    actual_str << " (nsec=#{actual.nsec})"
	  end
	  full_message = build_message(message, <<EOT)
<#{expected_str}> expected but was
<#{actual_str}>.
EOT
          # subsecond timestamp is not portable. 
	  assert_block(full_message) { expected.tv_sec == actual.tv_sec }
        }
      end

    end
  end
end
d='n371' href='#n371'>371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
/* imtemplate.c
 * 
 * This is NOT a real input module but a (copy)-template to create one. Please
 * do NOT edit this file directly. Rather, copy it, together with the rest of 
 * the directory, to a new location ./plugins/im<yourname>, then replace
 * all references to imtemplate in Makefile.am to im<yourname>. Be sure to
 * fix the copyright notices to gain proper credit ;) Any derived version,
 * however, needs to be placed under GPLv3 (see GPLv3 for details). If you
 * do not like that policy, do not use this template or any of the header
 * files. The rsyslog project greatly appreciates module contributions, so
 * please consider contributing your work - even if you may think it only
 * server a single very special purpose. It has turned out that at least some
 * folks have similiar special purposes ;)
 *
 * IMPORTANT
 * The comments in this file are actually the interface specification. I decided
 * not to put it into a separate file as it is much simpler to keep it up to
 * date when it is part of the actual template module.
 *
 * NAMING
 * All input modules shall be named im<something>. While this is not a hard
 * requirement, it helps keeping track of things.
 *
 * Global variables and functions should have a prefix - use as somewhat
 * longer one to prevent conflicts with rsyslog itself and other modules
 * (OK, hopefully I'll have some more precise advise in the future...).
 *
 * INCLUDE MODULE IN THE MAIN MAKE SCRIPT
 * If the module shall be provided as part of rsyslog (or simply as a build aid,
 * you need to add it to the main autoconf files). To do so, you need to edit
 * Makefile.am and configure.ac in the main directory. Search for imtemplate
 * and copy/modify the relevant code for your plugin.
 *
 * DEBUGGING
 * While you develop your code, you may want to add
 *   --enable-debug --enable-rtinst
 * to your ./configure settings. These enable extra run-time checks, which cost
 * a lot of performance but can help detect some of the most frequently made
 * bugs. These settings will also provide you with a nice stack dump if something
 * goes really wrong.
 *
 * MORE SAMPLES
 * Remember that rsyslog ships with a number of input modules (./plugins/im*). It
 * is always a good idea to have a look at them before starting your own. imudp
 * may be a good, relatively trivial, sample.
 *
 * --------------------------------------------------------------------------------
 *
 * This template was cretead on 2008-02-01 by Rainer Gerhards.
 *
 * Copyright 2008 Rainer Gerhards and Adiscon GmbH.
 *
 * This file is part of rsyslog.
 *
 * Rsyslog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rsyslog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Rsyslog.  If not, see <http://www.gnu.org/licenses/>.
 *
 * A copy of the GPL can be found in the file "COPYING" in this distribution.
 */
#include "config.h" /* this is for autotools and always must be the first include */
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>		/* do NOT remove: will soon be done by the module generation macros */
#include "rsyslog.h"		/* error codes etc... */
#include "cfsysline.h"		/* access to config file objects */
#include "module-template.h"	/* generic module interface code - very important, read it! */
#include "srUtils.h"		/* some utility functions */
#include "debug.h"		/* some debug helper functions */

MODULE_TYPE_INPUT	/* must be present for input modules, do not remove */

/* defines */

/* Module static data */
DEF_IMOD_STATIC_DATA	/* must be present, starts static data */

/* Here, define whatever static data is needed. Is it suggested that static variables only are
 * used (not externally visible). If you need externally visible variables, make sure you use a
 * prefix in order not to conflict with other modules or rsyslogd itself (also see comment
 * at file header).
 */
/* static int imtemplateWhateverVar = 0; */

/* config settings */


/* You may add any functions that you feel are useful for your needs. No specific restrictions
 * apply, but we suggest that you use the "iRet" call order, which enables you to use debug
 * support for your own functions and which also makes it easy to communicate exceptions back
 * to the upstream caller (rsyslog framework, for example.
 *
 * The function below is a sample of how one of your functions may look like. Again, the sample
 * below is *not* needed to be present in order to meet the interface requirements.
 *
 * Be sure to use static functions (suggested) or prefixes to prevent name conflicts -- see file
 * header for more information.
 */
static rsRetVal  /* rsRetVal is our generic error-reporting return type */
imtemplateMyFunc(int iMyParam)
{
	DEFiRet;	/* define iRet, the return code and other plumbing */
	/* define your local variables here */

	/* code whatever you need to code here. The "iRet" system can be helpful:
	 *
	 * CHKiRet(function(param1, param2, ...));
	 * 	calls a function and checks if it returns RS_RET_OK. If so, work
	 * 	proceeds. If some other code is returned, the function is aborted
	 * 	and control transferred to finalize_it (which you need to define)
	 *
	 * CHKiRet_Hdlr(function(param1, param2, ...))
	 * 	much like CHKiRet, but allows you to specify your own code that is
	 * 	executed if the function does not return RS_RET_OK, e.g.:
	 * 	CHKiRet_Hdlr(function(a, b)) {
	 * 		... some error handling here ...
	 * 	}
	 * 	control is not transferred to finalize_it, except if you use one
	 * 	of the relevant macros (described below)
	 *
	 * FINALIZE
	 * 	immediately transfers control to finalize_it, using the current
	 * 	value of iRet, e.g.
	 * 	if(bDone)
	 * 		FINALIZE;
	 *
	 * ABORT_FINALIZE(retcode)
	 * 	just like FINALIZE, except that iRet is set to the provided error
	 * 	code before control is transferred, e.g.
	 * 	if((ptr = MALLOC(20)) == NULL)
	 * 		ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
	 *
	 * In order for all this to work, you need to define finalize_it, e.g.
	 *
	 * finalize_it:
	 * 	RETiRet;
	 *
	 * RETiRet does some housekeeping and then does a "return iRet" to transfer
	 * control back to the caller. There shall only be one function exit and
	 * it shall be via RETiRet, preferrably at the end of the function code.
	 *
	 */

finalize_it:
	/* clean up anything that needs to be cleaned up if processing did not
	 * go well, for example:
	 */
	if(iRet != RS_RET_OK) {
		/* cleanup, e.g.
		 * free(somePtr);
		 */
	}

	RETiRet;
}


/* This function is the cancel cleanup handler. It is called when rsyslog decides the
 * module must be stopped, what most probably happens during shutdown of rsyslogd. When
 * this function is called, the runInput() function (below) is already terminated - somewhere
 * in the middle of what it was doing. The cancel cleanup handler below should take
 * care of any locked mutexes and such, things that really need to be cleaned up
 * before processing continues. In general, many plugins do not need to provide
 * any code at all here.
 *
 * IMPORTANT: the calling interface of this function can NOT be modified. It actually is
 * called by pthreads. The provided argument is currently not being used.
 */
/* ------------------------------------------------------------------------------------------ *
 * DO NOT TOUCH the following code - it will soon be part of the module generation macros!    */
static void
inputModuleCleanup(void *arg)
{
	BEGINfunc
/* END no-touch zone                                                                          *
 * ------------------------------------------------------------------------------------------ */



	/* your code here */



/* ------------------------------------------------------------------------------------------ *
 * DO NOT TOUCH the following code - it will soon be part of the module generation macros!    */
	ENDfunc
}
/* END no-touch zone                                                                          *
 * ------------------------------------------------------------------------------------------ */


/* This function is called by the framework to gather the input. The module stays
 * most of its lifetime inside this function. It MUST NEVER exit this function. Doing