$30 off During Our Annual Pro Sale. View Details »

Introduction to Test Kitchen

Nathen Harvey
February 11, 2014

Introduction to Test Kitchen

An introduction to testing Chef code and an intro to Test Kitchen including code examples. Code examples are all available at https://github.com/nathenharvey/intro-to-test-kitchen

Nathen Harvey

February 11, 2014
Tweet

More Decks by Nathen Harvey

Other Decks in Technology

Transcript

  1. Introduction to Test Kitchen 1 Thursday, February 13, 14

  2. Hello! • Nathen Harvey • Director Community & #learnchef •

    @nathenharvey • nharvey@getchef.com 2 Thursday, February 13, 14
  3. Who are you? • Developer? • System Administrator? • DevOp?

    • Experienced with Chef? • Test-driven development? (any flavor) • Ruby developer? 3 Thursday, February 13, 14
  4. Thursday, February 13, 14

  5. Software Development Workflows For Systems Administrators: A Crash Course Thursday,

    February 13, 14
  6. Developers Care • It’s great when developers care about •

    uptime! • scaling! • deployment! • Put them on call! etc. etc. etc. Thursday, February 13, 14
  7. Operations cares, too! • Systems Administrators also have as much

    or more to learn from developers as well! Thursday, February 13, 14
  8. Source Code Compiler Artifact Test Typical Development Workflow • Typical

    Software Development Workflow: • Write Source • Compile Source • Deploy Artifacts • Write Tests • Run Tests Thursday, February 13, 14
  9. Source Code Compiler Artifact Test Typical Development Workflow • Source

    Code • The recipe for a computer program • Edited directly • Managed via Source Control Software Thursday, February 13, 14
  10. Source Code Compiler Artifact Test Typical Development Workflow • Compiler

    • Takes source code and converts it into executable programs Thursday, February 13, 14
  11. Source Code Compiler Artifact Test Typical Development Workflow • Artifact

    • Artifacts are executable programs created by compilers. • Compiled artifacts cannot be edited directly. Source code must be changed and re-compiled to produce a new build artifact. Thursday, February 13, 14
  12. Source Code Compiler Artifact Test Typical Development Workflow • Testing

    • Write tests to verify that code works as intended • Run tests at different stages of the code lifecycle to ensure correctness Thursday, February 13, 14
  13. Source Code Compiler Artifact Test Typical Development Workflow • When

    developing software, most time isn’t actually spent coding • 10-second changes to source code can take minutes to vet • Compiling code • Deploying code • Writing & Running tests Thursday, February 13, 14
  14. Thursday, February 13, 14

  15. Source Code Compiler Artifact Test Typical Development Workflow Fun! Boring!

    • Things that are fun: • Designing programs! • Writing source code! • Things that are boring: • Compiling code • Deploying artifacts • Running tests Thursday, February 13, 14
  16. Cookbooks Are Source Code • Chef Cookbook source code is

    just like the source code for other programs. • Changes are tracked and versioned with source control software such as git • Code is inert until it has been deployed • Code can be used to re-create the desired system configuration from scratch Source' Code' Compiler' Ar/fact' Test' Thursday, February 13, 14
  17. Cookbooks Aren’t Pre-Compiled • Because Ruby is an interpreted language,

    Chef code is not compiled until it is executed by the Chef-Client • There is no lengthy compilation step between committing Chef Cookbooks and deploying them Source' Code' Compiler' Ar/fact' Test' X Thursday, February 13, 14
  18. Cookbooks Are Artifacts • Chef Cookbook artifacts are just like

    the artifacts for other programs. • Cookbooks are deployed • Cookbooks are released with versions • Cookbooks are immutable once deployed • Cookbooks are packaged for distribution • Cookbooks can depend on one another Source' Code' Compiler' Ar/fact' Test' X Thursday, February 13, 14
  19. Cookbooks Are Artifacts Close Encounters of the Third Kind, copyright

    1977, Columbia Pictures “This means something! This is important!” Thursday, February 13, 14
  20. Cookbooks Are Artifacts • Depending entirely on context, Chef Cookbooks

    can be either source code OR artifacts. Close Encounters of the Third Kind, copyright 1977, Columbia Pictures “This means something! This is important!” Thursday, February 13, 14
  21. Cookbooks Are Artifacts • This sounds obvious, but it’s easy

    to overlook: Treating cookbooks only as source code or only as artifacts will lead to long-term problems when using Chef cookbooks to manage infrastructure. • Because Chef cookbooks are source code and artifacts, depending on the context, they have all the properties of each. Source' Code' Ar+fact' Thursday, February 13, 14
  22. Cookbooks Are Artifacts • Cookbooks are source! • Versioned •

    Stored in a source repository • Inert while in development Source' Code' Ar+fact' A good Chef workflow treats cookbooks appropriately depending on context • Cookbooks are artifacts! • Versioned • Deployed to a Chef Server • Executable by Chef-client once deployed Thursday, February 13, 14
  23. Keeping Repos in Sync • Common question: “How do I

    automatically keep my Chef Code Repository and my Chef Server Artifact Repository in sync?” • Answer: The same way that one would automatically keep a code repo and an artifact repo in sync with any other software released to production. Deploy with care! Source' Code' Ar+fact' A brief aside about deploying cookbook artifacts: Thursday, February 13, 14
  24. • Cookbooks are Source! • knife cookbook create • Code:

    recipes/default.rb • Version & dependencies: metadata.rb • git commit • git tag Cookbooks as Source Code Source Code Compiler Artifact Test X Thursday, February 13, 14
  25. Cookbooks as Artifacts • Cookbooks are Artifacts! • knife cookbook

    upload • Immutable once deployed, can only be replaced • Version & dependencies: metadata.rb • chef-client Source Code Compiler Artifact Test Thursday, February 13, 14
  26. Testing Cookbooks Source' Code' Compiler' Ar/fact' Test' X Thursday, February

    13, 14
  27. Testing Cookbooks • Three things necessary to test a Chef

    cookbook: Source' Code' Compiler' Ar/fact' Test' X Thursday, February 13, 14
  28. Testing Cookbooks • Three things necessary to test a Chef

    cookbook: 1) A place to store the cookbook artifact. Source' Code' Compiler' Ar/fact' Test' X Thursday, February 13, 14
  29. Testing Cookbooks • Three things necessary to test a Chef

    cookbook: 1) A place to store the cookbook artifact. 2) A Chef-client with access to the cookbook. Source' Code' Compiler' Ar/fact' Test' X Thursday, February 13, 14
  30. Testing Cookbooks • Three things necessary to test a Chef

    cookbook: 1) A place to store the cookbook artifact. 2) A Chef-client with access to the cookbook. 3) A target server running the same OS as production. Source' Code' Compiler' Ar/fact' Test' X Thursday, February 13, 14
  31. Worksta(on:+ Knife+cookbook+ create+ Worksta(on:+ Edit+cookbook+ Worksta(on:+ Knife+cookbook+ upload+ Provision+target+ Bootstrap+

    target+ Worksta(on:+ Edit+target+run+ list+ ssh+target+ Target:+Run+ chef>client+ Traditional Cookbook Development: First Chef Run Thursday, February 13, 14
  32. Worksta(on:+ edit+cookbook+ Worksta(on:+ knife+cookbook+ upload+ Target:+Run+ chef9client+ Traditional Cookbook Development:

    Subsequent Runs Thursday, February 13, 14
  33. Source'Code' Compiler' Ar/fact' Test' Boooooooring • Too much time doing

    “paperwork” •vi recipes/something.rb •knife cookbook upload •sudo pkill -USR1 chef- client • #%$#%$ something broke, let me do that all again • Not enough time doing fun stuff! • Writing recipes Less Fun! More Boring! X Thursday, February 13, 14
  34. Source'Code' Compiler' Ar/fact' Test' Boooooooring • Too much time doing

    “paperwork” •vi recipes/something.rb •knife cookbook upload •sudo pkill -USR1 chef- client • #%$#%$ something broke, let me do that all again • Not enough time doing fun stuff! • Writing recipes Less Fun! More Boring! X TH IS SUCKS! Thursday, February 13, 14
  35. Source'Code' Compiler' Ar/fact' Test' Developing for Chef: Rapid Iteration •

    Less time waiting around for cookbook deploys and Chef runs • More frequent testing • Better code • Business needs met more quickly More Fun! Less Boring! X Thursday, February 13, 14
  36. Code Can (Help) Thursday, February 13, 14

  37. The Toolchain Thursday, February 13, 14

  38. The Toolchain • You’re becoming a developer! • Reasonably powerful

    computer • Good editor Thursday, February 13, 14
  39. Have a Good Computer • Virtualization is used to run

    acceptance tests • Running on a real virtualized “node” • Lots of memory (4GB+, 8GB recommended) • Fast disk (SSD) • Good processor (Intel i5+) • Modern OS X Thursday, February 13, 14
  40. Tools • Chef • Dependency manager • Virtualization provider •

    Virtualization automation • Testing frameworks Code Can (Help) Thursday, February 13, 14
  41. Requirements Thursday, February 13, 14

  42. Requirements • All servers should synchronize their clocks with ntp

    • Yes, • it’s super-simple • it will allow us to see test kitchen in action 36 Thursday, February 13, 14
  43. Worksta(on:+ edit+cookbook+ Worksta(on:+ knife+cookbook+ upload+ Target:+Run+ chef9client+ Traditional Cookbook Development:

    Subsequent Runs Thursday, February 13, 14
  44. Testing Thursday, February 13, 14

  45. Different Types of Testing • There are formal Software Engineering

    definitions for testing Thursday, February 13, 14
  46. Different Types of Testing • There are formal Software Engineering

    definitions for testing • Unit test Thursday, February 13, 14
  47. Different Types of Testing • There are formal Software Engineering

    definitions for testing • Unit test • Integration test Thursday, February 13, 14
  48. Different Types of Testing • There are formal Software Engineering

    definitions for testing • Unit test • Integration test • Acceptance test Thursday, February 13, 14
  49. Different Types of Testing • There are formal Software Engineering

    definitions for testing • Unit test • Integration test • Acceptance test • An easy explanation for SAs is ... Thursday, February 13, 14
  50. • Unit Test: Signal Input What and When To Test

    Flickr user: Rain Rabbit Thursday, February 13, 14
  51. • Unit Test: Signal Input • Did I send Chef

    the correct command? What and When To Test Flickr user: Rain Rabbit Thursday, February 13, 14
  52. • Unit Test: Signal Input • Did I send Chef

    the correct command? • Signal Processing What and When To Test Flickr user: Rain Rabbit Thursday, February 13, 14
  53. • Unit Test: Signal Input • Did I send Chef

    the correct command? • Signal Processing • Did Chef interpret my command correctly? What and When To Test Flickr user: Rain Rabbit Thursday, February 13, 14
  54. • Unit Test: Signal Input • Did I send Chef

    the correct command? • Signal Processing • Did Chef interpret my command correctly? • Acceptance Test: Signal Output What and When To Test Flickr user: Rain Rabbit Thursday, February 13, 14
  55. • Unit Test: Signal Input • Did I send Chef

    the correct command? • Signal Processing • Did Chef interpret my command correctly? • Acceptance Test: Signal Output • Did my expressed intent, executed by Chef, achieve the desired result? What and When To Test Flickr user: Rain Rabbit Thursday, February 13, 14
  56. Test Kitchen • Test harness tool to execute your configured

    code on one or more platforms in isolation • Acceptance test your cookbooks Thursday, February 13, 14
  57. Thursday, February 13, 14

  58. Demo Time • https://github.com/nathenharvey/intro-to-test-kitchen Thursday, February 13, 14

  59. Guard • Guard is a command line tool to easily

    handle events on file system modifications. Thursday, February 13, 14
  60. $ bundle exec guard init Initialize Guard 17:18:59 - INFO

    - Writing new Guardfile to /Users/ nathenharvey/devopsdc/cookbooks/ntp/Guardfile 17:18:59 - INFO - kitchen guard added to Guardfile, feel free to edit it Thursday, February 13, 14
  61. $ bundle exec guard start Start Guard 17:20:14 - INFO

    - Guard is using TerminalNotifier to send notifications. 17:20:14 - INFO - Guard is using Tmux to send notifications. 17:20:14 - INFO - Guard is using TerminalTitle to send notifications. 17:20:14 - INFO - Guard::Kitchen is starting -----> Starting Kitchen (v1.0.0) -----> Creating <default-ubuntu-1204>... Thursday, February 13, 14
  62. OPEN IN EDITOR: SAVE FILE! cookbooks/ntp/recipes/default.rb # # Cookbook Name::

    ntp # Recipe:: default # # Copyright (C) 2014 YOUR_NAME # # All rights reserved - Do Not Redistribute # package 'ntp' Install ntp package Thursday, February 13, 14
  63. OPEN IN EDITOR: SAVE FILE! cookbooks/ntp/recipes/default.rb package 'ntp' service 'ntp'

    do action :start end Start ntp service Thursday, February 13, 14
  64. OPEN IN EDITOR: SAVE FILE! cookbooks/ntp/recipes/default.rb package 'ntp' service 'ntp'

    do action [:enable, :start] end Enable the ntp service, too Thursday, February 13, 14
  65. Test Kitchen is failing! • The service is named differently

    on each Operating System • Debian / Ubuntu • ntp • RedHat / CentOS • ntpd Thursday, February 13, 14
  66. OPEN IN EDITOR: SAVE FILE! cookbooks/ntp/recipes/default.rb case node['platform_family'] when 'debian'

    node.default['ntp']['service'] = 'ntp' when 'rhel' node.default['ntp']['service'] = 'ntpd' end service node['ntp']['service'] do action [:enable, :start] end Account for OS Differences Thursday, February 13, 14
  67. Time to refactor • Test Kitchen is now passing but...

    • We’ve just put data into our recipes • Best Practice: Separate data and logic • Let’s move the data to a new file! Thursday, February 13, 14
  68. OPEN IN EDITOR: SAVE FILE! cookbooks/ntp/attributes/default.rb case node['platform_family'] when 'debian'

    default['ntp']['service'] = 'ntp' when 'rhel' default['ntp']['service'] = 'ntpd' end Attributes file Thursday, February 13, 14
  69. OPEN IN EDITOR: SAVE FILE! cookbooks/ntp/recipes/default.rb case node['platform_family'] when 'debian'

    node.default['ntp']['service'] = 'ntp' when 'rhel' node.default['ntp']['service'] = 'ntpd' end Remove from the recipe Delete this from the recipe! Thursday, February 13, 14
  70. OPEN IN EDITOR: SAVE FILE! cookbooks/ntp/test/integration/default/serverspec/ntp_spec.rb describe 'NTP' do it

    'is installed' do expect(package('ntp')).to be_installed end it 'is running' do case RSpec.configuration.os when 'Debian' expect(service('ntp')).to be_running when 'RedHat' expect(service('ntpd')).to be_running end end it 'is enabled' do case RSpec.configuration.os when 'Debian' expect(service('ntp')).to be_enabled when 'RedHat' expect(service('ntpd')).to be_enabled end end end Bonus: Serverspec See github for the full file! Thursday, February 13, 14
  71. More testing! • Some other testing tools you should consider:

    • knife cookbook test • rubocop • foodcritic • chefspec • And every sysadmin’s default testing tool: • monitoring! Thursday, February 13, 14
  72. Review & Wrap Up • Quick introduction to • Test-driven

    development • Guard • test-kitchen • serverspec Thursday, February 13, 14
  73. • https://github.com/nathenharvey/intro-to-test-kitchen • Initialize Guard • Install ntp package •

    enable and start ntp • add case in the recipe • move case to the attributes • add serverspec Thursday, February 13, 14
  74. Thank you! • What questions do you have? • Nathen

    Harvey • @nathenharvey • nharvey@getchef.com Thursday, February 13, 14