【问题标题】:How to unit test a "disk full" scenario with Ruby RSpec?如何使用 Ruby RSpec 对“磁盘已满”场景进行单元测试?
【发布时间】:2017-03-30 12:01:30
【问题描述】:

我需要对如下场景进行单元测试:

磁盘有 1MB 可用空间。我尝试将 2MB 的文件复制到磁盘。

使用 Ruby RSpec 执行此操作的最佳方法是什么?

如需更多信息,我需要对the following file cache method 进行单元测试,因为它似乎有一些问题:

def set first_key, second_key='', files=[]
  # If cache exists already, overwrite it.
  content_dir = get first_key, second_key
  second_key_file = nil

  begin
    if (content_dir.nil?)

      # Check the size of cache, and evict entries if too large
      check_cache_size if (rand(100) < check_size_percent)

      # Make sure cache dir doesn't exist already
      first_cache_dir = File.join(dir, first_key)
      if (File.exist?first_cache_dir)
        raise "BuildCache directory #{first_cache_dir} should be a directory" unless File.directory?(first_cache_dir)
      else
        FileUtils.mkpath(first_cache_dir)
      end
      num_second_dirs = Dir[first_cache_dir + '/*'].length
      cache_dir = File.join(first_cache_dir, num_second_dirs.to_s)
      # If cache directory already exists, then a directory must have been evicted here, so we pick another name
      while File.directory?cache_dir
        cache_dir = File.join(first_cache_dir, rand(num_second_dirs).to_s)
      end
      content_dir = File.join(cache_dir, '/content')
      FileUtils.mkpath(content_dir)

      # Create 'last_used' file
      last_used_filename = File.join(cache_dir, 'last_used')
      FileUtils.touch last_used_filename
      FileUtils.chmod(permissions, last_used_filename)

      # Copy second key
      second_key_file = File.open(cache_dir + '/second_key', 'w+')
      second_key_file.flock(File::LOCK_EX)
      second_key_file.write(second_key)

    else
      log "overwriting cache #{content_dir}"

      FileUtils.touch content_dir + '/../last_used'
      second_key_file = File.open(content_dir + '/../second_key', 'r')
      second_key_file.flock(File::LOCK_EX)
      # Clear any existing files out of cache directory
      FileUtils.rm_rf(content_dir + '/.')
    end

    # Copy files into content_dir
    files.each do |filename|
      FileUtils.cp(filename, content_dir)
    end
    FileUtils.chmod(permissions, Dir[content_dir + '/*'])

    # Release the lock
    second_key_file.close
    return content_dir
  rescue => e
    # Something went wrong, like a full disk or some other error.
    # Delete any work so we don't leave cache in corrupted state
    unless content_dir.nil?
      # Delete parent of content directory
      FileUtils.rm_rf(File.expand_path('..', content_dir))
    end
    log "ERROR: Could not set cache entry. #{e.to_s}"
    return 'ERROR: !NOT CACHED!'
  end

end

【问题讨论】:

    标签: ruby unit-testing rspec rubygems


    【解决方案1】:

    一种解决方案是存根写入磁盘以引发错误的方法。例如,对于测试磁盘空间错误的规范,您可以尝试:

    before do
      allow_any_instance_of(File).to receive(:open) { raise Errno::ENOSPC }
      # or maybe # allow(File).to receive(:write) { raise Errno::ENOSPC }
      # or       # allow(FileUtils).to receive(:cp) { raise Errno::ENOSPC }
      # or some combination of these 3...
    end
    
    it 'handles an out of disk space error' do
      expect{ my_disk_cache.set('key1', 'key2', [...]) }.to # your logic for how BuildCache::DiskCache should handle the error here.
    end
    

    这有两个问题:

    1) Errno::ENOSPC 可能不是您实际看到的错误。该错误符合您问题中的描述,但根据您的库的特性及其运行的系统,您可能不会真正收到Errno::ENOSPC 错误。也许你首先用完了 RAM 并得到Errno::ENOMEM,或者你打开了太多文件描述符并得到Errno::EMFILE。当然,如果你想严谨一点,你可以处理所有这些,但这很费时间,而且你会因为处理更模糊的错误而获得收益递减。

    See this 了解有关 Errno 错误的更多信息。

    2) 此解决方案涉及在特定类上存根特定方法。 (File.open) 这并不理想,因为它将测试设置与代码中的实现结合在一起。也就是说,如果您将BuildCache::DiskCache#set 重构为不使用File.open,那么即使方法可能正确,该测试也可能开始失败。

    也就是说,File.open 的级别相当低。我知道一些FileUtils 方法使用File.open,(特别是FileUtils.cp)所以我建议只使用第一行allow_any_instance_of。我希望它能够处理您的大部分用例。

    或者,有一个名为fakefs 的工具可以帮助您解决这个问题。我不熟悉它,但它很可能具有有助于测试此类错误的功能。你可能想调查一下。

    【讨论】:

    • 不幸的是,据我所知,还没有现成的磁盘 IO 存根工具。有一个名为 webmock 的工具用于处理同样的基本问题,但使用的是 web IO……我搜索了一下,但我自己没有找到任何东西。
    • 顺便说一下,有一个名为fakefs 的工具可能可以帮助您。我会检查一下。编辑了我的答案以包含它。
    【解决方案2】:

    您可以使用您知道在您需要测试的方法内部发生的任何方法调用,并将它们存根,以便它们引发错误。例如。 FileUtils.touch 被调用了很多次,所以我们可以这样做:

    it 'handles file write error gracefully' do
      allow(FileUtils).to receive(:touch).and_raise('oh no')
      # your expectations
      # your test trigger
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多