From f83b81670d69426928eb624aa173b96c144d3a29 Mon Sep 17 00:00:00 2001 From: Adam Leff Date: Mon, 20 Nov 2017 16:15:50 -0500 Subject: [PATCH 1/4] Add unix_mode_mask method to Train::File::Local::Unix (#215) * Add unix_mode_mask method to Train::File::Local::Unix During the refactor, the `#unix_mode_mask` method for Unix files was only added to the Remote class. It's still needed for the Local class. Signed-off-by: Adam Leff --- lib/train/file/local/unix.rb | 23 ++++++++++++++++++ test/unit/file/local/unix_test.rb | 40 ++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/lib/train/file/local/unix.rb b/lib/train/file/local/unix.rb index 0fe36ba4..6730a039 100644 --- a/lib/train/file/local/unix.rb +++ b/lib/train/file/local/unix.rb @@ -54,6 +54,16 @@ def grouped_into?(sth) group == sth end + def unix_mode_mask(owner, type) + o = UNIX_MODE_OWNERS[owner.to_sym] + return nil if o.nil? + + t = UNIX_MODE_TYPES[type.to_sym] + return nil if t.nil? + + t & o + end + private def pw_username(uid) @@ -67,6 +77,19 @@ def pw_groupname(gid) rescue ArgumentError => _ nil end + + UNIX_MODE_OWNERS = { + all: 00777, + owner: 00700, + group: 00070, + other: 00007, + }.freeze + + UNIX_MODE_TYPES = { + r: 00444, + w: 00222, + x: 00111, + }.freeze end end end diff --git a/test/unit/file/local/unix_test.rb b/test/unit/file/local/unix_test.rb index e4940856..65dbb2c3 100644 --- a/test/unit/file/local/unix_test.rb +++ b/test/unit/file/local/unix_test.rb @@ -5,7 +5,7 @@ describe Train::File::Local::Unix do let(:cls) { Train::File::Local::Unix } - + let(:backend) { backend = Train::Transports::Mock.new.connection backend.mock_os({ family: 'linux' }) @@ -86,8 +86,8 @@ def meta_stub(method, param, &block) it 'grouped_into' do meta_stub :stat, statres do connection.file(rand.to_s).grouped_into?('group').must_equal true - end - end + end + end it 'recognizes selinux label' do meta_stub :stat, statres do @@ -109,4 +109,38 @@ def meta_stub(method, param, &block) end end end + + describe '#unix_mode_mask' do + let(:file_tester) do + Class.new(cls) do + define_method :type do + :file + end + end.new(nil, nil, false) + end + + it 'check owner mode calculation' do + file_tester.unix_mode_mask('owner', 'x').must_equal 0100 + file_tester.unix_mode_mask('owner', 'w').must_equal 0200 + file_tester.unix_mode_mask('owner', 'r').must_equal 0400 + end + + it 'check group mode calculation' do + file_tester.unix_mode_mask('group', 'x').must_equal 0010 + file_tester.unix_mode_mask('group', 'w').must_equal 0020 + file_tester.unix_mode_mask('group', 'r').must_equal 0040 + end + + it 'check other mode calculation' do + file_tester.unix_mode_mask('other', 'x').must_equal 0001 + file_tester.unix_mode_mask('other', 'w').must_equal 0002 + file_tester.unix_mode_mask('other', 'r').must_equal 0004 + end + + it 'check all mode calculation' do + file_tester.unix_mode_mask('all', 'x').must_equal 0111 + file_tester.unix_mode_mask('all', 'w').must_equal 0222 + file_tester.unix_mode_mask('all', 'r').must_equal 0444 + end + end end \ No newline at end of file From 7c1121f36ee2f8a2302dd4a129dde5da81f09af3 Mon Sep 17 00:00:00 2001 From: Jonathan Hartman Date: Mon, 20 Nov 2017 13:30:07 -0800 Subject: [PATCH 2/4] Use the sanitized file path for remote linux files (#218) Had a test start failing recently and tracked it down to remote linux file resources no longer handling spaces in paths as of the refactor in 0.29.0. The test passes again after applying this change. Signed-off-by: Jonathan Hartman --- lib/train/file/remote/linux.rb | 2 +- test/integration/bootstrap.sh | 2 ++ test/integration/cookbooks/test/recipes/prep_files.rb | 4 ++++ test/integration/tests/path_file_test.rb | 7 +++++++ test/unit/file/remote/linux_test.rb | 8 +++++++- 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/train/file/remote/linux.rb b/lib/train/file/remote/linux.rb index dc93de4e..2cf64cd4 100644 --- a/lib/train/file/remote/linux.rb +++ b/lib/train/file/remote/linux.rb @@ -8,7 +8,7 @@ class Remote class Linux < Train::File::Remote::Unix def content return @content if defined?(@content) - @content = @backend.run_command("cat #{@path} || echo -n").stdout + @content = @backend.run_command("cat #{@spath} || echo -n").stdout return @content unless @content.empty? @content = nil if directory? or size.nil? or size > 0 @content diff --git a/test/integration/bootstrap.sh b/test/integration/bootstrap.sh index dfcf7f5d..1fdfaeea 100644 --- a/test/integration/bootstrap.sh +++ b/test/integration/bootstrap.sh @@ -12,6 +12,8 @@ chmod 0765 /tmp/file echo -n 'hello suid/sgid/sticky' > /tmp/sfile chmod 7765 /tmp/sfile +echo -n 'hello space' > /tmp/spaced\ file + test ! -e /tmp/pipe && \ mkfifo /tmp/pipe diff --git a/test/integration/cookbooks/test/recipes/prep_files.rb b/test/integration/cookbooks/test/recipes/prep_files.rb index ca4a9a1d..69298917 100644 --- a/test/integration/cookbooks/test/recipes/prep_files.rb +++ b/test/integration/cookbooks/test/recipes/prep_files.rb @@ -21,6 +21,10 @@ content 'hello suid/sgid/sticky' end +file '/tmp/spaced file' do + content 'hello space' +end + directory '/tmp/folder' do mode '0567' owner 'root' diff --git a/test/integration/tests/path_file_test.rb b/test/integration/tests/path_file_test.rb index c4b9ca69..8a578f3e 100644 --- a/test/integration/tests/path_file_test.rb +++ b/test/integration/tests/path_file_test.rb @@ -89,4 +89,11 @@ file.mode.must_equal(07765) end end + + describe 'regular file' do + let(:file) { backend.file('/tmp/spaced file') } + it 'has content' do + file.content.must_equal('hello space') + end + end end diff --git a/test/unit/file/remote/linux_test.rb b/test/unit/file/remote/linux_test.rb index 28993ce4..c574ca75 100644 --- a/test/unit/file/remote/linux_test.rb +++ b/test/unit/file/remote/linux_test.rb @@ -42,6 +42,12 @@ def mock_stat(args, out, err = '', code = 0) cls.new(backend, 'path').content.must_be_nil end + it 'reads file contents' do + out = rand.to_s + backend.mock_command('cat /spaced\\ path || echo -n', out) + cls.new(backend, '/spaced path').content.must_equal out + end + it 'checks for file existance' do backend.mock_command('test -e path', true) cls.new(backend, 'path').exist?.must_equal true @@ -164,4 +170,4 @@ def mock_stat(args, out, err = '', code = 0) f.selinux_label.must_equal 'labels' end end -end \ No newline at end of file +end From fdfbdd2505fb8ee8377fa58615825e50e187117a Mon Sep 17 00:00:00 2001 From: Adam Leff Date: Tue, 21 Nov 2017 12:32:31 -0500 Subject: [PATCH 3/4] Adding branches for testing Signed-off-by: Adam Leff --- .travis.yml | 1 + appveyor.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 870debcb..d8185e5a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,7 @@ before_install: branches: only: - master + - release-0.29.2 matrix: include: diff --git a/appveyor.yml b/appveyor.yml index 433fcfc8..2d214381 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -25,6 +25,7 @@ clone_depth: 1 branches: only: - master + - release-0.29.2 cache: - vendor/bundle -> appveyor.yml From 978b256204c29346408b8545defed99651786936 Mon Sep 17 00:00:00 2001 From: Adam Leff Date: Mon, 20 Nov 2017 12:08:51 -0500 Subject: [PATCH 4/4] Remove bundler install during Appveyor tests (#217) It appears that the Appveyor images changed recently and the Ruby installs for all version (2.2, 2.3, and 2.4) already include Bundler installed. Attempting to install it will hang the tests at an interactive prompt asking if you want to override the install, and --force'ing the install causes path issues and Rubygems won't be able to activate the gem. Removing the bundler install appears to solve the problem nicely. Signed-off-by: Adam Leff --- appveyor.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 2d214381..759d888e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,7 +6,6 @@ platform: environment: winrm_user: test_user winrm_pass: Pass@word1 - bundler_url: https://rubygems.org/downloads/bundler-1.9.9.gem matrix: - ruby_version: "22" @@ -41,8 +40,6 @@ install: - ps: Write-Host $env:PATH - ruby --version - gem --version - - appveyor DownloadFile -Url %bundler_url% -FileName bundler.gem - - gem install --local bundler --quiet --no-document - bundler --version - ruby -r rubygems -e "p Gem.path"