summaryrefslogtreecommitdiff
path: root/test/lib/dslkeywords/mode_test.rb
blob: a4bf0991430230b688fd1468b03909a6d6196c56 (plain)
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
require 'minitest/autorun'
require 'fileutils'

require_relative '../../../lib/dsl'

class RCMModeTest < Minitest::Test
  FILE1_PATH = './.file_test1.rcmtmp'.freeze
  FILE2_PATH = './.file_test2.rcmtmp'.freeze
  SYMLINK_PATH = './.symlink_test.rcmtmp'.freeze
  SYMLINK_TARGET_PATH = './.symlink_target_test.rcmtmp'.freeze
  DIR_PATH = './.dir_test.rcmtmp'.freeze

  Minitest.after_run do
    File.unlink(FILE1_PATH) if File.file?(FILE1_PATH)
    File.unlink(FILE2_PATH) if File.file?(FILE2_PATH)
    File.unlink(SYMLINK_PATH) if File.file?(SYMLINK_PATH)
    File.unlink(SYMLINK_TARGET_PATH) if File.file?(SYMLINK_TARGET_PATH)
    FileUtils.rm_r(DIR_PATH) if File.directory?(DIR_PATH)
  end

  def test_file_mode
    configure_from_scratch do
      touch FILE1_PATH do
        mode 0o600
      end
      file FILE2_PATH do
        mode 0o644
        'content'
      end
      directory DIR_PATH do
        mode 0o705
      end

      touch SYMLINK_TARGET_PATH do
        mode 0o777
      end
      symlink SYMLINK_PATH do
        mode 0o000 # mode won't do here anything!
        requires touch SYMLINK_TARGET_PATH
        SYMLINK_TARGET_PATH
      end
    end

    assert_equal 0o600, File.stat(FILE1_PATH).mode.to_s(8).split('')[-4..-1].join.to_i(8)
    assert_equal 0o644, File.stat(FILE2_PATH).mode.to_s(8).split('')[-4..-1].join.to_i(8)
    assert_equal 0o705, File.stat(DIR_PATH).mode.to_s(8).split('')[-4..-1].join.to_i(8)
    assert_equal 0o777, File.stat(SYMLINK_TARGET_PATH).mode.to_s(8).split('')[-4..-1].join.to_i(8)
  end

  def test_chown
    configure_from_scratch do
      # Well, test only makes sense that it doesn't throw any exception, as test
      # can't change files to other owners as test will likely run as non-root.
      user_name = Etc.getlogin
      group_name = Etc.getgrgid(Process.gid).name

      touch FILE1_PATH do
        owner user_name
        group group_name
      end
      directory DIR_PATH do
        owner user_name
        group group_name
      end

      stat = File.stat(FILE1_PATH)
      assert_equal user_name, Etc.getpwuid(stat.uid)
      assert_equal group_name, Etc.getgrgid(stat.gid)

      stat = File.stat(DIR_PATH)
      assert_equal user_name, Etc.getpwuid(stat.uid)
      assert_equal group_name, Etc.getgrgid(stat.gid)
    end
  end
end