Skip to content

Commit

Permalink
Merge pull request #1638 from sebastian-miclea/BKR-1641
Browse files Browse the repository at this point in the history
(BKR-1641) Added chmod and modified_at exec commands
  • Loading branch information
highb committed May 7, 2020
2 parents 6423e7f + a60451c commit 5416b0a
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 13 deletions.
9 changes: 9 additions & 0 deletions lib/beaker/host/mac/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ def selinux_enabled?()
false
end

# Update ModifiedDate on a file
# @param [String] file Path to the file
# @param [String] timestamp Timestamp to set
def modified_at(file, timestamp = nil)
require 'date'
time = timestamp ? DateTime.parse("#{timestamp}") : DateTime.now
timestamp = time.strftime('%Y%m%d%H%M')
execute("touch -mt #{timestamp} #{file}")
end
end
22 changes: 22 additions & 0 deletions lib/beaker/host/pswindows/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ def mv(orig, dest, rm=true)
execute("move /y #{orig} #{dest}")
end

# Update ModifiedDate on a file
# @param [String] file Path to the file
# @param [String] timestamp Timestamp to set
def modified_at(file, timestamp = nil)
require 'date'
time = timestamp ? DateTime.parse("#{timestamp}") : DateTime.now

result = execute("powershell Test-Path #{file} -PathType Leaf")

if result.include? 'False'
execute("powershell New-Item -ItemType file #{file}")
end
execute("powershell (gci #{file}).LastWriteTime = Get-Date " \
"-Year '#{time.year}'" \
"-Month '#{time.month}'" \
"-Day '#{time.day}'" \
"-Hour '#{time.hour}'" \
"-Minute '#{time.minute}'" \
"-Second '#{time.second}'"
)
end

def path
'c:/windows/system32;c:/windows'
end
Expand Down
24 changes: 17 additions & 7 deletions lib/beaker/host/unix/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Unix::Exec
include Beaker::CommandFactory

# Reboots the host, comparing uptime values to verify success
# @param [Integer] wait_time How long to wait after sending the reboot
# @param [Integer] wait_time How long to wait after sending the reboot
# command before attempting to check in on the host
# @param [Integer] max_connection_tries How many times to retry connecting to
# @param [Integer] max_connection_tries How many times to retry connecting to
# host after reboot. Note that there is an fibbonacci
# backoff when attempting retries so the time spent
# waiting on this can grow quickly.
Expand Down Expand Up @@ -40,7 +40,7 @@ def reboot(wait_time=10, max_connection_tries=9, uptime_retries=18)
current_uptime_str = parse_uptime current_uptime
current_uptime_int = uptime_int current_uptime_str
unless original_uptime_int > current_uptime_int
raise Beaker::Host::RebootFailure, "Uptime did not reset. Reboot appears to have failed."
raise Beaker::Host::RebootFailure, "Uptime did not reset. Reboot appears to have failed."
end
rescue Beaker::Host::RebootFailure => e
attempts += 1
Expand Down Expand Up @@ -89,7 +89,7 @@ def parse_uptime(uptime)
return "0 min"
end
raise "Couldn't parse uptime: #{uptime}" if result.nil?

result[1].strip.chomp(",")
end

Expand All @@ -101,6 +101,16 @@ def touch(file, abs=true)
(abs ? '/bin/touch' : 'touch') + " #{file}"
end

# Update ModifiedDate on a file
# @param [String] file Path to the file
# @param [String] timestamp Timestamp to set
def modified_at(file, timestamp = nil)
require 'date'
time = timestamp ? DateTime.parse("#{timestamp}") : DateTime.now
timestamp = time.strftime('%Y%m%d%H%M')
execute("/bin/touch -mt #{timestamp} #{file}")
end

def path
'/bin:/usr/bin'
end
Expand All @@ -117,15 +127,15 @@ def get_ip
# @param [String] dir The directory structure to create on the host
# @return [Boolean] True, if directory construction succeeded, otherwise False
def mkdir_p dir
cmd = "mkdir -p #{dir}"
cmd = "mkdir -p '#{dir}'"
result = exec(Beaker::Command.new(cmd), :acceptable_exit_codes => [0, 1])
result.exit_code == 0
end

# Recursively remove the path provided
# @param [String] path The path to remove
def rm_rf path
execute("rm -rf #{path}")
execute("rm -rf '#{path}'")
end

# Move the origin to destination. The destination is removed prior to moving.
Expand All @@ -134,7 +144,7 @@ def rm_rf path
# @param [Boolean] rm Remove the destination prior to move
def mv orig, dest, rm=true
rm_rf dest unless !rm
execute("mv #{orig} #{dest}")
execute("mv '#{orig}' '#{dest}'")
end

# Attempt to ping the provided target hostname
Expand Down
4 changes: 4 additions & 0 deletions lib/beaker/host/unix/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def chown(user, path, recursive=false)
execute("chown #{recursive ? '-R ' : ''}#{user} #{path}")
end

def chmod(mod, path, recursive=false)
execute("chmod #{recursive ? '-R ' : ''}#{mod} #{path}")
end

# Change group ownership of a path
#
# @see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/chgrp.html
Expand Down
3 changes: 3 additions & 0 deletions lib/beaker/host/windows/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def chgrp(group, path, recursive=false)
super(group, cygpath, recursive)
end

# Not needed on windows
def chmod(mod, path, recursive=false); end

