From baeb98d5f8d5df904f84845eb3bd8d9b7cb3c247 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mathieu=20Gagne=CC=81?= <mgagne@iweb.com>
Date: Thu, 30 Jan 2014 17:09:21 -0500
Subject: [PATCH] Cleanup rspec tests of nova::compute::libvirt

Change-Id: Ie410ac8f2fc2d0ebd9e79a869f51e9b7bb315a6e
---
 spec/classes/nova_compute_libvirt_spec.rb | 240 ++++++++++------------
 spec/shared_examples.rb                   |   5 +
 spec/spec_helper.rb                       |   2 +
 3 files changed, 111 insertions(+), 136 deletions(-)
 create mode 100644 spec/shared_examples.rb

diff --git a/spec/classes/nova_compute_libvirt_spec.rb b/spec/classes/nova_compute_libvirt_spec.rb
index bb07d4ad5..e7e2a55fe 100644
--- a/spec/classes/nova_compute_libvirt_spec.rb
+++ b/spec/classes/nova_compute_libvirt_spec.rb
@@ -5,168 +5,136 @@ describe 'nova::compute::libvirt' do
     "include nova\ninclude nova::compute"
   end
 
-  describe 'on debian platforms' do
+  shared_examples 'nova-compute with libvirt' do
+
+    it { should contain_class('nova::params') }
+
+    context 'with default parameters' do
+
+      it 'installs libvirt package and service' do
+        should contain_package('libvirt').with(
+          :name   => platform_params[:libvirt_package_name],
+          :ensure => 'present'
+        )
+        should contain_service('libvirt').with(
+          :name     => platform_params[:libvirt_service_name],
+          :ensure   => 'running',
+          :provider => platform_params[:special_service_provider],
+          :require  => 'Package[libvirt]',
+          :before   => 'Service[nova-compute]'
+        )
+      end
+
+      it 'configures nova.conf' do
+        should contain_nova_config('DEFAULT/compute_driver').with_value('libvirt.LibvirtDriver')
+        should contain_nova_config('DEFAULT/libvirt_type').with_value('kvm')
+        should contain_nova_config('DEFAULT/connection_type').with_value('libvirt')
+        should contain_nova_config('DEFAULT/vncserver_listen').with_value('127.0.0.1')
+      end
+    end
+
+    context 'with overridden parameters' do
+      let :params do
+        { :libvirt_type => 'qemu' }
+      end
+
+      it { should contain_nova_config('DEFAULT/libvirt_type').with_value('qemu')}
+    end
+
+    describe 'with migration_support enabled' do
+      let :params do
+        { :migration_support => true }
+      end
+
+      context 'with vncserver_listen set to 0.0.0.0' do
+        before do
+          params.merge!( :vncserver_listen => '0.0.0.0' )
+        end
+
+        it {
+          should contain_class('nova::migration::libvirt')
+          should contain_nova_config('DEFAULT/vncserver_listen').with_value('0.0.0.0')
+        }
+      end
+
+      context 'with vncserver_listen not set to 0.0.0.0' do
+        before do
+          params.merge!( :vncserver_listen => '127.0.0.1' )
+        end
+
+        it_raises 'a Puppet::Error', /For migration support to work, you MUST set vncserver_listen to '0.0.0.0'/
+      end
+    end
+  end
+
+  context 'on Debian platforms' do
     let :facts do
       { :osfamily => 'Debian' }
     end
 
-    describe 'with default parameters' do
-
-      it { should contain_class('nova::params')}
-
-      it { should contain_package('nova-compute-kvm').with(
-        :ensure => 'present',
-        :before => 'Package[nova-compute]'
-      ) }
-
-      it { should contain_package('libvirt').with(
-        :name   => 'libvirt-bin',
-        :ensure => 'present'
-      ) }
-
-      it { should contain_service('libvirt').with(
-        :name     => 'libvirt-bin',
-        :ensure   => 'running',
-        :provider => 'upstart',
-        :require  => 'Package[libvirt]',
-        :before   => 'Service[nova-compute]'
-      )}
-
-      it { should contain_nova_config('DEFAULT/compute_driver').with_value('libvirt.LibvirtDriver')}
-      it { should contain_nova_config('DEFAULT/libvirt_type').with_value('kvm')}
-      it { should contain_nova_config('DEFAULT/connection_type').with_value('libvirt')}
-      it { should contain_nova_config('DEFAULT/vncserver_listen').with_value('127.0.0.1')}
+    let :platform_params do
+      { :libvirt_package_name => 'libvirt-bin',
+        :libvirt_service_name => 'libvirt-bin' }
     end
 
-    describe 'with params' do
-      let :params do
-        { :libvirt_type     => 'qemu',
-          :vncserver_listen => '0.0.0.0'
-        }
+    it { should contain_package('nova-compute-kvm').with(
+      :ensure => 'present',
+      :before => 'Package[nova-compute]'
+    ) }
+
+    context 'on Debian operating systems' do
+      before do
+        facts.merge!(:operatingsystem => 'Debian')
+        platform_params.merge!(:special_service_provider => nil)
       end
 
-      it { should contain_nova_config('DEFAULT/libvirt_type').with_value('qemu')}
-      it { should contain_nova_config('DEFAULT/vncserver_listen').with_value('0.0.0.0')}
+      it_behaves_like 'nova-compute with libvirt'
     end
 
-    describe 'with migration_support enabled' do
-
-      context 'with vncserver_listen set to 0.0.0.0' do
-        let :params do
-          { :vncserver_listen => '0.0.0.0',
-            :migration_support => true }
-        end
-
-        it { should contain_class('nova::migration::libvirt')}
-        it { should contain_nova_config('DEFAULT/vncserver_listen').with_value('0.0.0.0')}
+    context 'on Ubuntu operating systems' do
+      before do
+        facts.merge!(:operatingsystem => 'Ubuntu')
+        platform_params.merge!(:special_service_provider => 'upstart')
       end
 
