Sha256: 7783c1b5a2426c6d62199530df055aa343715e9503d20f803f5090b75e9e948d

Contents?: true

Size: 1.85 KB

Versions: 32

Compression:

Stored size: 1.85 KB

Contents

# encoding: UTF-8
require 'rubygems'
require 'bundler/setup'
require 'winrm'
require 'json'
require_relative '../matchers'

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

  def connection_opts
    @config ||= begin
      cfg = symbolize_keys(YAML.load(File.read(winrm_config_path)))
      merge_environment(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')
    if ENV['use_ssl_peer_fingerprint']
      config[:ssl_peer_fingerprint] = ENV['winrm_cert']
    end
    config[:endpoint] = ENV['winrm_endpoint'] if ENV['winrm_endpoint']
    config
  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 ConnectionOpts
    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

  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
end

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

Version data entries

32 entries across 29 versions & 3 rubygems

Version Path
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.2.2.0 vendor/bundle/ruby/2.5.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.2.0.0 vendor/bundle/ruby/2.5.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.1.4.0 vendor/bundle/ruby/2.5.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.1.2.0 vendor/bundle/ruby/2.3.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.1.1.0 vendor/bundle/ruby/2.5.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.0.4.0 vendor/bundle/ruby/2.5.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.0.3.0 vendor/bundle/ruby/2.5.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.4.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.5.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.0.1.0 vendor/bundle/ruby/2.4.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-2.0.0.1 vendor/bundle/ruby/2.4.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-1.9.8.1 vendor/bundle/ruby/2.4.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb
vagrant-unbundled-1.9.7.1 vendor/bundle/ruby/2.4.0/gems/winrm-2.2.3/tests/integration/spec_helper.rb