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(); };
21 plan skip_all => "Can't create temporary test database: $@";
25 # Fetch the factories.
26 my $hostfac = MogileFS::Factory::Host->get_factory;
27 ok($hostfac, "got a host factory");
28 my $devfac = MogileFS::Factory::Device->get_factory;
29 ok($devfac, "got a device factory");
31 MogileFS::Config->set_config_no_broadcast("min_free_space", 100);
33 # Ensure the inherited singleton is good.
34 ok($hostfac != $devfac, "factories are not the same singleton");
38 my $host = $hostfac->set({ hostid => 1, hostname => 'foo', hostip =>
39 '127.0.0.5', status => 'alive', http_port => 7500, observed_state =>
41 ok($host, 'made a new host object');
42 is($host->id, 1, 'host id is 1');
43 is($host->name, 'foo', 'host name is foo');
46 my $dev = $devfac->set({ devid => 1, hostid => 1, status => 'alive',
47 weight => 100, mb_total => 5000, mb_used => 300, mb_asof => 1295217165,
48 observed_state => 'writeable'});
49 ok($dev, 'made a new dev object');
50 is($dev->id, 1, 'dev id is 1');
51 is($dev->host->name, 'foo', 'name of devs host is foo');
52 ok($dev->can_delete_from, 'can_delete_from works');
53 ok($dev->can_read_from, 'can_read_from works');
54 ok($dev->should_get_new_files, 'should_get_new_files works');
56 # monitor needs to respect can_read_from,
57 # everything else respects should_read_from
59 foreach my $s (qw/down dead/) {
61 ok(!$host->alive, "host is not alive when $s");
62 ok(!$dev->can_read_from, "can_read_from for device fails when host is $s");
63 ok(!$dev->should_read_from, "device should not be readable when host is $s");
65 $host->{status} = "readonly";
66 ok($dev->can_read_from, "device is readable from again");
67 ok(! $dev->should_get_new_files, "device should not get new files");
69 $host->{status} = "alive";
70 ok($dev->can_read_from, "device is readable from again");
71 ok($dev->should_get_new_files, "device should get new files again");
74 # first ensure device status is respected
76 foreach my $s (qw/down dead/) {
78 ok(!$dev->should_read_from, "device is NOT readable when $s");
80 foreach my $s (qw/readonly drain alive/) {
82 ok($dev->should_read_from, "device readable when $s");
86 # take host observed states into account for should_read_from
88 $host->{observed_state} = "unreachable";
89 ok($dev->can_read_from, "device can be read from by monitor of unreachable");
90 ok(! $dev->should_read_from, "device should not be read from by non-monitor workers");
91 ok(! $dev->observed_readable, "device not readable");
92 ok(! $dev->observed_writeable, "device not writeable");
93 ok($dev->observed_unreachable, "device is unreachable");
95 $host->{observed_state} = "reachable";
96 ok($dev->should_read_from, "device is readable again by non-monitor workers");
97 ok($dev->observed_writeable, "device writable again");
98 ok(! $dev->observed_unreachable, "device is reachable again");
101 # take device observed states into account for should_read_from
103 $dev->{observed_state} = "unreachable";
104 ok(!$dev->should_read_from, "device should not be read from when observed unreachable");
105 foreach my $s (qw/readable writeable/) {
106 $dev->{observed_state} = $s;
107 ok($dev->should_read_from, "device should be read from when observed $s");
111 $hostfac->remove($host);
112 $devfac->remove($dev);
115 # Might be able to skip the factory tests, as domain/class cover those.
118 # Add a host and two devices to the DB.
119 my $hostid = $sto->create_host('foo', '127.0.0.7');
120 is($hostid, 1, 'new host got id 1');
122 # returns 1 instead of the devid :(
123 # since this it the only place which doesn't autogenerate its id.
124 ok($sto->create_device(1, $hostid, 'alive'), 'created dev1');
125 ok($sto->create_device(2, $hostid, 'down'), 'created dev2');
127 # Update host details to DB and ensure they stick.
128 ok($sto->update_host($hostid, { http_port => 6500, http_get_port => 6501 }),
129 'updated host DB entry');
130 # Update device details in DB and ensure they stick.
131 ok($sto->update_device(1, { mb_total => 150, mb_used => 8 }),
132 'updated dev1 DB entry');
133 ok($sto->update_device(2, { mb_total => 100, mb_used => 3,
134 status => 'dead' }), 'updated dev2 DB entry');
136 # Test duplication errors.
140 # Reload from DB and confirm they match what we had before.
141 my @hosts = $sto->get_all_hosts;
142 my @devs = $sto->get_all_devices;
144 is_deeply($hosts[0], {
145 'http_get_port' => 6501,
147 'http_port' => '6500',
148 'hostip' => '127.0.0.7',
153 }, 'host is as expected');
155 is_deeply($devs[0], {
163 }, 'dev1 is as expected');
164 is_deeply($devs[1], {
172 }, 'dev2 is as expected');