From dbd6380dd7e06a7dd7c5a4d720128aab2f112e6f Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Thu, 24 Sep 2015 18:17:24 -0400 Subject: [PATCH 1/4] Added class to wrap timedatectl command --- lib/linux_admin.rb | 1 + lib/linux_admin/time_date.rb | 21 ++++++++++++ spec/data/time_date/timedatectl_output | 14 ++++++++ spec/time_date_spec.rb | 45 ++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 lib/linux_admin/time_date.rb create mode 100644 spec/data/time_date/timedatectl_output create mode 100644 spec/time_date_spec.rb diff --git a/lib/linux_admin.rb b/lib/linux_admin.rb index 0c78169..40b4b4c 100644 --- a/lib/linux_admin.rb +++ b/lib/linux_admin.rb @@ -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 diff --git a/lib/linux_admin/time_date.rb b/lib/linux_admin/time_date.rb new file mode 100644 index 0000000..7e7e5d7 --- /dev/null +++ b/lib/linux_admin/time_date.rb @@ -0,0 +1,21 @@ +module LinuxAdmin + class TimeDate + extend Common + COMMAND = 'timedatectl' + + def self.timezone + 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.set_system_time(time) + run!(cmd(COMMAND), :params => ["set-time", "#{time.strftime("%F %T")}", :adjust_system_clock]) + end + + def self.set_system_timezone(location, city) + run!(cmd(COMMAND), :params => ["set-timezone", "#{location}/#{city}"]) + end + end +end diff --git a/spec/data/time_date/timedatectl_output b/spec/data/time_date/timedatectl_output new file mode 100644 index 0000000..34f09e2 --- /dev/null +++ b/spec/data/time_date/timedatectl_output @@ -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 diff --git a/spec/time_date_spec.rb b/spec/time_date_spec.rb new file mode 100644 index 0000000..a06d62e --- /dev/null +++ b/spec/time_date_spec.rb @@ -0,0 +1,45 @@ +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 ".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.timezone + expect(tz).to eq("America/New_York (EDT, -0400)") + end + end + + describe ".set_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.set_system_time(time) + end + end + + describe ".set_system_timezone" do + it "sets the timezone" do + loc = "Location" + city = "City" + awesome_spawn_args = [ + RUN_COMMAND, + :params => ["set-timezone", "#{loc}/#{city}"] + ] + expect(AwesomeSpawn).to receive(:run!).with(*awesome_spawn_args) + described_class.set_system_timezone(loc, city) + end + end +end From 6994242167947cb8c07e9726d6221aba09282922 Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Fri, 25 Sep 2015 15:28:47 -0400 Subject: [PATCH 2/4] Normalized time and date method names --- lib/linux_admin/time_date.rb | 8 ++++---- spec/time_date_spec.rb | 17 ++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/linux_admin/time_date.rb b/lib/linux_admin/time_date.rb index 7e7e5d7..1329270 100644 --- a/lib/linux_admin/time_date.rb +++ b/lib/linux_admin/time_date.rb @@ -3,19 +3,19 @@ class TimeDate extend Common COMMAND = 'timedatectl' - def self.timezone + def self.system_timezone 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.set_system_time(time) + def self.system_time=(time) run!(cmd(COMMAND), :params => ["set-time", "#{time.strftime("%F %T")}", :adjust_system_clock]) end - def self.set_system_timezone(location, city) - run!(cmd(COMMAND), :params => ["set-timezone", "#{location}/#{city}"]) + def self.system_timezone=(zone) + run!(cmd(COMMAND), :params => ["set-timezone", zone]) end end end diff --git a/spec/time_date_spec.rb b/spec/time_date_spec.rb index a06d62e..0474150 100644 --- a/spec/time_date_spec.rb +++ b/spec/time_date_spec.rb @@ -6,19 +6,19 @@ def timedatectl_result AwesomeSpawn::CommandResult.new("", output, "", 0) end - describe ".timezone" do + 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.timezone + tz = described_class.system_timezone expect(tz).to eq("America/New_York (EDT, -0400)") end end - describe ".set_system_time" do + describe ".system_time=" do it "sets the time" do time = Time.new(2015, 1, 1, 1, 1, 1) awesome_spawn_args = [ @@ -26,20 +26,19 @@ def timedatectl_result :params => ["set-time", "2015-01-01 01:01:01", :adjust_system_clock] ] expect(AwesomeSpawn).to receive(:run!).with(*awesome_spawn_args) - described_class.set_system_time(time) + described_class.system_time = time end end - describe ".set_system_timezone" do + describe ".system_timezone" do it "sets the timezone" do - loc = "Location" - city = "City" + zone = "Location/City" awesome_spawn_args = [ RUN_COMMAND, - :params => ["set-timezone", "#{loc}/#{city}"] + :params => ["set-timezone", zone] ] expect(AwesomeSpawn).to receive(:run!).with(*awesome_spawn_args) - described_class.set_system_timezone(loc, city) + described_class.system_timezone = zone end end end From 3d278fb24d4079cd21e05bd7ffdcdc9a5d5920e7 Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Fri, 25 Sep 2015 17:14:10 -0400 Subject: [PATCH 3/4] Added time date specific exception class --- lib/linux_admin/time_date.rb | 6 ++++++ spec/time_date_spec.rb | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/linux_admin/time_date.rb b/lib/linux_admin/time_date.rb index 1329270..55300ce 100644 --- a/lib/linux_admin/time_date.rb +++ b/lib/linux_admin/time_date.rb @@ -3,6 +3,8 @@ class TimeDate extend Common COMMAND = 'timedatectl' + TimeCommandError = Class.new(StandardError) + def self.system_timezone result = run(cmd(COMMAND), :params => ["status"]) result.output.split("\n").each do |l| @@ -12,10 +14,14 @@ def self.system_timezone 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 diff --git a/spec/time_date_spec.rb b/spec/time_date_spec.rb index 0474150..dd22d1a 100644 --- a/spec/time_date_spec.rb +++ b/spec/time_date_spec.rb @@ -28,9 +28,18 @@ def timedatectl_result 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 + describe ".system_timezone=" do it "sets the timezone" do zone = "Location/City" awesome_spawn_args = [ @@ -40,5 +49,14 @@ def timedatectl_result 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 From 16720197a240c51667171cfe28476c33b54094cf Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Mon, 28 Sep 2015 10:01:12 -0400 Subject: [PATCH 4/4] Matched formatting for system_timezone and system_timezone= methods Now the format returned by system_timezone is the same as is taken by system_timezone= The full string is still returned by the new system_timezone_detailed method. --- lib/linux_admin/time_date.rb | 6 +++++- spec/time_date_spec.rb | 14 +++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/linux_admin/time_date.rb b/lib/linux_admin/time_date.rb index 55300ce..487d46b 100644 --- a/lib/linux_admin/time_date.rb +++ b/lib/linux_admin/time_date.rb @@ -5,13 +5,17 @@ class TimeDate TimeCommandError = Class.new(StandardError) - def self.system_timezone + 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 diff --git a/spec/time_date_spec.rb b/spec/time_date_spec.rb index dd22d1a..f8d39f7 100644 --- a/spec/time_date_spec.rb +++ b/spec/time_date_spec.rb @@ -6,6 +6,18 @@ def timedatectl_result 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)") + end + end + describe ".system_timezone" do it "returns the correct timezone" do awesome_spawn_args = [ @@ -14,7 +26,7 @@ def timedatectl_result ] 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 (EDT, -0400)") + expect(tz).to eq("America/New_York") end end