<commit_before># Copyright 2015-2017 SUSE LLC
require 'timeout'
require 'open-uri'
require 'tempfile'

Given(/^salt-minion is configured on "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos_minion
  else
    raise "no valid name of minion given! "
  end
  # cleanup the key in case the image was reused
  # to run the test twice
  step %(I delete "#{minion}" key in the Salt master)
  step %(I stop salt-minion on "#{minion}")
  step %(I stop salt-master)
  key = '/etc/salt/pki/minion/minion_master.pub'
  if file_exists?(target, key)
    file_delete(target, key)
    puts "Key #{key} has been removed on minion"
  end
  cmd = " echo  \'master : #{$server_ip}\' > /etc/salt/minion.d/susemanager.conf"
  target.run(cmd, false)
  step %(I start salt-master)
  step %(I start salt-minion on "#{minion}")
end

Given(/^the salt-master can reach "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target_fullhostname = $minion_fullhostname
  elsif minion == "ceos-minion"
    target_fullhostname = $ceos_minion_fullhostname
  else
    raise "no valid name of minion given! "
  end
  begin
    start = Time.now
    # 300 is the default 1st keepalive interval for the minion
    # where it realizes the connection is stuck
    KEEPALIVE_TIMEOUT = 300
    Timeout.timeout(KEEPALIVE_TIMEOUT) do
      # only try 3 times
      3.times do
        out, _code = $server.run("salt #{target_fullhostname} test.ping")
        if out.include?(target_fullhostname) && out.include?('True')
          finished = Time.now
          puts "Took #{finished.to_i - start.to_i} seconds to contact the minion"
          break
        end
        sleep(1)
      end
    end
  rescue Timeout::Error
      fail "Master can not communicate with #{minion}: #{@output[:stdout]}"
  end
end

Given(/^I am on the Systems overview page of "(.*?)"$/) do |minion|
  steps %(
    Given I am on the Systems page
    And I follow "Systems" in the left menu
    And I follow "#{minion}" link
    )
end

When(/^I follow "(.*?)" link$/) do |host|
  target_hostname = get_target_hostname(host)
  step %(I follow "#{target_hostname}")
end

When(/^I get the contents of the remote file "(.*?)"$/) do |filename|
  $output, _code = $server.run("cat #{filename}")
end

When(/^I stop salt-master$/) do
  $server.run("systemctl stop salt-master", false)
end

When(/^I start salt-master$/) do
  $server.run("systemctl start salt-master", false)
end

When(/^I stop salt-minion on "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos_minion
  else
    raise "no valid name of minion given! "
  end
  target.run("systemctl stop salt-minion", false)
end

When(/^I start salt-minion on "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos_minion
  else
    raise "no valid name of minion given! "
  end
  target.run("systemctl restart salt-minion", false)
end

When(/^I restart salt-minion on "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos_minion
  else
    raise "no valid name of minion given! "
  end
  target.run("systemctl restart salt-minion", false)
end

Then(/^salt-minion should be running on "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos_minion
  else
    raise "no valid name of minion given! "
  end
  i = 0
  MAX_ITER = 40
  loop do
    _out, code = target.run("systemctl status salt-minion", false)
    break if code.zero?
    sleep 5
    puts "sleeping 5 secs, minion not active."
    i += 1
    raise "TIMEOUT; something wrong with minion status" if i == MAX_ITER
  end
end

When(/^I list "(.*?)" keys at Salt Master$/) do |key_type|
  $output, _code = $server.run("salt-key --list #{key_type}", false)
  $output.strip
end

Then(/^the list of the "(.*?)" keys should contain "(.*?)" hostname$/) do |key_type, minion|
  if minion == "sle-minion"
    target_fullhostname = $minion_fullhostname
  elsif minion == "ceos-minion"
    target_fullhostname = $ceos_minion_fullhostname
  else
    raise "no valid name of minion given! "
  end
  sleep 30
  # FIXME: find better way then to wait 30 seconds
  $output, _code = $server.run("salt-key --list #{key_type}", false)
  assert_match(target_fullhostname, $output, "minion #{target_fullhostname} is not listed as #{key_type} key on salt-master #{$output}")
end

When(/^I wait until no Salt job is running on "(.*?)"$/) do |minion|
begin
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos
  elsif minion == "sle-migrated-minion"
    target = $client
  else
    raise "no valid name of minion given! "
  end
  Timeout.timeout(DEFAULT_TIMEOUT) do
    $output, _code = target.run("salt-call saltutil.running")
    break if $output.split('\n').length == 1
    sleep 3
    end
  rescue Timeout::Error
    raise "a Salt job is still running on %{minion} after timeout"
  end
end

When(/^we wait till Salt master sees "(.*?)" as "(.*?)"$/) do |minion, key_type|<commit_msg>Make rubocop happy<commit_after># Copyright 2015-2017 SUSE LLC
require 'timeout'
require 'open-uri'
require 'tempfile'

Given(/^salt-minion is configured on "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos_minion
  else
    raise "no valid name of minion given! "
  end
  # cleanup the key in case the image was reused
  # to run the test twice
  step %(I delete "#{minion}" key in the Salt master)
  step %(I stop salt-minion on "#{minion}")
  step %(I stop salt-master)
  key = '/etc/salt/pki/minion/minion_master.pub'
  if file_exists?(target, key)
    file_delete(target, key)
    puts "Key #{key} has been removed on minion"
  end
  cmd = " echo  \'master : #{$server_ip}\' > /etc/salt/minion.d/susemanager.conf"
  target.run(cmd, false)
  step %(I start salt-master)
  step %(I start salt-minion on "#{minion}")
end

Given(/^the salt-master can reach "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target_fullhostname = $minion_fullhostname
  elsif minion == "ceos-minion"
    target_fullhostname = $ceos_minion_fullhostname
  else
    raise "no valid name of minion given! "
  end
  begin
    start = Time.now
    # 300 is the default 1st keepalive interval for the minion
    # where it realizes the connection is stuck
    KEEPALIVE_TIMEOUT = 300
    Timeout.timeout(KEEPALIVE_TIMEOUT) do
      # only try 3 times
      3.times do
        out, _code = $server.run("salt #{target_fullhostname} test.ping")
        if out.include?(target_fullhostname) && out.include?('True')
          finished = Time.now
          puts "Took #{finished.to_i - start.to_i} seconds to contact the minion"
          break
        end
        sleep(1)
      end
    end
  rescue Timeout::Error
      fail "Master can not communicate with #{minion}: #{@output[:stdout]}"
  end
end

Given(/^I am on the Systems overview page of "(.*?)"$/) do |minion|
  steps %(
    Given I am on the Systems page
    And I follow "Systems" in the left menu
    And I follow "#{minion}" link
    )
end

When(/^I follow "(.*?)" link$/) do |host|
  target_hostname = get_target_hostname(host)
  step %(I follow "#{target_hostname}")
end

When(/^I get the contents of the remote file "(.*?)"$/) do |filename|
  $output, _code = $server.run("cat #{filename}")
end

When(/^I stop salt-master$/) do
  $server.run("systemctl stop salt-master", false)
end

When(/^I start salt-master$/) do
  $server.run("systemctl start salt-master", false)
end

When(/^I stop salt-minion on "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos_minion
  else
    raise "no valid name of minion given! "
  end
  target.run("systemctl stop salt-minion", false)
end

When(/^I start salt-minion on "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos_minion
  else
    raise "no valid name of minion given! "
  end
  target.run("systemctl restart salt-minion", false)
end

When(/^I restart salt-minion on "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos_minion
  else
    raise "no valid name of minion given! "
  end
  target.run("systemctl restart salt-minion", false)
end

Then(/^salt-minion should be running on "(.*?)"$/) do |minion|
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos_minion
  else
    raise "no valid name of minion given! "
  end
  i = 0
  MAX_ITER = 40
  loop do
    _out, code = target.run("systemctl status salt-minion", false)
    break if code.zero?
    sleep 5
    puts "sleeping 5 secs, minion not active."
    i += 1
    raise "TIMEOUT; something wrong with minion status" if i == MAX_ITER
  end
end

When(/^I list "(.*?)" keys at Salt Master$/) do |key_type|
  $output, _code = $server.run("salt-key --list #{key_type}", false)
  $output.strip
end

Then(/^the list of the "(.*?)" keys should contain "(.*?)" hostname$/) do |key_type, minion|
  if minion == "sle-minion"
    target_fullhostname = $minion_fullhostname
  elsif minion == "ceos-minion"
    target_fullhostname = $ceos_minion_fullhostname
  else
    raise "no valid name of minion given! "
  end
  sleep 30
  # FIXME: find better way then to wait 30 seconds
  $output, _code = $server.run("salt-key --list #{key_type}", false)
  assert_match(target_fullhostname, $output, "minion #{target_fullhostname} is not listed as #{key_type} key on salt-master #{$output}")
end

When(/^I wait until no Salt job is running on "(.*?)"$/) do |minion|
begin
  if minion == "sle-minion"
    target = $minion
  elsif minion == "ceos-minion"
    target = $ceos
  elsif minion == "sle-migrated-minion"
    target = $client
  else
    raise "no valid name of minion given! "
  end
  Timeout.timeout(DEFAULT_TIMEOUT) do
    $output, _code = target.run("salt-call saltutil.running")
    break if $output.split('\n').length == 1
    sleep 3
  end
  rescue Timeout::Error
    raise "a Salt job is still running on #{minion} after timeout"
  end
end

When(/^we wait till Salt master sees "(.*?)" as "(.*?)"$/) do |minion, key_type|
{'fraction_non_alphanumeric': 0.13612262604839487, 'fraction_numerical': 0.005784247565795816, 'mean_word_length': 3.2119366626065773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1871727', 'n_tokens_mistral': 3694, 'n_tokens_neox': 3673, 'n_words': 1153}