Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Triple-D: Test driven docker development

Triple-D: Test driven docker development

# Triple-D: Test driven docker development

Here you find my docker slide deck container from my docker-tdd talks

* [WJAX 2014 - GER: Testgetriebene Infrastruktur als Code][1] talk.
* [Continuous Lifecycle 2014 - GER: Testgetriebene Infrastruktur als Code][2] talk.

## Get the slides

- start with `docker run -d -p 8001:80 rossbachp/docker-tdd:wjax2014`
- start with `docker run -d -p 8001:80 rossbachp/docker-tdd:conli2014`
- open your browser with `http :8001/docker-tdd`
- view german slides[3]

## find source

- [serverspecbox](https://github.com/rossbachp/serverspecbox)

Feedback welcome

[Peter Rossbach][4]

[1]: https://jax.de/wjax2014/sessions/testgetriebene-infrastruktur-als-code
[2]: http://www.continuouslifecycle.de/lecture.php?id=4575
[3]: https://speakerdeck.com/rossbachp/triple-d-test-driven-docker-development
[4]: https://twitter.com/PRossbach

Peter Rossbach

November 18, 2014
Tweet

More Decks by Peter Rossbach

Other Decks in Programming

Transcript

  1. 10 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim FROM ubuntu:14.04 RUN apt-get update && apt-get upgrade -yq #upgrade only to shell shocker fix #RUN apt-get install --only-upgrade bash RUN apt-get install -y apache2 RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/log/apache2 EXPOSE 80 CMD "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
  2. 28 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim $ mkdir -p tomcat/spec $ cd tomcat/spec $ vi tomcat_specs.rb
  3. 29 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim require 'docker' describe "apache tomcat8 image" do before(:all) { @image = Docker::Image.all(). detect{|i| i.info['RepoTags']. detect{|r| r == "bee42/tomcat:8"}} } it "should be available" do expect(@image).to_not be_nil end end
  4. 30 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim source 'https://rubygems.org' ruby '2.1.2' gem 'rspec', '~> 2.99' gem 'docker-api', '~>1.13.2' cd .. vi Gemfile bundle install --path vendor echo 'alias tdd="bundle exec rspec $@"' >> ~/.bash_aliases source . ~/.bash_aliases
  5. 31 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim $ tdd spec/tomcat_spec.rb F Failures: 1) apache tomcat8 image should be availble Failure/Error: expect(@image).to_not be_nil expected: not nil got: nil # ./spec/tomcat_spec.rb:9:in `block (2 levels) in <top (required)>' Finished in 0.12047 seconds 1 example, 1 failure Failed examples: rspec ./spec/tomcat_spec.rb:8 # apache tomcat8 image should be availble
  6. 33 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim $ git config --global user.email "[email protected]" $ git config --global user.name "rossbachp" $ vi .gitignore $ git init $ git add . $ git commit -m "Setup at first failing test" .DS_Store Gemfile.lock vendor .bundle
  7. 34 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim $ mkdir -p docker/bee42/tomcat8 $ cd docker/bee42/tomcat8 $ vi Dockerfile $ docker build -t="bee42/tomcat:8" . Sending build context to Docker daemon 2.56 kB Sending build context to Docker daemon Step 0 : FROM ubuntu:14.04 ---> 96864a7d2df3 Step 1 : MAINTAINER Peter Rossbach <[email protected]> ---> Using cache ---> 9ec0f7eb38d8 Successfully built 9ec0f7eb38d8 $ docker images | grep 'bee42/tomcat:8' bee42/tomcat8 latest 9ec0f7eb38d8 24 hours ago FROM ubuntu:14.04 MAINTAINER Peter Rossbach <[email protected]>
  8. 35 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim $ cd ../../.. $ tdd spec/tomcat_spec.rb . Finished in 0.10493 seconds 1 example, 0 failures
  9. 37 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim $ vi build.sh $ chmod -x build.sh $ ./build.sh
  10. 38 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim #!/bin/bash echo "Building docker image:" cd docker/bee42/tomcat8 docker build -t=bee42/tomcat8 . echo cd ../../.. echo "Executing tests:" bundle exec rspec spec/tomcat_spec.rb git add . git commit -m "add content of images, build it and check that is available a
  11. 39 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim it "should expose the default tomcat tcp port" do expect(@image.json["Config"]["ExposedPorts"]). to include("8080/tcp") end
  12. 40 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim Executing tests: .F Failures: 1) apache tomcat8 image should expose the default tomcat tcp port Failure/Error: expect(@image.json["Config"]["ExposedPorts"]).to include( NoMethodError: undefined method `include?' for nil:NilClass # ./spec/tomcat_spec.rb:13:in `block (2 levels) in <top (required)>' Finished in 0.11131 seconds 2 examples, 1 failure Failed examples: rspec ./spec/tomcat_spec.rb:12 # apache tomcat8 image should expose the default
  13. 42 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim FROM ubuntu:14.04 MAINTAINER Peter Rossbach <[email protected]> EXPOSE 8080 $ ./build.sh Building docker image: Sending build context to Docker daemon 2.56 kB Sending build context to Docker daemon Step 0 : FROM ubuntu:14.04 ---> 96864a7d2df3 Step 1 : MAINTAINER Peter Rossbach <[email protected]> ---> Using cache ---> 9ec0f7eb38d8 Step 2 : EXPOSE 8080 ---> Using cache ---> 8e2f8e5a7e4b Successfully built 8e2f8e5a7e4b Executing tests: .. Finished in 0.12662 seconds 2 examples, 0 failures
  14. 44 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim describe "running java" do before(:all) { @container = Docker::Container.create( 'Image' => 'bee42/tomcat8:latest', 'Cmd' => ['java', '-version'], 'Tty' => true) } after(:all) do @container.kill @container.delete(:force => true) end it "is java expected" do expect(@container.tap(&:start). attach(:tty => true)[0][0]).to include( "1.7.0_65") end end
  15. 46 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim RUN apt-get update && \ apt-get install -yq openjdk-7-jre-headless
  16. 48 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim describe "check tomcat version" do before(:all) { @container = Docker::Container.create( 'Image' => 'bee42/tomcat:8', 'Cmd' => '/opt/tomcat/bin/version.sh', 'Tty' => true) } after(:all) do @container.kill @container.delete(:force => true) end it "is tomcat expected" do expect(@container.tap(&:start). attach(:tty => true)[0][2]). to include( "Apache Tomcat/8.0.12") end end
  17. 50 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim ... RUN apt-get install -yq wget RUN wget -q https://archive.apache.org/dist/tomcat/tomcat-8/v8.0.12/bin/apac RUN mv apache-tomcat* /opt/tomcat
  18. 52 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim require 'net/http' ... describe "running tomcat as a container" do before(:all) do # docker run -d -p 8080:8080 bee42/tomcat8 \ # /opt/tomcat/bin/catalina.sh run id = `docker run -d -p 8080:8080 bee42/tomcat8 /opt/tomcat/bin/catalina.sh run`.chom @container = Docker::Container.get(id) sleep 3 uri = URI('http://127.0.0.1:8080/index.jsp') @res = Net::HTTP.get_response(uri) end after(:all) do @container.kill end it "should accept connection to the container tomcat port" do expect(@res.code).to match '200' end end
  19. 53 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim $ tdd spec/tomcat_spec.rb ..... Finished in 7.38 seconds 5 examples, 0 failures
  20. 55 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim ENV TOMCAT_MAJOR_VERSION 8 ENV TOMCAT_MINOR_VERSION 8.0.12 ENV CATALINA_HOME /opt/tomcat RUN apt-get install -yq wget RUN wget -q https://archive.apache.org/dist/tomcat/tomcat-${TOMCAT_MAJOR_VER wget -qO- https://www.apache.org/dist/tomcat/tomcat-${TOMCAT_MAJOR_VERSI tar zxf apache-tomcat-*.tar.gz && \ rm apache-tomcat-*.tar.gz && \ mv apache-tomcat* ${CATALINA_HOME}
  21. 58 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim #!/bin/bash DATE=`date +'%Y%m%d%H%M'` ACCOUNT=bee42 IMAGE=tomcat8 IMAGETAG=${ACCOUNT}/${IMAGE}:${DATE} echo "= build image $IMAGETAG" cd docker/bee42/tomcat8 docker build -t="$IMAGETAG" . BUILDID=$(docker inspect -f "{{.Id}}" $IMAGETAG) echo "= squash image $IMAGETAG" docker save $BUILDID | docker-squash -t $IMAGETAG-squash -from root | docker load SQUASHID=$(docker inspect -f "{{.Id}}" $IMAGETAG-squash) docker tag $SQUASHID ${ACCOUNT}/${IMAGE}:latest if [ "$1" == "-rmi" ] ; then echo "= remove build image $IMAGETAG with id $BUILDID" docker rmi $BUILDID fi
  22. 59 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim = squash image bee42/tomcat8:201409241010 $ docker images
  23. 60 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim $ ls build.sh docker Gemfile Gemfile.lock spec squash.sh vendor $ tdd spec/tomcat_spec.rb ..... Finished in 8.43 seconds 5 examples, 0 failures
  24. 65 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim require 'spec_helper' describe package('httpd') do it { should be_installed } end describe service('httpd') do it { should be_enabled } it { should be_running } end describe port(80) do it { should be_listening } end describe file('/etc/httpd/conf/httpd.conf') do it { should be_file } its(:content) { should match /ServerName www.example.jp/ } end
  25. 66 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim describe docker_image('busybox:latest') do it { should exist } its(['Architecture']) { should eq 'amd64' } its(['Config.Cmd']) { should include '/bin/sh' } end describe docker_image("bee42/tomcat:8") do its(['Config.Cmd']) { should include '/opt/tomcat/bin/catalina.sh' } end describe docker_container('tomcat8') do it { should exist } it { should be_running } it { should have_volume('/tmp', '/data') } its(['HostConfig.NetworkMode']) { should eq 'bridge' } end
  26. 72 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim require 'serverspec' set :os, :arch => 'x86_64', :family => 'debian', :distro => 'ubuntu', :relea set :backend, :nsenter set :docker_cid, "tomcat8" set :nsenter_pid, nil puts "#{Specinfra.configuration.docker_cid}" describe package('wget') do it { should be_installed } end
  27. 74 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim Rollen Konfiguration Aspekten
  28. 75 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim Host Container Roles Tags spec ├── all │ ├── lldpd_spec.rb │ └── network_spec.rb ├── memcache │ └── memcached_spec.rb └── web └── apache2_spec.rb
  29. 76 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim describe file('/data/images'), :tag => "paris" do it { should be_mounted.with( :type => 'nfs' ) } end
  30. 97 / 99 © 2014 <[email protected]>, @PRossbach und <[email protected]>, @aschmidt75

    - WJAX Munich and ConLi Mannheim http://<docker host>:8000/docker-tdd http://<docker host>:8000/docker-tdd/tomcat-reference.tar.gz docker run -tid -p 8000:80 rossbachp/docker-tdd:wjax2014 docker run -tid -p 8000:80 rossbachp/docker-tdd:conli2014