Mysql: fix get_paths
[ruby-mogilefs-client.git] / test / test_mysql.rb
blob9295f3838a4b3d77e7cfb04cf40347f43a705cb6
1 require 'test/setup'
2 require 'mogilefs/mysql'
4 class MogileFS::Mysql
5   public :refresh_device
6   public :refresh_domain
7 end
9 class TestMogileFS__Mysql < Test::Unit::TestCase
11   def setup
12     @my = FakeMysql.new
13     @mg = MogileFS::Mysql.new(:mysql => @my)
14     super
15   end
17   def test_refresh_device
18     expect = {
19      1=>
20       {:hostip=>"10.0.0.1",
21        :http_get_port=>7600,
22        :http_port=>7500,
23        :altip=>"192.168.0.1"},
24      2=>
25       {:hostip=>"10.0.0.2",
26        :http_get_port=>7600,
27        :http_port=>7500,
28        :altip=>"192.168.0.2"},
29      3=>
30       {:hostip=>"10.0.0.3",
31        :http_get_port=>7500,
32        :http_port=>7500,
33        :altip=>"10.0.0.3"},
34      4=>
35       {:hostip=>"10.0.0.4",
36        :http_get_port=>7500,
37        :http_port=>7500,
38        :altip=>"10.0.0.4"}
39     }
40     assert_equal expect, @mg.refresh_device
41   end
43   def test_refresh_domain
44     expect = { 'test' => 1, 'foo' => 2 }
45     assert_equal expect, @mg.refresh_domain
46   end
48   def test_get_paths
49     @my.expect << [ [ 12 ] ] # fid
50     @my.expect << [ [ 1 ], [ 3 ] ] # devids
51     expect = [ "http://10.0.0.1:7600/dev1/0/000/000/0000000012.fid",
52                "http://10.0.0.3:7500/dev3/0/000/000/0000000012.fid" ]
53     assert_equal expect, @mg._get_paths(:domain => 'test', :key => 'fookey')
54   end
56   def test_get_paths_alt
57     @my.expect <<  [ [ 12 ] ] # fid
58     @my.expect << [ [ 1 ], [ 3 ] ] # devids
59     expect = [ "http://192.168.0.1:7600/dev1/0/000/000/0000000012.fid",
60                "http://10.0.0.3:7500/dev3/0/000/000/0000000012.fid"]
61     params = { :domain => 'test', :key => 'fookey', :zone => 'alt' }
62     assert_equal expect, @mg._get_paths(params)
63   end
65   def test_list_keys
66     expect_full = [ [ 'foo', 123, 2 ], [ 'bar', 456, 1 ] ]
67     expect_keys = [ [ 'foo', 'bar' ], 'bar' ]
68     @my.expect << expect_full
69     full = []
70     keys = @mg._list_keys('test') do |dkey,length,devcount|
71       full << [ dkey, length, devcount ]
72     end
73     assert_equal expect_keys, keys
74     assert_equal expect_full, full
75   end
77   def test_list_keys_empty
78     @my.expect << []
79     assert_nil @mg._list_keys('test')
80   end
82   def test_size
83     @my.expect << [ [ '123' ] ]
84     assert_equal 123, @mg._size('test', 'foo')
86     @my.expect << [ [ '456' ] ]
87     assert_equal 456, @mg._size('test', 'foo')
88   end
90   def test_sleep
91     assert_nothing_raised { assert_equal({}, @mg.sleep(:duration => 1)) }
92   end
94 end