Browse Source

(FM-161) Add beaker tests for parameter coverage

Hunter Haugen 10 năm trước cách đây
mục cha
commit
eda78abb6e

+ 1 - 1
.fixtures.yml

@@ -1,3 +1,3 @@
 fixtures:
   symlinks:
-    inifile: '#{source_dir}'
+    inifile: "#{source_dir}"

+ 4 - 0
Gemfile

@@ -5,6 +5,10 @@ group :development, :test do
   gem 'rspec-puppet',           :require => false
   gem 'puppetlabs_spec_helper', :require => false
   gem 'simplecov',              :require => false
+  gem 'beaker',                 :require => false
+  gem 'beaker-rspec',           :require => false
+  gem 'puppet-lint',            :require => false
+  gem 'serverspec',             :require => false
   gem 'pry',                    :require => false
 end
 

+ 12 - 0
spec/acceptance/basic_spec.rb

@@ -0,0 +1,12 @@
+require 'spec_helper_acceptance'
+
+# Here we put the more basic fundamental tests, ultra obvious stuff.
+describe "basic tests:" do
+  it 'copies the module across' do
+    # No point diagnosing any more if the module wasn't copied properly
+    shell "ls #{default['distmoduledir']}/inifile" do |r|
+      expect(r.stdout).to match(/Modulefile/)
+      expect(r.stderr).to be_empty
+    end
+  end
+end

+ 288 - 0
spec/acceptance/ini_setting_spec.rb

