From a24e814a1a073e0e952820d1e8ca8a25e35992e4 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 6 Oct 2022 16:11:49 +0530 Subject: [PATCH] feat: try serverspec DESCRIPTION Used local runner to perform the following tasks on my Arch Linux dev machine 1. Check if apache is installed on arch 2. Check if httpd service is enabled and running 3. Check if port 80 is listening COMMENTS Limited capabilities but seems to have support for basic testing on mulitple environments: local, SSH and Docker API using multiple configuration management tools like Puppet and Ansible. REFERENCES [0]: https://serverspec.org/tutorial.html [1]: https://serverspec.org/resource_types.html --- sandbox/serverspec/getting-started/.rspec | 2 + sandbox/serverspec/getting-started/Gemfile | 1 + sandbox/serverspec/getting-started/Rakefile | 27 ++++++++++++++ .../spec/localhost/sample_spec.rb | 37 +++++++++++++++++++ .../getting-started/spec/spec_helper.rb | 4 ++ 5 files changed, 71 insertions(+) create mode 100644 sandbox/serverspec/getting-started/.rspec create mode 100644 sandbox/serverspec/getting-started/Gemfile create mode 100644 sandbox/serverspec/getting-started/Rakefile create mode 100644 sandbox/serverspec/getting-started/spec/localhost/sample_spec.rb create mode 100644 sandbox/serverspec/getting-started/spec/spec_helper.rb diff --git a/sandbox/serverspec/getting-started/.rspec b/sandbox/serverspec/getting-started/.rspec new file mode 100644 index 0000000..16f9cdb --- /dev/null +++ b/sandbox/serverspec/getting-started/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/sandbox/serverspec/getting-started/Gemfile b/sandbox/serverspec/getting-started/Gemfile new file mode 100644 index 0000000..9b288d0 --- /dev/null +++ b/sandbox/serverspec/getting-started/Gemfile @@ -0,0 +1 @@ +gem 'serverspec' diff --git a/sandbox/serverspec/getting-started/Rakefile b/sandbox/serverspec/getting-started/Rakefile new file mode 100644 index 0000000..11ba867 --- /dev/null +++ b/sandbox/serverspec/getting-started/Rakefile @@ -0,0 +1,27 @@ +require 'rake' +require 'rspec/core/rake_task' + +task :spec => 'spec:all' +task :default => :spec + +namespace :spec do + targets = [] + Dir.glob('./spec/*').each do |dir| + next unless File.directory?(dir) + target = File.basename(dir) + target = "_#{target}" if target == "default" + targets << target + end + + task :all => targets + task :default => :all + + targets.each do |target| + original_target = target == "_default" ? target[1..-1] : target + desc "Run serverspec tests to #{original_target}" + RSpec::Core::RakeTask.new(target.to_sym) do |t| + ENV['TARGET_HOST'] = original_target + t.pattern = "spec/#{original_target}/*_spec.rb" + end + end +end diff --git a/sandbox/serverspec/getting-started/spec/localhost/sample_spec.rb b/sandbox/serverspec/getting-started/spec/localhost/sample_spec.rb new file mode 100644 index 0000000..53754ac --- /dev/null +++ b/sandbox/serverspec/getting-started/spec/localhost/sample_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' + +describe package('apache'), if: os[:family] == 'arch' do + it { should be_installed } +end + +describe service('httpd'), if: os[:family] == 'arch' do + it { should be_enabled } + it { should be_running } +end + +describe package('httpd'), if: os[:family] == 'redhat' do + it { should be_installed } +end + +describe package('apache2'), if: os[:family] == 'ubuntu' do + it { should be_installed } +end + +describe service('httpd'), if: os[:family] == 'redhat' do + it { should be_enabled } + it { should be_running } +end + +describe service('apache2'), if: os[:family] == 'ubuntu' do + it { should be_enabled } + it { should be_running } +end + +describe service('org.apache.httpd'), if: os[:family] == 'darwin' do + it { should be_enabled } + it { should be_running } +end + +describe port(80) do + it { should be_listening } +end diff --git a/sandbox/serverspec/getting-started/spec/spec_helper.rb b/sandbox/serverspec/getting-started/spec/spec_helper.rb new file mode 100644 index 0000000..677ae95 --- /dev/null +++ b/sandbox/serverspec/getting-started/spec/spec_helper.rb @@ -0,0 +1,4 @@ +require 'serverspec' + +set :backend, :exec +