systemadmin.es > Gestión de cofiguración > Beaker: Pruebas de modulos de puppet en maquinas virtuales (vagrant)

Beaker: Pruebas de modulos de puppet en maquinas virtuales (vagrant)

Una buena forma de comprobar si un modulo de puppet hace lo que se espera de él es hacer pruebas con maquinas virtuales. Vamos a ver como crear pruebas mediante beaker con vagrant

Usaremos de ejemplo eyp-yuicompressor. Dejaremos los ficheros necesarios dentro del modulo el directorio /spec/acceptance

Dentro de éste crearemos la definición de las maquinas virtuales en spec/acceptance/nodesets

$ mkdir -p eyp-yuicompressor/spec/acceptance/nodesets

Editaremos el fichero eyp-yuicompressor/spec/acceptance/nodesets/default.yml con la maquina a instanciar:

HOSTS:
  centos-65-x64:
    roles:
      - master
    platform: el-6-x86_64
    box : centos-65-x64-vbox436-nocm
    box_url : http://puppet-vagrant-boxes.puppetlabs.com/centos-65-x64-virtualbox-nocm.box
    hypervisor : vagrant
CONFIG:
  type: foss

A continuación deberemos crear el helper para instalar el modulo y sus dependencias (eyp-yuicompressor/spec/spec_helper_acceptance.rb)

require 'beaker-rspec'
require 'beaker/puppet_install_helper'

run_puppet_install_helper

RSpec.configure do |c|
  # Project root
  proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))

  # Readable test descriptions
  c.formatter = :documentation

  # Configure all nodes in nodeset
  c.before :suite do
    # Install module and dependencies
    puppet_module_install(:source => proj_root, :module_name => 'yuicompressor')
    hosts.each do |host|
      # dependencies
      on host, puppet('module', 'install', 'puppetlabs-stdlib'), { :acceptable_exit_codes => [0,1] }
    end
  end
end

A continuación definiremos los tests que queremos hacer (eyp-yuicompressor/spec/acceptance/standard_spec.rb)

require 'spec_helper_acceptance'

describe 'yuicompressor class' do

  context 'default parameters' do
    # Using puppet_apply as a helper
    it 'should work with no errors based on the example' do
      pp = <<-EOS

        package { 'java-1.8.0-openjdk':
          ensure => 'installed',
        } ->

        class { 'yuicompressor':
        }
      EOS

      # Run it twice and test for idempotency
      expect(apply_manifest(pp).exit_code).to_not eq(1)
      expect(apply_manifest(pp).exit_code).to eq(0)
    end

    it 'yui --version should give us the default version (2.4.8)' do
       shell("/usr/local/bin/yui --version") do |r|
         expect(r.stderr).to match(/2\.4\.8/)
       end
     end

  end
end

Podemos ejecutar los tests mediante rspec:

$ rspec spec/acceptance/
/opt/beaker/lib/ruby/gems/2.1.0/gems/beaker-rspec-5.3.0/lib/beaker-rspec/helpers/serverspec.rb:43: warning: already initialized constant Module::VALID_OPTIONS_KEYS
/opt/beaker/lib/ruby/gems/2.1.0/gems/specinfra-2.44.1/lib/specinfra/configuration.rb:4: warning: previous definition of VALID_OPTIONS_KEYS was here
Beaker::Hypervisor, found some vagrant boxes to create
Bringing machine 'centos-65-x64' up with 'virtualbox' provider...
==> centos-65-x64: Importing base box 'centos-65-x64-vbox4210-nocm'...
Progress: 10%Progress: 20%Progress: 40%Progress: 60%Progress: 80%Progress: 90%==> centos-65-x64: Matching MAC address for NAT networking...
==> centos-65-x64: Setting the name of the VM: defaultyml_centos-65-x64_1445272394032_72091
==> centos-65-x64: Clearing any previously set forwarded ports...
==> centos-65-x64: Clearing any previously set network interfaces...
==> centos-65-x64: Preparing network interfaces based on configuration...
    centos-65-x64: Adapter 1: nat
    centos-65-x64: Adapter 2: hostonly
==> centos-65-x64: Forwarding ports...
    centos-65-x64: 22 => 2222 (adapter 1)
==> centos-65-x64: Running 'pre-boot' VM customizations...
==> centos-65-x64: Booting VM...
==> centos-65-x64: Waiting for machine to boot. This may take a few minutes...
    centos-65-x64: SSH address: 127.0.0.1:2222
    centos-65-x64: SSH username: vagrant
    centos-65-x64: SSH auth method: private key
    centos-65-x64: Warning: Connection timeout. Retrying...
    centos-65-x64: Warning: Connection timeout. Retrying...
    centos-65-x64: Warning: Connection timeout. Retrying...
    centos-65-x64: Warning: Connection timeout. Retrying...
    centos-65-x64: Warning: Connection timeout. Retrying...
    centos-65-x64: Warning: Remote connection disconnect. Retrying...
==> centos-65-x64: Machine booted and ready!
==> centos-65-x64: Checking for guest additions in VM...
    centos-65-x64: The guest additions on this VM do not match the installed version of
    centos-65-x64: VirtualBox! In most cases this is fine, but in rare cases it can
    centos-65-x64: prevent things such as shared folders from working properly. If you see
    centos-65-x64: shared folder errors, please make sure the guest additions within the
    centos-65-x64: virtual machine match the version of VirtualBox you have installed on
    centos-65-x64: your host and reload your VM.
    centos-65-x64: 
    centos-65-x64: Guest Additions Version: 4.2.10
    centos-65-x64: VirtualBox Version: 4.3
==> centos-65-x64: Setting hostname...
==> centos-65-x64: Configuring and enabling network interfaces...
==> centos-65-x64: Mounting shared folders...
    centos-65-x64: /vagrant => /home/jprats/git/puppet-things/audi/eyp-yuicompressor/.vagrant/beaker_vagrant_files/default.yml
Warning: ssh connection to centos-65-x64 has been terminated
Warning: ssh connection to centos-65-x64 has been terminated
localhost $ scp /home/jprats/git/puppet-things/audi/eyp-yuicompressor centos-65-x64:/etc/puppet/modules {:ignore => [".bundle", ".git", ".idea", ".vagrant", ".vendor", "vendor", "acceptance", "bundle", "spec", "tests", "log", ".", ".."]}

yuicompressor class
  default parameters
localhost $ scp /tmp/beaker20151019-29442-yd5gbk centos-65-x64:/tmp/apply_manifest.pp.ItQkXn {:ignore => }
localhost $ scp /tmp/beaker20151019-29442-xde0kj centos-65-x64:/tmp/apply_manifest.pp.gFyNfF {:ignore => }
    should work with no errors based on the example
    yui --version should give us the default version (2.4.8)
Warning: ssh connection to centos-65-x64 has been terminated
Destroying vagrant boxes
==> centos-65-x64: Forcing shutdown of VM...
==> centos-65-x64: Destroying VM and associated drives...

Finished in 2 minutes 30.1 seconds (files took 3 minutes 46.5 seconds to load)
2 examples, 0 failures

Deja un comentario:

XHTML - Tags permitidos:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>