spec/spec_helper.rb in hawkular-client-2.2.1 vs spec/spec_helper.rb in hawkular-client-2.3.0
- old
+ new
@@ -1,8 +1,9 @@
# This needs to go before all requires to be able to record full coverage
require 'coveralls'
Coveralls.wear!
+
# Now the application requires.
require 'hawkular/hawkular_client'
require 'hawkular/hawkular_client_utils'
require 'rspec/core'
require 'rspec/mocks'
@@ -26,32 +27,73 @@
)
end
end
module Hawkular::Metrics::RSpec
+ def setup_client_without_tenant(options = {})
+ options[:tenant] = nil
+ mocked_version = options[:mocked_version]
+ ::RSpec::Mocks.with_temporary_scope do
+ mocked_version.nil? ? mock_metrics_version : mock_metrics_version(mocked_version)
+ @client = Hawkular::Metrics::Client.new(config['url'],
+ credentials(options), options)
+ return @client
+ end
+ end
+
def setup_client(options = {})
- credentials = {
- username: options[:username].nil? ? config['user'] : options[:username],
- password: options[:password].nil? ? config['password'] : options[:password]
- }
- @client = Hawkular::Metrics::Client.new(config['url'],
- credentials, options)
+ options[:tenant] ||= 'hawkular'
+ mocked_version = options[:mocked_version]
+ ::RSpec::Mocks.with_temporary_scope do
+ mocked_version.nil? ? mock_metrics_version : mock_metrics_version(mocked_version)
+ @client = Hawkular::Metrics::Client.new(config['url'],
+ credentials(options), options)
+ end
+ @client
end
- def setup_client_new_tenant(_options = {})
- setup_client
+ def setup_v8_client(options = {})
+ options[:tenant] ||= 'hawkular'
+ options[:verify_ssl] ||= OpenSSL::SSL::VERIFY_NONE
+ ::RSpec::Mocks.with_temporary_scope do
+ mock_metrics_version '0.8.0'
+ @client = Hawkular::Metrics::Client.new(config['url_v8'],
+ credentials_v8(options), options)
+ end
+ @client
+ end
+
+ def setup_client_new_tenant(options = {})
@tenant = 'vcr-test-tenant-123'
- # @client.tenants.create(@tenant)
- setup_client(tenant: @tenant)
+ setup_client(tenant: @tenant, mocked_version: options[:mocked_version])
end
+ def credentials_v8(options = {})
+ {
+ token: options[:token].nil? ? config['token_v8'] : options[:token]
+ }
+ end
+
+ def credentials(options = {})
+ {
+ username: options[:username].nil? ? config['user'] : options[:username],
+ password: options[:password].nil? ? config['password'] : options[:password]
+ }
+ end
+
def config
@config ||= YAML.load(
File.read(File.expand_path('endpoint.yml', File.dirname(__FILE__)))
)
end
+ def mock_metrics_version(version = '0.9.0.Final')
+ allow_any_instance_of(Hawkular::Metrics::Client).to receive(:fetch_version_and_status).and_return(
+ 'Implementation-Version' => version
+ )
+ end
+
# more or less generic method common for all metric types (counters, gauges, availabilities)
def create_metric_using_hash(endpoint, id, tenant_id)
endpoint.create(id: id, dataRetention: 123, tags: { some: 'value' }, tenantId: tenant_id)
metric = endpoint.get(id)
@@ -132,24 +174,30 @@
# make sure the directory structure is there
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
text = File.read(input_file_path)
bindings.select { |_, v| v.size >= 3 }.each do |k, v|
- text.gsub! v, "<%= #{k} %>"
+ text.gsub! v.to_s, "<%= #{k} %>"
end
File.open(output_file_path, 'w') { |file| file.write text }
File.delete(input_file_path)
end
def record(prefix, bindings, explicit_cassette_name, example: nil)
run = lambda do
- example.run unless example.nil?
+ unless example.nil?
+ if example.respond_to?(:run)
+ example.run
+ elsif example.respond_to?(:call)
+ example.call
+ end
+ end
yield if block_given?
end
- if (!example.nil? && example.metadata[:update_template]) || ENV['VCR_UPDATE'] == '1'
+ if ENV['VCR_UPDATE'] == '1'
VCR.use_cassette(prefix + '/tmp/' + explicit_cassette_name,
decode_compressed_response: true,
record: :all) do
run.call
end
@@ -183,7 +231,19 @@
VCR.eject_cassette
VCR.turn_off!(ignore_cassettes: true)
WebSocketVCR.turn_off! # TODO: this does not work as the impl is empty
WebMock.allow_net_connect!
puts 'VCR is turned off!'
+ end
+
+ module RestClient
+ class Request
+ def default_headers
+ {
+ accept: '*/*',
+ accept_encoding: 'identity',
+ user_agent: 'hawkular-client-ruby'
+ }
+ end
+ end
end
end