From 132019a6d97478a00ad7169e275d9749b724e820 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Wed, 14 Oct 2015 23:44:15 +0200 Subject: [PATCH] move backend creation to profile context keeping it in the runner will create conflicts with rspec runners --- lib/vulcano/profile_context.rb | 32 ++++++++++++++++++++++++++++++++ lib/vulcano/runner.rb | 30 +----------------------------- test/helper.rb | 16 ++++++++-------- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/lib/vulcano/profile_context.rb b/lib/vulcano/profile_context.rb index 376eccfff..cdead884e 100644 --- a/lib/vulcano/profile_context.rb +++ b/lib/vulcano/profile_context.rb @@ -4,6 +4,7 @@ require 'vulcano/rule' require 'vulcano/dsl' +require 'train' module Vulcano class ProfileContext @@ -140,5 +141,36 @@ module Vulcano end # rubocop:enable all end + + # Create the transport backend with aggregated resources. + # + # @param [Hash] config for the transport backend + # @return [Transport] enriched transport instance + def self.create_backend(config) + conf = Train.target_config(config) + name = conf[:backend] || :local + transport = Train.create(name, conf) + if transport.nil? + fail "Can't find transport backend '#{name}'." + end + + connection = transport.connection + if connection.nil? + fail "Can't connect to transport backend '#{name}'." + end + + cls = Class.new do + define_method :backend do + connection + end + Vulcano::Resource.registry.each do |id, r| + define_method id.to_sym do |*args| + r.new(self, id.to_s, *args) + end + end + end + + cls.new + end end end diff --git a/lib/vulcano/runner.rb b/lib/vulcano/runner.rb index 892c58583..a4340275c 100644 --- a/lib/vulcano/runner.rb +++ b/lib/vulcano/runner.rb @@ -5,7 +5,6 @@ # author: Christoph Hartmann require 'uri' -require 'train' require 'vulcano/targets' require 'vulcano/profile_context' # spec requirements @@ -38,35 +37,8 @@ module Vulcano RSpec.configuration.add_formatter(@conf['format'] || 'progress') end - def self.create_backend(config) - conf = Train.target_config(config) - name = conf[:backend] || :local - transport = Train.create(name, conf) - if transport.nil? - fail "Can't find transport backend '#{name}'." - end - - connection = transport.connection - if connection.nil? - fail "Can't connect to transport backend '#{name}'." - end - - cls = Class.new do - define_method :backend do - connection - end - Vulcano::Resource.registry.each do |id, r| - define_method id.to_sym do |*args| - r.new(self, id.to_s, *args) - end - end - end - - cls.new - end - def configure_transport - @backend = self.class.create_backend(@conf) + @backend = ProfileContext.create_backend(@conf) end def add_tests(tests) diff --git a/test/helper.rb b/test/helper.rb index 22d231a70..e5f253b02 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -14,7 +14,7 @@ SimpleCov.start do end require 'vulcano/resource' -require 'train' +require 'vulcano/profile_context' class MockLoader # pass the os identifier to emulate a specific operating system @@ -47,8 +47,11 @@ class MockLoader scriptpath = ::File.realpath(::File.dirname(__FILE__)) # create mock backend - @backend = Train.create('mock') - mock = @backend.connection + @backend = Vulcano::ProfileContext.create_backend({ backend: :mock }) + mock = @backend.backend + + # set os emulation + mock.mock_os(@os) # create all mock files local = Train.create('local').connection @@ -92,11 +95,11 @@ class MockLoader # create all mock commands cmd = lambda {|x| stdout = ::File.read(::File.join(scriptpath, '/unit/mock/cmd/'+x)) - mock.mock_command(stdout, '', 0) + mock.mock_command('', stdout, '', 0) } empty = lambda { - mock.mock_command('', '', 0) + mock.mock_command('', '', '', 0) } mock.commands = { @@ -179,9 +182,6 @@ class MockLoader 'iptables -S' => cmd.call('iptables-s'), } - # set os emulation - mock.os = @os - @backend end