Delivery Deployment Pipeline Overview Developer Version Control Production Environment Code check-in Application release query check-out CI/CD Server (Pipeline Executor) Release Test Deploy Build working and deployable software a build advances along the pipeline in stages (in case of error, the pipeline is stopped)
Cycle of Test-Driven Development code is designed and tested cycle iterates in small increments Write a Failing Test Make the Test Pass Clean up Your Code
Cycle of Test-Driven Development code is designed and tested cycle iterates in small increments code is of higher quality Write a Failing Test Make the Test Pass Clean up Your Code
Cycle of Test-Driven Development code is designed and tested cycle iterates in small increments code is of higher quality Write a Failing Test Make the Test Pass Clean up Your Code cycle creates regression safety
to Trust but Verify You want to verify that your automation tool does the job right. You Want to Refactor your Code You want to simplify existing code or integrate a third-party module without compromising quality. You Want to Migrate to a Different Platform You want to migrate to a different operating system or cloud provider. You Want to Migrate to a Different Automation Tool You want to migrate from Bash or Perl to Ansible, Chef, Puppet or Terraform. Test-Driven Infrastructure Practical Reasons
you express your intentions twice; once as a test, and once as production code. If the two approaches don’t match, your tests fail, and you’ve caught a bug.“ Kent Beck, Creator of Extreme Programming
Let you run infrastructure code on various cloud providers and virtualization technologies. § Amazon EC2 § Azure RM § DigitalOcean Test Kitchen Basic Concepts § Docker § Google GCE § Microsoft Hyper-V § Rackspace § Vagrant § VMware vSphere
Let you run infrastructure code on various cloud providers and virtualization technologies. § Amazon EC2 § Azure RM § DigitalOcean 2. Platforms Represent a configuration of an operating system. § Linux § Windows Test Kitchen Basic Concepts § Docker § Google GCE § Microsoft Hyper-V § Rackspace § Vagrant § VMware vSphere
These are the tools needed to converge the enrionment. § Ansible § Chef § Puppet 4. Test Suites Define the tests you want to run against an instance using a verifier. § Bash § Bats § Cucumber Test Kitchen Basic Concepts § Terraform § SaltStack § InSpec § RSpec § Serverspec
8 9 10 11 MARTIN ETMAJER Founder | GetCloudnative e.U. RSpec Behaviour-Driven Development for Ruby File foo_spec.rb 1 require ‘foo’ 2 3 describe Foo do 4 before Foo do 5 @foo = Foo.new 6 end 7 8 it “method #bar does something useful” do 9 @foo.bar should eq “something useful” 10 end 11 end
15 5 16 6 17 7 18 8 19 9 20 10 21 11 22 MARTIN ETMAJER Founder | GetCloudnative e.U. Serverspec RSpec Tests for Your Servers File server_spec.rb 1 require “serverspec” 12 describe service(“bar”) do 2 describe user(“foo”) do 13 it { should be_enabled } 3 it { should exist } 14 it { should be_running } 4 it { should belong_to_group “foo” } 15 end 5 end 16 describe port(8080) do 6 describe file(“/opt/bar”) do 17 it { should be_listening } 7 it { should be_directory } 18 end 8 it { should be_mode 777 } 19 describe command(“apachectl –M”) do 9 it { should be_owned_by “foo” } 20 its(:stdout) { should contain(“proxy_module”) } 10 it { should be_grouped_into “foo” } 21 end 11 end
8 9 10 11 MARTIN ETMAJER Founder | GetCloudnative e.U. awspec RSpec Tests for Your AWS Resources File ec2_spec.rb 1 require “awspec” 2 describe ec2(“my-ec2-tag-name”) do 3 it { should be_running } 4 its(:instance_id) { should eq “i-ec12345a” } 5 its(:image_id) { should eq “ami-abc12def” } 6 its(:public_ip_address) { should eq “123.0.456.789” } 7 it { should have_security_group(“my-security-group-name”) } 8 it { should belong_to_vpc(“my-vpc”) } 9 it { should belong_to_subnet(“subnet-1234a567”) } 10 it { should have_eip(“123.0.456.789”) } 11 end
8 9 10 11 MARTIN ETMAJER Founder | GetCloudnative e.U. How to Test-Driven Infrastructure? 5. Write a Failing Test File test/integration/default/default_spec.rb 1 require ‘awspec’ 2 require_relative ‘./spec_helper’ 3 4 describe “My AWS Deployment” do 5 describe ec2(EC2Helper.get_ec2_instance_id_from_tag_name(“my-aws-ec2-instance”)) do 6 it { should exist } 7 it { should be_running } 8 end 9 end
Test-Driven Infrastructure? 5. Write a Failing Test $ kitchen list Instance Driver Provisioner Verifier Transport Last Action default-aws Terraform Terraform Awspec Ssh Kitchen:ActionFailed
Test-Driven Infrastructure? 6. Make the Test Pass $ kitchen list Instance Driver Provisioner Verifier Transport Last Action default-aws Terraform Terraform Awspec Ssh Verified