# (see {Beaker::Host::Unix::File#ls_ld})
# @note Cygwin's `ls_ld` implementation does not support
# windows-, DOS-, or mixed-style paths, only UNIX/POSIX-style.
Expand Down
10 changes: 10 additions & 0 deletions spec/beaker/host/mac/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,15 @@ def to_s
expect(instance.selinux_enabled?).to be === false
end
end

describe '#modified_at' do
it 'calls execute with touch and timestamp' do
time = '190101010000'
path = '/path/to/file'
expect( instance ).to receive(:execute).with("touch -mt #{time} #{path}").and_return(0)

instance.modified_at(path, time)
end
end
end
end
32 changes: 32 additions & 0 deletions spec/beaker/host/pswindows/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ def to_s
expect( instance.mv(origin, destination, false) ).to be === 0
end
end

describe '#modified_at' do
before do
allow(instance).to receive(:execute).and_return(stdout)
end

context 'file exists' do
let(:stdout) { 'True' }
it 'sets the modified_at date' do
file = 'C:\path\to\file'
expect(instance).to receive(:execute).with("powershell Test-Path #{file} -PathType Leaf")
expect(instance).to receive(:execute).with(
"powershell (gci C:\\path\\to\\file).LastWriteTime = Get-Date -Year '1970'-Month '1'-Day '1'-Hour '0'-Minute '0'-Second '0'"
)
instance.modified_at(file, '197001010000')
end
end

context 'file does not exist' do
let(:stdout) { 'False' }
it 'creates it and sets the modified_at date' do
file = 'C:\path\to\file'
expect(instance).to receive(:execute).with("powershell Test-Path #{file} -PathType Leaf")
expect(instance).to receive(:execute).with("powershell New-Item -ItemType file #{file}")
expect(instance).to receive(:execute).with(
"powershell (gci C:\\path\\to\\file).LastWriteTime = Get-Date -Year '1970'-Month '1'-Day '1'-Hour '0'-Minute '0'-Second '0'"
)
instance.modified_at(file, '197001010000')
end
end
end

describe '#environment_string' do
let(:host) { {'pathseparator' => ':'} }

Expand Down
18 changes: 14 additions & 4 deletions spec/beaker/host/unix/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def to_s

it "deletes" do
path = '/path/to/delete'
expect( instance ).to receive(:execute).with("rm -rf #{path}").and_return(0)
expect( instance ).to receive(:execute).with("rm -rf '#{path}'").and_return(0)
expect( instance.rm_rf(path) ).to be === 0
end
end
Expand All @@ -39,18 +39,28 @@ def to_s
let(:destination) { '/destination/path/of/content' }

it 'rm first' do
expect( instance ).to receive(:execute).with("rm -rf #{destination}").and_return(0)
expect( instance ).to receive(:execute).with("mv #{origin} #{destination}").and_return(0)
expect( instance ).to receive(:execute).with("rm -rf '#{destination}'").and_return(0)
expect( instance ).to receive(:execute).with("mv '#{origin}' '#{destination}'").and_return(0)
expect( instance.mv(origin, destination) ).to be === 0

end

it 'does not rm' do
expect( instance ).to receive(:execute).with("mv #{origin} #{destination}").and_return(0)
expect( instance ).to receive(:execute).with("mv '#{origin}' '#{destination}'").and_return(0)
expect( instance.mv(origin, destination, false) ).to be === 0
end
end

describe '#modified_at' do
it 'calls execute with touch and timestamp' do
time = '190101010000'
path = '/path/to/file'
expect( instance ).to receive(:execute).with("/bin/touch -mt #{time} #{path}").and_return(0)

instance.modified_at(path, time)
end
end

describe '#environment_string' do
let(:host) { {'pathseparator' => ':'} }

Expand Down
22 changes: 22 additions & 0 deletions spec/beaker/host/unix/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ def logger
end
end

describe '#chmod' do
context 'not recursive' do
it 'calls execute with chmod' do
path = '/path/to/file'
mod = '+x'

expect( instance ).to receive(:execute).with("chmod #{mod} #{path}")
instance.chmod(mod, path)
end
end

context 'recursive' do
it 'calls execute with chmod' do
path = '/path/to/file'
mod = '+x'

expect( instance ).to receive(:execute).with("chmod -R #{mod} #{path}")
instance.chmod(mod, path, true)
end
end
end

describe '#chgrp' do
let (:group) { 'somegroup' }
let (:path) { '/path/to/chgrp/on' }
Expand Down
4 changes: 2 additions & 2 deletions spec/beaker/host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ module Beaker
allow( result ).to receive( :exit_code ).and_return( 0 )
allow( host ).to receive( :exec ).and_return( result )

expect( Beaker::Command ).to receive(:new).with("mkdir -p test/test/test")
expect( Beaker::Command ).to receive(:new).with("mkdir -p 'test/test/test'")
expect( host.mkdir_p('test/test/test') ).to be == true

end
Expand All @@ -337,7 +337,7 @@ module Beaker
allow( result ).to receive( :exit_code ).and_return( 0 )
allow( host ).to receive( :exec ).and_return( result )

expect( Beaker::Command ).to receive(:new).with("mkdir -p test/test/test")
expect( Beaker::Command ).to receive(:new).with("mkdir -p 'test/test/test'")
expect( host.mkdir_p('test/test/test') ).to be == true

end
Expand Down

0 comments on commit 5416b0a

Please sign in to comment.