test_smbclient_tarmode.pl

NAME

test_smbclient_tarmode.pl - Test for smbclient tar backup feature

SYNOPSIS

 test_smbclient_tarmode.pl [options] -- [smbclient options]

 Options:
    -h, --help    brief help message
    --man         full documentation

  Environment:
    -u, --user      USER
    -p, --password  PW
    -h, --host      HOST
    -i, --ip        IP
    -s, --share     SHARE
    -d, --dir       PATH
        sub-path to use on the share

    -l, --local-path  PATH
        path to the root of the samba share on the machine.

    -t, --tmp  PATH
        temporary dir to use

    -b, --bin  BIN
        path to the smbclient binary to use

  Test:
    --list
       list tests

    --test N
       only run test number N

DOCUMENTATION

Defining a test

The function must be placed in the @TESTS list along with a short description and optional arguments.

Useful functions

Here are a list of useful functions and helpers to define tests.

combine ( \@set, $n )

combine ( ['a', 'b', 'c'], 2 )

Return a list of all possible n-uplet (or combinaison of $n element) of @set.

reset_remote( )

Remove all files in the server $DIR (not root)

reset_tmp( )

Remove all files in the temp directory $TMP

reset_env( )

Remove both temp and remote ($DIR) files

file_list ( @files )

Make a multiline string of all the files remote path, one path per line.

@files must be a list of File instance.

check_remote( $remotepath, \@files )

Check if $remotepath has exactly all the @files.

Print a summary on STDOUT.

@files must be a list of File instance.

check_tar( $path_to_tar, \@files )

Check if the archive $path_to_tar has exactly all the @files.

Print a summary on STDOUT;

@files must be a list of File instance.

smb_client ( @args )

Run smbclient with @args passed as argument and return output.

Each element of @args becomes one escaped argument of smbclient.

Host, share, user, password and the additionnal arguments provided on the command-line are already inserted.

The output contains both the STDOUT and STDERR.

Die if smbclient crashes or exits with an error code.

smb_tar( $cmd, @args )

smb_tar( 'tarmode inc', '-Tc', $TAR, $DIR )

Run $cmd command and use @args as argument and return output.

Wrapper around smb_client for tar calls.

random( $min, $max )

Return integer in [ $min ; $max [

The File module

All the test should use the File class. It has nice functions and methods to deal with paths, to create random files, to list the content of the server, to change attributes, etc.

There are 2 kinds of File: remote and local.

Thus, some methods only works on remote files. If they do not make sense for local ones, they always return undef.

Constructors

File->new_remote($path [, $abs])

Creates a file accessible on the server at $DIR/$path ie. not at the root of the share.

If you want to remove the automatic prefix $DIR, set $abs to 1.

The file is created without any DOS attributes.

If $path contains non-existent directories, they are automatically created.

File->new_local($abs_path [, $data])

Creates a file at $abs_path with $data in it on the system. If $data is not provided, fill it with random bytes.

Methods

$f->localpath

Return path on the system eg. /opt/samba/share/test_tar_mode/file

$f->remotepath

Return path on the server share.

Return undef if the file is local.

$f->remotedir

Return the directory path of the file on the server.

Like $f->remotepath but without the final file name.

$f->tarpath

Return path as it would appear in a tar archive.

Like $f->remotepath but prefixed with ./

$f->delete_on_destruction( 0 )

$f->delete_on_destruction( 1 )

By default, a File is not deleted on the filesystem when it is not referenced anymore in Perl memory.

When set to 1, the destructor unlink the file if it is not already removed. If the File created directories when constructed, it does not remove them.

$f->set_attr( )

$f->set_attr( 'a' )

$f->set_attr( 'a', 'r', 's', 'h' )

Remove all DOS attributes and only set the one provided.

$f->attr_any( 'a' )

$f->attr_any( 'a', 's', ... )

Return 1 if the file has any of the DOS attributes provided.

$f->attr( 'a' )

$f->attr( 'a', 's', ... )

Return 1 if the file has all the DOS attributes provided.

$f->attr_str

Return DOS attributes as a compact string.

  Read-only, hiden, system, archive => "rhsa"

$f->set_time($t)

Set modification and access time of the file to $t.

$t must be in Epoch time (number of seconds since 1970/1/1).

$f->md5

Return md5 sum of the file.

The result is cached.

Functions

File::walk( \&function, @files)

File::walk( sub { ... }, @files)

Iterate on file hierachy in @files and return accumulated results.

Use $_ in the sub to access the current File.

The @files must come from a call to the File::tree function.

File::list( $remotepath )

Return list of file (File instance) in $remotepath.

$remotepath must be a directory.

File::tree( $remotepath )

Return recursive list of file in $remotepath.

$remotepath must be a directory.

Use File::walk() to iterate over all the files.

Examples

    # create remote file in $DIR/foo/bar
        my $f = File->new_remote("foo/bar/myfile");
        say $f->localpath;  # /opt/share/$DIR/foo/bar/myfile
        say $f->remotepath; # $DIR/foo/bar/myfile
        say $f->remotedir;  # $DIR/foo/bar


    # same but in root dir
        my $f = File->new_remote("myfile", 1);
        say $f->localpath;  # /opt/share/myfile
        say $f->remotepath; # myfile
        say $f->remotedir;  #


    # create local random temp file in $TMP
        my $f = File->new_local("$TMP/temp");
        say $f->remotepath; # undef because it's not on the server


    # same but file contains "hello"
        my $f = File->new_local("$TMP/temp", "hello");


    # list of files in $DIR (1 level)
        for (File::list($DIR)) {
            say $_->remotepath;
        }


    # list of all files in dir and subdir of $DIR
        File::walk(sub { say $_->remotepath }, File::tree($DIR));
 test_smbclient_tarmode.pl