mogilefs-client.git  about / heads / tags
MogileFS client library for Ruby
blob b619a6a6ab76a02c7be1282c85c8f84ea73daa27 4647 bytes (raw)
$ git show v3.5.0:test/test_fresh.rb	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
 
# -*- encoding: binary -*-
require "./test/fresh"

class TestMogFresh < Test::Unit::TestCase
  include TestFreshSetup
  alias setup setup_mogilefs
  alias teardown teardown_mogilefs

  def test_change_device_weight
    add_host_device_domain
    assert_equal true, @admin.change_device_weight("me", 1, 50)
    assert_equal 50, @admin.get_devices(1)[0]["weight"]
  end

  def test_list_keys_invalid_domain
    add_host_device_domain
    domain = @domain + ".non-existent"
    client = MogileFS::MogileFS.new :hosts => @hosts, :domain => domain
    assert_raises(MogileFS::Backend::UnregDomainError) do
      client.list_keys
    end
  end

  def test_invalid_key_exists
    add_host_device_domain
    domain = @domain + ".non-existent"
    client = MogileFS::MogileFS.new :hosts => @hosts, :domain => domain
    assert_raises(MogileFS::Backend::UnregDomainError) do
      client.exist?("FOO")
    end

    client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
    assert_equal false, client.exist?("non-existent")
  end

  def test_new_file_info(checksum = nil)
    add_host_device_domain unless checksum
    @client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
    info = {}
    opts = { :info => info }
    key = "new_file_info"
    content = "ZZZZ"
    if checksum
      opts[:content_md5] = [ Digest::MD5.digest(content) ].pack('m').rstrip
      opts[:class] = "check"
    end
    @client.new_file(key, opts) do |http_file|
      http_file << content
    end

    uris = info.delete(:uris)
    assert_kind_of Array, uris
    assert_equal(uris, (@client.get_uris(key) & uris))
    expect_info = @client.file_info(key, :devices => true)
    match_keys = %w(class fid key domain length)
    match_keys << "checksum" if checksum
    match_keys.each do |field|
      assert_equal expect_info.delete(field), info.delete(field)
    end
    assert_operator expect_info.delete("devcount"), :>=, info.delete("devcount")
    devids = info.delete("devids")
    assert_equal(devids, (expect_info.delete("devids") & devids))

    assert info.empty?, info.inspect
    assert expect_info.empty?, expect_info.inspect
  ensure
    @client.delete(key)
  end

  def test_new_file_info_checksum
    add_host_device_domain
    opts = @admin.get_domains[@domain]["default"]
    opts["hashtype"] = "MD5"
    @admin.create_class(@domain, "check", opts)
    yield_for_monitor_update do
      tmp = @admin.get_domains[@domain]["check"]
      if tmp
        case tmp["hashtype"]
        when "MD5"
          break
        when nil
          warn "skipping checksum test, MogileFS server too old"
          return
        else
          raise "Unhandled hashtype: #{tmp['hashtype']}"
        end
      end
    end
    test_new_file_info(:md5)
  ensure
    @admin.delete_class @domain, "check"
  end

  def test_create_open_close_opts
    add_host_device_domain
    client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
    socket = client.backend.socket
    args = {
      :create_open_args => { :hello => "world" },
      :create_close_args => { :farewell => "goodnight" },
    }
    io = client.new_file("foo", args)
    socket.write "!recent\n"
    buf = ""
    buf << socket.readpartial(666) until buf =~ /\.\r?\n\z/
    line = buf.split(/\r?\n/).grep(/\screate_open\s/)[0]
    assert_equal 0, buf.split(/\r?\n/).grep(/\screate_close\s/).size
    assert_equal 0, buf.split(/\r?\n/).grep(/\sfarewell\s/).size
    assert_match(/\bhello=world\b/, line)
    assert_equal 1, io.write('.')
    assert_nil io.close

    socket.write "!recent\n"
    buf = ""
    buf << socket.readpartial(666) until buf =~ /\.\r?\n\z/
    line = buf.split(/\r?\n/).grep(/\screate_close\s/)[0]
    assert_match(/\bfarewell=goodnight\b/, line)
  end

  def test_get_file_data_range
    add_host_device_domain
    client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
    data = "data"
    client.store_content("key", "default", data)

    assert_equal data, client.get_file_data("key")

    # ensure offset/length matches IO.copy_stream
    src = Tempfile.new("tmp")
    src.write(data)
    src.flush
    [ [1,nil], [1,2], [3,1] ].each do |range|
      dst2 = StringIO.new
      client.get_file_data("key", dst2, *range)

      src.rewind

      if IO.respond_to?(:copy_stream)
        # ensure we match IO.copy_stream semantics
        dst = StringIO.new
        IO.copy_stream(src, dst, *range)
        assert_equal dst.string, dst2.string
        assert_equal dst.string, client.get_file_data("key", nil, *range)
      end

      assert_equal dst2.string, client.get_file_data("key", nil, *range)
    end
    src.close!
  end
end

git clone https://yhbt.net/mogilefs-client.git