-
Notifications
You must be signed in to change notification settings - Fork 30
Added class to wrap timedatectl command #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dbd6380
Added class to wrap timedatectl command
carbonin 6994242
Normalized time and date method names
carbonin 3d278fb
Added time date specific exception class
carbonin 1672019
Matched formatting for system_timezone and system_timezone= methods
carbonin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module LinuxAdmin | ||
class TimeDate | ||
extend Common | ||
COMMAND = 'timedatectl' | ||
|
||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this method should match what you would pass into |
||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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