@@ -0,0 +1,288 @@
+require 'spec_helper_acceptance'
+
+describe 'ini_setting resource' do
+  after :all do
+    shell("rm /tmp/*.ini", :acceptable_exit_codes => [0,1])
+  end
+
+  shared_examples 'has_content' do |path,pp,content|
+    before :all do
+      shell("rm #{path}", :acceptable_exit_codes => [0,1])
+    end
+    after :all do
+      shell("cat #{path}", :acceptable_exit_codes => [0,1])
+      shell("rm #{path}", :acceptable_exit_codes => [0,1])
+    end
+
+    it 'applies the manifest twice with no stderr' do
+      expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
+      expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("")
+    end
+
+    describe file(path) do
+      it { should be_file }
+      it { should contain(content) }
+    end
+  end
+
+  shared_examples 'has_error' do |path,pp,error|
+    before :all do
+      shell("rm #{path}", :acceptable_exit_codes => [0,1])
+    end
+    after :all do
+      shell("cat #{path}", :acceptable_exit_codes => [0,1])
+      shell("rm #{path}", :acceptable_exit_codes => [0,1])
+    end
+
+    it 'applies the manifest and gets a failure message' do
+      expect(apply_manifest(pp, :expect_failures => true).stderr).to match(error)
+    end
+
+    describe file(path) do
+      it { should_not be_file }
+    end
+  end
+
+  describe 'ensure parameter' do
+    context '=> present for global and section' do
+      pp = <<-EOS
+      ini_setting { 'ensure => present for section':
+        ensure  => present,
+        path    => '/tmp/ini_setting.ini',
+        section => 'one',
+        setting => 'two',
+        value   => 'three',
+      }
+      ini_setting { 'ensure => present for global':
+        ensure  => present,
+        path    => '/tmp/ini_setting.ini',
+        section => '',
+        setting => 'four',
+        value   => 'five',
+      }
+      EOS
+
+      it 'applies the manifest twice with no stderr' do
+        expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
+        expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("")
+      end
+
+      describe file('/tmp/ini_setting.ini') do
+        it { should be_file }
+        it { should contain("four = five\n[one]\ntwo = three") }
+      end
+    end
+
+    context '=> absent for key/value' do
+      before :all do
+        shell('echo -e "four = five\n[one]\ntwo = three" > /tmp/ini_setting.ini')
+      end
+
+      pp = <<-EOS
+      ini_setting { 'ensure => absent for key/value':
+        ensure  => absent,
+        path    => '/tmp/ini_setting.ini',
+        section => 'one',
+        setting => 'two',
+        value   => 'three',
+      }
+      EOS
+
+      it 'applies the manifest twice with no stderr' do
+        expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
+        expect(apply_manifest(pp, :catch_changes  => true).stderr).to eq("")
+      end
+
+      describe file('/tmp/ini_setting.ini') do
+        it { should be_file }
+        it { should contain('four = five') }
+        it { should contain('[one]') }
+        it { should_not contain('two = three') }
+      end
+    end
+
+    context '=> absent for section', :pending => "cannot ensure absent on a section"  do
+      before :all do
+        shell('echo -e "four = five\n[one]\ntwo = three" > /tmp/ini_setting.ini')
+      end
+      after :all do
+        shell("cat /tmp/ini_setting.ini", :acceptable_exit_codes => [0,1])
+        shell("rm /tmp/ini_setting.ini", :acceptable_exit_codes => [0,1])
+      end
+
+      pp = <<-EOS
+      ini_setting { 'ensure => absent for section':
+        ensure  => absent,
+        path    => '/tmp/ini_setting.ini',
+        section => 'one',
+      }
+      EOS
+
+      it 'applies the manifest twice with no stderr' do
+        expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
+        expect(apply_manifest(pp, :catch_changes  => true).stderr).to eq("")
+      end
+
+      describe file('/tmp/ini_setting.ini') do
+        it { should be_file }
+        it { should contain('four = five') }
+        it { should_not contain('[one]') }
+        it { should_not contain('two = three') }
+      end
+    end
+
+    context '=> absent for global' do
+      before :all do
+        shell('echo -e "four = five\n[one]\ntwo = three" > /tmp/ini_setting.ini')
+      end
+      after :all do
+        shell("cat /tmp/ini_setting.ini", :acceptable_exit_codes => [0,1])
+        shell("rm /tmp/ini_setting.ini", :acceptable_exit_codes => [0,1])
+      end
+
+      pp = <<-EOS
+      ini_setting { 'ensure => absent for global':
+        ensure  => absent,
+        path    => '/tmp/ini_setting.ini',
+        section => '',
+        setting => 'four',
+        value   => 'five',
+      }
+      EOS
+
+      it 'applies the manifest twice with no stderr' do
+        expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
+        expect(apply_manifest(pp, :catch_changes  => true).stderr).to eq("")
+      end
+
+      describe file('/tmp/ini_setting.ini') do
+        it { should be_file }
+        it { should_not contain('four = five') }
+        it { should contain('[one]') }
+        it { should contain('two = three') }
+      end
+    end
+  end
+
+  describe 'section, setting, value parameters' do
+    {
+      "section => 'test', setting => 'foo', value => 'bar',"   => "[test]\nfoo = bar",
+      "section => 'more', setting => 'baz', value => 'quux',"  => "[more]\nbaz = quux",
+      "section => '',     setting => 'top', value => 'level'," => "top = level",
+    }.each do |parameter_list, content|
+      context parameter_list do
+        pp = <<-EOS
+        ini_setting { "#{parameter_list}":
+          ensure  => present,
+          path    => '/tmp/ini_setting.ini',
+          #{parameter_list}
+        }
+        EOS
+
+        it_behaves_like 'has_content', '/tmp/ini_setting.ini', pp, content
+      end
+    end
+
+    {
+      "section => 'test',"                   => /setting is a required.+value is a required/,
+      "setting => 'foo',  value   => 'bar'," => /section is a required/,
+      "section => 'test', setting => 'foo'," => /value is a required/,
+      "section => 'test', value   => 'bar'," => /setting is a required/,
+      "value   => 'bar',"                    => /section is a required.+setting is a required/,
+      "setting => 'foo',"                    => /section is a required.+value is a required/,
+    }.each do |parameter_list, error|
+      context parameter_list, :pending => 'no error checking yet' do
+        pp = <<-EOS
+        ini_setting { "#{parameter_list}":
+          ensure  => present,
+          path    => '/tmp/ini_setting.ini',
+          #{parameter_list}
+        }
+        EOS
+
+        it_behaves_like 'has_error', '/tmp/ini_setting.ini', pp, error
+      end
+    end
+  end
+
+  describe 'path parameter' do
+    [
+      "/tmp/one.ini",
+      "/tmp/two.ini",
+      "/tmp/three.ini",
+    ].each do |path|
+      context "path => #{path}" do
+        pp = <<-EOS
+        ini_setting { 'path => #{path}':
+          ensure  => present,
+          section => 'one',
+          setting => 'two',
+          value   => 'three',
+          path    => '#{path}',
+        }
+        EOS
+
+        it_behaves_like 'has_content', path, pp, "[one]\ntwo = three"
+      end
+    end
+
+    context "path => foo" do
+      pp = <<-EOS
+        ini_setting { 'path => foo':
+          ensure     => present,
+          section    => 'one',
+          setting    => 'two',
+          value      => 'three',
+          path       => 'foo',
+        }
+      EOS
+
+      it_behaves_like 'has_error', 'foo', pp, /must be fully qualified/
+    end
+  end
+
+  describe 'key_val_separator parameter' do
+    {
+      ""                             => "two = three",
+      "key_val_separator => '=',"    => "two=three",
+      "key_val_separator => ' =  '," => "two =  three",
+    }.each do |parameter, content|
+      context "with \"#{parameter}\" makes \"#{content}\"" do
+        pp = <<-EOS
+        ini_setting { "with #{parameter} makes #{content}":
+          ensure  => present,
+          section => 'one',
+          setting => 'two',
+          value   => 'three',
+          path    => '/tmp/key_val_separator.ini',
+          #{parameter}
+        }
+        EOS
+
+        it_behaves_like 'has_content', '/tmp/key_val_separator.ini', pp, content
+      end
+    end
+
+    {
+      "key_val_separator => '',"      => /must contain exactly one/,
+      "key_val_separator => ',',"     => /must contain exactly one/,
+      "key_val_separator => '   ',"   => /must contain exactly one/,
+      "key_val_separator => ' ==  '," => /must contain exactly one/,
+    }.each do |parameter, error|
+      context "with \"#{parameter}\" raises \"#{error}\"" do
+        pp = <<-EOS
+        ini_setting { "with #{parameter} raises #{error}":
+          ensure  => present,
+          section => 'one',
+          setting => 'two',
+          value   => 'three',
+          path    => '/tmp/key_val_separator.ini',
+          #{parameter}
+        }
+        EOS
+
+        it_behaves_like 'has_error', '/tmp/key_val_separator.ini', pp, error
+      end
+    end
+  end
+end

