42
TESTING SERVERS LIKE SOFTWARE

Testing servers like software

Embed Size (px)

DESCRIPTION

It's easy enough to test the correctness of the infracode we write with unit-tests and parsers, but testing it does what it's supposed to do in the enviornment itself is a little more challenging. In this talk, I'm going to talk about some of the tools and approaches to use to test your configuration automation tool of choice.

Citation preview

Page 1: Testing servers like software

TESTING SERVERS LIKE SOFTWARE

Page 2: Testing servers like software

MEDEVELOPER TURNED ops guy

WORKING AT KAINOS, CONTRACTING ON GOVERNMENT PROJECTS

Page 3: Testing servers like software

PREVIOUSLY ON THE IER PROJECT

NOW LIVE! HTTPS://WWW.GOV.UK/REGISTER-TO-VOTE

Page 4: Testing servers like software

NOW ON THE DEFRA CAPD PROJECT

Page 5: Testing servers like software

FOREWARNING▸ Ruby and Puppet Biased

▸ They're the tools I use the most!▸ But most tools mentioned are system and tool agnostic

Page 6: Testing servers like software

SO YOU'VE MADE A CONFIGURATION CODE

CHANGE...

Page 7: Testing servers like software

THE UNIT TESTS PASS...

Page 8: Testing servers like software

IT'S BEEN CODE REVIEWED

Page 9: Testing servers like software

SO YOU PUSH TO PRODUCTION!

Page 10: Testing servers like software

IT DOESNT WORK...

Page 11: Testing servers like software

WHAT'S THE MISSING STEP?

Page 12: Testing servers like software

ACCEPTANCE TESTING:Serverspec

Page 13: Testing servers like software

"Serverspec tests your servers' actual state through SSH access"

Page 14: Testing servers like software

CHECK YOUR SERVER▸ Is $package installed?▸ Does file contain $foo?

▸ Does the firewall have the correct rules?▸ Is service running?

▸ etc...

Page 15: Testing servers like software

RESOURCE TYPEScgroup, command, cron, default_gateway, file,

group, host, iis_app_pool, iis_website, interface, ipfilter, ipnat, iptables,

kernel_module, linux_kernel_parameter, lxc, mail_alias, package, php_config, port, ppa,

process, routing_table, selinux, service, user, windows_feature, windows_registry_key, yumrepo,

zfsfs

Page 16: Testing servers like software

MOST ARE FAIRLY SELF

EXPLANATORY

Page 17: Testing servers like software

return_stdout

describe command('cat /etc/resolv.conf') do it { should return_stdout /8\.8\.8\.8/ }end

content

describe file('/etc/httpd/conf/httpd.conf') do its(:content) { should match /ServerName www.example.jp/ }end

Page 18: Testing servers like software

MY BREAD AND BUTTER

Page 19: Testing servers like software

BE_RESOLVABLEdescribe host('serverspec.org') do it { should be_resolvable }end

describe host('serverspec.org') do it { should be_resolvable.by('hosts') }end

describe host('serverspec.org') do it { should be_resolvable.by('dns') }end

Page 20: Testing servers like software

BE_REACHABLEdescribe host('target.example.jp') do # ping it { should be_reachable } # tcp port 22 it { should be_reachable.with( :port => 22 ) } # set protocol explicitly it { should be_reachable.with( :port => 22, :proto => 'tcp' ) } # udp port 53 it { should be_reachable.with( :port => 53, :proto => 'udp' ) } # timeout setting (default is 5 seconds) it { should be_reachable.with( :port => 22, :proto => 'tcp', :timeout => 1 ) }end

Page 21: Testing servers like software

FULL SPECS FOR A WEB SERVERrequire 'spec_helper'

describe package('apache2') do it { should be_installed }end

describe service('apache2') do it { should be_enabled } it { should be_running }end

describe port(80) do it { should be_listening }end

Page 22: Testing servers like software

EXAMPLE:HTTPS://GITHUB.COM/JVOORHIS/

