filecluster 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +11 -0
- data/TODO +9 -0
- data/bin/fc-daemon +70 -0
- data/bin/fc-manage +102 -0
- data/bin/fc-setup-db +47 -0
- data/filecluster.gemspec +24 -0
- data/lib/daemon.rb +91 -0
- data/lib/daemon/base_thread.rb +13 -0
- data/lib/daemon/check_thread.rb +12 -0
- data/lib/daemon/global_daemon_thread.rb +60 -0
- data/lib/daemon/task_thread.rb +37 -0
- data/lib/fc/base.rb +82 -0
- data/lib/fc/db.rb +168 -0
- data/lib/fc/error.rb +12 -0
- data/lib/fc/item.rb +102 -0
- data/lib/fc/item_storage.rb +8 -0
- data/lib/fc/policy.rb +18 -0
- data/lib/fc/storage.rb +80 -0
- data/lib/fc/version.rb +3 -0
- data/lib/filecluster.rb +13 -0
- data/lib/manage.rb +4 -0
- data/lib/manage/policies.rb +70 -0
- data/lib/manage/show.rb +57 -0
- data/lib/manage/storages.rb +87 -0
- data/lib/utils.rb +76 -0
- data/test/base_test.rb +74 -0
- data/test/daemon_test.rb +98 -0
- data/test/db_test.rb +182 -0
- data/test/error_test.rb +15 -0
- data/test/functional_test.rb +97 -0
- data/test/helper.rb +17 -0
- data/test/item_test.rb +49 -0
- data/test/policy_test.rb +34 -0
- data/test/storage_test.rb +29 -0
- data/test/version_test.rb +7 -0
- metadata +199 -0
data/test/daemon_test.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'open3'
|
3
|
+
require 'timeout'
|
4
|
+
|
5
|
+
class DaemonTest < Test::Unit::TestCase
|
6
|
+
class << self
|
7
|
+
def startup
|
8
|
+
@debug = false #show stdout and sterr of fc-daemon
|
9
|
+
|
10
|
+
dir = File.expand_path(File.dirname(__FILE__))
|
11
|
+
db_config_file = dir+'/db_test.yml'
|
12
|
+
daemon_bin = File.expand_path(dir+'/../bin/fc-daemon')
|
13
|
+
|
14
|
+
File.open(db_config_file, 'w') do |f|
|
15
|
+
f.write(FC::DB.options.to_yaml)
|
16
|
+
end
|
17
|
+
|
18
|
+
@@errors_count = FC::Error.where.count
|
19
|
+
|
20
|
+
@stotage_checks = 0
|
21
|
+
Thread.new do
|
22
|
+
Open3.popen2e("#{daemon_bin} -c #{db_config_file} -l debug -t 1 -g 1 -h host1") do |stdin, stdout, t|
|
23
|
+
@@pid = t.pid
|
24
|
+
while line = stdout.readline
|
25
|
+
@stotage_checks += 1 if line =~ /Finish stotage check/i
|
26
|
+
puts line if @debug
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# tmp fake storages dirs
|
32
|
+
`rm -rf /tmp/host*-sd*`
|
33
|
+
`mkdir -p /tmp/host1-sda/ /tmp/host1-sdb/ /tmp/host1-sdc/`
|
34
|
+
|
35
|
+
# test file to copy
|
36
|
+
@@test_file_path = '/tmp/fc_test_file'
|
37
|
+
`dd if=/dev/urandom of=#{@@test_file_path} bs=1M count=1 2>&1`
|
38
|
+
|
39
|
+
@@storages = []
|
40
|
+
@@storages << FC::Storage.new(:name => 'host1-sda', :host => 'host1', :path => '/tmp/host1-sda/', :size_limit => 1000000000)
|
41
|
+
@@storages << FC::Storage.new(:name => 'host1-sdb', :host => 'host1', :path => '/tmp/host1-sdb/', :size_limit => 1000000000)
|
42
|
+
@@storages << FC::Storage.new(:name => 'host1-sdc', :host => 'host1', :path => '/tmp/host1-sdc/', :size_limit => 1000000000)
|
43
|
+
@@storages.each { |storage| storage.save}
|
44
|
+
|
45
|
+
@@policy = FC::Policy.new(:storages => 'host1-sda,host1-sdb,host1-sdc', :copies => 2)
|
46
|
+
@@policy.save
|
47
|
+
|
48
|
+
# wait for running fc-daemon
|
49
|
+
Timeout::timeout(5) do
|
50
|
+
while @stotage_checks < @@storages.size
|
51
|
+
sleep 0.1
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def shutdown
|
57
|
+
Process.kill("KILL", @@pid)
|
58
|
+
FC::DB.connect.query("DELETE FROM items_storages")
|
59
|
+
FC::DB.connect.query("DELETE FROM items")
|
60
|
+
FC::DB.connect.query("DELETE FROM policies")
|
61
|
+
FC::DB.connect.query("DELETE FROM storages")
|
62
|
+
`rm -rf /tmp/host1-sda /tmp/host1-sdb /tmp/host1-sdc`
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
should "daemon_all" do
|
67
|
+
@@storages.each {|storage| storage.reload}
|
68
|
+
assert @@storages[0].up?, "Storage #{@@storages[0].name} down"
|
69
|
+
assert @@storages[1].up?, "Storage #{@@storages[1].name} down"
|
70
|
+
|
71
|
+
FC::Storage.any_instance.stubs(:host).returns('host1')
|
72
|
+
FC::Storage.stubs(:curr_host).returns('host1')
|
73
|
+
assert_nothing_raised { @item1 = FC::Item.create_from_local(@@test_file_path, 'test1', @@policy, {:tag => 'test1'}) }
|
74
|
+
assert_nothing_raised { @item2 = FC::Item.create_from_local(@@test_file_path, 'test2', @@policy, {:tag => 'test2'}) }
|
75
|
+
assert_nothing_raised { @item3 = FC::Item.create_from_local(@@test_file_path, 'test3', @@policy, {:tag => 'test3'}) }
|
76
|
+
|
77
|
+
@@policy.copies = 3
|
78
|
+
@@policy.save
|
79
|
+
sleep 2
|
80
|
+
|
81
|
+
# wait for copy
|
82
|
+
[1, 2, 3].each do |i|
|
83
|
+
['b', 'c'].each do |j|
|
84
|
+
assert_equal `du -b /tmp/host1-sda/test$i 2>&1`.to_i, `du -b /tmp/host$i-sd$j/test$i 2>&1`.to_i
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
@@policy.copies = 2
|
89
|
+
@@policy.save
|
90
|
+
item_storage = FC::ItemStorage.where('item_id = ? AND storage_name = ?', @item1.id, 'host1-sdc').first
|
91
|
+
item_storage.status = 'delete'
|
92
|
+
item_storage.save
|
93
|
+
sleep 2
|
94
|
+
assert_equal 0, `du -b /tmp/host1-sdc/test1 2>&1`.to_i
|
95
|
+
|
96
|
+
assert_equal @@errors_count, FC::Error.where.count, "new errors in errors table"
|
97
|
+
end
|
98
|
+
end
|
data/test/db_test.rb
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class DbTest < Test::Unit::TestCase
|
4
|
+
class << self
|
5
|
+
def startup
|
6
|
+
storages = []
|
7
|
+
storages << FC::Storage.new(:name => 'rec1-sda', :host => 'rec1')
|
8
|
+
storages << FC::Storage.new(:name => 'rec1-sdb', :host => 'rec1')
|
9
|
+
storages << FC::Storage.new(:name => 'rec1-sdc', :host => 'rec1')
|
10
|
+
storages << FC::Storage.new(:name => 'rec1-sdd', :host => 'rec1')
|
11
|
+
storages << FC::Storage.new(:name => 'rec2-sda', :host => 'rec2')
|
12
|
+
storages << FC::Storage.new(:name => 'rec2-sdb', :host => 'rec2')
|
13
|
+
storages << FC::Storage.new(:name => 'rec2-sdc', :host => 'rec2')
|
14
|
+
storages << FC::Storage.new(:name => 'rec2-sdd', :host => 'rec2')
|
15
|
+
@@storages_ids = storages.map{|storage| storage.save; storage.id }
|
16
|
+
|
17
|
+
policies = []
|
18
|
+
policies << FC::Policy.new(:storages => 'rec1-sda,rec1-sdd', :copies => 2)
|
19
|
+
policies << FC::Policy.new(:storages => 'rec1-sda,bla,rec2-sdd', :copies => 3)
|
20
|
+
policies << FC::Policy.new(:storages => 'bla,rec1-sda,test', :copies => 4)
|
21
|
+
@@policies_ids = policies.map{|policy| policy.save; policy.id }
|
22
|
+
|
23
|
+
items = []
|
24
|
+
items << FC::Item.new(:name => 'test1', :policy_id => policies.first.id, :size => 150)
|
25
|
+
items << FC::Item.new(:name => 'test2', :policy_id => policies.first.id, :size => 200)
|
26
|
+
items << FC::Item.new(:name => 'test3', :policy_id => policies.first.id, :size => 400)
|
27
|
+
@@items_ids = items.map{|item| item.save; item.id }
|
28
|
+
|
29
|
+
item_storages = []
|
30
|
+
items.each do |item|
|
31
|
+
item_storages << FC::ItemStorage.new(:item_id => item.id, :storage_name => 'rec1-sda')
|
32
|
+
item_storages << FC::ItemStorage.new(:item_id => item.id, :storage_name => 'rec2-sda')
|
33
|
+
end
|
34
|
+
@@item_storages_ids = item_storages.map{|is| is.save; is.id }
|
35
|
+
end
|
36
|
+
def shutdown
|
37
|
+
FC::DB.connect.query("DELETE FROM items_storages")
|
38
|
+
FC::DB.connect.query("DELETE FROM items")
|
39
|
+
FC::DB.connect.query("DELETE FROM policies")
|
40
|
+
FC::DB.connect.query("DELETE FROM storages")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
def setup
|
44
|
+
@storages = @@storages_ids.map{|id| FC::Storage.find(id)}
|
45
|
+
@storage = @storages.first
|
46
|
+
@policies = @@policies_ids.map{|id| FC::Policy.find(id)}
|
47
|
+
@policy = @policies.first
|
48
|
+
@items = @@items_ids.map{|id| FC::Item.find(id)}
|
49
|
+
@item = @items.first
|
50
|
+
@item_storages = @@item_storages_ids.map{|id| FC::ItemStorage.find(id)}
|
51
|
+
@item_storage = @item_storages.first
|
52
|
+
@item_storage2 = @item_storages[1]
|
53
|
+
end
|
54
|
+
|
55
|
+
should "items" do
|
56
|
+
assert @items.count > 0, 'Items not loaded'
|
57
|
+
@items.each{|item| assert item.time > 0, "Item (id=#{item.id}) time = 0"}
|
58
|
+
sleep 1
|
59
|
+
time = @item.time
|
60
|
+
@item.tag = 'blabla'
|
61
|
+
@item.save
|
62
|
+
@item.reload
|
63
|
+
assert_not_equal time, @item.time, "Item (id=#{@item.id}) time not changed after save"
|
64
|
+
item2 = FC::Item.new(:name => @item.name, :policy_id => @item.policy_id)
|
65
|
+
item2.save
|
66
|
+
assert_equal 0, item2.id, "Item (id=#{item2.id}) successfull insert on uniq key"
|
67
|
+
end
|
68
|
+
|
69
|
+
should "storages" do
|
70
|
+
assert @storages.count > 0, 'Storages not loaded'
|
71
|
+
storage = FC::Storage.new(:name => 'rec1-sda', :host => 'rec1')
|
72
|
+
storage.save
|
73
|
+
assert_equal storage.id, 0, "Storage duplicate name"
|
74
|
+
end
|
75
|
+
|
76
|
+
should "policies and storages" do
|
77
|
+
assert @policies.count > 0, 'Policies not loaded'
|
78
|
+
assert_equal 'rec1-sda,rec1-sdd', @policies[0].storages, "Policy (id=#{@policies[0].id}) incorrect storages"
|
79
|
+
assert_equal 'rec1-sda,rec2-sdd', @policies[1].storages, "Policy (id=#{@policies[0].id}) incorrect storages"
|
80
|
+
assert_equal 'rec1-sda', @policies[2].storages, "Policy (id=#{@policies[0].id}) incorrect storages"
|
81
|
+
assert_raise(Mysql2::Error, 'Create policy with incorrect storages') { FC::Policy.new(:storages => 'bla,test').save }
|
82
|
+
assert_raise(Mysql2::Error, 'Change storage name with linked polices') { @storages[0].name = 'blabla'; @storages[0].save }
|
83
|
+
assert_raise(Mysql2::Error, 'Delete storage name with linked polices') { @storages[0].delete }
|
84
|
+
assert_nothing_raised { @storages[6].name = 'rec2-sdc-new'; @storages[6].save }
|
85
|
+
@storages[3].name = 'rec1-sdd-new' #rec1-sdd
|
86
|
+
@storages[3].save
|
87
|
+
@policies[0].reload
|
88
|
+
assert_equal 'rec1-sda', @policies[0].storages, "Policy (id=#{@policies[0].id}) incorrect storages after storage change"
|
89
|
+
@storages[7].delete #rec2-sdd
|
90
|
+
@@storages_ids.delete(@storages[7].id)
|
91
|
+
@policies[1].reload
|
92
|
+
assert_equal 'rec1-sda', @policies[1].storages, "Policy (id=#{@policies[1].id}) incorrect storages after storage delete"
|
93
|
+
@policies[0].storages = 'rec1-sda,rec2-sda,bla bla'
|
94
|
+
@policies[0].save
|
95
|
+
@policies[0].reload
|
96
|
+
assert_equal 'rec1-sda,rec2-sda', @policies[0].storages, "Policy (id=#{@policies[0].id}) incorrect storages after change"
|
97
|
+
assert_raise(Mysql2::Error, 'Save empty policy storage') { @policies[0].storages = 'blabla'; @policies[0].save }
|
98
|
+
end
|
99
|
+
|
100
|
+
should "item_storages doubles" do
|
101
|
+
is = FC::ItemStorage.new(:item_id => @item_storage.item_id, :storage_name => @item_storage.storage_name)
|
102
|
+
is.save
|
103
|
+
assert_equal 0, is.id, 'Item_storages successfull insert on uniq key'
|
104
|
+
end
|
105
|
+
|
106
|
+
should "item_storages times" do
|
107
|
+
assert @item_storages.count > 0, 'Item_storages not loaded'
|
108
|
+
@item_storages.each{|is| assert is.time > 0, "Item_storage (id=#{is.id}) time = 0"}
|
109
|
+
sleep 1
|
110
|
+
storage_name = @item_storage.storage_name
|
111
|
+
time = @item_storage.time
|
112
|
+
@item_storage.storage_name = 'rec1-sdc'
|
113
|
+
@item_storage.save
|
114
|
+
@item_storage.reload
|
115
|
+
assert_not_equal time, @item_storage.time, "Item_storage (id=#{@item_storage.id}) time not changed after save"
|
116
|
+
@item_storage.storage_name = storage_name
|
117
|
+
@item_storage.save
|
118
|
+
end
|
119
|
+
|
120
|
+
should "item_storages references" do
|
121
|
+
assert_raise(Mysql2::Error, "Delete item (id=#{@items[2].id} with references to item_storages") { @items[2].delete }
|
122
|
+
assert_raise(Mysql2::Error, "Delete storage (id=#{@storages[4].id} with references to item_storages") { @storages[4].delete }
|
123
|
+
end
|
124
|
+
|
125
|
+
should "item_storages copies, statuses, size" do
|
126
|
+
@items.each{|item| assert_equal 2, item.copies, "Item (id=#{item.id}) copies not inc after add item_storage"}
|
127
|
+
size_sum = @items.inject(0){|sum, item| sum+item.size}
|
128
|
+
assert_equal size_sum, @storage.size, "storage (id=#{@storage.id}) size <> ready not inc after item_storage.status='ready'"
|
129
|
+
|
130
|
+
assert_equal 'new', @item.status, "Item (id=#{@item.id}) status <> 'new'"
|
131
|
+
@item_storage.status = 'ready'
|
132
|
+
@item_storage.save
|
133
|
+
@item_storage2.status = 'ready'
|
134
|
+
@item_storage2.save
|
135
|
+
@item.reload
|
136
|
+
assert_equal 'ready', @item.status, "Item (id=#{@item.id}) status <> 'ready' not changed after item_storage.status='ready'"
|
137
|
+
@item_storage.status = 'error'
|
138
|
+
@item_storage.save
|
139
|
+
@item.reload
|
140
|
+
assert_equal 'ready', @item.status, "Item (id=#{@item.id}) status <> 'ready' changed after one of two item_storage.status='error'"
|
141
|
+
@item_storage2.status = 'error'
|
142
|
+
@item_storage2.save
|
143
|
+
@item.reload
|
144
|
+
assert_equal 'error', @item.status, "Item (id=#{@item.id}) status <> 'error' not changed after item_storage.status='error'"
|
145
|
+
assert_equal 2, @item.copies, "Item (id=#{@item.id}) copies changed after item_storage.status='error'"
|
146
|
+
|
147
|
+
@item_storage.status = 'ready'
|
148
|
+
@item_storage.save
|
149
|
+
@item.reload
|
150
|
+
assert_equal 'ready', @item.status, "Item (id=#{@item.id}) status <> 'ready' not changed after item_storage.status='ready'"
|
151
|
+
@item_storage.status = 'delete'
|
152
|
+
@item_storage.save
|
153
|
+
@item.reload
|
154
|
+
assert_equal 'error', @item.status, "Item (id=#{@item.id}) status <> 'error' not changed after item_storage.status='delete'"
|
155
|
+
assert_equal 1, @item.copies, "Item (id=#{@item.id}) copies not decreased after item_storage.status='delete'"
|
156
|
+
|
157
|
+
@item_storage.status = 'ready'
|
158
|
+
@item_storage.save
|
159
|
+
@item_storage2.status = 'ready'
|
160
|
+
@item_storage2.save
|
161
|
+
@item.reload
|
162
|
+
assert_equal 'ready', @item.status, "Item (id=#{@item.id}) status <> 'ready' not changed after delete item_storage"
|
163
|
+
@item_storage.delete
|
164
|
+
@@item_storages_ids.delete(@item_storage.id)
|
165
|
+
@item.reload
|
166
|
+
@storage.reload
|
167
|
+
assert_equal 'ready', @item.status, "Item (id=#{@item.id}) status <> 'ready' changed after delete item_storage"
|
168
|
+
size_sum -= @item.size
|
169
|
+
assert_equal size_sum, @storage.size, "storage (id=#{@storage.id}) size not dec after delete item_storage"
|
170
|
+
@item_storage2.delete
|
171
|
+
@@item_storages_ids.delete(@item_storage2.id)
|
172
|
+
@item.reload
|
173
|
+
assert_equal 'error', @item.status, "Item (id=#{@item.id}) status <> 'error' not changed after delete all item_storage"
|
174
|
+
end
|
175
|
+
|
176
|
+
should "errors time" do
|
177
|
+
error = FC::Error.new(:message => 'test error')
|
178
|
+
error.save
|
179
|
+
error.reload
|
180
|
+
assert error.time > 0, "Error (id=#{error.id}) time = 0"
|
181
|
+
end
|
182
|
+
end
|
data/test/error_test.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ErrorTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
FC::Storage.stubs(:curr_host).returns('localhost')
|
6
|
+
end
|
7
|
+
|
8
|
+
should "get_storages" do
|
9
|
+
assert_raise(RuntimeError) { FC::Error.raise 'error message', :item_id => 111 }
|
10
|
+
assert error = FC::Error.where('1 ORDER BY id desc LIMIT 1').first
|
11
|
+
assert_equal 'error message', error.message
|
12
|
+
assert_equal 'localhost', error.host
|
13
|
+
assert_equal 111, error.item_id
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class FunctionalTest < Test::Unit::TestCase
|
4
|
+
class << self
|
5
|
+
def startup
|
6
|
+
# tmp fake storages dirs
|
7
|
+
`rm -rf /tmp/host*-sd*`
|
8
|
+
`mkdir -p /tmp/host1-sda/ /tmp/host2-sda/`
|
9
|
+
|
10
|
+
# test file to copy
|
11
|
+
@@test_file_path = '/tmp/fc_test_file'
|
12
|
+
`dd if=/dev/urandom of=#{@@test_file_path} bs=100K count=1 2>&1`
|
13
|
+
|
14
|
+
@@storages = []
|
15
|
+
@@storages << FC::Storage.new(:name => 'host1-sda', :host => 'host1', :path => '/tmp/host1-sda/', :size_limit => 1000000, :check_time => Time.new.to_i)
|
16
|
+
@@storages << FC::Storage.new(:name => 'host1-sdb', :host => 'host1', :path => '/tmp/host1-sdb/', :size_limit => 1000000, :check_time => Time.new.to_i)
|
17
|
+
@@storages << FC::Storage.new(:name => 'host2-sda', :host => 'host2', :path => '/tmp/host2-sda/', :size_limit => 1000000, :check_time => Time.new.to_i)
|
18
|
+
@@storages << FC::Storage.new(:name => 'host2-sdb', :host => 'host2', :path => '/tmp/host2-sdb/', :size_limit => 1000000, :check_time => Time.new.to_i)
|
19
|
+
@@storages << FC::Storage.new(:name => 'host3-sda', :host => 'host3', :path => '/tmp/host3-sda/', :size_limit => 1000000)
|
20
|
+
@@storages.each { |storage| storage.save}
|
21
|
+
|
22
|
+
@@policies = []
|
23
|
+
@@policies << FC::Policy.new(:storages => 'host1-sda,host2-sda', :copies => 2)
|
24
|
+
@@policies << FC::Policy.new(:storages => 'host1-sdb,host2-sdb', :copies => 2)
|
25
|
+
@@policies << FC::Policy.new(:storages => 'host3-sda', :copies => 1)
|
26
|
+
@@policies << FC::Policy.new(:storages => 'host2-sda', :copies => 1)
|
27
|
+
@@policies.each { |policy| policy.save}
|
28
|
+
end
|
29
|
+
def shutdown
|
30
|
+
FC::DB.connect.query("DELETE FROM items_storages")
|
31
|
+
FC::DB.connect.query("DELETE FROM items")
|
32
|
+
FC::DB.connect.query("DELETE FROM policies")
|
33
|
+
FC::DB.connect.query("DELETE FROM storages")
|
34
|
+
`rm -rf /tmp/host1-sda /tmp/host2-sda`
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def setup
|
39
|
+
FC::Storage.any_instance.stubs(:host).returns('localhost')
|
40
|
+
FC::Storage.stubs(:curr_host).returns('localhost')
|
41
|
+
end
|
42
|
+
|
43
|
+
should "item create_from_local successful" do
|
44
|
+
assert_nothing_raised { @item = FC::Item.create_from_local(@@test_file_path, 'test1', @@policies[0], {:tag => 'test'}) }
|
45
|
+
assert_kind_of FC::Item, @item
|
46
|
+
assert_equal `du -b /tmp/host1-sda/test1 2>&1`.to_i, `du -b #{@@test_file_path} 2>&1`.to_i
|
47
|
+
assert_equal `du -b /tmp/host1-sda/test1 2>&1`.to_i, @item.size
|
48
|
+
assert_equal 'ready', @item.status
|
49
|
+
item_storages = @item.get_item_storages
|
50
|
+
assert_equal 1, item_storages.count
|
51
|
+
item_storage = item_storages.first
|
52
|
+
assert_equal 'ready', item_storage.status
|
53
|
+
assert_equal 'host1-sda', item_storage.storage_name
|
54
|
+
end
|
55
|
+
|
56
|
+
should "item create_from_local error local path" do
|
57
|
+
errors_count = FC::Error.where.count
|
58
|
+
assert_raise(RuntimeError) { FC::Item.create_from_local(@@test_file_path, 'test2', @@policies[1], {:tag => 'test'}) }
|
59
|
+
assert_equal errors_count+1, FC::Error.where.count, "Error not saved after error local path"
|
60
|
+
end
|
61
|
+
|
62
|
+
should "item create_from_local replace" do
|
63
|
+
@item = FC::Item.new(:name => 'test2', :policy_id => @@policies[0].id)
|
64
|
+
@item.save
|
65
|
+
errors_count = FC::Error.where.count
|
66
|
+
assert_raise(RuntimeError, "replace item") { FC::Item.create_from_local(@@test_file_path, 'test2', @@policies[0], {:tag => 'test'}) }
|
67
|
+
assert_equal errors_count+1, FC::Error.where.count, "Error not saved after replace item"
|
68
|
+
assert_nothing_raised { @item2 = FC::Item.create_from_local(@@test_file_path, 'test2', @@policies[0], {:replace => true, :tag => 'test'}) }
|
69
|
+
assert_equal @item.id, @item2.id, "Item (id1=#{@item.id}, id2=#{@item2.id}) change id after replace"
|
70
|
+
end
|
71
|
+
|
72
|
+
should "item create_from_local available storage" do
|
73
|
+
errors_count = FC::Error.where.count
|
74
|
+
assert_raise(RuntimeError, "available storage") { FC::Item.create_from_local(@@test_file_path, 'test3', @@policies[2], {:tag => 'test'}) }
|
75
|
+
assert_equal errors_count+1, FC::Error.where.count, "Error not saved on available storage"
|
76
|
+
end
|
77
|
+
|
78
|
+
should "item create_from_local delete item_storage" do
|
79
|
+
@item = FC::Item.new(:name => 'test4', :policy_id => @@policies[3].id)
|
80
|
+
@item.save
|
81
|
+
item_storage = FC::ItemStorage.new(:item_id => @item.id, :storage_name => 'host2-sda')
|
82
|
+
item_storage.save
|
83
|
+
assert_nothing_raised { @item = FC::Item.create_from_local(@@test_file_path, 'test4', @@policies[3], {:tag => 'test', :replace => true}) }
|
84
|
+
item_storages = @item.get_item_storages
|
85
|
+
assert_equal 1, item_storages.count
|
86
|
+
assert_not_equal item_storage.id, item_storages.first.id
|
87
|
+
assert_raise(RuntimeError) { item_storage.reload }
|
88
|
+
end
|
89
|
+
|
90
|
+
should "item create_from_local check size" do
|
91
|
+
FC::Storage.any_instance.stubs(:copy_path => true, :file_size => 10)
|
92
|
+
errors_count = FC::Error.where.count
|
93
|
+
assert_raise(RuntimeError) { FC::Item.create_from_local(@@test_file_path, 'test5', @@policies[1], {:tag => 'test'}) }
|
94
|
+
assert_equal errors_count+1, FC::Error.where.count, "Error not saved after check size"
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
$:.unshift File.expand_path('../lib', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require "test/unit"
|
4
|
+
require "shoulda-context"
|
5
|
+
require "filecluster"
|
6
|
+
require "mocha/setup"
|
7
|
+
|
8
|
+
TEST_DATABASE = 'fc_test'
|
9
|
+
TEST_USER = 'root'
|
10
|
+
TEST_PASSWORD = ''
|
11
|
+
|
12
|
+
FC::DB.connect_by_config(:username => TEST_USER, :password => TEST_PASSWORD)
|
13
|
+
FC::DB.connect.query("DROP DATABASE IF EXISTS #{TEST_DATABASE}")
|
14
|
+
FC::DB.connect.query("CREATE DATABASE #{TEST_DATABASE}")
|
15
|
+
FC::DB.connect.query("USE #{TEST_DATABASE}")
|
16
|
+
FC::DB.init_db
|
17
|
+
FC::DB.options[:database] = TEST_DATABASE
|
data/test/item_test.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ItemTest < Test::Unit::TestCase
|
4
|
+
class << self
|
5
|
+
def startup
|
6
|
+
@@item = FC::Item.new(:name => 'test item', :policy_id => 1, :size => 150)
|
7
|
+
@@item.save
|
8
|
+
|
9
|
+
@@storages = []
|
10
|
+
@@storages << FC::Storage.new(:name => 'rec1-sda', :host => 'rec1')
|
11
|
+
@@storages << FC::Storage.new(:name => 'rec2-sda', :host => 'rec2')
|
12
|
+
@@item_storages = @@storages.map do |storage|
|
13
|
+
storage.save
|
14
|
+
item_storage = FC::ItemStorage.new(:item_id => @@item.id, :storage_name => storage.name)
|
15
|
+
item_storage.save
|
16
|
+
item_storage
|
17
|
+
end
|
18
|
+
end
|
19
|
+
def shutdown
|
20
|
+
FC::DB.connect.query("DELETE FROM items_storages")
|
21
|
+
FC::DB.connect.query("DELETE FROM items")
|
22
|
+
FC::DB.connect.query("DELETE FROM storages")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
should "get_item_storages" do
|
27
|
+
assert_same_elements @@item_storages.map(&:id), @@item.get_item_storages.map(&:id)
|
28
|
+
end
|
29
|
+
|
30
|
+
should "create_from_local" do
|
31
|
+
policy = FC::Policy.new
|
32
|
+
assert_raise(ArgumentError) { FC::Item.create_from_local }
|
33
|
+
assert_raise(ArgumentError) { FC::Item.create_from_local '/bla/bla' }
|
34
|
+
assert_raise(ArgumentError) { FC::Item.create_from_local '/bla/bla', 'test' }
|
35
|
+
assert_raise(RuntimeError) { FC::Item.create_from_local '/bla/bla', 'test', {}}
|
36
|
+
assert_raise(RuntimeError) { FC::Item.create_from_local '/bla/bla/bla', 'test', policy}
|
37
|
+
end
|
38
|
+
|
39
|
+
should "mark_deleted" do
|
40
|
+
@@item.mark_deleted
|
41
|
+
@@item.reload
|
42
|
+
assert_equal 'delete', @@item.status
|
43
|
+
@@item_storages.each do |item_storage|
|
44
|
+
item_storage.reload
|
45
|
+
assert_equal 'delete', item_storage.status
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|