stop being lazy and test your software

75
Stop Being Lazy and Test Your Software! Laura Frank Software Engineer, Codeship

Upload: laura-frank

Post on 14-Apr-2017

14.132 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Stop Being Lazy and Test Your Software

Stop Being Lazy and Test Your Software!

Laura FrankSoftware Engineer, Codeship

Page 2: Stop Being Lazy and Test Your Software

0.7.0

ImageLayers

Panamax

Berlin

Page 3: Stop Being Lazy and Test Your Software

Agenda

Continuous Delivery

Why Do We Test?

Testing with Docker Compose

Faster Testing with Docker

Page 4: Stop Being Lazy and Test Your Software

Why Do We Test?Motivations and frustrations

Page 5: Stop Being Lazy and Test Your Software

Testing software has been a practice since the very first machines were used

for computing.

Page 6: Stop Being Lazy and Test Your Software
Page 7: Stop Being Lazy and Test Your Software

The women who programmed the ENIAC to

calculate trajectories periodically checked the

results with a correct, hand-computed table.

Page 8: Stop Being Lazy and Test Your Software

working software in production

testing

code reviews

version control

pair programming

high-availability architecture

Page 9: Stop Being Lazy and Test Your Software

MotivatorsUpdate application without breaking things!

Validate functionality of updates

Be able to trust deployment checks (in CI/CD workflow)

Confirm that refactoring didn’t break existing functionality

Page 10: Stop Being Lazy and Test Your Software

Why do you skip tests?

45%

15%5%

35% Too slow to run suite 35%

Annoying extra step 15%

Don’t see the point 5%

Slows down deployment 45%

10

Page 11: Stop Being Lazy and Test Your Software

FrustratorsIt takes me an hour to write a single test

My new tests just duplicate existing coverages so there’s no point (integration overlapping with unit)

My suite fails all the time for no apparent reason…?

It takes my test suite over 20 minutes to run, so I don’t run it locally

Testing distracts me from my normal development workflow

Setting up a testing environment is a huge pain

It takes too long to deploy when I have a huge test suite

Page 12: Stop Being Lazy and Test Your Software

FrustratorsIt takes me an hour to write a single test

My new tests just duplicate existing coverages so there’s no point (integration overlapping with unit)

My suite fails all the time for no apparent reason…?

It takes my test suite over 20 minutes to run, so I don’t run it locally

Testing distracts me from my normal development workflow

Setting up a testing environment is a huge pain

It takes too long to deploy when I have a huge test suite

Page 13: Stop Being Lazy and Test Your Software

Using Docker can alleviate some frustrations

associated with testing.

Page 14: Stop Being Lazy and Test Your Software

Testing with DockerComposeIt’s easy, I promise

Page 15: Stop Being Lazy and Test Your Software

Docker and testing are a great pair.

❌✔

Page 16: Stop Being Lazy and Test Your Software

With Docker Compose, it is incredibly easy to create consistent, reproducible environments.

❌✔

Page 17: Stop Being Lazy and Test Your Software

Many of us already useDocker Compose to setup dev environments.

Page 18: Stop Being Lazy and Test Your Software

…But we stop there.

Page 19: Stop Being Lazy and Test Your Software

Use your Docker Composeenvironment for testing

as well.

Page 20: Stop Being Lazy and Test Your Software

app: build: . command: rails server -p 3000 -b '0.0.0.0'volumes: - .:/app

ports: - '3000:3000'

links: - postgres

postgres:image: postgres:9.4

A simple Rails app

Page 21: Stop Being Lazy and Test Your Software

?!

git clone && docker-compose up

hackity hack

How do I make tests happen?

Development workflow

Page 22: Stop Being Lazy and Test Your Software

app: build: . command: rails server -p 3000 -b '0.0.0.0'volumes: - .:/app

ports: - '3000:3000'

links: - postgres

postgres:image: postgres:9.4

A simple Rails app

