summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndreas Baumann <abaumann@yahoo.com>2012-04-24 08:39:22 +0200
committerAndreas Baumann <abaumann@yahoo.com>2012-04-24 08:39:22 +0200
commite1669b74ddec239ce2b987180bb6b81bb4fb0580 (patch)
tree182c3a48802746eff6438c28de133ca0173f4303 /tests
parent85b2ec441def308a9fbb8c54e9dc14d2f1427dfb (diff)
downloadpgfuse-e1669b74ddec239ce2b987180bb6b81bb4fb0580.tar.gz
pgfuse-e1669b74ddec239ce2b987180bb6b81bb4fb0580.tar.bz2
updated developer documentation
moved tests into their own subdirectory, added inc.mak for common makefilen stuff
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile73
-rw-r--r--tests/clean.sql4
-rw-r--r--tests/testfsync.c52
-rw-r--r--tests/testpgsql.c155
4 files changed, 284 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644
index 0000000..d4aec77
--- /dev/null
+++ b/tests/Makefile
@@ -0,0 +1,73 @@
+include ../inc.mak
+
+PG_CONNINFO = ""
+
+test: testfsync testpgsql
+ psql < clean.sql
+ psql < ../schema.sql
+ test -d mnt || mkdir mnt
+ ../pgfuse -s -v "$(PG_CONNINFO)" mnt
+ mount | grep pgfuse
+ # expect success for making directories
+ -mkdir mnt/dir
+ -mkdir mnt/dir/dir2
+ -mkdir mnt/dir/dir3
+ # expect success on open and file write
+ -echo "hello" > mnt/dir/dir2/afile
+ -cp Makefile mnt/dir/dir2/bfile
+ # expect success on open and file read
+ -cat mnt/dir/dir2/afile
+ -ls -al mnt
+ -ls -al mnt/dir/dir2
+ # expect success on rmdir
+ -rmdir mnt/dir/dir3
+ # expect success on chmod
+ -chmod 777 mnt/dir/dir2/bfile
+ -ls -al mnt/dir/dir2/bfile
+ # expect success on symlink creation
+ -ln -s bfile mnt/dir/dir2/clink
+ -ls -al mnt/dir/dir2/clink
+ # expect success on file removal
+ -rm mnt/dir/dir2/bfile
+ # expect success on rename
+ -mkdir mnt/dir/dir3
+ -mv mnt/dir/dir3 mnt/dir/dir4
+ -mv mnt/dir/dir2/afile mnt/dir/dir4/bfile
+ # expect fail (directory not empty)
+ -rmdir mnt/dir
+ # expect fail (not a directory)
+ -rmdir mnt/dir/dir2/bfile
+ # test fdatasync and fsync
+ ./testfsync
+ # show times of dirs, files and symlinks
+ -stat mnt/dir/dir2/afile
+ -stat mnt/dir/dir3
+ -stat mnt/dir/dir2/clink
+ # show filesystem stats (statvfs)
+ -stat -f mnt
+ # expect success, truncate a file (grow and shrink)
+ -touch mnt/trunc
+ -ls -al mnt/trunc
+ -truncate --size 2049 mnt/trunc
+ -ls -al mnt/trunc
+ -dd if=/dev/zero of=mnt/trunc bs=512 count=10
+ -truncate --size 513 mnt/trunc
+ -ls -al mnt/trunc
+ # END: unmount FUSE file system
+ fusermount -u mnt
+
+clean:
+ rm -f testfsync testfsync.o
+ rm -f testpgsql testpgsql.o
+
+testfsync: testfsync.o
+ $(CC) -o testfsync testfsync.o
+
+testfsync.o: testfsync.c
+ $(CC) -c $(CFLAGS) -o testfsync.o testfsync.c
+
+testpgsql: testpgsql.o
+ $(CC) -o testpgsql testpgsql.o $(LDFLAGS)
+
+testpgsql.o: testpgsql.c
+ $(CC) -c $(CFLAGS) -o testpgsql.o testpgsql.c
diff --git a/tests/clean.sql b/tests/clean.sql
new file mode 100644
index 0000000..6d94089
--- /dev/null
+++ b/tests/clean.sql
@@ -0,0 +1,4 @@
+DROP RULE dir_insert ON dir;
+DROP RULE dir_remove ON dir;
+DROP TABLE data;
+DROP TABLE dir;
diff --git a/tests/testfsync.c b/tests/testfsync.c
new file mode 100644
index 0000000..571b816
--- /dev/null
+++ b/tests/testfsync.c
@@ -0,0 +1,52 @@
+/*
+ Copyright (C) 2012 Andreas Baumann <abaumann@yahoo.com>
+
+ This program 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.
+
+ This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+int main( void )
+{
+ char buf[4096];
+ int fd;
+ ssize_t res;
+
+ memset( buf, 0, 4096 );
+
+ fd = open( "./mnt/testfsync.data", O_WRONLY | O_CREAT,
+ S_IRUSR | S_IWUSR );
+ if( fd < 0 ) {
+ perror( "Unable to open testfile" );
+ return 1;
+ }
+
+ res = write( fd, buf, 4096 );
+ if( res != 4096 ) {
+ perror( "Error writing" );
+ (void)close( fd );
+ return 1;
+ }
+
+ fdatasync( fd );
+ fsync( fd );
+
+ (void)close( fd );
+
+ return 0;
+}
diff --git a/tests/testpgsql.c b/tests/testpgsql.c
new file mode 100644
index 0000000..3159e2d
--- /dev/null
+++ b/tests/testpgsql.c
@@ -0,0 +1,155 @@
+/*
+ Copyright (C) 2012 Andreas Baumann <abaumann@yahoo.com>
+
+ This program 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.
+
+ This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <libpq-fe.h> /* for Postgresql database access */
+
+#include <stdio.h> /* for fprintf */
+#include <string.h> /* for strcmp */
+#include <stdbool.h> /* for bool */
+#include <stdint.h> /* for uint64_t */
+#include <sys/time.h> /* for struct timespec, gettimeofday */
+
+#include "endian.h" /* for be64toh */
+
+/* January 1, 2000, 00:00:00 UTC (in Unix epoch seconds) */
+#define POSTGRES_EPOCH_DATE 946684800
+
+static struct timespec get_now( void )
+{
+ int res;
+ struct timeval t;
+ struct timezone tz;
+ struct timespec s;
+
+ res = gettimeofday( &t, &tz );
+ if( res != 0 ) {
+ s.tv_sec = 0;
+ s.tv_nsec = 0;
+ return s;
+ }
+
+ s.tv_sec = t.tv_sec;
+ s.tv_nsec = t.tv_usec * 1000;
+
+ return s;
+}
+
+int main( int argc, char *argv[] )
+{
+ PGconn *conn;
+ const char *value;
+ bool integer_datetimes;
+ PGresult *res;
+ uint64_t tmp;
+ uint64_t param1;
+ const char *values[1] = { (const char *)&param1 };
+ int lengths[1] = { sizeof( param1 ) };
+ int binary[1] = { 1 };
+ struct timespec now;
+
+ if( argc != 2 ) {
+ fprintf( stderr, "usage: testpgsql <Pg conn info>\n" );
+ return 1;
+ }
+
+ conn = PQconnectdb( argv[1] );
+ if( PQstatus( conn ) != CONNECTION_OK ) {
+ fprintf( stderr, "Connection to database failed: %s",
+ PQerrorMessage( conn ) );
+ PQfinish( conn );
+ return 1;
+ }
+
+ value = PQparameterStatus( conn, "integer_datetimes" );
+ if( value == NULL ) {
+ fprintf( stderr, "PQ param integer_datetimes empty?\n" );
+ PQfinish( conn );
+ return 1;
+ }
+
+ integer_datetimes = strcmp( value, "on" ) == 0 ? true : false;
+ printf( "integer_datetimes: %s\n", integer_datetimes ? "true" : "false" );
+
+ now = get_now( );
+ tmp = ( (uint64_t)now.tv_sec - POSTGRES_EPOCH_DATE ) * 1000000;
+ tmp += now.tv_nsec / 1000;
+ param1 = htobe64( tmp );
+
+ res = PQexecParams( conn, "SELECT now(),$1::timestamp",
+ 1, NULL, values, lengths, binary, 1 );
+
+ if( PQresultStatus( res ) != PGRES_TUPLES_OK ) {
+ fprintf( stderr, "select error: %s\n", PQerrorMessage( conn ) );
+ PQclear( res );
+ PQfinish( conn );
+ return 1;
+ }
+
+ if( PQntuples( res ) == 0 ) {
+ PQclear( res );
+ PQfinish( conn );
+ return 1;
+ }
+
+ if( PQntuples( res ) > 1 ) {
+ fprintf( stderr, "Expecting exactly one tuple as result." );
+ PQclear( res );
+ PQfinish( conn );
+ return 1;
+ }
+
+ /* Since PostgreSQL 8.4 int64 representation should be the default
+ * unless changed at compilation time
+ */
+ if( integer_datetimes ) {
+ char *data_db;
+ char *data_select;
+ struct timespec time_db;
+ struct timespec time_select;
+ uint64_t t_db;
+ uint64_t t_select;
+
+ data_db = PQgetvalue( res, 0, 0 );
+
+ t_db = be64toh( *( (uint64_t *)data_db ) );
+
+ time_db.tv_sec = POSTGRES_EPOCH_DATE + t_db / 1000000;
+ time_db.tv_nsec = ( t_db % 1000000 ) * 1000;
+
+ data_select = PQgetvalue( res, 0, 1 );
+
+ t_select = be64toh( *( (uint64_t *)data_select ) );
+
+ time_select.tv_sec = POSTGRES_EPOCH_DATE + t_select / 1000000;
+ time_select.tv_nsec = ( t_select % 1000000 ) * 1000;
+
+ now = get_now( );
+
+ printf( "now passed as param: %lu.%lu, now from database: %lu.%lu, now computed: %lu.%lu\n",
+ time_select.tv_sec, time_select.tv_nsec, time_db.tv_sec, time_db.tv_nsec, now.tv_sec, now.tv_nsec );
+ } else {
+ /* doubles have no standard network representation! */
+ fprintf( stderr, "Not supporting dates as doubles!\n" );
+ PQclear( res );
+ PQfinish( conn );
+ return 1;
+ }
+
+ PQfinish( conn );
+
+ return 0;
+}