-      context 'with vncserver_listen not set to 0.0.0.0' do
-        let :params do
-          { :vncserver_listen => '127.0.0.1',
-            :migration_support => true }
-        end
-
-        it { expect { should contain_class('nova::compute::libvirt') }.to \
-          raise_error(Puppet::Error, /For migration support to work, you MUST set vncserver_listen to '0.0.0.0'/) }
-      end
+      it_behaves_like 'nova-compute with libvirt'
     end
   end
 
-
-  describe 'on rhel platforms' do
+  context 'on RedHat platforms' do
     let :facts do
-      { :operatingsystem => 'RedHat', :osfamily => 'RedHat' }
+      { :osfamily => 'RedHat' }
     end
 
-    describe 'with default parameters' do
+    let :platform_params do
+      { :libvirt_package_name     => 'libvirt',
+        :libvirt_service_name     => 'libvirtd',
+        :special_service_provider => 'init' }
+    end
 
-      it { should contain_class('nova::params')}
+    context 'on Fedora operating systems' do
+      before do
+        facts.merge!(:operatingsystem => 'Fedora')
+        platform_params.merge!(:special_service_provider => nil)
+      end
 
-      it { should contain_package('libvirt').with(
-        :name   => 'libvirt',
-        :ensure => 'present'
-      ) }
+      it_behaves_like 'nova-compute with libvirt'
+    end
+
+    context 'on other operating systems' do
+      before do
+        facts.merge!(:operatingsystem => 'RedHat')
+        platform_params.merge!(:special_service_provider => 'init')
+      end
+
+      it_behaves_like 'nova-compute with libvirt'
 
-      it { should contain_service('libvirt').with(
-        :name     => 'libvirtd',
-        :ensure   => 'running',
-        :provider => 'init',
-        :require  => 'Package[libvirt]',
-        :before   => 'Service[nova-compute]'
-      )}
       it { should contain_service('messagebus').with(
         :ensure   => 'running',
         :enable   => true,
         :before   => 'Service[libvirt]',
-        :provider => 'init'
+        :provider => platform_params[:special_service_provider]
       ) }
-
-      it { should contain_nova_config('DEFAULT/compute_driver').with_value('libvirt.LibvirtDriver')}
-      it { should contain_nova_config('DEFAULT/libvirt_type').with_value('kvm')}
-      it { should contain_nova_config('DEFAULT/connection_type').with_value('libvirt')}
-      it { should contain_nova_config('DEFAULT/vncserver_listen').with_value('127.0.0.1')}
     end
-
-    describe 'with params' do
-      let :params do
-        { :libvirt_type     => 'qemu',
-          :vncserver_listen => '0.0.0.0'
-        }
-      end
-
-      it { should contain_nova_config('DEFAULT/libvirt_type').with_value('qemu')}
-      it { should contain_nova_config('DEFAULT/vncserver_listen').with_value('0.0.0.0')}
-    end
-
-    describe 'with migration_support enabled' do
-
-      context 'with vncserver_listen set to 0.0.0.0' do
-        let :params do
-          { :vncserver_listen => '0.0.0.0',
-            :migration_support => true }
-        end
-
-        it { should contain_class('nova::migration::libvirt')}
-        it { should contain_nova_config('DEFAULT/vncserver_listen').with_value('0.0.0.0')}
-      end
-
-      context 'with vncserver_listen not set to 0.0.0.0' do
-        let :params do
-          { :vncserver_listen => '127.0.0.1',
-            :migration_support => true }
-        end
-
-        it { expect { should contain_class('nova::compute::libvirt') }.to \
-          raise_error(Puppet::Error, /For migration support to work, you MUST set vncserver_listen to '0.0.0.0'/) }
-      end
-    end
-
-    describe 'with default parameters on Fedora' do
-      let :facts do
-        { :operatingsystem => 'Fedora', :osfamily => 'RedHat' }
-      end
-
-      it { should contain_class('nova::params')}
-
-      it { should contain_package('libvirt').with(
-        :name   => 'libvirt',
-        :ensure => 'present'
-      ) }
-
-      it { should contain_service('libvirt').with(
-        :name     => 'libvirtd',
-        :ensure   => 'running',
-        :provider => nil,
-        :require  => 'Package[libvirt]',
-        :before   => 'Service[nova-compute]'
-      )}
-
-      it { should contain_nova_config('DEFAULT/compute_driver').with_value('libvirt.LibvirtDriver')}
-      it { should contain_nova_config('DEFAULT/libvirt_type').with_value('kvm')}
-      it { should contain_nova_config('DEFAULT/connection_type').with_value('libvirt')}
-      it { should contain_nova_config('DEFAULT/vncserver_listen').with_value('127.0.0.1')}
-    end
-
   end
 end
diff --git a/spec/shared_examples.rb b/spec/shared_examples.rb
new file mode 100644
index 000000000..d92156a36
--- /dev/null
+++ b/spec/shared_examples.rb
@@ -0,0 +1,5 @@
+shared_examples_for "a Puppet::Error" do |description|
+  it "with message matching #{description.inspect}" do
+    expect { should have_class_count(1) }.to raise_error(Puppet::Error, description)
+  end
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index e17ce17d6..53d4dd02d 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,5 +1,7 @@
 require 'puppetlabs_spec_helper/module_spec_helper'
+require 'shared_examples'
 
 RSpec.configure do |c|
   c.alias_it_should_behave_like_to :it_configures, 'configures'
+  c.alias_it_should_behave_like_to :it_raises, 'raises'
 end