Page 23: Stop Being Lazy and Test Your Software

app: build: . command: rspec specvolumes: - .:/app

ports: - '3000:3000'

links: - postgres

postgres:image: postgres:9.4

A simple Rails app

Page 24: Stop Being Lazy and Test Your Software
Page 25: Stop Being Lazy and Test Your Software

In most cases, we need to do a bit of setup

before running tests.

Page 26: Stop Being Lazy and Test Your Software

You might even need a different Dockerfile.

Page 27: Stop Being Lazy and Test Your Software

Docker Compose makes this easy

app: build: . dockerfile: Dockerfile.testcommand: rake testvolumes:

- .:/app

Page 28: Stop Being Lazy and Test Your Software

🚨 A word of warning! 🚨

Page 29: Stop Being Lazy and Test Your Software

🚨 Race conditions! 🚨

Page 30: Stop Being Lazy and Test Your Software

You can also run one-off commands against a service

with Docker Compose.

Page 31: Stop Being Lazy and Test Your Software

docker-compose run -e "RAILS_ENV=test" \app rake db:setup

docker-compose run -e "RAILS_ENV=test" \app test-command path/to/spec.rb

docker-compose up #-d if you wish

Usual Docker Compose development environment

Run rake task to set up db

Then run tests against Docker services

Page 32: Stop Being Lazy and Test Your Software

Additional Docker Compose Testing Config Options

environment:RACK_ENV: test

Page 33: Stop Being Lazy and Test Your Software

Additional Docker Compose Testing Config Options

$ docker-compose up -d $ ./run_tests$ docker-compose stop $ docker-compose rm –f

Page 34: Stop Being Lazy and Test Your Software

Docker delivers apredictable, reproducible

testing environment. Period.

Page 35: Stop Being Lazy and Test Your Software

Continuous Integration, Testing, and DockerFail fast and not in production!

Page 36: Stop Being Lazy and Test Your Software

Continuous integration and testing go hand in hand.

👯

Page 37: Stop Being Lazy and Test Your Software
Page 38: Stop Being Lazy and Test Your Software
Page 39: Stop Being Lazy and Test Your Software

Relying on CI/CD withoutadequate test coverage

is not a great idea.

Page 40: Stop Being Lazy and Test Your Software

Would you rather…

💩Find out your code is broken by seeing a failed run of your CI/CD system?

Find out your code is broken by seeing 500s on 500s on 500s in production?

Page 41: Stop Being Lazy and Test Your Software

What about running tests like this…

…inside a container?

Page 42: Stop Being Lazy and Test Your Software
Page 43: Stop Being Lazy and Test Your Software

We run our unit tests in a conatiner during development,

so we should do that during deployment too, right?

Page 44: Stop Being Lazy and Test Your Software

And if we’re running our services in containers during development,

they should be running in containers in

production, right?

Page 45: Stop Being Lazy and Test Your Software

🚨 A word of warning! 🚨

Page 46: Stop Being Lazy and Test Your Software

—Jérôme Petazzoni

“Docker-in-Docker is not 100% made of sparkles, ponies, and unicorns.

46

Page 47: Stop Being Lazy and Test Your Software

Docker in Docker

Page 48: Stop Being Lazy and Test Your Software

CAUTION!YMMVSee https://jpetazzo.github.io

- Mixing file systems == bad time- Shared build cache == bad time- Lots of other stuff == bad time

Page 49: Stop Being Lazy and Test Your Software

Shared daemon – what we really want

Page 50: Stop Being Lazy and Test Your Software

We get this by binding the Docker socket.

Page 51: Stop Being Lazy and Test Your Software

Parallel Testing with DockerBreak it up.

Page 52: Stop Being Lazy and Test Your Software

Why do you skip tests?

45%

15%5%

35% Too slow to run suite 35%

Annoying extra step 15%

Don’t see the point 5%

Slows down deployment 45%

52

Page 53: Stop Being Lazy and Test Your Software

