Serverspec: Quick Start - Run Sample Test to EC2 server
In this post, I am going to install Serverspec and run a sample test on a EC2 instance.
what is Serverspec?
Serverspec is a server-testing framework in Ruby.
A similar technology may include Goss (https://web-quickstart.blogspot.com/2021/04/goss-quick-start-on-awsec2.html).
install
% gem install serverspec
% gem list serverspec
serverspec (2.41.5)
If you get "FilePermissionError" see https://web-quickstart.blogspot.com/2021/04/troubleshoot-gem-install.html
test server
I have created an EC2 instance as:
ssh -i test ec2-user@34.203.28.211
reference: https://web-quickstart.blogspot.com/2021/04/terraform-ec2-instance-with-ssh-setup.html
init project
% serverspec-init
Select OS type:
1) UN*X
2) WindowsSelect number: 1Select a backend type:
1) SSH
2) Exec (local)Select number: 1Vagrant instance y/n: nInput target host name: 34.203.28.211
+ spec/
+ spec/34.203.28.211/
+ spec/34.203.28.211/sample_spec.rb
+ spec/spec_helper.rb
+ Rakefile
% tree -a -I .git
.
├── .rspec
├── .terraform
│ └── ...
├── .terraform.lock.hcl
├── Rakefile
├── main.tf
├── spec
│ ├── 34.203.28.211
│ │ └── sample_spec.rb
│ └── spec_helper.rb
├── terraform.tfstate
├── terraform.tfstate.backup
├── test
└── test.pub
ssh config
For Serverspec to be able to ssh -i test ec2-user@34.203.28.211, you can modify ~/.ssh/config .
Alternatively, you can specify the user and private key in spec/spec_helper.rb as:
options[:user] ||= 'ec2-user'
options[:keys] ||= 'test'
test (fail)
Replace the spec/34.203.28.211/sample_spec.rb with the following code:
require 'spec_helper'
describe package('httpd') do
it { should be_installed }
end
describe port(80) do
it { should be_listening }
end
You should get two failures if you have not configured the server yet.
% rake spec
Package "httpd"
is expected to be installed (FAILED - 1)
Port "80"
is expected to be listening (FAILED - 2)
fix and test again (succeed)
Install and start apache on the target server:
$ sudo yum install httpd
$ sudo service httpd start
The test should be successful now.
% rake spec
Package "httpd"
is expected to be installed
Port "80"
is expected to be listening
Finished in 9.64 seconds (files took 0.27145 seconds to load)
2 examples, 0 failures
Troubleshoot
FilePermissionError
see https://web-quickstart.blogspot.com/2021/04/troubleshoot-gem-install.html
NotImplementedError
install the missing modules
% rake spec
NotImplementedError:
OpenSSH keys only supported if ED25519 is available
net-ssh requires the following gems for ed25519 support:
* ed25519 (>= 1.2, < 2.0)
* bcrypt_pbkdf (>= 1.0, < 2.0)
% gem install ed25519
% gem install bcrypt_pbkdf
set :request_pty, true
add the line
% rake spec
Please write "set :request_pty, true" in your spec_helper.rb or other appropriate file.
% vim spec/spec_helper.rb
+ set :request_pty, true
Wrong sudo password
set SUDO_PASSWORD
% rake spec
Wrong sudo password! Please confirm your password on 10.157.87.100.
% export SUDO_PASSWORD=YOUR_PASSWORD
Comments
Post a Comment