Back to Repositories

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

The test suite provides comprehensive coverage of file system operations and permission checks in Fluentd.

  • Tests file existence and writability states
  • Validates directory permission scenarios
  • Handles nested directory structures
  • Covers edge cases like non-existent paths

Implementation Analysis

The testing approach utilizes Test::Unit framework with sub_test_case blocks for organized test grouping. Each test method validates specific file system scenarios using assertions and file manipulation utilities.

  • Implements setup/teardown pattern
  • Uses FileUtils for test environment preparation
  • Employs permission modifications with File.chmod

Technical Details

  • Test::Unit as the testing framework
  • FileUtils for file system operations
  • File permission manipulation using chmod
  • Dynamic test directory path generation
  • Isolated test environment in tmp directory

Best Practices Demonstrated

The test suite exemplifies robust testing practices for file system operations.

  • Proper test isolation through directory cleanup
  • Comprehensive edge case coverage
  • Clear test case organization
  • Consistent assertion patterns
  • Descriptive test method naming

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