Skip to content

Commit

Permalink
Improvements in the creation of the clients for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aalexmonteiro committed Mar 9, 2017
1 parent 2f393c0 commit 4cfd929
Show file tree
Hide file tree
Showing 54 changed files with 936 additions and 919 deletions.
22 changes: 12 additions & 10 deletions spec/shared_context.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# General context for unit testing:
RSpec.shared_context 'shared context', a: :b do
before :each do
options_120 = { url: 'https://oneview.example.com', user: 'Administrator', password: 'secret123', api_version: 120 }
@client_120 = OneviewSDK::Client.new(options_120)

options_200 = { url: 'https://oneview.example.com', user: 'Administrator', password: 'secret123' }
@client = OneviewSDK::Client.new(options_200)

options_300 = { url: 'https://oneview.example.com', user: 'Administrator', password: 'secret123', api_version: 300 }
@client_300 = OneviewSDK::Client.new(options_300)
options = { url: 'https://oneview.example.com', user: 'Administrator', password: 'secret123' }
# Creates dynamically the variables @client_120, @client_200 and etc.
api_versions = [120, 200, 300, 500]
api_versions.each do |v|
instance_variable_set("@client_#{v}", OneviewSDK::Client.new(options.merge(api_version: v)))
end

options_i3s_300 = { url: 'https://oneview.example.com', token: 'token123' }
@client_i3s_300 = OneviewSDK::ImageStreamer::Client.new(options_i3s_300)
options_i3s = { url: 'https://oneview.example.com', token: 'token123' }
# Creates dynamically the variables @client_i3s_300 and etc.
i3s_api_versions = [300]
i3s_api_versions.each do |v|
instance_variable_set("@client_i3s_#{v}", OneviewSDK::ImageStreamer::Client.new(options_i3s.merge(api_version: v)))
end
end
end

Expand Down
10 changes: 5 additions & 5 deletions spec/unit/cli/create_from_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
context 'with valid options' do
before :each do
@resource_data = { 'name' => 'My', 'uri' => '/rest/fake', 'description' => 'Blah' }
response = [OneviewSDK::EthernetNetwork.new(@client, @resource_data)]
response = [OneviewSDK::EthernetNetwork.new(@client_200, @resource_data)]
allow(OneviewSDK::Resource).to receive(:find_by).and_return(response)
allow_any_instance_of(OneviewSDK::Resource).to receive(:create).and_return(true)
allow_any_instance_of(OneviewSDK::Resource).to receive(:update).and_return(true)
Expand All @@ -32,15 +32,15 @@

it 'updates a valid resource by name' do
resource_data = { 'name' => 'My_Ethernet_Network', 'description' => 'Blah' }
response = [OneviewSDK::EthernetNetwork.new(@client, resource_data)]
response = [OneviewSDK::EthernetNetwork.new(@client_200, resource_data)]
allow(OneviewSDK::Resource).to receive(:find_by).and_return(response)
expect { OneviewSDK::Cli.start(['create_from_file', yaml_file]) }
.to output(/Updated Successfully!/).to_stdout_from_any_process
end

it 'makes no changes if the resource is up to date' do
resource_data = { 'name' => 'My_Ethernet_Network', 'description' => 'Short Description' }
response = [OneviewSDK::EthernetNetwork.new(@client, resource_data)]
response = [OneviewSDK::EthernetNetwork.new(@client_200, resource_data)]
allow(OneviewSDK::Resource).to receive(:find_by).and_return(response)
expect { OneviewSDK::Cli.start(['create_from_file', yaml_file]) }
.to output(/Skipped.*up to date/).to_stdout_from_any_process
Expand All @@ -58,15 +58,15 @@
end

it 'fails if the resource is a generic "Resource" type' do
resource = OneviewSDK::Resource.new(@client)
resource = OneviewSDK::Resource.new(@client_200)
allow(OneviewSDK::Resource).to receive(:from_file).and_return(resource)
expect(STDOUT).to receive(:puts).with(/Failed to determine resource type/)
expect { OneviewSDK::Cli.start(['create_from_file', yaml_file]) }
.to raise_error SystemExit
end

it 'fails if the file does not specify a unique identifier' do
resource = OneviewSDK::EthernetNetwork.new(@client)
resource = OneviewSDK::EthernetNetwork.new(@client_200)
allow(OneviewSDK::Resource).to receive(:from_file).and_return(resource)
expect(STDOUT).to receive(:puts).with(/Must set/)
expect { OneviewSDK::Cli.start(['create_from_file', yaml_file]) }
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/cli/delete_from_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
context 'with valid options' do
before :each do
@resource_data = { 'name' => 'My', 'uri' => '/rest/fake', 'description' => 'Blah' }
response = [OneviewSDK::EthernetNetwork.new(@client, @resource_data)]
response = [OneviewSDK::EthernetNetwork.new(@client_200, @resource_data)]
allow(OneviewSDK::Resource).to receive(:find_by).and_return(response)
allow_any_instance_of(OneviewSDK::Resource).to receive(:delete).and_return(true)
allow_any_instance_of(OneviewSDK::Resource).to receive(:retrieve!).and_return(true)
Expand Down Expand Up @@ -51,7 +51,7 @@
end

