1 # -*- encoding: binary -*-
4 class TestMogFresh < Test::Unit::TestCase
6 alias setup setup_mogilefs
7 alias teardown teardown_mogilefs
9 def test_change_device_weight
10 add_host_device_domain
11 assert_equal true, @admin.change_device_weight("me", 1, 50)
12 assert_equal 50, @admin.get_devices(1)[0]["weight"]
15 def test_list_keys_invalid_domain
16 add_host_device_domain
17 domain = @domain + ".non-existent"
18 client = MogileFS::MogileFS.new :hosts => @hosts, :domain => domain
19 assert_raises(MogileFS::Backend::UnregDomainError) do
24 def test_invalid_key_exists
25 add_host_device_domain
26 domain = @domain + ".non-existent"
27 client = MogileFS::MogileFS.new :hosts => @hosts, :domain => domain
28 assert_raises(MogileFS::Backend::UnregDomainError) do
32 client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
33 assert_equal false, client.exist?("non-existent")
36 def test_new_file_info(checksum = nil)
37 add_host_device_domain unless checksum
38 @client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
40 opts = { :info => info }
44 opts[:content_md5] = [ Digest::MD5.digest(content) ].pack('m').rstrip
45 opts[:class] = "check"
47 rv = @client.new_file(key, opts) do |http_file|
51 uris = info.delete(:uris)
52 assert_kind_of Array, uris
53 assert_equal(uris, (@client.get_uris(key) & uris))
54 expect_info = @client.file_info(key, :devices => true)
55 match_keys = %w(class fid key domain length)
56 match_keys << "checksum" if checksum
57 match_keys.each do |field|
58 assert_equal expect_info.delete(field), info.delete(field)
60 assert_operator expect_info.delete("devcount"), :>=, info.delete("devcount")
61 devids = info.delete("devids")
62 assert_equal(devids, (expect_info.delete("devids") & devids))
64 assert info.empty?, info.inspect
65 assert expect_info.empty?, expect_info.inspect
70 def test_new_file_info_checksum
71 add_host_device_domain
72 opts = @admin.get_domains[@domain]["default"]
73 opts["hashtype"] = "MD5"
74 @admin.create_class(@domain, "check", opts)
75 yield_for_monitor_update do
76 tmp = @admin.get_domains[@domain]["check"]
82 warn "skipping checksum test, MogileFS server too old"
85 raise "Unhandled hashtype: #{tmp['hashtype']}"
89 test_new_file_info(:md5)
91 @admin.delete_class @domain, "check"
94 def test_create_open_close_opts
95 add_host_device_domain
96 client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
97 socket = client.backend.socket
99 :create_open_args => { :hello => "world" },
100 :create_close_args => { :farewell => "goodnight" },
102 io = client.new_file("foo", args)
103 socket.write "!recent\n"
105 buf << socket.readpartial(666) until buf =~ /\.\r?\n\z/
106 line = buf.split(/\r?\n/).grep(/\screate_open\s/)[0]
107 assert_equal 0, buf.split(/\r?\n/).grep(/\screate_close\s/).size
108 assert_equal 0, buf.split(/\r?\n/).grep(/\sfarewell\s/).size
109 assert_match /\bhello=world\b/, line
110 assert_equal 1, io.write('.')
113 socket.write "!recent\n"
115 buf << socket.readpartial(666) until buf =~ /\.\r?\n\z/
116 line = buf.split(/\r?\n/).grep(/\screate_close\s/)[0]
117 assert_match /\bfarewell=goodnight\b/, line