Slide 1

Slide 1 text

Test-driven Development with Chef 1 Thursday, February 6, 14

Slide 2

Slide 2 text

Hello! • Nathen Harvey • Director Community & #learnchef • @nathenharvey • [email protected] 2 Thursday, February 6, 14

Slide 3

Slide 3 text

Who are you? • Developer? • System Administrator? • DevOp? • Experienced with Chef? • Test-driven development? (any flavor) • Ruby developer? 3 Thursday, February 6, 14

Slide 4

Slide 4 text

Thursday, February 6, 14

Slide 5

Slide 5 text

Software Development Workflows For Systems Administrators: A Crash Course Thursday, February 6, 14

Slide 6

Slide 6 text

Developers Care • It’s great when developers care about • uptime! • scaling! • deployment! • Put them on call! etc. etc. etc. Thursday, February 6, 14

Slide 7

Slide 7 text

Operations cares, too! • Systems Administrators also have as much or more to learn from developers as well! Thursday, February 6, 14

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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 6, 14

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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 6, 14

Slide 12

Slide 12 text

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 6, 14

Slide 13

Slide 13 text

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 6, 14

Slide 14

Slide 14 text

Thursday, February 6, 14

Slide 15

Slide 15 text

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 6, 14

Slide 16

Slide 16 text

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 6, 14

Slide 17

Slide 17 text

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 6, 14

Slide 18

Slide 18 text

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 6, 14

Slide 19

Slide 19 text

Cookbooks Are Artifacts Close Encounters of the Third Kind, copyright 1977, Columbia Pictures “This means something! This is important!” Thursday, February 6, 14

Slide 20

Slide 20 text

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 6, 14

Slide 21

Slide 21 text

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 6, 14

Slide 22

Slide 22 text

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 6, 14

Slide 23

Slide 23 text

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 6, 14

Slide 24

Slide 24 text

• 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 6, 14

Slide 25

Slide 25 text

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 6, 14

Slide 26

Slide 26 text

Testing Cookbooks Source' Code' Compiler' Ar/fact' Test' X Thursday, February 6, 14

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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 6, 14

Slide 29

Slide 29 text

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 6, 14

Slide 30

Slide 30 text

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 6, 14

Slide 31

Slide 31 text

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 6, 14

Slide 32

Slide 32 text

