9 use MogileFS::Util qw(error_code);
11 use MogileFS::Factory;
12 use MogileFS::Factory::Host;
13 use MogileFS::Factory::Device;
17 use Data::Dumper qw/Dumper/;
19 my $sto = eval { temp_store(); };
23 plan skip_all => "Can't create temporary test database: $@";
27 # Fetch the factories.
28 my $hostfac = MogileFS::Factory::Host->get_factory;
29 ok($hostfac, "got a host factory");
30 my $devfac = MogileFS::Factory::Device->get_factory;
31 ok($devfac, "got a device factory");
33 MogileFS::Config->set_config_no_broadcast("min_free_space", 100);
35 # Ensure the inherited singleton is good.
36 ok($hostfac != $devfac, "factories are not the same singleton");
40 my $host = $hostfac->set({ hostid => 1, hostname => 'foo', hostip =>
41 '127.0.0.5', status => 'alive', http_port => 7500, observed_state =>
43 ok($host, 'made a new host object');
44 is($host->id, 1, 'host id is 1');
45 is($host->name, 'foo', 'host name is foo');
48 my $dev = $devfac->set({ devid => 1, hostid => 1, status => 'alive',
49 weight => 100, mb_total => 5000, mb_used => 300, mb_asof => 1295217165,
50 observed_state => 'writeable'});
51 ok($dev, 'made a new dev object');
52 is($dev->id, 1, 'dev id is 1');
53 is($dev->host->name, 'foo', 'name of devs host is foo');
54 ok($dev->can_delete_from, 'can_delete_from works');
55 ok($dev->can_read_from, 'can_read_from works');
56 ok($dev->should_get_new_files, 'should_get_new_files works');
58 $hostfac->remove($host);
59 $devfac->remove($dev);
62 # Might be able to skip the factory tests, as domain/class cover those.
65 # Add a host and two devices to the DB.
66 my $hostid = $sto->create_host('foo', '127.0.0.7');
67 is($hostid, 1, 'new host got id 1');
69 # returns 1 instead of the devid :(
70 # since this it the only place which doesn't autogenerate its id.
71 ok($sto->create_device(1, $hostid, 'alive'), 'created dev1');
72 ok($sto->create_device(2, $hostid, 'down'), 'created dev2');
74 # Update host details to DB and ensure they stick.
75 ok($sto->update_host($hostid, { http_port => 6500, http_get_port => 6501 }),
76 'updated host DB entry');
77 # Update device details in DB and ensure they stick.
78 ok($sto->update_device(1, { mb_total => 150, mb_used => 8 }),
79 'updated dev1 DB entry');
80 ok($sto->update_device(2, { mb_total => 100, mb_used => 3,
81 status => 'dead' }), 'updated dev2 DB entry');
83 # Test duplication errors.
87 # Reload from DB and confirm they match what we had before.
88 my @hosts = $sto->get_all_hosts;
89 my @devs = $sto->get_all_devices;
91 is_deeply($hosts[0], {
92 'http_get_port' => 6501,
94 'http_port' => '6500',
95 'hostip' => '127.0.0.7',
100 }, 'host is as expected');
102 is_deeply($devs[0], {
110 }, 'dev1 is as expected');
111 is_deeply($devs[1], {
119 }, 'dev2 is as expected');