9 use MogileFS::Util qw(error_code);
11 use MogileFS::Factory;
12 use MogileFS::Factory::Domain;
13 use MogileFS::Factory::Class;
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 $domfac = MogileFS::Factory::Domain->get_factory;
27 ok($domfac, "got a domain factory");
28 my $classfac = MogileFS::Factory::Class->get_factory;
29 ok($classfac, "got a class factory");
31 # Ensure the inherited singleton is good.
32 ok($domfac != $classfac, "factories are not the same singleton");
35 # Add in a test domain.
36 my $dom = $domfac->set({ dmid => 1, namespace => 'toast'});
37 ok($dom, "made a new domain object");
38 is($dom->id, 1, "domain id is 1");
39 is($dom->name, 'toast', 'domain namespace is toast');
41 # Add in a test class.
42 my $cls = $classfac->set({ classid => 1, dmid => 1, mindevcount => 3,
43 replpolicy => '', classname => 'fried'});
44 ok($cls, "got a class object");
45 is($cls->id, 1, "class id is 1");
46 is($cls->name, 'fried', 'class name is fried');
47 is(ref($cls->domain), 'MogileFS::Domain',
48 'class can find a domain object');
51 # Add a few more classes and domains.
53 my $dom2 = $domfac->set({ dmid => 2, namespace => 'harro' });
54 $classfac->set({ classid => 1, dmid => 2, mindevcount => 2,
55 replpolicy => '', classname => 'red' });
56 $classfac->set({ classid => 2, dmid => 2, mindevcount => 3,
57 replpolicy => 'MultipleHosts(2)', classname => 'green' });
58 $classfac->set({ classid => 3, dmid => 2, mindevcount => 4,
59 replpolicy => 'MultipleHosts(5)', classname => 'blue' });
62 # Ensure the select and remove factory methods work.
64 my $dom = $domfac->get_by_id(1);
65 is($dom->name, 'toast', 'got the right domain from get_by_id');
69 my $dom = $domfac->get_by_name('harro');
70 is($dom->id, 2, 'got the right domain from get_by_name');
74 my @doms = $domfac->get_all;
75 is(scalar(@doms), 2, 'got two domains back from get_all');
77 is(ref($_), 'MogileFS::Domain', 'and both are domains');
79 isnt($doms[0]->id, $doms[1]->id, 'and both are not the same');
83 my $dom = $domfac->get_by_name('harro');
84 my $clsmap = $classfac->map_by_id($dom);
85 is(ref($clsmap), 'HASH', 'got a mapped class hash');
86 is($clsmap->{2}->name, 'green', 'got the right class set');
88 $classfac->remove($clsmap->{2});
90 my $cls = $classfac->get_by_name($dom, 'green');
91 ok(!$cls, "class removed from factory");
94 # Test the domain routines harder.
96 my $dom = $domfac->get_by_name('harro');
97 my @classes = $dom->classes;
98 # Magic "default" class is included
99 is(scalar(@classes), 3, 'found three classes');
101 ok($dom->class('blue'), 'found the blue class');
102 ok(!$dom->class('fried'), 'did not find the fried class');
105 # Test the class routines harder.
107 my $dom = $domfac->get_by_name('harro');
108 my $cls = $dom->class('blue');
109 my $polobj = $cls->repl_policy_obj;
110 ok($polobj, 'class can create policy object');
113 # Add a domain and two classes to the DB.
115 my $domid = $sto->create_domain('foo');
116 ok($domid, 'new domain stored in database: ' . $domid);
118 my $clsid1 = $sto->create_class($domid, 'bar');
119 my $clsid2 = $sto->create_class($domid, 'baz');
120 is($clsid1, 1, 'new class1 stored in database');
121 is($clsid2, 2, 'new class2 stored in database');
123 ok($sto->update_class_mindevcount(dmid => $domid, classid => $clsid2,
124 mindevcount => 3), 'can set mindevcount');
125 ok($sto->update_class_replpolicy(dmid => $domid, classid => $clsid2,
126 replpolicy => 'MultipleHosts(6)'), 'can set replpolicy');
127 ok($sto->update_class_name(dmid => $domid, classid => $clsid2,
128 classname => 'boo'), 'can rename class');
129 ok($sto->update_class_hashtype(dmid => $domid, classid => $clsid2,
130 hashtype => 1), 'can set checksum type');
131 ok($sto->update_class_hashtype(dmid => $domid, classid => $clsid2,
132 hashtype => undef), 'can unset checksum type');
136 # Reload from the DB and confirm they came back the way they went in.
137 my %domains = $sto->get_all_domains;
138 ok(exists $domains{foo}, 'domain foo exists');
139 is($domains{foo}, 1, 'and the id is 1');
140 my @classes = $sto->get_all_classes;
141 is_deeply($classes[0], {
142 'replpolicy' => undef,
145 'mindevcount' => '2',
146 'classname' => 'bar',
148 }, 'class bar came back');
149 # We edited class2 a bunch, make sure that all stuck.
150 is_deeply($classes[1], {
151 'replpolicy' => 'MultipleHosts(6)',
154 'mindevcount' => '3',
155 'classname' => 'boo',
157 }, 'class baz came back as boo');