r147@llin (orig r146): dpavlin | 2007-10-28 00:43:25 +0200
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 27 Oct 2007 22:55:45 +0000 (22:55 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 27 Oct 2007 22:55:45 +0000 (22:55 +0000)
 - added clean parametar to stores to start with empty database
 - much less chatty without debug
 - test both existing store plugins
 - finish API rename in CWMP::Store, version bump [0.05]

git-svn-id: https://perl-cwmp.googlecode.com/svn/trunk@161 836a5e1a-633d-0410-964b-294494ad4392

Makefile.PL
lib/CWMP/Store.pm
lib/CWMP/Store/DBMDeep.pm
lib/CWMP/Store/YAML.pm
t/05-store.t

index 501e9fc..25d66c7 100644 (file)
@@ -3,7 +3,7 @@ use lib './lib';
 use inc::Module::Install;
 
 name           'CWMP';
-version                '0.04';
+version                '0.05';
 license                'GPL';
 requires       'Net::Server';
 requires       'HTTP::Daemon';
@@ -26,7 +26,7 @@ build_requires 'Test::More';
 
 my_targets();
 
-clean_files('dump/* yaml state.db html');
+clean_files('dump/* yaml state.db html t/var/*');
 
 auto_install;
 
index 8787175..8bd72a5 100644 (file)
@@ -27,6 +27,7 @@ CWMP::Store - parsist CPE state on disk
   my $store = CWMP::Store->new({
        module => 'DBMDeep',
        path => '/path/to/state.db',
+       clean => 1,
        debug => 1,
   });
 
@@ -38,9 +39,10 @@ sub new {
 
        confess "requed parametar module is missing" unless $self->module;
 
-       warn "created ", __PACKAGE__, "(", dump( @_ ), ") object\n" if $self->debug;
+       # XXX it's important to call possible_stores once, because current_store won't work
+       my @plugins = $self->possible_stores();
 
-       warn "Found store plugins: ", join(", ", __PACKAGE__->possible_stores() );
+       warn "Found store plugins: ", join(", ", @plugins ), "\n" if $self->debug;
 
        $self->current_store->open( @_ );
 
@@ -61,7 +63,7 @@ sub current_store {
 
        confess "unknown store module $module not one of ", dump( $self->possible_stores ) unless $s;
 
-       warn "current store = ",dump( $s );
+       warn "## current store = $s\n" if $self->debug;
 
        return $s;
 }
@@ -100,22 +102,22 @@ sub update_state {
        $self->current_store->update_uid_state( $uid, $state );
 }
 
-=head2 state
+=head2 get_state
 
-  my $state = $store->state( ID => $ID );
-  my $state = $store->state( uid => $uid );
+  my $state = $store->get_state( ID => $ID );
+  my $state = $store->get_state( uid => $uid );
 
 Returns normal unblessed hash (actually, in-memory copy of state in database).
 
 =cut
 
-sub state {
+sub get_state {
        my $self = shift;
        my ( $k, $v ) = @_;
        confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
        confess "need $k value" unless $v;
 
-       warn "## state( $k => $v )\n" if $self->debug;
+       warn "## get_state( $k => $v )\n" if $self->debug;
 
        my $uid;
 
@@ -134,16 +136,16 @@ sub state {
 
 }
 
-=head2 known_CPE
+=head2 all_uids
 
-  my @cpe = $store->known_CPE;
+  my @cpe = $store->all_uids;
 
 =cut
 
-sub known_CPE {
+sub all_uids {
        my $self = shift;
        my @cpes = $self->current_store->all_uids;
-       warn "all CPE: ", dump( @cpes ), "\n" if $self->debug;
+       warn "## all_uids = ", dump( @cpes ), "\n" if $self->debug;
        return @cpes;
 }
 
index 985a124..04e4959 100644 (file)
@@ -16,11 +16,17 @@ CWMP::Store::DBMDeep - use DBM::Deep as storage
 
 =head2 open
 
+  $store->open({
+       path => 'var/',
+       debug => 1,
+       clean => 1,
+  });
+
 =cut
 
 my $db;
 
-my $debug = 1;
+my $debug = 0;
 
 sub open {
        my $self = shift;
@@ -34,6 +40,11 @@ sub open {
 
        $path = "$path/state.db" if ( -d $args->{path} );
 
+       if ( $args->{clean} && -e $path ) {
+               warn "removed old $path\n";
+               unlink $path || die "can't remove $path: $!";
+       }
+
        $db = DBM::Deep->new(
                file => $path,
                locking => 1,
index 8c88573..0fa1950 100644 (file)
@@ -17,11 +17,17 @@ CWMP::Store::YAML - use YAML as storage
 
 =head2 open
 
+  $store->open({
+       path => 'var/',
+       debug => 1,
+       clean => 1,
+  });
+
 =cut
 
 my $path;
 
-my $debug = 1;
+my $debug = 0;
 
 sub open {
        my $self = shift;
@@ -37,9 +43,16 @@ sub open {
 
        if ( ! -e $path ) {
                mkdir $path || die "can't create $path: $!";
-               warn "created $path directory\n";
+               warn "created $path directory\n" if $debug;
+       } elsif ( $args->{clean} ) {
+               warn "removed old $path\n" if $debug;
+               foreach my $uid ( $self->all_uids ) {
+                       my $file = "$path/$uid.yml";
+                       unlink $file || die "can't remove $file: $!";
+               }
        }
 
+
 }
 
 =head2 update_uid_state
index fb42380..6e315a3 100755 (executable)
@@ -4,7 +4,7 @@ use warnings;
 
 my $debug = shift @ARGV;
 
-use Test::More tests => 18;
+use Test::More tests => 34;
 use Data::Dump qw/dump/;
 use Cwd qw/abs_path/;
 use lib 'lib';
@@ -12,6 +12,7 @@ use lib 'lib';
 BEGIN {
        use_ok('CWMP::Store');
        use_ok('CWMP::Store::DBMDeep');
+       use_ok('CWMP::Store::YAML');
 }
 
 ok(my $abs_path = abs_path($0), "abs_path");
@@ -19,48 +20,58 @@ $abs_path =~ s!/[^/]*$!/!;  #!fix-vim
 
 my $path = "$abs_path/var/";
 
-unlink $path if -e $path;
+sub test_store {
+       my $module = shift;
 
-ok( my $store = CWMP::Store->new({
-       debug => $debug,
-#      module => 'DBMDeep',
-       module => 'YAML',
-       path => $path,
-}), 'new' );
-isa_ok( $store, 'CWMP::Store' );
+       diag "testing store plugin $module";
 
-cmp_ok( $store->path, 'eq', $path, 'path' );
+       ok( my $store = CWMP::Store->new({
+               debug => $debug,
+               module => $module,
+               path => $path,
+               clean => 1,
+       }), 'new' );
+       isa_ok( $store, 'CWMP::Store' );
 
-my $state = {
-       foo => 'bar',
-       DeviceID => {
-               SerialNumber => 123456,
-       },
-};
+       cmp_ok( $store->path, 'eq', $path, 'path' );
 
-cmp_ok( $store->ID_to_uid( 42, $state ), 'eq', 123456, 'ID_to_uid' );
+       my $state = {
+               foo => 'bar',
+               DeviceID => {
+                       SerialNumber => 123456,
+               },
+       };
 
-ok( $store->update_state( ID => 42, $state ), 'update_state new' );
+       cmp_ok( $store->ID_to_uid( 42, $state ), 'eq', 123456, 'ID_to_uid' );
 
-ok( my $store_state = $store->state( ID => '42'), 'db->get' );
+       ok( $store->update_state( ID => 42, $state ), 'update_state new' );
 
-is_deeply( $store_state, $state, 'state ID' );
+       ok( my $store_state = $store->get_state( ID => '42'), 'get_state ID' );
 
-ok( $store_state = $store->state( uid =>  123456 ), 'db->get' );
+       is_deeply( $store_state, $state, 'state ID' );
 
-is_deeply( $store_state, $state, 'state uid' );
+       ok( $store_state = $store->get_state( uid =>  123456 ), 'get_state uid' );
 
-ok( $store->update_state( ID => 42, { baz => 12345 } ), 'update_state existing' );
+       is_deeply( $store_state, $state, 'state ID same as uid' );
 
-$state->{baz} = 12345;
+       ok( $store->update_state( ID => 42, { baz => 12345 } ), 'update_state existing' );
 
-is_deeply( $store->state( ID => 42 ), $state, 'store->state ID' );
+       $state->{baz} = 12345;
 
-is_deeply( $store->state( uid => 123456 ), $state, 'store->state uid' );
+       is_deeply( $store->get_state( ID => 42 ), $state, 'get_state ID' );
 
-is_deeply( [ $store->known_CPE ], [ 123456 ], 'known_CPE' );
+       is_deeply( $store->get_state( uid => 123456 ), $state, 'get_state uid' );
 
-ok( $store->update_state( ID => 11, { DeviceID => { SerialNumber => 99999 } } ), 'new device' );
+       is_deeply( [ $store->all_uids ], [ 123456 ], 'all_uids' );
 
-is_deeply( [ $store->known_CPE ], [ 123456, 99999 ], 'known_CPE' );
+       ok( $store->update_state( ID => 11, { DeviceID => { SerialNumber => 99999 } } ), 'new device' );
+
+       is_deeply( [ $store->all_uids ], [ 123456, 99999 ], 'all_uids' );
+
+}
+
+# now test all stores
+
+test_store('DBMDeep');
+test_store('YAML');