# # encoding: utf-8

# Inspec test for recipe irrigate-backup::default

# The Inspec reference, with examples and extensive documentation, can be
# found at http://inspec.io/docs/reference/resources/

node_dir = '/tmp/kitchen/nodes'
instance = command("ls #{node_dir}").stdout.strip
node_json = "#{node_dir}/#{instance}"
node = json(node_json).params

describe firewalld do
  it { should be_running }
  it { should have_service_enabled_in_zone('ssh', 'public') }
end

# Required packages
packages = %w(
  git
  openssl
  ruby-shadow
)

# Install packages
packages.each do |name|
  describe package(name) do
    it { should be_installed }
  end
end

backup_user    = node['default']['backup']['backup_user']
home_directory = node['default']['backup']['backup_user_home_directory']
uid            = node['default']['backup']['uid']

describe user(backup_user) do
  it { should exist }
  its('uid') { should eq uid }
  its('home') { should eq home_directory }
  its('shell') { should eq '/bin/bash' }
end

describe sshd_config do
  its('Port') { should cmp 22 }
  its('PasswordAuthentication') { should eq 'yes' }
end

describe file("#{home_directory}/.aws/credentials") do
  it { should exist }
  its('owner') { should eq backup_user }
  its('mode') { should cmp '0640' }
end

describe crontab(backup_user) do
  its('commands') { should cmp "find /home/backup_user -type f -name '*.tar.gz' -mtime +7 -exec rm -rf {} \\;" }
  its('hours') { should cmp '0' }
  its('minutes') { should cmp '0' }
end