it 'fails if the file does not specify a name or uri' do
resource = OneviewSDK::Resource.new(@client)
resource = OneviewSDK::Resource.new(@client_200)
allow(OneviewSDK::Resource).to receive(:from_file).and_return(resource)
allow_any_instance_of(OneviewSDK::Resource).to receive(:retrieve!).and_call_original
expect(STDOUT).to receive(:puts).with(/Must set/)
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/cli/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
context 'with valid options' do
before :each do
@resource_data = { 'name' => 'Profile1', 'uri' => '/rest/fake', 'description' => 'Blah' }
response = [OneviewSDK::ServerProfile.new(@client, @resource_data)]
response = [OneviewSDK::ServerProfile.new(@client_200, @resource_data)]
allow(OneviewSDK::Resource).to receive(:find_by).and_return(response)
allow_any_instance_of(OneviewSDK::Resource).to receive(:delete).and_return(true)
allow_any_instance_of(HighLine).to receive(:agree).and_return(true)
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/cli/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

let(:response) do
[
OneviewSDK::ServerProfile.new(@client, resource_data),
OneviewSDK::ServerProfile.new(@client, resource_data2)
OneviewSDK::ServerProfile.new(@client_200, resource_data),
OneviewSDK::ServerProfile.new(@client_200, resource_data2)
]
end

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/cli/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

before :each do
@resource_data = { 'name' => 'Profile1', 'uri' => '/rest/fake', 'description' => 'Blah' }
response = [OneviewSDK::Resource.new(@client, @resource_data)]
response = [OneviewSDK::Resource.new(@client_200, @resource_data)]
allow(OneviewSDK::Resource).to receive(:find_by).and_return(response)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/unit/cli/to_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
context 'with valid options' do
before :each do
@resource_data = { 'name' => 'Test', 'uri' => '/rest/fake' }
@response = [OneviewSDK::EthernetNetwork.new(@client, @resource_data)]
@response = [OneviewSDK::EthernetNetwork.new(@client_200, @resource_data)]
allow(OneviewSDK::Resource).to receive(:find_by).and_return(@response)
end

Expand All @@ -66,7 +66,7 @@
end

it 'accepts an api-version parameter: 300' do
@response = [OneviewSDK::API300::EthernetNetwork.new(@client, @resource_data)]
@response = [OneviewSDK::API300::EthernetNetwork.new(@client_200, @resource_data)]
expect(OneviewSDK::Resource).to receive(:find_by).and_return(@response)
expect_any_instance_of(OneviewSDK::API300::EthernetNetwork).to receive(:to_file).and_return(true)
expect { OneviewSDK::Cli.start(['to_file', 'EthernetNetwork', 'Test', '-p', path, '--api-version', 300]) }
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/cli/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
end

let(:sp1) do
OneviewSDK::ServerProfile.new(@client, resource_data)
OneviewSDK::ServerProfile.new(@client_200, resource_data)
end

let(:sp_list) do
Expand Down
82 changes: 41 additions & 41 deletions spec/unit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@
end

it 'warns if the api level is greater than the appliance api version' do
options = { url: 'https://oneview.example.com', token: 'token123', api_version: 400 }
options = { url: 'https://oneview.example.com', token: 'token123', api_version: 600 }
client = nil
expect { client = OneviewSDK::Client.new(options) }.to output(/is greater than the appliance API version/).to_stdout_from_any_process
expect(client.api_version).to eq(400)
expect(client.api_version).to eq(600)
end

it 'sets @print_wait_dots to false by default' do
Expand Down Expand Up @@ -139,34 +139,34 @@
include_context 'shared context'

it 'allows the url to be re-set' do
@client.url = 'https://new-url.example.com'
expect(@client.url).to eq('https://new-url.example.com')
@client_200.url = 'https://new-url.example.com'
expect(@client_200.url).to eq('https://new-url.example.com')
end

it 'allows the user to be re-set' do
@client.user = 'updatedUser'
expect(@client.user).to eq('updatedUser')
@client_200.user = 'updatedUser'
expect(@client_200.user).to eq('updatedUser')
end

it 'allows the password to be re-set' do
@client.password = 'updatedPassword'
expect(@client.password).to eq('updatedPassword')
@client_200.password = 'updatedPassword'
expect(@client_200.password).to eq('updatedPassword')
end