+ 143 - 0
spec/acceptance/ini_subsetting_spec.rb

@@ -0,0 +1,143 @@
+require 'spec_helper_acceptance'
+
+describe 'ini_subsetting resource' do
+  after :all do
+    shell("rm /tmp/*.ini", :acceptable_exit_codes => [0,1])
+  end
+
+  shared_examples 'has_content' do |path,pp,content|
+    before :all do
+      shell("rm #{path}", :acceptable_exit_codes => [0,1])
+    end
+    after :all do
+      shell("cat #{path}", :acceptable_exit_codes => [0,1])
+      shell("rm #{path}", :acceptable_exit_codes => [0,1])
+    end
+
+    it 'applies the manifest twice with no stderr' do
+      expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
+      expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("")
+    end
+
+    describe file(path) do
+      it { should be_file }
+      it { should contain(content) }
+    end
+  end
+
+  shared_examples 'has_error' do |path,pp,error|
+    before :all do
+      shell("rm #{path}", :acceptable_exit_codes => [0,1])
+    end
+    after :all do
+      shell("cat #{path}", :acceptable_exit_codes => [0,1])
+      shell("rm #{path}", :acceptable_exit_codes => [0,1])
+    end
+
+    it 'applies the manifest and gets a failure message' do
+      expect(apply_manifest(pp, :expect_failures => true).stderr).to match(error)
+    end
+
+    describe file(path) do
+      it { should_not be_file }
+    end
+  end
+
+  describe 'ensure, section, setting, subsetting, & value parameters' do
+    context '=> present with subsections' do
+      pp = <<-EOS
+      ini_subsetting { 'ensure => present for alpha':
+        ensure     => present,
+        path       => '/tmp/ini_subsetting.ini',
+        section    => 'one',
+        setting    => 'key',
+        subsetting => 'alpha',
+        value      => 'bet',
+      }
+      ini_subsetting { 'ensure => present for beta':
+        ensure     => present,
+        path       => '/tmp/ini_subsetting.ini',
+        section    => 'one',
+        setting    => 'key',
+        subsetting => 'beta',
+        value      => 'trons',
+      }
+      EOS
+
+      it 'applies the manifest twice with no stderr' do
+        expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
+        expect(apply_manifest(pp, :catch_changes => true).stderr).to eq("")
+      end
+
+      describe file('/tmp/ini_subsetting.ini') do
+        it { should be_file }
+        it { should contain("[one]\nkey = alphabet betatrons") }
+      end
+    end
+
+    context 'ensure => absent' do
+      before :all do
+        shell('echo -e "[one]\nkey = alphabet betatrons" > /tmp/ini_subsetting.ini')
+      end
+
+      pp = <<-EOS
+      ini_subsetting { 'ensure => absent for subsetting':
+        ensure     => absent,
+        path       => '/tmp/ini_subsetting.ini',
+        section    => 'one',
+        setting    => 'key',
+        subsetting => 'alpha',
+      }
+      EOS
+
+      it 'applies the manifest twice with no stderr' do
+        expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
+        expect(apply_manifest(pp, :catch_changes  => true).stderr).to eq("")
+      end
+
+      describe file('/tmp/ini_subsetting.ini') do
+        it { should be_file }
+        it { should contain('[one]') }
+        it { should contain('key = betatrons') }
+        it { should_not contain('alphabet') }
+      end
+    end
+  end
+
+  describe 'subsetting_separator' do
+    {
+      ""                                => "two = twinethree foobar",
+      #"subsetting_separator => '',"     => "two = twinethreefoobar", # breaks regex
+      "subsetting_separator => ',',"    => "two = twinethree,foobar",
+      "subsetting_separator => '   ',"  => "two = twinethree   foobar",
+      "subsetting_separator => ' == '," => "two = twinethree == foobar",
+      "subsetting_separator => '=',"    => "two = twinethree=foobar",
+      #"subsetting_separator => '---',"  => "two = twinethree---foobar", # breaks regex
+    }.each do |parameter, content|
+      context "with \"#{parameter}\" makes \"#{content}\"" do
+        pp = <<-EOS
+        ini_subsetting { "with #{parameter} makes #{content}":
+          ensure     => present,
+          section    => 'one',
+          setting    => 'two',
+          subsetting => 'twine',
+          value      => 'three',
+          path       => '/tmp/subsetting_separator.ini',
+          #{parameter}
+        }
+        ini_subsetting { "foobar":
+          ensure     => present,
+          section    => 'one',
+          setting    => 'two',
+          subsetting => 'foo',
+          value      => 'bar',
+          path       => '/tmp/subsetting_separator.ini',
+          #{parameter}
+        }
+        EOS
+
+        it_behaves_like 'has_content', '/tmp/subsetting_separator.ini', pp, content
+      end
+    end
+  end
+end

