Testing File System Writability Operations in Fluentd
This test suite validates file utility functions in Fluentd, focusing on file and directory writability checks. It thoroughly tests the writable? and writable_p? methods across various file system scenarios and permission configurations.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
fluent/fluentd
test/plugin/test_file_util.rb
require_relative '../helper'
require 'fluent/plugin/file_util'
require 'fileutils'
class FileUtilTest < Test::Unit::TestCase
def setup
FileUtils.rm_rf(TEST_DIR)
FileUtils.mkdir_p(TEST_DIR)
end
TEST_DIR = File.expand_path(File.dirname(__FILE__) + "/../tmp/file_util")
sub_test_case 'writable?' do
test 'file exists and writable' do
FileUtils.touch("#{TEST_DIR}/test_file")
assert_true Fluent::FileUtil.writable?("#{TEST_DIR}/test_file")
end
test 'file exists and not writable' do
FileUtils.touch("#{TEST_DIR}/test_file")
File.chmod(0444, "#{TEST_DIR}/test_file")
assert_false Fluent::FileUtil.writable?("#{TEST_DIR}/test_file")
end
test 'directory exists' do
FileUtils.mkdir_p("#{TEST_DIR}/test_dir")
assert_false Fluent::FileUtil.writable?("#{TEST_DIR}/test_dir")
end
test 'file does not exist and parent directory is writable' do
FileUtils.mkdir_p("#{TEST_DIR}/test_dir")
assert_true Fluent::FileUtil.writable?("#{TEST_DIR}/test_dir/test_file")
end
test 'file does not exist and parent directory is not writable' do
FileUtils.mkdir_p("#{TEST_DIR}/test_dir")
File.chmod(0444, "#{TEST_DIR}/test_dir")
assert_false Fluent::FileUtil.writable?("#{TEST_DIR}/test_dir/test_file")
end
test 'parent directory does not exist' do
FileUtils.rm_rf("#{TEST_DIR}/test_dir")
assert_false Fluent::FileUtil.writable?("#{TEST_DIR}/test_dir/test_file")
end
test 'parent file (not directory) exists' do
FileUtils.touch("#{TEST_DIR}/test_file")
assert_false Fluent::FileUtil.writable?("#{TEST_DIR}/test_file/foo")
end
end
sub_test_case 'writable_p?' do
test 'file exists and writable' do
FileUtils.touch("#{TEST_DIR}/test_file")
assert_true Fluent::FileUtil.writable_p?("#{TEST_DIR}/test_file")
end
test 'file exists and not writable' do
FileUtils.touch("#{TEST_DIR}/test_file")
File.chmod(0444, "#{TEST_DIR}/test_file")
assert_false Fluent::FileUtil.writable_p?("#{TEST_DIR}/test_file")
end
test 'directory exists' do
FileUtils.mkdir_p("#{TEST_DIR}/test_dir")
assert_false Fluent::FileUtil.writable_p?("#{TEST_DIR}/test_dir")
end
test 'parent directory exists and writable' do
FileUtils.mkdir_p("#{TEST_DIR}/test_dir")
assert_true Fluent::FileUtil.writable_p?("#{TEST_DIR}/test_dir/test_file")
end
test 'parent directory exists and not writable' do
FileUtils.mkdir_p("#{TEST_DIR}/test_dir")
File.chmod(0555, "#{TEST_DIR}/test_dir")
assert_false Fluent::FileUtil.writable_p?("#{TEST_DIR}/test_dir/test_file")
end
test 'parent of parent (of parent ...) directory exists and writable' do
FileUtils.mkdir_p("#{TEST_DIR}/test_dir")
assert_true Fluent::FileUtil.writable_p?("#{TEST_DIR}/test_dir/foo/bar/baz")
end
test 'parent of parent (of parent ...) directory exists and not writable' do
FileUtils.mkdir_p("#{TEST_DIR}/test_dir")
File.chmod(0555, "#{TEST_DIR}/test_dir")
assert_false Fluent::FileUtil.writable_p?("#{TEST_DIR}/test_dir/foo/bar/baz")
end
test 'parent of parent (of parent ...) file (not directory) exists' do
FileUtils.touch("#{TEST_DIR}/test_file")
assert_false Fluent::FileUtil.writable_p?("#{TEST_DIR}/test_file/foo/bar/baz")
end
end
end