Introduce serverspec to Linux environment.
Build a Linux environment. https://qiita.com/mkuser9/items/079cc4244821c8e220c2
serverspec requires ruby.
console
$ sudo yum -y install git
$ sudo yum install -y gcc gcc-c++ libyaml-devel libffi-devel libxml2 libxslt libxml2-devel libslt-devel
$ sudo yum install git-core
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ exec $SHELL -l
$ source ~/.bash_profile
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ sudo yum install -y bzip2 gdbm-devel openssl-devel libffi-devel libyaml-devel ncurses-devel readline-devel zlib-devel
$ RUBY_CONFIGURE_OPTS=--disable-install-doc ~/.rbenv/bin/rbenv install 2.6.3
$ rbenv global 2.6.3 && rbenv rehash
Create a Gemfile and add gem.
console
$ sudo vi Gemfile
Gemfile
source "https://rubygems.org"
gem 'serverspec'
gem 'rake'
console
$ bundle install
Install serverspec with the following command.
console
$ gem install serverspec
console
$ serverspec-init
Select OS type:
  1) UN*X
  2) Windows
Select number: 1
Select a backend type:
  1) SSH
  2) Exec (local)
Select number: 2
Vagrant instance y/n: n
Input target host name: www.example.jp
 + spec/
 + spec/www.example.jp/
 + spec/www.example.jp/sample_spec.rb
 + spec/spec_helper.rb
 + Rakefile
 + .rspec
The test file is generated in spec / localhost / sample_spec.rb, so modify the test file according to the contents of the test.
console
vi spec/localhost/sample_spec.rb
Write the test in sample_spec.rb.
sample_spec.rb(sample)
require 'spec_helper'
describe "Testing for git" do
  context "Environmental setting" do
    describe package('git'), :if => os[:family] == 'redhat' do
      it "Installed" do
        expect be_installed
      end
    end
  end
end
describe "git-Testing for core" do
  context "Environmental setting" do
    describe package('git-core'), :if => os[:family] == 'redhat' do
      it "Installed" do
        expect be_installed
      end
    end
  end
end
describe "Testing for httpd" do
  context "Environmental setting" do
    describe package('httpd'), :if => os[:family] == 'redhat' do
      it "Installed" do
        expect be_installed
      end
      it "Enabled" do
        expect be_enabled
      end
      it "Running" do
        expect be_running
      end
    end
  end
end
describe "Test for port" do
  context "Environmental setting" do
    describe port(80) do
      it "Being listened to" do
        expect be_listening 
      end
    end
  end
end
The initial state of sample_spec.rb.
sample_spec.rb(default)
require 'spec_helper'
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
Run the test with the following command.
console
$ rake spec
        Recommended Posts