Sha256: 22bcecd19dd29c0bf3b779193b23d5c0786eb41f8226c7305e4c753c5e2ac21c

Contents?: true

Size: 1.98 KB

Versions: 15

Compression:

Stored size: 1.98 KB

Contents

require 'rubygems'
require 'bundler/setup'
require 'winrm-fs'
require 'json'
require_relative 'matchers'

# Creates a WinRM connection for integration tests
module ConnectionHelper
  def winrm_connection
    WinRM::Connection.new(config)
  end

  def config
    @config ||= begin
      cfg = symbolize_keys(YAML.safe_load(File.read(winrm_config_path)))
      cfg[:basic_auth_only] = true unless cfg[:transport].eql? :kerberos
      merge_environment!(cfg)
      cfg
    end
  end

  def merge_environment!(config)
    merge_config_option_from_environment(config, 'user')
    merge_config_option_from_environment(config, 'password')
    merge_config_option_from_environment(config, 'no_ssl_peer_verification')
    config[:options][:ssl_peer_fingerprint] = ENV['winrm_cert'] if ENV['use_ssl_peer_fingerprint']
    config[:endpoint] = ENV['winrm_endpoint'] if ENV['winrm_endpoint']
    config[:transport] = ENV['winrm_transport'] if ENV['winrm_transport']
  end

  def merge_config_option_from_environment(config, key)
    env_key = 'winrm_' + key
    config[key.to_sym] = ENV[env_key] if ENV[env_key]
  end

  def winrm_config_path
    # Copy config-example.yml to config.yml and edit for your local configuration
    path = File.expand_path("#{File.dirname(__FILE__)}/config.yml")
    unless File.exist?(path)
      # user hasn't done this, so use sane defaults for unit tests
      path = File.expand_path("#{File.dirname(__FILE__)}/config-example.yml")
    end
    path
  end

  # rubocop:disable Metrics/MethodLength
  def symbolize_keys(hash)
    hash.each_with_object({}) do |(key, value), result|
      new_key = case key
                when String then key.to_sym
                else key
                end
      new_value = case value
                  when Hash then symbolize_keys(value)
                  else value
                  end
      result[new_key] = new_value
      result
    end
  end
  # rubocop:enable Metrics/MethodLength
end

RSpec.configure do |config|
  config.include(ConnectionHelper)
end

Version data entries

15 entries across 10 versions & 2 rubygems

Version Path
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/winrm-fs-1.3.1/spec/spec_helper.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/winrm-fs-1.3.0/spec/spec_helper.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/winrm-fs-1.3.1/spec/spec_helper.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/winrm-fs-1.3.0/spec/spec_helper.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/winrm-fs-1.3.0/spec/spec_helper.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/winrm-fs-1.3.1/spec/spec_helper.rb
vagrant-unbundled-2.2.2.0 vendor/bundle/ruby/2.5.0/gems/winrm-fs-1.3.0/spec/spec_helper.rb
vagrant-unbundled-2.2.2.0 vendor/bundle/ruby/2.5.0/gems/winrm-fs-1.3.1/spec/spec_helper.rb
vagrant-unbundled-2.2.0.0 vendor/bundle/ruby/2.5.0/gems/winrm-fs-1.3.1/spec/spec_helper.rb
vagrant-unbundled-2.2.0.0 vendor/bundle/ruby/2.5.0/gems/winrm-fs-1.3.0/spec/spec_helper.rb
winrm-fs-1.3.1 spec/spec_helper.rb
vagrant-unbundled-2.1.4.0 vendor/bundle/ruby/2.5.0/gems/winrm-fs-1.3.0/spec/spec_helper.rb
winrm-fs-1.3.0 spec/spec_helper.rb
vagrant-unbundled-2.1.2.0 vendor/bundle/ruby/2.3.0/gems/winrm-fs-1.2.1/spec/spec_helper.rb
winrm-fs-1.2.1 spec/spec_helper.rb