it 'allows the token to be re-set' do
@client.token = 'updatedToken'
expect(@client.token).to eq('updatedToken')
@client_200.token = 'updatedToken'
expect(@client_200.token).to eq('updatedToken')
end

it 'allows the log level to be re-set' do
expect(@client.log_level).to_not eq(:error)
@client.log_level = :error
expect(@client.log_level).to eq(:error)
expect(@client.logger.level).to eq(Logger.const_get(:ERROR))
expect(@client_200.log_level).to_not eq(:error)
@client_200.log_level = :error
expect(@client_200.log_level).to eq(:error)
expect(@client_200.logger.level).to eq(Logger.const_get(:ERROR))
end

it 'does not allow the max_api_version to be set manually' do
expect { @client.max_api_version = 1 }.to raise_error(NoMethodError, /undefined method/)
expect { @client_200.max_api_version = 1 }.to raise_error(NoMethodError, /undefined method/)
end
end

Expand Down Expand Up @@ -219,117 +219,117 @@
include_context 'shared context'

before :each do
@resource = OneviewSDK::Resource.new(@client)
@resource = OneviewSDK::Resource.new(@client_200)
end

it 'implements the #create method' do
expect(@resource).to receive(:create)
@client.create(@resource)
@client_200.create(@resource)
end

it 'implements the #update method' do
expect(@resource).to receive(:update)
@client.update(@resource, name: 'NewName')
@client_200.update(@resource, name: 'NewName')
end

it 'implements the #refresh method' do
expect(@resource).to receive(:refresh)
@client.refresh(@resource)
@client_200.refresh(@resource)
end

it 'implements the #delete method' do
expect(@resource).to receive(:delete)
@client.delete(@resource)
@client_200.delete(@resource)
end
end

describe '#get_all' do
include_context 'shared context'

it "calls the correct resource's get_all method" do
expect(OneviewSDK::API200::ServerProfile).to receive(:get_all).with(@client)
@client.get_all('ServerProfiles')
expect(OneviewSDK::API200::ServerProfile).to receive(:get_all).with(@client_200)
@client_200.get_all('ServerProfiles')
end

it 'accepts API version and variant parameters' do
expect(OneviewSDK::API300::Synergy::ServerProfile).to receive(:get_all).with(@client)
@client.get_all('ServerProfiles', 300, 'Synergy')
expect(OneviewSDK::API300::Synergy::ServerProfile).to receive(:get_all).with(@client_200)
@client_200.get_all('ServerProfiles', 300, 'Synergy')
end

it 'accepts symbols instead of strings' do
expect(OneviewSDK::API300::Synergy::ServerProfile).to receive(:get_all).with(@client)
@client.get_all(:ServerProfiles, 300, :Synergy)
expect(OneviewSDK::API300::Synergy::ServerProfile).to receive(:get_all).with(@client_200)
@client_200.get_all(:ServerProfiles, 300, :Synergy)
end

it 'fails when a bogus resource type is given' do
expect { @client.get_all('BogusResources') }.to raise_error(TypeError, /Invalid resource type/)
expect { @client_200.get_all('BogusResources') }.to raise_error(TypeError, /Invalid resource type/)
end

it 'fails when a bogus API version is given' do
expect { @client.get_all('ServerProfiles', 100) }.to raise_error(OneviewSDK::UnsupportedVersion, /version 100 is not supported/)
expect { @client_200.get_all('ServerProfiles', 100) }.to raise_error(OneviewSDK::UnsupportedVersion, /version 100 is not supported/)
end

it 'fails when a bogus variant is given' do
expect { @client.get_all('ServerProfiles', 300, 'Bogus') }.to raise_error(/variant 'Bogus' is not supported/)
expect { @client_200.get_all('ServerProfiles', 300, 'Bogus') }.to raise_error(/variant 'Bogus' is not supported/)
end
end

describe '#refresh_login' do
include_context 'shared context'

it 'refreshes the token and max_api_version' do
expect(@client).to receive(:appliance_api_version).and_return(250)
expect(@client).to receive(:login).and_return('newToken')
@client.refresh_login
expect(@client.max_api_version).to eq(250)
expect(@client.token).to eq('newToken')
expect(@client_200).to receive(:appliance_api_version).and_return(250)
expect(@client_200).to receive(:login).and_return('newToken')
@client_200.refresh_login
expect(@client_200.max_api_version).to eq(250)
expect(@client_200.token).to eq('newToken')
end
end

describe '#destroy_session' do
include_context 'shared context'

it 'makes a REST call to destroy the session' do
expect(@client).to receive(:rest_delete).with('/rest/login-sessions').and_return(FakeResponse.new)
expect(@client.destroy_session).to eq(@client)
expect(@client_200).to receive(:rest_delete).with('/rest/login-sessions').and_return(FakeResponse.new)
expect(@client_200.destroy_session).to eq(@client_200)
end
end

