More testing

This commit is contained in:
f 2022-07-02 19:26:54 -03:00
parent 5f884b10ac
commit ee92873499
6 changed files with 115 additions and 32 deletions

View file

@ -2,40 +2,29 @@
require 'rake' require 'rake'
require 'rspec/core/rake_task' require 'rspec/core/rake_task'
require 'yaml' require_relative 'spec/ansible_inventory'
require 'pry' require 'pry'
ansible_inventory = YAML.safe_load(File.read('./inventory.yml'))
ansible_groups = ansible_inventory.keys
ansible_groups.each do |group|
ansible_inventory[group]['hosts'] ||= {}
ansible_inventory[group]['hosts'].tap do |hosts|
hosts.each_pair do |host, _vars|
host_vars = File.join('host_vars', "#{host}.yml")
if File.exist? host_vars
hosts[host] = YAML.safe_load(File.read(host_vars))
else
puts "Warning: #{host_vars} doesn't exist"
end
end
end
end
task spec: 'spec:all' task spec: 'spec:all'
task default: :spec task default: :spec
AnsibleInventory.read
namespace :spec do namespace :spec do
desc 'Run serverspec' desc 'Run serverspec'
task all: (ansible_groups.map do |group| task all: (AnsibleInventory.groups.map do |group|
"#{group}:all" "#{group}:all"
end) end)
ansible_groups.each do |group| AnsibleInventory.groups.each do |group|
short_names = ansible_inventory[group]['hosts'].map do |_, vars| short_names = AnsibleInventory.inventory[group]['hosts'].map do |host, vars|
[vars['ekumen'], vars['ansible_host']] {
end.to_h vars['ekumen'] => {
host: vars['ansible_host'],
hostname: host
}
}
end.inject(&:merge) || {}
namespace group.to_sym do namespace group.to_sym do
desc "Run serverspec on #{group}" desc "Run serverspec on #{group}"
@ -44,10 +33,11 @@ namespace :spec do
short_names.each_key do |name| short_names.each_key do |name|
desc "Run serverspec on #{group}:#{name}" desc "Run serverspec on #{group}:#{name}"
RSpec::Core::RakeTask.new(name) do |t| RSpec::Core::RakeTask.new(name) do |t|
ENV['TARGET_HOST'] = short_names[name] ENV['TARGET_HOST'] = short_names[name][:host]
ENV['TARGET_HOSTNAME'] = short_names[name][:hostname]
puts "Running serverspec on #{group}:#{name}" puts "Running serverspec on #{group}:#{name}"
t.pattern = "spec/{base,#{group}}/*_spec.rb" t.pattern = "spec/{base,#{group},#{name}}/*_spec.rb"
end end
end end
end end

41
spec/ansible_inventory.rb Normal file
View file

@ -0,0 +1,41 @@
# frozen_string_literal: true
require 'yaml'
# Represents Ansible inventory
module AnsibleInventory
# @return [Hash]
def self.inventory
@@inventory ||= YAML.safe_load(File.read('./inventory.yml'))
end
# @return [Array]
def self.groups
@@groups ||= inventory.keys
end
# @return [nil]
def self.read
groups.each do |group|
inventory[group]['hosts'] ||= {}
inventory[group]['hosts'].tap do |hosts|
hosts.each_pair do |host, _vars|
hosts[host] = host_vars(host)
end
end
end
nil
end
def self.host_vars(host)
file = File.join('host_vars', "#{host}.yml")
if File.exist? file
YAML.safe_load(File.read(file))
else
puts "Warning: #{file} doesn't exist"
{}
end
end
end

View file

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'spec_helper'
require 'ansible_inventory'
host_name = ENV['TARGET_HOSTNAME']
host_vars = AnsibleInventory.host_vars host_name
describe default_gateway do
its(:ipaddress) { should eq host_vars['gateway'] }
its(:interface) { should eq 'eth0' }
end

View file

@ -2,15 +2,11 @@
require 'spec_helper' require 'spec_helper'
describe package('tinc') do describe package('docker') do
it { should be_installed } it { should be_installed }
end end
describe service('tincd') do describe service('docker') do
it { should be_enabled } it { should be_enabled }
it { should be_running } it { should be_running }
end end
describe port(65_000) do
it { should be_listening }
end

View file

@ -0,0 +1,22 @@
# frozen_string_literal: true
require 'spec_helper'
describe package('tinc') do
it { should be_installed }
end
describe service('tincd') do
it { should be_enabled }
it { should be_running }
end
describe port(65_000) do
it { should be_listening.with('tcp') }
it { should be_listening.with('udp') }
end
describe interface('ekumen') do
it { should exist }
its(:ipv6_address) { should match /\Afd00:acab::/ }
end

View file

@ -0,0 +1,22 @@
# frozen_string_literal: true
require 'spec_helper'
require 'ansible_inventory'
host_name = ENV['TARGET_HOSTNAME']
host_vars = AnsibleInventory.host_vars host_name
describe service('ipset') do
it { should be_enabled }
it { should be_running }
end
describe service('iptables') do
it { should be_enabled }
it { should be_running }
end
describe service('ip6tables') do
it { should be_enabled }
it { should be_running }
end