spec/spec_helper.rb in hawkular-client-2.6.0 vs spec/spec_helper.rb in hawkular-client-2.7.0

- old
+ new

@@ -28,47 +28,45 @@ end end module Hawkular::Metrics::RSpec def setup_client_without_tenant(options = {}) + options = options.dup 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'], + @client = Hawkular::Metrics::Client.new(entrypoint(options[:type], 'metrics'), credentials(options), options) return @client end end def setup_client(options = {}) + options = options.dup 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'], + @client = Hawkular::Metrics::Client.new(entrypoint(options[:type], 'metrics'), credentials(options), options) end @client end def setup_v8_client(options = {}) + options = options.dup 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'], + @client = Hawkular::Metrics::Client.new(entrypoint('v8', 'metrics'), credentials_v8(options), options) end @client end - def setup_client_new_tenant(options = {}) - @tenant = 'vcr-test-tenant-123' - setup_client(tenant: @tenant, mocked_version: options[:mocked_version]) - end - def credentials_v8(options = {}) { token: options[:token].nil? ? config['token_v8'] : options[:token] } end @@ -78,25 +76,19 @@ 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) + endpoint.create(id: id, dataRetention: 123, tags: { some: 'value' }) metric = endpoint.get(id) expect(metric).to be_a(Hawkular::Metrics::MetricDefinition) expect(metric.id).to eql(id) expect(metric.data_retention).to eql(123) @@ -158,14 +150,35 @@ {} else object[:data] end end + + def host_with_scheme(host, use_secure_connection) + "#{use_secure_connection ? 'https' : 'http'}://#{host}" + end end # globally used helper functions module Helpers + def config + @config ||= YAML.load( + File.read(File.expand_path('endpoint.yml', File.dirname(__FILE__))) + ) + end + + def entrypoint(type, component = nil) + base = config[type.to_s.downcase] + entrypoint = "#{base['is_secure'] ? 'https' : 'http'}://#{base['host']}:#{base['port']}/" + entrypoint << config[component] unless component.nil? + end + + def host(type) + base = config[type.to_s.downcase] + "#{base['host']}:#{base['port']}" + end + def make_template(base_directory, cassette_name, bindings) cassette = cassette_name.gsub(/\s+/, '_') input_file_path = "#{VCR.configuration.cassette_library_dir}/#{base_directory}/tmp/#{cassette}.yml" return unless File.exist? input_file_path output_file_path = "#{VCR.configuration.cassette_library_dir}/#{base_directory}/Templates/#{cassette}.yml" @@ -192,24 +205,65 @@ example.call end end yield if block_given? end + record_cassette(prefix, bindings, explicit_cassette_name, run) + end - if ENV['VCR_UPDATE'] == '1' + def record_cleanup(prefix) + FileUtils.rm_rf "#{VCR.configuration.cassette_library_dir}/#{prefix}/tmp" + end + + def record_websocket(prefix, bindings, explicit_cassette_name, example = nil) + prefix.gsub!(/\s/, '_') + explicit_cassette_name.gsub!(/\s/, '_') + run = lambda do + unless example.nil? + if example.respond_to?(:run) + example.run + elsif example.respond_to?(:call) + example.call + end + end + yield if block_given? + end + + record_websocket_cassette(prefix, bindings, explicit_cassette_name, run) + end + + private + + def record_cassette(prefix, bindings, explicit_cassette_name, run_lambda) + if ENV['VCR_UPDATE'] == '1' && bindings VCR.use_cassette(prefix + '/tmp/' + explicit_cassette_name, decode_compressed_response: true, record: :all) do - run.call + run_lambda.call end make_template prefix, explicit_cassette_name, bindings else VCR.use_cassette(prefix + '/Templates/' + explicit_cassette_name, decode_compressed_response: true, erb: bindings, - record: :none) do - run.call + record: ENV['VCR_UPDATE'] == '1' ? :all : :none) do + run_lambda.call end + end + end + + def record_websocket_cassette(prefix, bindings, explicit_cassette_name, run_lambda) + options = { + record: :none, + decode_compressed_response: true + } + options[:erb] = bindings if bindings + if ENV['VCR_UPDATE'] == '1' + options[:record] = :all + options[:reverse_substitution] = true if bindings + end + WebSocketVCR.use_cassette(prefix + '/' + explicit_cassette_name, options) do + run_lambda.call end end end RSpec.configure do |config|