Implemented timezone resource to test timezone configuration values

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
This commit is contained in:
Nikita Mathur 2021-12-06 14:37:53 +05:30
parent bf1a2b1296
commit 4008af0be8
6 changed files with 135 additions and 0 deletions

View file

@ -0,0 +1,75 @@
+++
title = "timezone resource"
draft = false
gh_repo = "inspec"
platform = "linux"
[menu]
[menu.inspec]
title = "timezone"
identifier = "inspec/resources/os/timezone.md timezone resource"
parent = "inspec/resources/os"
+++
Use the `timezone` Chef InSpec audit resource to test timezone configurations of the system.
## Availability
### Installation
This resource is distributed along with Chef InSpec itself. You can use it automatically.
### Version
This resource first became available in v1.0.0 of InSpec.
## Syntax
A `timezone` resource fetches the timezone configurations of the system and compares the output with the test:
describe timezone do
its('property') { should eq 'expected value' }
end
where
- `'property'` is one of `identifier` , `abbreviation` 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('time_offset') { should eq '+0530' }
end
## Properties
### identifier
The `identifier` property returns the location identifier of the timezone.
An example of checking the **identifier** for India Time :
its('identifier') { should eq 'Asia/Kolkata }
### abbreviation
The `abbreviation` property returns the abbreviated representation of the timezone.
An example of checking the **abbreviation** of India Time :
its('abbreviation') { should eq 'IST' }
### time_offset
The `time_offset` property returns the identifier of a time offset from UTC (Coordinated Universal Time).
An example of checking the **time_offset** of India Time:
its('time_offset') { should eq '+0530' }
## Matchers
For a full list of available matchers, please visit our [matchers page](/inspec/matchers/).

View file

@ -41,6 +41,7 @@ require "inspec/resources/cassandradb_session"
require "inspec/resources/cassandradb_conf"
require "inspec/resources/cassandra"
require "inspec/resources/crontab"
require "inspec/resources/timezone"
require "inspec/resources/dh_params"
require "inspec/resources/directory"
require "inspec/resources/docker"

View file

@ -0,0 +1,44 @@
require "inspec/resources/command"
module Inspec::Resources
class TimeZone < Cmd
name "timezone"
supports platform: "unix"
desc "Check for timezone configurations"
example <<~EXAMPLE
describe timezone do
its('identifier') { should eq 'Asia/Kolkata' }
its('abbreviation') { 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'")
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(" ") || []
end
end
def identifier
@output[0]
end
def abbreviation
@output[1]
end
def time_offset
@output[2]
end
def to_s
"Time Zone resource"
end
end
end

View file

@ -0,0 +1 @@
Time zone: Asia/Kolkata (IST, +0530)

View file

@ -231,6 +231,7 @@ class MockLoader
"ps -o pid,vsz,rss,tty,stat,time,ruser,args" => cmd.call("ps-busybox"),
"env" => cmd.call("env"),
"${Env:PATH}" => cmd.call("$env-PATH"),
"timedatectl status | grep -i 'Time zone'" => cmd.call("timedatectl-timezone"),
# registry key test using winrm 2.0
"9417f24311a9dcd90f1b1734080a2d4c6516ec8ff2d452a2328f68eb0ed676cf" => cmd.call("reg_schedule"),
"Auditpol /get /subcategory:'User Account Management' /r" => cmd.call("auditpol"),

View file

@ -0,0 +1,13 @@
require "helper"
require "inspec/resource"
require "inspec/resources/timezone"
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.time_offset).must_equal "+0530"
_(resource.stderr).must_equal ""
end
end