Introduce serverspec to Linux

Introduce serverspec to Linux environment.

Building a Linux environment

Build a Linux environment. https://qiita.com/mkuser9/items/079cc4244821c8e220c2

install ruby

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

Creating a Gemfile

Create a Gemfile and add gem.

console


$ sudo vi Gemfile

Gemfile


source "https://rubygems.org"

gem 'serverspec'
gem 'rake'

console


$ bundle install

Install serverspec

Install serverspec with the following command.

console


$ gem install serverspec

Creating a sample test

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

Describe the test content

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

Run the test with the following command.

console


$ rake spec

Recommended Posts

Introduce serverspec to Linux
[Linux] Introduction to Linux
Linux commands to remember
Introduce elpy to emacs
Introduce PyQt 5 to MacOS (Linux can also be used)
[Windows] RDP to Windows via Linux
A super introduction to Linux
[Linux] welcome to emergency mode!
I tried to reintroduce Linux
I tried to introduce Pylint
Introduction to Linux Commands ~ LS-DYNA Edition ~
Easy copy to clipboard on Linux
Command to create Linux Live USB
A memorandum to change to Manjaro Linux
How to install wkhtmltopdf (Amazon Linux2)
How to install VMware-Tools on Linux
Try to introduce the theme to Pelican
How to install MBDyn (Linux Ubuntu)
[linux] kill command to kill the process
Road to Linux Intermediate: Network Edition
Linux
Forcibly introduce real-time translation to Zoom
Introducing Amplify to Amazon Linux 2 AMI
Linux commands related to character code
How to check Linux OS version
[Windows] Transfer event logs to Linux
Introduce Python 3.5.2 environment on Amazon Linux
How to build my own Linux server
[Linux] How to subdivide files and folders
[Rails] How to introduce Google Analytics [Easy]
[Linux] Flow from power-on to PC startup
How to install aws-session-manager-plugin on Manajro Linux
[Reading memo] Linux standard textbook (Chapter 1 to Chapter 6)
I want to know how LINUX works!
[Linux] How to use the echo command
I want to use Linux on mac
How to use the Linux grep command
How to update php on Amazon linux 2
[linux] Split files to the specified size
How to display emoji on Manjaro Linux
Upgraded mysql on Cloud9 (Amazon Linux) (5.5 to 5,7)
How to install packages on Alpine Linux
Tools used to check Linux network communication
5 reasons to install Linux on your laptop.
Connect to GNU / Linux with Remote Desktop
How to install Anisble on Amazon Linux 2
How to operate Linux from the console
[Linux convenient command] Try to insert vivid
How to install Windows Subsystem For Linux
How to power off Linux with Ultra96-V2
How to update security on CentOS Linux 8
Introduce Python library TRML2PDF to MacOSX (10.11.xx)
Introducing Japanese input system to Serene Linux
How to install php7.4 on Linux (Ubuntu)
On Ubuntu Linux, set Tab to q
Introducing Docker Engine to Linux From Scratch
Arch Linux install to BIOS boot system
Granting sudo privileges to general Linux users
How to find large files on Linux
[Amazon Linux] Switching from Python 2 series to Python 3 series
What to do after installing Linux (Ubuntu)