From e76bd24dcb33ab71440633303124ea99d59b7783 Mon Sep 17 00:00:00 2001 From: Enis Inan Date: Thu, 13 Dec 2018 02:10:54 -0800 Subject: (maint) Add more crontab provider tests This commit adds the following tests to the cron resource: * A test to ensure that the crontab provider writes the crontab file of a new user * A test to ensure that the crontab provider fails to write the crontab file of a nonexistent user * A test to ensure that the crontab provider writes an originally unauthorized user's crontab file _if_ Puppet authorizes them in the middle of the run These tests are part of the crontab provider's specifications and should have been added before. --- spec/spec_helper_acceptance.rb | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'spec/spec_helper_acceptance.rb') diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index e4f0182..57c5b21 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -4,6 +4,34 @@ require 'beaker/puppet_install_helper' $LOAD_PATH << File.join(__dir__, 'acceptance/lib') +# TODO: This should be added to Beaker +def assert_matching_arrays(expected, actual, message = '') + assert_equal(expected.sort, actual.sort, message) +end + +# TODO: Remove the wrappers to user_present +# and user_absent if Beaker::Host's user_present +# and user_absent functions are fixed to work with +# Arista (EOS). + +def user_present(host, username) + case host['platform'] + when %r{eos} + on(host, "useradd #{username}") + else + host.user_present(username) + end +end + +def user_absent(host, username) + case host['platform'] + when %r{eos} + on(host, "userdel #{username}", acceptable_exit_codes: [0, 1]) + else + host.user_absent(username) + end +end + def beaker_opts { debug: true, trace: true, expect_failures: true, acceptable_exit_codes: (0...256) } # { expect_failures: true, acceptable_exit_codes: (0...256) } @@ -29,6 +57,43 @@ def setup(agent, o = {}) package {'cron': name=> $cron, ensure=>present, })) end +# Returns all of the lines that correspond to crontab entries +# on the agent. For simplicity, these are any lines that do +# not begin with '#'. +def crontab_entries_of(host, username) + crontab_contents = run_cron_on(host, :list, username).stdout.strip + crontab_contents.lines.map(&:chomp).reject { |line| line =~ %r{^#} } +end + +def resource_manifest(resource, title, params = {}) + params_str = params.map { |param, value| + # This is not quite correct for all parameter values, + # but it is good enough for most purposes. + value_str = value.to_s + value_str = "\"#{value_str}\"" if value.is_a?(String) + + " #{param} => #{value_str}" + }.join(",\n") + + <<-MANIFEST + #{resource} { '#{title}': + #{params_str} +} + MANIFEST +end + +def file_manifest(path, params = {}) + resource_manifest('file', path, params) +end + +def cron_manifest(entry_name, params = {}) + resource_manifest('cron', entry_name, params) +end + +def user_manifest(username, params = {}) + resource_manifest('user', username, params) +end + RSpec.configure do |c| c.before :suite do unless ENV['BEAKER_provision'] == 'no' -- cgit v1.2.3