When developing, it’s easy to think of a container as

a small VM that runs a specific workload.

Page 54: Stop Being Lazy and Test Your Software

Page 55: Stop Being Lazy and Test Your Software

But if we change our thinkingto treat containers as just processes, then we can do

some pretty cool stuff.

Page 56: Stop Being Lazy and Test Your Software

Page 57: Stop Being Lazy and Test Your Software

A syntax similar to Docker Compose forservice definition…

57

web:build:image: my_appdockerfile_path: Dockerfile

links:- redis- postgres

redis:image: redis

postgres:image: postgres

Page 58: Stop Being Lazy and Test Your Software

And use it to also define testing steps…

58

- type: parallelsteps:- service: democommand: ruby check.rb

- service: democommand: rake teaspoon suite=jasmine

- service: democommand: rake test

Page 59: Stop Being Lazy and Test Your Software

- type: parallelsteps:

- service: democommand: some-test-command

- service: democommand: another-test-command

- service: democommand: yet-another-test-command

Each step is run independently in a container

59

Page 60: Stop Being Lazy and Test Your Software

- service: democommand: some-test-command

- service: democommand: another-test-command

- service: democommand: yet-another-test-command

And each container may be located in a range of availability zones

60

Page 61: Stop Being Lazy and Test Your Software

Test compose context

Test runner

jettest-1

test-0

test-3

Page 62: Stop Being Lazy and Test Your Software

Docker makes this possible byproviding the means to create

a predictable, reproducibletesting environment.

Page 63: Stop Being Lazy and Test Your Software

Super Cool Tip™ Because quality is everyone’s problem.

Page 64: Stop Being Lazy and Test Your Software

Add an additional pipeline to fail builds if

quality decreases.

Page 65: Stop Being Lazy and Test Your Software

Two good examples arecode coverate and

linting errors.

Page 66: Stop Being Lazy and Test Your Software

#!/bin/bashset –eALLOWED_WARNINGS=100 #some arbitrary threshold

warnings=`grep "offenses detected" rubocop.txt | cut -d " " –f4`

if [ $warnings -gt $ALLOWED_WARNINGS ]then

echo -e "\033[31mallowed warnings $ALLOWED_WARNINGS\033[0m" echo -e "\033[31mactual warnings $warnings\033[0m" echo -e "\033[31mToo many rubocop warnings\033[0m" echo -e "\033[31mTry running 'bin/check_rubocop_against_master’\033[0m" exit 1

else echo $warnings/$ALLOWED_WARNINGS is close enough ಠ_ಠexit 0

fi

Page 67: Stop Being Lazy and Test Your Software

Page 68: Stop Being Lazy and Test Your Software

—Solomon (more or less)

“Improving quality is a lot of unglamourous

work that really adds up.”

68

Page 69: Stop Being Lazy and Test Your Software

Resources#keepshipping

Page 70: Stop Being Lazy and Test Your Software

Highly recommended talks about development, testing, and lots of interesting stuff: https://speakerdeck.com/searls

Ruby gem for parallel tests: grosser/parallel_tests

Parallel Docker testing: Jet (from Codeship)

CI/CD with Docker: http://pages.codeship.com/docker

Running commands with Docker Compose: http://docs.docker.com/compose

The perils of Docker-In-Docker: https://jpetazzo.github.io

This talk: slideshare.net/rheinwein

Page 71: Stop Being Lazy and Test Your Software

—Edsger Dijkstra

“Testing can be a very effective way to show the presence of bugs, but is

hopelessly inadequate for showing their absence.”

71

Page 72: Stop Being Lazy and Test Your Software

Testing does not guarantee that

our software works.

Page 73: Stop Being Lazy and Test Your Software

We test to know when our software is

definitely broken.

Page 74: Stop Being Lazy and Test Your Software

Work harder to know when you’re wrong.

Page 75: Stop Being Lazy and Test Your Software

Thank you!Laura Frank@[email protected]