require 'inifile' def obtain_credentials if ENV['ORBIT_USER'] && ENV['ORBIT_KEY'] [ENV['ORBIT_USER'], ENV['ORBIT_KEY']] else config = IniFile.load(ENV['HOME']+ '/.brightbox/config') if config.nil? || config.sections.empty? raise(RuntimeError, "No brightbox config in home directory. Can't obtain auth token for tests") end section = config.sections.first [config[section]['client_id'], config[section]['secret']] end end def fetch_orbit_token @clientid, @key = obtain_credentials @current_url_target = ENV['ORBIT_URL'] || 'https://orbit.brightbox.com/v1' cmd = "curl -I #{@current_url_target} -H 'X-Auth-User: #{@clientid}' -H 'X-Auth-Key: #{@key}'" run_simple(unescape(cmd)) stdout_from(cmd).each_line do |line| key, value = line.strip.split(/\s*:\s*/,2) @current_auth_token = value if key == 'X-Auth-Token' @current_storage_url = value if key == 'X-Storage-Url' end raise(RuntimeError, "Credentials not accepted. Can't obtain auth token for tests.") unless @current_auth_token && @current_storage_url end def create_download_file(filetype, name, container) upload_url = File.join(@current_storage_url, container, name) tempfile = '/tmp/fred55322' FileUtils.rm_f(Dir.glob(tempfile+'*')) format = 'raw' size = '1G' case filetype when 'qcow2' format = 'qcow2' when 'zero' size = '0' end cmd = "qemu-img create -f #{format} #{tempfile} #{size} >/dev/null 2>&1" system(unescape(cmd)) cmd = "cat #{tempfile}" if filetype == 'lz4' cmd += " | lz4c " end cmd += "| curl --silent --fail -T - -H 'X-Auth-Token: #{@current_auth_token}' #{upload_url} " system(unescape(cmd)) FileUtils.rm_f(Dir.glob(tempfile+'*')) end def create_container(name) url = File.join(@current_storage_url, name) cmd = "curl --silent --fail -I -H 'X-Auth-Token: #{@current_auth_token}' #{url}>/dev/null || curl --silent --fail -I -H 'X-Auth-Token: #{@current_auth_token}' #{url} -X PUT" system(unescape(cmd)) end Given(/^a container called "([^"]*)"$/) do |container| create_container(container) end Given(/^a cache of "(.*?)" from container "(.*?)" at "(.*?)"$/) do |name, container, cache| url = File.join(@current_storage_url, container, name) target = File.join(cache, 'cache', name) steps %{ When I successfully run `curl --fail --silent -o #{target} -H 'X-Auth-Token: #{@current_auth_token}' #{url}` } end Given /^a (\d+) byte lz4 file named "([^"]*)"$/ do |file_size, file_name| lz4_target=file_name+'.lz4' write_fixed_size_file(file_name, file_size.to_i) system("lz4c #{file_name} #{lz4_target}") File.rename lz4_target, file_name end When(/^I store "([^"]*)" into container "([^"]*)" from "([^"]*)"$/) do |name, container, source| steps %{ When I store "#{name}" with options "" into container "#{container}" from "#{source}" } end When(/^I retrieve "([^"]*)" with options "([^"]*)" from container "([^"]*)" into "([^"]*)"$/) do |name, runoptions, container, target| url = File.join(@current_storage_url, container, name) steps %{ When I successfully run `uricp #{runoptions} --auth-token #{@current_auth_token} #{url} #{target}` } end When(/^I retrieve "([^"]*)" from container "([^"]*)" into "([^"]*)" with a userid$/) do |name, container, target| url = File.join(@current_storage_url, container, name) steps %{ When I successfully run `uricp --auth-user '#{@clientid}' --auth-key '#{@key}' #{url} #{target}` } end When(/^I store "([^"]*)" into container "([^"]*)" from "([^"]*)" with a userid$/) do |name, container, source| @current_container = container @current_name = name url = File.join(@current_storage_url, container, name) steps %{ When I successfully run `uricp --auth-user '#{@clientid}' --auth-key '#{@key}' #{source} #{url}` } end When(/^I retrieve "([^"]*)" from container "([^"]*)" into "([^"]*)"$/) do |name, container, target| steps %{ When I retrieve "#{name}" with options "" from container "#{container}" into "#{target}" } end When(/^I store "([^"]*)" with options "([^"]*)" into container "([^"]*)" from "([^"]*)"$/) do |name, runoptions, container, source| @current_container = container @current_name = name url = File.join(@current_storage_url, container, name) steps %{ When I successfully run `uricp #{runoptions} --auth-token #{@current_auth_token} #{source} #{url}` } end When(/^I store "([^"]*)" with segment size "([^"]*)" into container "([^"]*)" from "([^"]*)" with a userid$/) do |name, segment_size, container, source| @current_container = container @current_name = name url = File.join(@current_storage_url, container, name) steps %{ When I successfully run `uricp --auth-user '#{@clientid}' --auth-key '#{@key}' --segment-size '#{segment_size}' #{source} #{url}` } end When(/^I store "([^"]*)" with segment size "([^"]*)" and options "([^"]*)" into container "([^"]*)" from "([^"]*)" with a userid$/) do |name, segment_size, runoptions, container, source| @current_container = container @current_name = name url = File.join(@current_storage_url, container, name) steps %{ When I successfully run `uricp #{runoptions} --auth-user '#{@clientid}' --auth-key '#{@key}' --segment-size '#{segment_size}' #{source} #{url}` } end When(/^I upload "([^"]*)" with segment size "([^"]*)" into container "([^"]*)" from "([^"]*)"$/) do |name, segment_size, container, source| @current_container = container @current_name = name url = File.join(@current_storage_url, container, name) steps %{ When I successfully run `segment_upload --from #{source} --auth-token #{@current_auth_token} --segment-size #{segment_size} #{url}` } end When(/^I upload "([^"]*)" with segment size "([^"]*)" into container "([^"]*)" from "([^"]*)" as a stream$/) do |name, segment_size, container, source| @current_container = container @current_name = name url = File.join(@current_storage_url, container, name) steps %{ When I successfully run `sh -c 'cat #{source} | segment_upload --auth-token #{@current_auth_token} --segment-size #{segment_size} #{url}'` } end Then(/^a (\d+) byte entry should exist in container "(.*?)" called "(.*?)"$/) do |size, container, name| url = File.join(@current_storage_url, container, name) command = "curl -I --fail --silent -H 'X-Auth-Token:#{@current_auth_token}' #{url}" steps %{ When I successfully run `#{command}` Then the output should match /\\sContent-Length: #{size}\\s/ } end Then(/^the container "(.*?)" should contain (\d+) entries$/) do |container, quantity| url = File.join(@current_storage_url, container) command = "curl -I --fail --silent -H 'X-Auth-Token:#{@current_auth_token}' #{url}" steps %{ When I successfully run `#{command}` Then the output should match /\\sX-Container-Object-Count: #{quantity}\\s/ } end Then(/^an lz4 compressed entry should exist in container "(.*?)" called "(.*?)"$/) do |container, name| url = File.join(@current_storage_url, container, name) cmd="curl -r 0-3 --fail --silent -H X-Auth-Token:#{@current_auth_token} #{url}" run_simple(unescape(cmd)) assert_exact_output([0x184D2204].pack('V'), stdout_from(cmd)) end Then(/^a qcow2 entry should exist in container "(.*?)" called "(.*?)"$/) do |container, name| url = File.join(@current_storage_url, container, name) cmd="curl -r 0-3 --fail --silent -H X-Auth-Token:#{@current_auth_token} #{url}" run_simple(unescape(cmd)) assert_exact_output(['QFI',0xfb].pack('a3C'), stdout_from(cmd)) end Before('@orbit') do fetch_orbit_token unless $orbit_setup system("swift --quiet --insecure --os-storage-url=#{@current_storage_url} --os-auth-token=#{@current_auth_token} delete test") system("swift --quiet --insecure --os-storage-url=#{@current_storage_url} --os-auth-token=#{@current_auth_token} delete temp_dlo") system("swift --quiet --insecure --os-storage-url=#{@current_storage_url} --os-auth-token=#{@current_auth_token} delete test_upload") end end Before('@orbitdownloads') do $orbit_setup ||= false unless $orbit_setup create_container('test') create_download_file('qcow2', 'img-qcow2', 'test') create_download_file('lz4', 'img-lz4cy', 'test') create_download_file('zero', 'img-zeroy', 'test') $orbit_setup = true end end After('@orbit') do if @current_auth_token && @current_container system("swift --quiet --insecure --os-storage-url=#{@current_storage_url} --os-auth-token=#{@current_auth_token} delete #{@current_container} #{@current_name}") @current_container = @current_name = nil end end