new_file: support create_open_args and create_close_args
[ruby-mogilefs-client.git] / test / test_fresh.rb
bloba87ebd5ba908c46db22b99fdd2ff976e1335b1ec
1 # -*- encoding: binary -*-
2 require "./test/fresh"
4 class TestMogFresh < Test::Unit::TestCase
5   include TestFreshSetup
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"]
13   end
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
20       client.list_keys
21     end
22   end
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
29       client.exist?("FOO")
30     end
32     client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
33     assert_equal false, client.exist?("non-existent")
34   end
36   def test_new_file_info(checksum = nil)
37     add_host_device_domain unless checksum
38     @client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
39     info = {}
40     opts = { :info => info }
41     key = "new_file_info"
42     content = "ZZZZ"
43     if checksum
44       opts[:content_md5] = [ Digest::MD5.digest(content) ].pack('m').rstrip
45       opts[:class] = "check"
46     end
47     rv = @client.new_file(key, opts) do |http_file|
48       http_file << content
49     end
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)
59     end
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
66   ensure
67     @client.delete(key)
68   end
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"]
77       if tmp
78         case tmp["hashtype"]
79         when "MD5"
80           break
81         when nil
82           warn "skipping checksum test, MogileFS server too old"
83           return
84         else
85           raise "Unhandled hashtype: #{tmp['hashtype']}"
86         end
87       end
88     end
89     test_new_file_info(:md5)
90   ensure
91     @admin.delete_class @domain, "check"
92   end
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
98     args = {
99       :create_open_args => { :hello => "world" },
100       :create_close_args => { :farewell => "goodnight" },
101     }
102     io = client.new_file("foo", args)
103     socket.write "!recent\n"
104     buf = ""
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('.')
111     assert_nil io.close
113     socket.write "!recent\n"
114     buf = ""
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
118   end