describe '#wait_for' do
include_context 'shared context'

it 'requires a task_uri' do
expect { @client.wait_for('') }.to raise_error(ArgumentError, /Must specify a task_uri/)
expect { @client_200.wait_for('') }.to raise_error(ArgumentError, /Must specify a task_uri/)
end

it 'returns the response body for completed tasks' do
fake_response = FakeResponse.new(taskState: 'Completed', name: 'NewName')
allow_any_instance_of(OneviewSDK::Client).to receive(:rest_get).and_return(fake_response)
ret = @client.wait_for('/rest/tasks/1')
ret = @client_200.wait_for('/rest/tasks/1')
expect(ret).to eq('taskState' => 'Completed', 'name' => 'NewName')
end

it 'shows warnings' do
fake_response = FakeResponse.new(taskState: 'Warning', taskErrors: 'Blah')
allow_any_instance_of(OneviewSDK::Client).to receive(:rest_get).and_return(fake_response)
ret = nil
expect { ret = @client.wait_for('/rest/tasks/1') }.to output(/ended with warning.*Blah/).to_stdout_from_any_process
expect { ret = @client_200.wait_for('/rest/tasks/1') }.to output(/ended with warning.*Blah/).to_stdout_from_any_process
expect(ret).to eq('taskState' => 'Warning', 'taskErrors' => 'Blah')
end

it 'raises an error if the task fails' do
%w(Error Killed Terminated).each do |state|
fake_response = FakeResponse.new(taskState: state, message: 'Blah')
allow_any_instance_of(OneviewSDK::Client).to receive(:rest_get).and_return(fake_response)
expect { @client.wait_for('/rest/tasks/1') }.to raise_error(OneviewSDK::TaskError, /ended with bad state[\S\s]*Blah/)
expect { @client_200.wait_for('/rest/tasks/1') }.to raise_error(OneviewSDK::TaskError, /ended with bad state[\S\s]*Blah/)
end
end

it 'raises an error with the error details if the task fails' do
%w(Error Killed Terminated).each do |state|
fake_response = FakeResponse.new(taskState: state, taskErrors: { message: 'Blah' })
allow_any_instance_of(OneviewSDK::Client).to receive(:rest_get).and_return(fake_response)
expect { @client.wait_for('/rest/tasks/1') }.to raise_error(OneviewSDK::TaskError, /ended with bad state[\S\s]*Blah/)
expect { @client_200.wait_for('/rest/tasks/1') }.to raise_error(OneviewSDK::TaskError, /ended with bad state[\S\s]*Blah/)
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/resource/api200/connection_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

describe '#initialize' do
it 'sets the defaults correctly' do
connection = OneviewSDK::ConnectionTemplate.new(@client)
connection = OneviewSDK::ConnectionTemplate.new(@client_200)
expect(connection['bandwidth']).to eq({})
expect(connection['type']).to eq('connection-template')
end

it 'sets maximum and typical bandwidth' do
connection = OneviewSDK::ConnectionTemplate.new(@client)
connection = OneviewSDK::ConnectionTemplate.new(@client_200)
connection['bandwidth']['maximumBandwidth'] = 1000
connection['bandwidth']['typicalBandwidth'] = 5000
expect(connection['bandwidth']['maximumBandwidth']).to eq(1000)
Expand All @@ -21,22 +21,22 @@

describe '#get_default' do
it 'verify endpoint' do
expect(@client).to receive(:rest_get).with('/rest/connection-templates/defaultConnectionTemplate').and_return(FakeResponse.new({}))
connection = OneviewSDK::ConnectionTemplate.get_default(@client)
expect(@client_200).to receive(:rest_get).with('/rest/connection-templates/defaultConnectionTemplate').and_return(FakeResponse.new({}))
connection = OneviewSDK::ConnectionTemplate.get_default(@client_200)
expect(connection).to be_an_instance_of OneviewSDK::ConnectionTemplate
end
end

describe '#create' do
it 'is unavailable' do
connection = OneviewSDK::ConnectionTemplate.new(@client)
connection = OneviewSDK::ConnectionTemplate.new(@client_200)
expect { connection.create }.to raise_error(/The method #create is unavailable for this resource/)
end
end

describe '#delete' do
it 'is unavailable' do
connection = OneviewSDK::ConnectionTemplate.new(@client)
connection = OneviewSDK::ConnectionTemplate.new(@client_200)
expect { connection.delete }.to raise_error(/The method #delete is unavailable for this resource/)
end
end
Expand Down
Loading

0 comments on commit 4cfd929

Please sign in to comment.