- 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/branches/store-pluggable@146
836a5e1a-633d-0410-964b-
294494ad4392
use inc::Module::Install;
name 'CWMP';
use inc::Module::Install;
name 'CWMP';
license 'GPL';
requires 'Net::Server';
requires 'HTTP::Daemon';
license 'GPL';
requires 'Net::Server';
requires 'HTTP::Daemon';
-clean_files('dump/* yaml state.db html');
+clean_files('dump/* yaml state.db html t/var/*');
my $store = CWMP::Store->new({
module => 'DBMDeep',
path => '/path/to/state.db',
my $store = CWMP::Store->new({
module => 'DBMDeep',
path => '/path/to/state.db',
confess "requed parametar module is missing" unless $self->module;
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( @_ );
$self->current_store->open( @_ );
confess "unknown store module $module not one of ", dump( $self->possible_stores ) unless $s;
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;
$self->current_store->update_uid_state( $uid, $state );
}
$self->current_store->update_uid_state( $uid, $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
Returns normal unblessed hash (actually, in-memory copy of state in database).
=cut
my $self = shift;
my ( $k, $v ) = @_;
confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
confess "need $k value" unless $v;
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 @cpe = $store->known_CPE;
+ my @cpe = $store->all_uids;
my $self = shift;
my @cpes = $self->current_store->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;
+ $store->open({
+ path => 'var/',
+ debug => 1,
+ clean => 1,
+ });
+
sub open {
my $self = shift;
sub open {
my $self = shift;
$path = "$path/state.db" if ( -d $args->{path} );
$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,
$db = DBM::Deep->new(
file => $path,
locking => 1,
+ $store->open({
+ path => 'var/',
+ debug => 1,
+ clean => 1,
+ });
+
sub open {
my $self = shift;
sub open {
my $self = shift;
if ( ! -e $path ) {
mkdir $path || die "can't create $path: $!";
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
}
=head2 update_uid_state
-use Test::More tests => 18;
+use Test::More tests => 34;
use Data::Dump qw/dump/;
use Cwd qw/abs_path/;
use lib 'lib';
use Data::Dump qw/dump/;
use Cwd qw/abs_path/;
use lib 'lib';
BEGIN {
use_ok('CWMP::Store');
use_ok('CWMP::Store::DBMDeep');
BEGIN {
use_ok('CWMP::Store');
use_ok('CWMP::Store::DBMDeep');
+ use_ok('CWMP::Store::YAML');
}
ok(my $abs_path = abs_path($0), "abs_path");
}
ok(my $abs_path = abs_path($0), "abs_path");
my $path = "$abs_path/var/";
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' );
+ ok( $store->update_state( ID => 42, { baz => 12345 } ), 'update_state existing' );
-is_deeply( $store->state( ID => 42 ), $state, 'store->state ID' );
-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');