VAGRANT-SERVERSPEC

Page 23: Testing servers like software

LIVE DEMO TIME: SERVERSPEC!

Page 24: Testing servers like software

OUR WORKFLOW:CHANGE IN PUPPET MADE =>TESTED IN VAGRANT INSTANCE

CODE REVIEWED, PASSES CI AND MERGEDPUSHED TO INTEGRATION ENVIRONMENT

SERVERSPEC TESTS RUN ON INTEGRATION ENVIRONMENTANY ISSUES: FIX OR IF BIG ENOUGH, REVERT

CONTINUES DOWN THE PIPELINE TO PRODUCTION

Page 25: Testing servers like software

SERVERSPEC:PRETTY NEAT

BUT THERE'S ALSO LANGUAGE SPECIFIC TOOLS!

Page 26: Testing servers like software

TEST KITCHEN

Page 27: Testing servers like software

▸ Uses serverspec as it's core▸ It's basically helper wrappers to install chef

▸ And run the cookbooks given▸ Used for cookbook acceptance testing

Page 28: Testing servers like software

EXAMPLE:HTTPS://GITHUB.COM/OPSCODE-

COOKBOOKS/APT/

Page 29: Testing servers like software

LIVE DEMO TIME: TEST-KITCHEN!

Page 30: Testing servers like software

BEAKER

Page 31: Testing servers like software

▸ Again: serverspec as it's core▸ But specs customised with Puppet references

▸ Specific rspec grammer around running manifests etc.

Page 32: Testing servers like software

EXAMPLE:HTTPS://GITHUB.COM/PETEMS/PUPPET-

SWAP_FILE

Page 33: Testing servers like software

require 'spec_helper_acceptance'

describe 'swap_file class', :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do

context 'swap_file' do context 'ensure => present' do it 'should work with no errors' do pp = <<-EOS class { 'swap_file': } EOS

# Run it twice and test for idempotency expect(apply_manifest(pp).exit_code).to_not eq(1) expect(apply_manifest(pp).exit_code).to eq(0) end

Page 34: Testing servers like software

context 'custom parameters' do it 'should work with no errors' do pp = <<-EOS class { 'swap_file': swapfile => '/tmp/swapfile', swapfilesize => '5 MB', } EOS

it 'should contain the given swapfile' do shell('/sbin/swapon -s | grep /tmp/swapfile', :acceptable_exit_codes => [0]) shell('/sbin/swapon -s | grep 5116', :acceptable_exit_codes => [0]) end end endend

Page 35: Testing servers like software

I KNOW WHAT YOU MIGHT BE THINKING...

Page 36: Testing servers like software

Wait, whats the difference between this and monitoring?

Page 37: Testing servers like software

MONITORINGKEEP THE LIGHTS ON▸ "Fix me now!" issues

▸ Dynamic changes - Things crashing, hard drives are full

Page 38: Testing servers like software

ACCEPTANCE"MY CHANGES DIDN'T BREAK ANYTHING"

▸ Smoke tests▸ Human readable

▸ One-off for edge-cases▸ Auditing

Page 39: Testing servers like software

HOWEVER

Page 40: Testing servers like software

USING SERVERSPEC AS MONITORING IS POSSIBLE!

AND HAS SOME PRETTY SWEET BENEFITSHTTP://WWW.SLIDESHARE.NET/M_RICHARDSON/SERVERSPEC-AND-SENSU-TESTING-AND-MONITORING-

COLLIDE

Page 41: Testing servers like software

Q&A

Page 42: Testing servers like software

LINKShttp://serverspec.org/

http://vincent.bernat.im/en/blog/2014-serverspec-test-infrastructure.html

https://github.com/serverspec/serverspec

http://www.debian-administration.org/article/703/A_brief_introduction_to_server-testing_with_serverspec

"ChefConf 2014: Gosuke Miyashita, "Serverspec: The Simplest Server Testing Tool Ever" - http://www.youtube.com/watch?

v=6GvlHImeloo*