+ 10 - 0
spec/acceptance/nodesets/centos-510-x64.yml

@@ -0,0 +1,10 @@
+HOSTS:
+  centos-510-x64:
+    roles:
+      - master
+    platform: el-5-x86_64
+    box : centos-510-x64-virtualbox-nocm
+    box_url : http://puppet-vagrant-boxes.puppetlabs.com/centos-510-x64-virtualbox-nocm.box
+    hypervisor : vagrant
+CONFIG:
+  type: git

+ 12 - 0
spec/acceptance/nodesets/centos-64-x64-pe.yml

@@ -0,0 +1,12 @@
+HOSTS:
+  centos-64-x64:
+    roles:
+      - master
+      - database
+      - dashboard
+    platform: el-6-x86_64
+    box : centos-64-x64-vbox4210-nocm
+    box_url : http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x64-vbox4210-nocm.box
+    hypervisor : vagrant
+CONFIG:
+  type: pe

+ 10 - 0
spec/acceptance/nodesets/centos-64-x64.yml

@@ -0,0 +1,10 @@
+HOSTS:
+  centos-64-x64:
+    roles:
+      - master
+    platform: el-6-x86_64
+    box : centos-64-x64-vbox4210-nocm
+    box_url : http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x64-vbox4210-nocm.box
+    hypervisor : vagrant
+CONFIG:
+  type: git

+ 10 - 0
spec/acceptance/nodesets/debian-607-x64.yml

@@ -0,0 +1,10 @@
+HOSTS:
+  debian-607-x64:
+    roles:
+      - master
+    platform: debian-6-amd64
+    box : debian-607-x64-vbox4210-nocm
+    box_url : http://puppet-vagrant-boxes.puppetlabs.com/debian-607-x64-vbox4210-nocm.box
+    hypervisor : vagrant
+CONFIG:
+  type: git

