Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/linux_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
require 'linux_admin/physical_volume'
require 'linux_admin/volume_group'
require 'linux_admin/scap'
require 'linux_admin/time_date'

module LinuxAdmin
extend Common
Expand Down
31 changes: 31 additions & 0 deletions lib/linux_admin/time_date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module LinuxAdmin
class TimeDate
extend Common
COMMAND = 'timedatectl'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we want to do something like #123 (comment) for this too? @Fryguy


TimeCommandError = Class.new(StandardError)

def self.system_timezone_detailed
result = run(cmd(COMMAND), :params => ["status"])
result.output.split("\n").each do |l|
return l.split(':')[1].strip if l =~ /Time.*zone/
end
end

def self.system_timezone
system_timezone_detailed.split[0]
end

def self.system_time=(time)
run!(cmd(COMMAND), :params => ["set-time", "#{time.strftime("%F %T")}", :adjust_system_clock])
rescue AwesomeSpawn::CommandResultError => e
raise TimeCommandError, e.message
end

def self.system_timezone=(zone)
run!(cmd(COMMAND), :params => ["set-timezone", zone])
rescue AwesomeSpawn::CommandResultError => e
raise TimeCommandError, e.message
end
end
end
14 changes: 14 additions & 0 deletions spec/data/time_date/timedatectl_output
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Local time: Thu 2015-09-24 17:54:02 EDT
Universal time: Thu 2015-09-24 21:54:02 UTC
RTC time: Thu 2015-09-24 21:54:02
Time zone: America/New_York (EDT, -0400)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
Sun 2015-03-08 01:59:59 EST
Sun 2015-03-08 03:00:00 EDT
Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2015-11-01 01:59:59 EDT
Sun 2015-11-01 01:00:00 EST
74 changes: 74 additions & 0 deletions spec/time_date_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
describe LinuxAdmin::TimeDate do
RUN_COMMAND = described_class.cmd("timedatectl")

def timedatectl_result
output = File.read(Pathname.new(data_file_path("time_date/timedatectl_output")))
AwesomeSpawn::CommandResult.new("", output, "", 0)
end

describe ".system_timezone_detailed" do
it "returns the correct timezone" do
awesome_spawn_args = [
RUN_COMMAND,
:params => ["status"]
]
expect(AwesomeSpawn).to receive(:run).with(*awesome_spawn_args).and_return(timedatectl_result)
tz = described_class.system_timezone_detailed
expect(tz).to eq("America/New_York (EDT, -0400)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this method should match what you would pass into timezone=, though I can understand needing the details. Perhaps a second method timezone_detailed or something?

end
end

describe ".system_timezone" do
it "returns the correct timezone" do
awesome_spawn_args = [
RUN_COMMAND,
:params => ["status"]
]
expect(AwesomeSpawn).to receive(:run).with(*awesome_spawn_args).and_return(timedatectl_result)
tz = described_class.system_timezone
expect(tz).to eq("America/New_York")
end
end

describe ".system_time=" do
it "sets the time" do
time = Time.new(2015, 1, 1, 1, 1, 1)
awesome_spawn_args = [
RUN_COMMAND,
:params => ["set-time", "2015-01-01 01:01:01", :adjust_system_clock]
]
expect(AwesomeSpawn).to receive(:run!).with(*awesome_spawn_args)
described_class.system_time = time
end

it "raises when the command fails" do
time = Time.new(2015, 1, 1, 1, 1, 1)
err = AwesomeSpawn::CommandResultError.new("message", nil)
allow(AwesomeSpawn).to receive(:run!).and_raise(err)
expect do
described_class.send(:system_time=, time)
end.to raise_error(described_class::TimeCommandError, "message")
end
end

describe ".system_timezone=" do
it "sets the timezone" do
zone = "Location/City"
awesome_spawn_args = [
RUN_COMMAND,
:params => ["set-timezone", zone]
]
expect(AwesomeSpawn).to receive(:run!).with(*awesome_spawn_args)
described_class.system_timezone = zone
end

it "raises when the command fails" do
zone = "Location/City"
err = AwesomeSpawn::CommandResultError.new("message", nil)
allow(AwesomeSpawn).to receive(:run!).and_raise(err)
expect do
described_class.send(:system_timezone=, zone)
end.to raise_error(described_class::TimeCommandError, "message")
end
end
end