summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorAdam Edwards <adamed@opscode.com>2014-05-17 06:48:53 -0700
committerAdam Edwards <adamed@opscode.com>2014-05-17 06:48:53 -0700
commitcabc5171e3d65b9a9a9831a0a07588f20c90c3e7 (patch)
tree476f8dca1941ffa239f1b0ba97a6238080f4b56f /spec
parent9902fd92d533c30ffc167177826e61de122800e0 (diff)
downloadwmi-lite-cabc5171e3d65b9a9a9831a0a07588f20c90c3e7.tar.gz
More specific error handling
Diffstat (limited to 'spec')
-rw-r--r--spec/functional/wmi_spec.rb25
-rw-r--r--spec/unit/wmi_spec.rb57
2 files changed, 76 insertions, 6 deletions
diff --git a/spec/functional/wmi_spec.rb b/spec/functional/wmi_spec.rb
index 53bf548..0be3778 100644
--- a/spec/functional/wmi_spec.rb
+++ b/spec/functional/wmi_spec.rb
@@ -35,16 +35,22 @@ describe WmiLite::Wmi, :windows_only do
results = wmi.send(query_method, query_parameter)
validate_wmi_results(results, wmi_class)
end
+
+ describe 'when the namespace is invalid' do
+ it_behaves_like 'an invalid namespace'
+ end
end
shared_examples_for 'an invalid query' do
- it 'should raise an exception if an invalid namespace is specified' do
- invalid_wmi = WmiLite::Wmi.new('root/notvalid')
- expect { invalid_wmi.ExecQuery('Win32_Process') }.to raise_error
+ it 'should raise an exception' do
+ expect { wmi.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
end
+ end
- it 'should raise an exception if an invalid class is specified' do
- expect { wmi.send(query_method, wmi_query) }.to raise_error
+ shared_examples_for 'an invalid namespace' do
+ it 'should raise an exception if an invalid namespace is specified' do
+ invalid_wmi = WmiLite::Wmi.new('root/notvalid')
+ expect { invalid_wmi.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
end
end
@@ -88,10 +94,17 @@ describe WmiLite::Wmi, :windows_only do
context 'when making invalid queries' do
let(:namespace) { nil }
- let(:wmi_query) { 'invalidclass' }
+ let(:wmi_query) { 'invalidclass' }
let(:query_method) { :first_of }
it_behaves_like 'an invalid query'
+
+ let(:query_method) { :instances_of }
+ it_behaves_like 'an invalid query'
+
+ let(:query_method) { :query }
+ let(:wmi_query) { 'nosql_4_life' }
+ it_behaves_like 'an invalid query'
end
let(:namespace) { nil }
diff --git a/spec/unit/wmi_spec.rb b/spec/unit/wmi_spec.rb
index beaaa46..d240f3e 100644
--- a/spec/unit/wmi_spec.rb
+++ b/spec/unit/wmi_spec.rb
@@ -148,6 +148,44 @@ describe WmiLite::Wmi do
end
+ shared_examples_for 'an invalid query' do
+ let(:unparseable_error) { 'unparseableerror' }
+ it 'should raise an exception' do
+ wbem_connection.should_receive(:ExecQuery).and_raise(WIN32OLERuntimeError.new(unparseable_error))
+ wmi_service = WmiLite::Wmi.new
+ expect { wmi_service.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
+ end
+
+ it 'should raise an exception that starts with the original exception message' do
+ wbem_connection.should_receive(:ExecQuery).and_raise(WIN32OLERuntimeError.new(unparseable_error))
+ wmi_service = WmiLite::Wmi.new
+ begin
+ wmi_service.send(query_method, wmi_query)
+ rescue WmiLite::WmiException => e
+ expect(e.message.start_with?(unparseable_error)).to eql(true)
+ end
+ end
+ end
+
+ shared_examples_for 'an invalid namespace' do
+ let(:unparseable_error) { 'unparseableerror' }
+ it 'should raise an exception' do
+ wbem_locator.should_receive(:ConnectServer).and_raise(WIN32OLERuntimeError.new('unparseableerror'))
+ wmi_service = WmiLite::Wmi.new('notavalidnamespace')
+ expect { wmi_service.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
+ end
+
+ it 'should raise an exception that starts with the original exception message' do
+ wbem_locator.should_receive(:ConnectServer).and_raise(WIN32OLERuntimeError.new('unparseableerror'))
+ wmi_service = WmiLite::Wmi.new
+ begin
+ wmi_service.send(query_method, wmi_query)
+ rescue WmiLite::WmiException => e
+ expect(e.message.start_with?(unparseable_error)).to eql(true)
+ end
+ end
+ end
+
shared_examples_for "the query method" do
let(:wmi_properties1) { { 'cores' => 4, 'name' => 'mycomputer1', 'diskspace' => 400, 'os' => 'windows' } }
@@ -212,6 +250,25 @@ describe WmiLite::Wmi do
end
end
+ context 'when making invalid queries' do
+ let(:namespace) { nil }
+
+ let(:wmi_query) { 'invalidclass' }
+ let(:query_method) { :first_of }
+
+ it_behaves_like 'an invalid query'
+ it_behaves_like 'an invalid namespace'
+
+ let(:query_method) { :instances_of }
+ it_behaves_like 'an invalid query'
+ it_behaves_like 'an invalid namespace'
+
+ let(:query_method) { :query }
+ let(:wmi_query) { 'nosql_4_life' }
+ it_behaves_like 'an invalid query'
+ it_behaves_like 'an invalid namespace'
+ end
+
it_should_behave_like "the first_of method"
it_should_behave_like "the instances_of method"