5 use Test::More tests => 11;
9 use MogileFS::ReplicationPolicy::MultipleHosts;
15 $all_devs{$i} = MogileFS::Test::Device->new(
20 should_get_replicated_files => 0,
24 $all_devs{$i} = MogileFS::Test::Device->new(
29 should_get_replicated_files => 0,
33 $all_devs{$i} = MogileFS::Test::Device->new(
38 should_get_replicated_files => 1,
42 $all_devs{$i} = MogileFS::Test::Device->new(
47 should_get_replicated_files => 1,
51 # Now for the actual tests
56 policy_class => "MogileFS::ReplicationPolicy::MultipleHosts",
57 on_devs => [ $all_devs{4}, $all_devs{7}, $all_devs{10} ],
58 all_devs => \%all_devs,
61 ok($res->is_happy, "Expected happiness");
62 ok(!$res->too_happy, "... but not too happy");
68 policy_class => "MogileFS::ReplicationPolicy::MultipleHosts",
69 on_devs => [ $all_devs{1}, $all_devs{4}, $all_devs{7}, $all_devs{10} ],
70 all_devs => \%all_devs,
73 ok($res->is_happy, "Expected happiness");
74 ok($res->too_happy, "... and too happy too");
80 policy_class => "MogileFS::ReplicationPolicy::MultipleHosts",
81 on_devs => [ $all_devs{1}, $all_devs{2}, $all_devs{4} ],
82 all_devs => \%all_devs,
85 ok(!$res->is_happy, "Expected unhappiness");
87 my @ideals = $res->copy_to_one_of_ideally;
88 ok(@ideals, "List of ideal devices");
90 my @desperate = $res->copy_to_one_of_desperate;
91 is(@desperate, 0, "Empty list of desperate devices");
97 policy_class => "MogileFS::ReplicationPolicy::MultipleHosts",
98 on_devs => [ $all_devs{7}, $all_devs{10} ],
99 all_devs => \%all_devs,
102 ok(!$res->is_happy, "Expected unhappiness");
104 my @ideals = $res->copy_to_one_of_ideally;
105 is(@ideals, 0, "No ideal devices");
107 my @desperate = $res->copy_to_one_of_desperate;
108 ok(@desperate, "List of desperate devices");
114 policy_class => "MogileFS::ReplicationPolicy::MultipleHosts",
115 on_devs => [ $all_devs{7}, $all_devs{10}, $all_devs{11} ],
116 all_devs => \%all_devs,
119 ok($res->temp_fail, "Expected temporary failure");
126 my $fidid = delete($args{fidid}) || 1;
127 my $mindevcount = delete($args{mindevcount}) || croak "mindevcount arg required";
128 my $policy_class = delete($args{policy_class}) || croak "policy_class arg required";
129 my $on_devs = delete($args{on_devs}) || croak "on_devs arg required";
130 my $all_devs = delete($args{all_devs}) || croak "all_devs arg required";
131 my $failed = delete($args{failed}) || {};
133 eval "use $policy_class";
134 my $policy = $policy_class->new;
135 my $class = MogileFS::Test::Class->new(
136 repl_policy_obj => $policy,
137 mindevcount => $mindevcount,
139 my $devfid = MogileFS::Test::DevFID->new(
144 my $polobj = $class->repl_policy_obj;
146 return $polobj->replicate_to(
149 all_devs => $all_devs,
155 package MogileFS::Test::Device;
157 use MogileFS::DeviceState;
165 my $self = bless {}, (ref $class || $class);
167 foreach my $optkey (qw(id hostid state should_get_replicated_files percent_free)) {
168 croak "$optkey argument not supplied" unless exists $opts{$optkey};
169 $self->{$optkey} = delete $opts{$optkey};
172 croak "Extra args:" if (keys %opts);
174 $self->{dstate} = MogileFS::DeviceState->of_string($self->{state});
180 return $_[0]->{hostid};
192 return $_[0]->{dstate};
195 sub should_get_replicated_files {
196 return $_[0]->{should_get_replicated_files};
200 return $_[0]->{percent_free};
203 package MogileFS::Test::DevFID;
212 my $self = bless {}, (ref $class || $class);
214 foreach my $optkey (qw(id class)) {
215 $self->{$optkey} = delete $opts{$optkey} || die("$optkey argument not supplied");
218 die "Extra args:" if (keys %opts);
228 return $_[0]->{class};
231 package MogileFS::Test::Class;
241 my $self = bless {}, (ref $class || $class);
243 foreach my $optkey (qw(repl_policy_obj mindevcount)) {
244 $self->{$optkey} = delete $opts{$optkey} || die("$optkey argument not supplied");
247 die "Extra args:" if (keys %opts);
252 sub repl_policy_obj {
253 return $_[0]->{repl_policy_obj};
257 return $_[0]->{mindevcount};