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

Testumgebungen automatisiert und reproduzierbar...

Testumgebungen automatisiert und reproduzierbar erstellen

Der Vortrag zeigt, wie mit Vagrant virtualisierte Testumgebungen automatisiert erstellt werden können. Die Umgebungen können von Entwicklern als Sandbox für exploratives Testen verwendet werden. Außerdem stellen sie automatisierten Builds die benötigten Dienste für Integrationstests zur Verfügung.

Jörg Rathlev

May 13, 2014
Tweet

Other Decks in Programming

Transcript

  1. ‣ Testsysteme automatisiert erstellen
 mit Vagrant und Maven ‣ Überblick

    über die Tool-Landschaft ‣ Tipps und Tricks aus der Praxis Inhalt
  2. Wie testet man ein
 Data Warehouse? Image © Larry Johnson,

    https://www.flickr.com/photos/drljohnson/4917215160
  3. public class ExampleDaoTest { private EmbeddedDatabase db; @Before
 public void

    setUp() throws Exception {
 db = new EmbeddedDatabaseBuilder()
 .setType(EmbeddedDatabaseType.HSQL)
 .addScript("jdbc/schema.sql")
 .build();
 } Was bisher geschah …
  4. public class ExampleDaoTest { private EmbeddedDatabase db; @Before
 public void

    setUp() throws Exception {
 db = new EmbeddedDatabaseBuilder()
 .setType(EmbeddedDatabaseType.HSQL)
 .addScript("jdbc/schema.sql")
 .build();
 } Was bisher geschah … Aber: Datenbank in Produktion
 ist PostgreSQL
  5. Vagrant Vagrant ist ein Werkzeug, mit dem automatisiert virtuelle Maschinen

    erzeugt und konfiguriert werden. ! http://www.vagrantup.com/
  6. ‣ Öffnet SSH-Verbindung zur virtuellen Maschine ‣ Default: User „vagrant“,

    Anmeldung mit in Vagrant hinterlegtem SSH-Schlüssel ‣ Achtung: „privater“ Schlüssel wird mit Vagrant ausgeliefert. Nicht verwenden für öffentlich zugängliche Maschinen! ‣ Alternative Schlüssel können konfiguriert werden vagrant ssh
  7. Vagrantfile Vagrant.configure("2") do |config| ! config.vm.box = "centos6-6.4" config.vm.box_url =

    "http://..." config.vm.network :forwarded_port, guest: 5432, host: 5432 config.vm.network :forwarded_port, guest: 8080, host: 8080 config.vm.synced_folder "src/main/resources/database/", "/database" ! config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "2048"] end ! config.vm.provision "puppet_server" do |puppet| ... end end
  8. Vagrantfile: Base Box Vagrant.configure("2") do |config| ! config.vm.box = "centos6-6.4"

    config.vm.box_url = "http://..." config.vm.network :forwarded_port, guest: 5432, host: 5432 config.vm.network :forwarded_port, guest: 8080, host: 8080 config.vm.synced_folder "src/main/resources/database/", "/database" ! config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "2048"] end ! config.vm.provision "puppet_server" do |puppet| ... end end
  9. Vagrantfile: Provisioning Vagrant.configure("2") do |config| ! config.vm.box = "centos6-6.4" config.vm.box_url

    = "http://..." config.vm.network :forwarded_port, guest: 5432, host: 5432 config.vm.network :forwarded_port, guest: 8080, host: 8080 config.vm.synced_folder "src/main/resources/database/", "/database" ! config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "2048"] end ! config.vm.provision "puppet_server" do |puppet| ... end end
  10. Vagrantfile: Provisioning Vagrant.configure("2") do |config| ! config.vm.box = "centos6-6.4" config.vm.box_url

    = "http://..." config.vm.network :forwarded_port, guest: 5432, host: 5432 config.vm.network :forwarded_port, guest: 8080, host: 8080 config.vm.synced_folder "src/main/resources/database/", "/database" ! config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "2048"] end ! config.vm.provision "puppet_server" do |puppet| ... end end ‣ Puppet ‣ Chef ‣ Shell-Skripte ‣ Ansible ‣ CFEngine ‣ …
  11. Vagrantfile: Port Forwarding Vagrant.configure("2") do |config| ! config.vm.box = "centos6-6.4"

    config.vm.box_url = "http://..." config.vm.network :forwarded_port, guest: 5432, host: 5432 config.vm.network :forwarded_port, guest: 8080, host: 8080 config.vm.synced_folder "src/main/resources/database/", "/database" ! config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "2048"] end ! config.vm.provision "puppet_server" do |puppet| ... end end
  12. Vagrantfile: Synced Folders Vagrant.configure("2") do |config| ! config.vm.box = "centos6-6.4"

    config.vm.box_url = "http://..." config.vm.network :forwarded_port, guest: 5432, host: 5432 config.vm.network :forwarded_port, guest: 8080, host: 8080 config.vm.synced_folder "src/main/resources/database/", "/database" ! config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "2048"] end ! config.vm.provision "puppet_server" do |puppet| ... end end
  13. Puppet-Manifest class s24::roles::sea_controlling { class { 'java': vendor => 'oracle',

    } ! class { 'postgresql::server': listen_addresses => '*', postgres_password => 'secret', } ! ! # memory settings (not in vagrant) if (!$::is_vagrant) { postgresql::server::config_entry { 'shared_buffers': value => '180GB', } } ! ...
  14. Flyway als Maven-Plugin <plugin> <groupId>com.googlecode.flyway</groupId> <artifactId>flyway-maven-plugin</artifactId> <version>2.3.1</version> <configuration> <locations> <location>

    filesystem:${basedir}/src/main/resources/database </location> </locations> <url>${database-url}</url> </configuration> <dependencies> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.1-901.jdbc4</version> </dependency> </dependencies> </plugin>
  15. Maven: Flyway konfigurieren <plugin> <groupId>com.googlecode.flyway</groupId> <artifactId>flyway-maven-plugin</artifactId> <version>2.3.1</version> <configuration> <locations> <location>

    filesystem:${basedir}/src/main/resources/database </location> </locations> <url>${database-url}</url> </configuration> <dependencies> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.1-901.jdbc4</version> </dependency> </dependencies> </plugin> Wird über ein Maven-Profil gesetzt (live / test)
  16. Maven: Flyway konfigurieren <profile> <!-- Run integration tests against the

    integration test database --> <id>integration-test</id> <properties> <database-url>jdbc:postgresql://localhost:5432/...</database-url> </properties> <build> <plugins> <plugin> <groupId>com.googlecode.flyway</groupId> <artifactId>flyway-maven-plugin</artifactId> <executions> <execution> <goals> <goal>clean</goal> <goal>migrate</goal> </goals> ...
  17. Maven: Testausführung mit Flyway <profile> <!-- Run integration tests against

    the integration test database --> <id>integration-test</id> <properties> <database-url>jdbc:postgresql://localhost:5432/...</database-url> </properties> <build> <plugins> <plugin> <groupId>com.googlecode.flyway</groupId> <artifactId>flyway-maven-plugin</artifactId> <executions> <execution> <goals> <goal>clean</goal> <goal>migrate</goal> </goals> ...
  18. Testumgebung in der Cloud $ vagrant up --provider=aws ‣ Virtualbox

    ‣ AWS ‣ VMWare ‣ Hyper-V ‣ Docker ‣ …
  19. Testsystem richtet Datenbank für Test ein Vagrant erzeugt startet richtet

    System ein ‣ PostgreSQL ‣ Tomcat ‣ … Tests verwenden Maven startet
  20. vagrant ssh-config $ vagrant ssh-config > tmpfile $ ssh -F

    tmpfile In Deployment-Skripten verwenden
  21. “One of the nice things about being on the cloooooooooooud

    is that, if you've got your infrastructure managed right, a developer who wants a sandbox – for any reason – should be able to get one with about as much difficulty as getting office paper, and be able to bin it with about as much regret.” ! Patrick McKenzie
  22. ‣ Torsten Bøgh Köster: Provisioning Vagrant using a Puppet master

    - the reliable way
 https://developer.s24.com/blog/11-14-2013/vagrant_with_puppetmaster.html
 Erklärt, wie man in einer Vagrant-Umgebung die Zertifikate für eine Authentifizierung am Puppetmaster einrichtet. ‣ Jörg Rathlev: Database Integration Tests with Vagrant and Maven
 https://developer.s24.com/blog/02-21-2014/database-integration-tests-vagrant-maven.html ‣ Daniel Lucks: Tutorial: Create a development environment using Vagrant and Puppet
 https://developer.s24.com/blog/12-22-2013/development-environment-with-vagrant-and-puppet.html
 Testumgebungen mit Vagrant für die Web-Entwicklung erstellen. ‣ Adam Wiggins: The Twelve-Factor App
 http://12factor.net/
 Empfehlungen für Architektur und Entwicklungsprozess, um „Cloud-fähige“ Anwendungen zu entwickeln. ‣ Tools
 Vagrant: http://www.vagrantup.com/
 Virtualbox: https://www.virtualbox.org/
 Puppet: http://puppetlabs.com/
 Maven: http://maven.apache.org/
 Flyway: http://flywaydb.org/
 Packer: http://www.packer.io/ (Tool zum Erstellen von Vagrant-Baseboxen, AMIs etc.)
 Quellen & Lesetipps