+ 10 - 0
spec/acceptance/nodesets/debian-73-x64.yml

@@ -0,0 +1,10 @@
+HOSTS:
+  debian-73-x64:
+    roles:
+      - master
+    platform: debian-7-amd64
+    box : debian-73-x64-virtualbox-nocm
+    box_url : http://puppet-vagrant-boxes.puppetlabs.com/debian-73-x64-virtualbox-nocm.box
+    hypervisor : vagrant
+CONFIG:
+  type: git

+ 10 - 0
spec/acceptance/nodesets/default.yml

@@ -0,0 +1,10 @@
+HOSTS:
+  centos-64-x64:
+    roles:
+      - master
+    platform: el-6-x86_64
+    box : centos-64-x64-vbox4210-nocm
+    box_url : http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x64-vbox4210-nocm.box
+    hypervisor : vagrant
+CONFIG:
+  type: git

+ 10 - 0
spec/acceptance/nodesets/fedora-18-x64.yml

@@ -0,0 +1,10 @@
+HOSTS:
+  fedora-18-x64:
+    roles:
+      - master
+    platform: fedora-18-x86_64
+    box : fedora-18-x64-vbox4210-nocm
+    box_url : http://puppet-vagrant-boxes.puppetlabs.com/fedora-18-x64-vbox4210-nocm.box
+    hypervisor : vagrant
+CONFIG:
+  type: git

+ 10 - 0
spec/acceptance/nodesets/sles-11sp1-x64.yml

@@ -0,0 +1,10 @@
+HOSTS:
+  sles-11sp1-x64:
+    roles:
+      - master
+    platform: sles-11-x86_64
+    box : sles-11sp1-x64-vbox4210-nocm
+    box_url : http://puppet-vagrant-boxes.puppetlabs.com/sles-11sp1-x64-vbox4210-nocm.box
+    hypervisor : vagrant
+CONFIG:
+  type: git

+ 10 - 0
spec/acceptance/nodesets/ubuntu-server-10044-x64.yml

@@ -0,0 +1,10 @@
+HOSTS:
+  ubuntu-server-10044-x64:
+    roles:
+      - master
+    platform: ubuntu-10.04-amd64
+    box : ubuntu-server-10044-x64-vbox4210-nocm
+    box_url : http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-10044-x64-vbox4210-nocm.box
+    hypervisor : vagrant
+CONFIG:
+  type: git

+ 10 - 0
spec/acceptance/nodesets/ubuntu-server-12042-x64.yml

@@ -0,0 +1,10 @@
+HOSTS:
+  ubuntu-server-12042-x64:
+    roles:
+      - master
+    platform: ubuntu-12.04-amd64
+    box : ubuntu-server-12042-x64-vbox4210-nocm
+    box_url : http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-12042-x64-vbox4210-nocm.box
+    hypervisor : vagrant
+CONFIG:
+  type: git

+ 0 - 8
spec/spec_helper.rb

@@ -1,11 +1,3 @@
-gem 'rspec', '>=2.0.0'
-require 'rspec/expectations'
-
-
-require 'puppetlabs_spec_helper/puppetlabs_spec_helper'
-
-require 'puppetlabs_spec_helper/puppetlabs_spec/files'
-
 require 'puppetlabs_spec_helper/module_spec_helper'
 
 RSpec.configure do |config|

+ 32 - 0
spec/spec_helper_acceptance.rb

@@ -0,0 +1,32 @@
+require 'beaker-rspec/spec_helper'
+require 'beaker-rspec/helpers/serverspec'
+
+hosts.each do |host|
+  if host['platform'] =~ /debian/
+    on host, 'echo \'export PATH=/var/lib/gems/1.8/bin/:${PATH}\' >> ~/.bashrc'
+  end
+  if host.is_pe?
+    install_pe
+  else
+    # Install Puppet
+    install_package host, 'rubygems'
+    on host, 'gem install puppet --no-ri --no-rdoc'
+    on host, "mkdir -p #{host['distmoduledir']}"
+  end
+end
+
+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 => 'inifile')
+  end
+
+  c.treat_symbols_as_metadata_keys_with_true_values = true
+end