Worksta(on:+ edit+cookbook+ Worksta(on:+ knife+cookbook+ upload+ Target:+Run+ chef9client+ Traditional Cookbook Development: Subsequent Runs Thursday, February 6, 14

Slide 33

Slide 33 text

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 6, 14

Slide 34

Slide 34 text

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 6, 14

Slide 35

Slide 35 text

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 6, 14

Slide 36

Slide 36 text

Code Can (Help) Thursday, February 6, 14

Slide 37

Slide 37 text

The Toolchain Thursday, February 6, 14

Slide 38

Slide 38 text

The Toolchain • You’re becoming a developer! • Reasonably powerful computer • Good editor Thursday, February 6, 14

Slide 39

Slide 39 text

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 6, 14

Slide 40

Slide 40 text

Tools • Chef • Virtualization provider • Virtualization automation • Testing frameworks Code Can (Help) Thursday, February 6, 14

Slide 41

Slide 41 text

Requirements Thursday, February 6, 14

Slide 42

Slide 42 text

Requirements • We need a web server configured to serve up our home page. • Yes, • it’s super-simple • it’s not a “real-life” example • it will allow us to see lots of testing tools 36 Thursday, February 6, 14

Slide 43

Slide 43 text

Worksta(on:+ edit+cookbook+ Worksta(on:+ knife+cookbook+ upload+ Target:+Run+ chef9client+ Traditional Cookbook Development: Subsequent Runs Thursday, February 6, 14

Slide 44

Slide 44 text

Pre-testing Thursday, February 6, 14

Slide 45

Slide 45 text

Rubocop • A robust Ruby code analyzer, based on the community Ruby style guide. Thursday, February 6, 14

Slide 46

Slide 46 text

$ rubocop . Exercise: Run Foodcritic Inspecting 3 files CCC Offences: metadata.rb:1:1: C: Missing utf-8 encoding comment. name 'apache' ^ Thursday, February 6, 14

Slide 47

Slide 47 text

$ rubocop --auto-gen-config Rubocop Workflow ... 3 files inspected, 5 offences detected Created rubocop-todo.yml. Run rubocop with --config rubocop-todo.yml, or add inherit_from: rubocop-todo.yml in a .rubocop.yml file. Thursday, February 6, 14

Slide 48

Slide 48 text

$ echo "inherit_from: rubocop-todo.yml" >> .rubocop.yml Rubocop Workflow Thursday, February 6, 14

Slide 49

Slide 49 text

$ rubocop . Rubocop Workflow Inspecting 3 files ... 3 files inspected, no offences detected Thursday, February 6, 14

Slide 50

Slide 50 text

Rubocop Workflow • Uncomment lines in rubocop-todo.yml • Fix offences • git commit • Repeat Thursday, February 6, 14

Slide 51

Slide 51 text

Foodcritic • Foodcritic is a code linting tool for Chef • Checks code correctness • Catches common mistakes before they cause problems • Extensible & Configurable • Create new rulesets • Ignore existing rulesets • Integrates nicely into CI pipelines Thursday, February 6, 14

Slide 52

Slide 52 text

$ foodcritic . Exercise: Run Foodcritic FC008: Generated cookbook metadata needs updating: ./metadata.rb:2 FC008: Generated cookbook metadata needs updating: ./metadata.rb:3 Thursday, February 6, 14

Slide 53

Slide 53 text

Foodcritic http://foodcritic.io Thursday, February 6, 14

Slide 54

Slide 54 text

OPEN IN EDITOR: SAVE FILE! metadata.rb name 'myface' maintainer 'Your Name' maintainer_email '[email protected]' license 'All rights reserved' description 'Installs/Configures myface' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.1.0' Correct Foodcritic Errors Thursday, February 6, 14

Slide 55

Slide 55 text

$ foodcritic . Run Foodcritic • Foodcritic returns a zero value if there are no violations Thursday, February 6, 14

Slide 56

Slide 56 text

Best Practice: Run Foodcritic Before Each Commit • Always check in corrected code! • Make Foodcritic a part of your build pipeline with commit hooks or other methods • The Foodcritic web page has extensive documentation & examples showing how to build Foodcritic rules into Jenkins or Travis-CI Thursday, February 6, 14

Slide 57

Slide 57 text

Testing Thursday, February 6, 14

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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 6, 14

Slide 63

Slide 63 text

• Unit Test: Signal Input What and When To Test Flickr user: Rain Rabbit Thursday, February 6, 14

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

• 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 6, 14

Slide 67

Slide 67 text

• 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 6, 14

Slide 68

Slide 68 text

• 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 6, 14

Slide 69

Slide 69 text

Chef Testing Tools Thursday, February 6, 14

Slide 70

Slide 70 text

Chef Testing Tools • Signal Input • ChefSpec Thursday, February 6, 14

Slide 71

Slide 71 text

Chef Testing Tools • Signal Input • ChefSpec • Signal out • serverspec Thursday, February 6, 14

Slide 72

Slide 72 text

Chef Testing Tools • Signal Input • ChefSpec • Signal out • serverspec • Signal processing • Who cares; that’s what Chef’s own tests are for! Thursday, February 6, 14

Slide 73

Slide 73 text

Unit Testing: ChefSpec Thursday, February 6, 14

Slide 74

Slide 74 text

ChefSpec Overview • Built on top of RSpec • Converge a Chef run in memory • Overrides all providers to take no action • Make assertions about the in-memory Chef run • Mock Ohai data (automatic attributes) with Fauxhai Thursday, February 6, 14

Slide 75

Slide 75 text

The Test Files: Chefspec • Each recipe gets an individual test file. • recipes/default.rb • specs/default_test.rb • In addition, a helper file lets us define our own functions for testing. • specs/spec_helper.rb Thursday, February 6, 14

Slide 76

Slide 76 text

Integration Testing Thursday, February 6, 14

Slide 77

Slide 77 text

Thursday, February 6, 14

Slide 78

Slide 78 text

Old School Testing Thursday, February 6, 14

Slide 79

Slide 79 text

Review & Wrap Up • knife cookbook test • rubocop • foodcritic • chefspec • test-kitchen • serverspec • monitoring Thursday, February 6, 14

Slide 80

Slide 80 text

Thank you! • What questions do you have? • Nathen Harvey • @nathenharvey • [email protected] Thursday, February 6, 14