Added windows platform support for timezone resource

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
This commit is contained in:
Nikita Mathur 2021-12-07 18:16:44 +05:30
parent 4008af0be8
commit 4743da84f7
5 changed files with 53 additions and 17 deletions

View file

@ -33,14 +33,14 @@ A `timezone` resource fetches the timezone configurations of the system and comp
where
- `'property'` is one of `identifier` , `abbreviation` and `time_offset`
- `'property'` is one of `identifier` , `name` and `time_offset`
- `'expected value'` tests the output of the command run on the system versus the expected output stated in the test
For example:
describe timezone do
its('identifier') { should eq 'Asia/Kolkata' }
its('abbreviation') { should eq 'IST' }
its('name') { should eq 'IST' }
its('time_offset') { should eq '+0530' }
end
@ -54,13 +54,13 @@ An example of checking the **identifier** for India Time :
its('identifier') { should eq 'Asia/Kolkata }
### abbreviation
### name
The `abbreviation` property returns the abbreviated representation of the timezone.
The `name` property returns the name of the timezone.
An example of checking the **abbreviation** of India Time :
An example of checking the **name** of India Time :
its('abbreviation') { should eq 'IST' }
its('name') { should eq 'IST' }
### time_offset

View file

@ -4,41 +4,62 @@ module Inspec::Resources
class TimeZone < Cmd
name "timezone"
supports platform: "unix"
supports platform: "windows"
desc "Check for timezone configurations"
example <<~EXAMPLE
describe timezone do
its('identifier') { should eq 'Asia/Kolkata' }
its('abbreviation') { should eq 'IST' }
its('name') { should eq 'IST' }
its('time_offset') { should eq '+0530' }
its('stderr') { should eq '' }
its('exit_status') { should eq 0 }
end
EXAMPLE
def initialize
@output = []
cmd = inspec.command("timedatectl status | grep -i 'Time zone'")
@output = {}
os = inspec.os
cmd = if os.windows?
inspec.command("Get-TimeZone")
else
inspec.command("timedatectl status | grep -i 'Time zone'")
end
if cmd.exit_status != 0
raise Inspec::Exceptions::ResourceFailed, "Time Zone resource with error: #{cmd.stderr}"
else
@output = cmd.stdout.split(":")[-1]&.strip&.gsub(/[(),^]*/, "")&.split(" ") || []
if os.windows?
splitted_output = cmd.stdout.strip.gsub(/\r/, "").split("\n").select { |out| (out.include? "Id") || (out.include? "DisplayName") || (out.include? "BaseUtcOffset") }
@output["identifier"] = split_and_fetch_last(splitted_output[1])
@output["name"] = split_and_fetch_last(splitted_output[0])
@output["time_offset"] = split_and_fetch_last(splitted_output[2])
else
splitted_output = cmd.stdout.split(":")[-1]&.strip&.gsub(/[(),^]*/, "")&.split(" ") || []
@output["identifier"] = splitted_output[0]
@output["name"] = splitted_output[1]
@output["time_offset"] = splitted_output[2]
end
end
end
def identifier
@output[0]
@output["identifier"]
end
def abbreviation
@output[1]
def name
@output["name"]
end
def time_offset
@output[2]
@output["time_offset"]
end
def to_s
"Time Zone resource"
end
private
def split_and_fetch_last(string_value)
string_value.split(" :")[-1].strip
end
end
end

6
test/fixtures/cmd/get-timezone vendored Normal file
View file

@ -0,0 +1,6 @@
Id : India Standard Time
DisplayName : (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi
StandardName : India Standard Time
DaylightName : India Daylight Time
BaseUtcOffset : 05:30:00
SupportsDaylightSavingTime : False

View file

@ -265,6 +265,7 @@ class MockLoader
"Get-Package -Name 'Mozilla Firefox' | ConvertTo-Json" => cmd.call("get-package-firefox"),
"Get-Package -Name 'Ruby 2.1.6-p336-x64' | ConvertTo-Json" => cmd.call("get-package-ruby"),
'Get-Command "choco"' => empty.call,
"Get-TimeZone" => cmd.call("get-timezone"),
'sh -c \'type "choco"\'' => cmd_exit_1.call,
'(choco list --local-only --exact --include-programs --limit-output \'nssm\') -Replace "\|", "=" | ConvertFrom-StringData | ConvertTo-JSON' => cmd.call("choco-list-nssm"),
'(choco list --local-only --exact --include-programs --limit-output \'git\') -Replace "\|", "=" | ConvertFrom-StringData | ConvertTo-JSON' => empty.call,

View file

@ -6,8 +6,16 @@ describe "Inspec::Resources::TimeZone" do
it "verify time configurations" do
resource = MockLoader.new(:centos7).load_resource("timezone")
_(resource.identifier).must_equal "Asia/Kolkata"
_(resource.abbreviation).must_equal "IST"
_(resource.name).must_equal "IST"
_(resource.time_offset).must_equal "+0530"
_(resource.stderr).must_equal ""
end
it "verify time configurations" do
resource = MockLoader.new(:windows).load_resource("timezone")
_(resource.identifier).must_equal "(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi"
_(resource.name).must_equal "India Standard Time"
_(resource.time_offset).must_equal "05:30:00"
_(resource.stderr).must_equal ""
end
end