From 65f4766bec8f62ebd7b09c25a7a00f6827549eb2 Mon Sep 17 00:00:00 2001 From: Sonu Saha Date: Wed, 30 Mar 2022 17:26:15 +0530 Subject: [PATCH] CFINSPEC-80: Define aliased_to matcher and add unit test Signed-off-by: Sonu Saha --- lib/inspec/resources/mail_alias.rb | 13 ++++--------- test/fixtures/cmd/mail-alias | 1 + test/helpers/mock_loader.rb | 2 ++ test/unit/resources/mail_alias_test.rb | 18 ++++++++++++------ 4 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 test/fixtures/cmd/mail-alias diff --git a/lib/inspec/resources/mail_alias.rb b/lib/inspec/resources/mail_alias.rb index 9d8833d44..9c7ee3777 100644 --- a/lib/inspec/resources/mail_alias.rb +++ b/lib/inspec/resources/mail_alias.rb @@ -32,16 +32,11 @@ module Inspec::Resources "mail_alias #{resource_id}" end - def is_aliased_to?(alias_value) - # positive or negative expectations specific to this resource instance - true - end + def aliased_to?(alias_value) + cmd = inspec.command("cat /etc/aliases | grep #{@alias_key}") + raise Inspec::Exceptions::ResourceFailed, "#{@alias_key} is not a valid key in the aliases" if cmd.exit_status.to_i != 0 - private - - # Methods to help the resource's public methods - def helper_method - # Add anything you need here + alias_value == cmd.stdout.split(":").map(&:strip)[1] end end end diff --git a/test/fixtures/cmd/mail-alias b/test/fixtures/cmd/mail-alias new file mode 100644 index 000000000..05c8a3015 --- /dev/null +++ b/test/fixtures/cmd/mail-alias @@ -0,0 +1 @@ +daemon: root diff --git a/test/helpers/mock_loader.rb b/test/helpers/mock_loader.rb index 07fdf9369..e8d860b59 100644 --- a/test/helpers/mock_loader.rb +++ b/test/helpers/mock_loader.rb @@ -383,6 +383,8 @@ class MockLoader "cgget -n -r cpuset.cpus carrotking" => cmd.call("cgget-n-r"), "cgget -n -r memory.stat carrotking" => cmd.call("cgget-n-r-stat"), %{sh -c 'type "cgget"'} => empty.call, + # mail_alias + "cat /etc/aliases | grep daemon" => cmd.call("mail-alias"), # apache_conf "sh -c 'find /etc/apache2/ports.conf -type f -maxdepth 1'" => cmd.call("find-apache2-ports-conf"), "sh -c 'find /etc/httpd/conf.d/*.conf -type f -maxdepth 1'" => cmd.call("find-httpd-ssl-conf"), diff --git a/test/unit/resources/mail_alias_test.rb b/test/unit/resources/mail_alias_test.rb index 6e482d2bb..0162689b3 100644 --- a/test/unit/resources/mail_alias_test.rb +++ b/test/unit/resources/mail_alias_test.rb @@ -3,18 +3,24 @@ require "#{Inspec.src_root}/test/helper" require_relative "../../../lib/inspec/resources/mail_alias" describe Inspec::Resources::Mailalias do - it "check mail alias on ubuntu" do + it "check mail_alias on ubuntu" do resource = MockLoader.new(:ubuntu).load_resource("mail_alias", "daemon") - _(resource.is_aliased_to?("root")).must_equal true + _(resource.aliased_to?("root")).must_equal true end - it "check mail alias on macos" do + it "check mail_alias on macos" do resource = MockLoader.new(:macos10_10).load_resource("mail_alias", "daemon") - _(resource.is_aliased_to?("root")).must_equal true + _(resource.aliased_to?("root")).must_equal true end - it "check mail alias on freebsd" do + it "check mail_alias on freebsd" do resource = MockLoader.new(:freebsd11).load_resource("mail_alias", "daemon") - _(resource.is_aliased_to?("root")).must_equal true + _(resource.aliased_to?("root")).must_equal true + end + + it "check mail_alias on ubuntu with a key that is not included as an alias" do + resource = MockLoader.new(:ubuntu).load_resource("mail_alias", "cheesecake") + ex = _ { resource.aliased_to?("root") }.must_raise(Inspec::Exceptions::ResourceFailed) + _(ex.message).must_include "cheesecake is not a valid key in the aliases" end end