diff --git a/lib/puppet/parser/functions/get_provider_for.rb b/lib/puppet/parser/functions/get_provider_for.rb index 2fb117b..de49e6c 100644 --- a/lib/puppet/parser/functions/get_provider_for.rb +++ b/lib/puppet/parser/functions/get_provider_for.rb @@ -1,24 +1,13 @@ - module Puppet::Parser::Functions newfunction(:get_provider_for, :type => :rvalue, :doc => <<-EOS - Get the default provider of a type + Get resource provider by given name and type EOS ) do |argv| - type_name = argv[0] - res_name = argv[1] + type_name = argv[0].to_s + resource_name = argv[1].to_s fail('No type name provided!') if ! type_name - Puppet::Type.loadall() - type_name = type_name.capitalize.to_sym - return 'undef' if ! Puppet::Type.const_defined? type_name - type = Puppet::Type.const_get type_name -# require 'pry' -# binding.pry - type.loadall() - rv = type.instances.select{|i| i.name.to_s.downcase == res_name.to_s.downcase}.map{|j| j[:provider].to_s} -# require 'pry' -# binding.pry - rv = rv[0] - debug("Provider for '#{type_name}[#{res_name}]' is a '#{rv}'.") - return rv + # type.loadall() + resource = catalog.resources.select{|res| res.type.to_s==type_name and res.title.to_s==resource_name }[0] + ( resource.nil? ? nil : resource[:provider].to_s ) end -end \ No newline at end of file +end diff --git a/manifests/l2/patch.pp b/manifests/l2/patch.pp index 85db349..c4f85a5 100644 --- a/manifests/l2/patch.pp +++ b/manifests/l2/patch.pp @@ -26,13 +26,24 @@ include ::stdlib include ::l23network::params - #$provider_1 = get_provider_for('L2_bridge', bridges[0]) # this didn't work, because parser functions - #$provider_2 = get_provider_for('L2_bridge', bridges[1]) # executed before resources prefetch - # Architecture limitation. - # We can't create more one patch between same bridges. - $patch_name = get_patch_name($bridges) - $patch_jacks_names = get_pair_of_jack_names($bridges) + # We can't create more then one patch between the same bridges. + $bridge1_provider = get_provider_for('L2_bridge', $bridges[0]) + $bridge2_provider = get_provider_for('L2_bridge', $bridges[1]) + + if $bridge1_provider == 'ovs' and $bridge2_provider == 'ovs' { + $act_bridges = sort($bridges) + $do_not_create_stored_config = true + } elsif $bridge1_provider == 'ovs' and $bridge2_provider == 'lnx' { + $act_bridges = [$bridges[0], $bridges[1]] + } elsif $bridge1_provider == 'lnx' and $bridge2_provider == 'ovs' { + $act_bridges = [$bridges[1], $bridges[0]] + } else { + $act_bridges = $bridges + } + + $patch_name = get_patch_name($act_bridges) + $patch_jacks_names = get_pair_of_jack_names($act_bridges) if ! defined(L2_patch[$patch_name]) { if $provider { @@ -41,27 +52,33 @@ $config_provider = undef } - if ! defined(L23_stored_config[$patch_jacks_names[0]]) { - # we use only one (last) patch jack name here and later, - # because a both jacks for patch - # creates by one command. This command stores in one config file. - l23_stored_config { $patch_jacks_names[0]: } - } - L23_stored_config <| title == $patch_jacks_names[0] |> { - ensure => $ensure, - if_type => 'ethernet', - bridge => $bridges, - jacks => $patch_jacks_names, - mtu => $mtu, - onboot => true, - vendor_specific => $vendor_specific, - provider => $config_provider + if ! $do_not_create_stored_config { + # we do not create any configs for ovs2ovs patchcords, because + # neither CenOS5 nor Ubuntu with OVS < 2.4 supports creating patch resources + # from network config files. But OVSDB stores patch configuration and this is + # enough to restore after reboot + if ! defined(L23_stored_config[$patch_jacks_names[0]]) { + # we use only one (first) patch jack name here and later, + # because a both jacks for patch are created by + # one command. This command stores in one config file. + l23_stored_config { $patch_jacks_names[0]: } + } + L23_stored_config <| title == $patch_jacks_names[0] |> { + ensure => $ensure, + if_type => 'ethernet', + bridge => $act_bridges, + jacks => $patch_jacks_names, + mtu => $mtu, + onboot => true, + vendor_specific => $vendor_specific, + provider => $config_provider + } + L23_stored_config[$patch_jacks_names[0]] -> L2_patch[$patch_name] } - L23_stored_config[$patch_jacks_names[0]] -> L2_patch[$patch_name] l2_patch{ $patch_name : ensure => $ensure, - bridges => $bridges, + bridges => $act_bridges, use_ovs => $use_ovs, jacks => $patch_jacks_names, vlan_ids => $vlan_ids, @@ -73,4 +90,4 @@ Anchor['l23network::init'] -> K_mod<||> -> L2_patch<||> } } -# vim: set ts=2 sw=2 et : \ No newline at end of file +# vim: set ts=2 sw=2 et : diff --git a/spec/classes/ovs2lnx_patch__spec.rb b/spec/classes/ovs2lnx__ovs_patch__spec.rb similarity index 97% rename from spec/classes/ovs2lnx_patch__spec.rb rename to spec/classes/ovs2lnx__ovs_patch__spec.rb index 8c4c30c..8056210 100644 --- a/spec/classes/ovs2lnx_patch__spec.rb +++ b/spec/classes/ovs2lnx__ovs_patch__spec.rb @@ -30,7 +30,6 @@ end context 'Patch between OVS and LNX bridges.' do - let(:title) { 'Centos has delay for port after boot' } let(:facts) { { :osfamily => 'Debian', @@ -116,7 +115,6 @@ 'provider' => 'ovs_ubuntu' }) end - end end diff --git a/spec/classes/ovs2ovs__ovs_patch__spec.rb b/spec/classes/ovs2ovs__ovs_patch__spec.rb new file mode 100644 index 0000000..e32f32d --- /dev/null +++ b/spec/classes/ovs2ovs__ovs_patch__spec.rb @@ -0,0 +1,111 @@ +require 'spec_helper' + +describe 'l23network::examples::run_network_scheme', :type => :class do +let(:network_scheme) do +< 'Debian', + :operatingsystem => 'Ubuntu', + :kernel => 'Linux', + :l23_os => 'ubuntu', + :l3_fqdn_hostname => 'stupid_hostname', + } + } + + let(:params) do { + :settings_yaml => network_scheme, + } end + + get_provider_for = {} + before(:each) do + if ENV['SPEC_PUPPET_DEBUG'] + Puppet::Util::Log.level = :debug + Puppet::Util::Log.newdestination(:console) + end + + Puppet::Parser::Functions.newfunction(:get_provider_for, :type => :rvalue) { + |args| get_provider_for.call(args[0], args[1]) + } + + get_provider_for.stubs(:call).with('L2_bridge', 'br-ovs1').returns('ovs') + get_provider_for.stubs(:call).with('L2_bridge', 'br-ovs2').returns('ovs') + end + + it do + should compile.with_all_deps + end + + it do + should contain_l23_stored_config('br-ovs1').with({ + 'ensure' => 'present', + 'provider' => 'ovs_ubuntu' + }) + end + + it do + should contain_l23_stored_config('br-ovs2').with({ + 'ensure' => 'present', + 'provider' => 'ovs_ubuntu' + }) + end + + it do + should contain_l2_bridge('br-ovs1').with({ + 'ensure' => 'present', + 'provider' => 'ovs' + }) + end + + it do + should contain_l2_bridge('br-ovs2').with({ + 'ensure' => 'present', + 'provider' => 'ovs' + }) + end + + it do + should contain_l2_patch('patch__br-ovs1--br-ovs2').with({ + 'ensure' => 'present', + 'bridges' => ['br-ovs1', 'br-ovs2'], + 'vlan_ids' => ['0', '0'], + 'provider' => 'ovs' + }) + end + + it do + should contain_l2_patch('patch__br-ovs1--br-ovs2').with_jacks(['p_f277dc2b-0', 'p_f277dc2b-1']) + end + + it do + should_not contain_l23_stored_config('p_f277dc2b-0') + end + end + +end + +# vim: set ts=2 sw=2 et diff --git a/spec/defines/l2_patch__spec.rb b/spec/defines/l2_patch__spec.rb index aa7fcc1..6933cc1 100644 --- a/spec/defines/l2_patch__spec.rb +++ b/spec/defines/l2_patch__spec.rb @@ -6,7 +6,7 @@ # names, that connected by patchcord. describe 'l23network::l2::patch', :type => :define do - let(:title) { 'Spec for l23network::l2::port' } + let(:title) { 'test ovs2lnx patchcord' } let(:facts) { { :osfamily => 'Debian', :operatingsystem => 'Ubuntu', @@ -14,6 +14,17 @@ :l23_os => 'ubuntu', :l3_fqdn_hostname => 'stupid_hostname', } } + + get_provider_for = {} + before(:each) { + Puppet::Parser::Functions.newfunction(:get_provider_for, :type => :rvalue) { + |args| get_provider_for.call(args[0], args[1]) + } + + get_provider_for.stubs(:call).with('L2_bridge', 'br1').returns('ovs') + get_provider_for.stubs(:call).with('L2_bridge', 'br2').returns('lnx') + } + let(:pre_condition) { [ "class {'l23network': }" ] } @@ -64,7 +75,7 @@ 'ipaddr' => nil, 'gateway' => nil, 'onboot' => true, - 'bridge' => ['br2', 'br1'], + 'bridge' => ['br1', 'br2'], 'jacks' => ['p_39a440c1-0', 'p_39a440c1-1'] }) end @@ -72,12 +83,12 @@ it do should contain_l2_patch('patch__br1--br2').with({ 'ensure' => 'present', - 'bridges' => ['br2', 'br1'], + 'bridges' => ['br1', 'br2'], }).that_requires('L23_stored_config[p_39a440c1-0]') end end - context 'Just a patch between two OVS bridges' do + context 'Patch between two bridges, with explicitly defined OVS provider.' do let(:params) do { :bridges => ['br1', 'br2'], @@ -117,12 +128,18 @@ end it do - should compile + should compile.with_all_deps + end + + it do should contain_l23_stored_config('p_39a440c1-0').with({ 'bridge' => ['br1', 'br2'], 'jacks' => ['p_39a440c1-0', 'p_39a440c1-1'], 'mtu' => 9000, }) + end + + it do should contain_l2_patch('patch__br1--br2').with({ 'ensure' => 'present', 'mtu' => 9000, @@ -146,7 +163,10 @@ end it do - should compile + should compile.with_all_deps + end + + it do should contain_l23_stored_config('p_39a440c1-0').with({ 'bridge' => ['br1', 'br2'], 'jacks' => ['p_39a440c1-0', 'p_39a440c1-1'], @@ -158,6 +178,9 @@ }, }, }) + end + + it do should contain_l2_patch('patch__br1--br2').with({ 'ensure' => 'present', 'bridges' => ['br1', 'br2'], @@ -172,7 +195,7 @@ end end - context 'Tagged patchcord between OVS bridges' do + context 'Tagged patchcord with explicitly defined OVS provider.' do let(:params) do { :bridges => ['br1', 'br2'], @@ -201,6 +224,63 @@ end end +end + +describe 'l23network::l2::patch', :type => :define do + let(:title) { 'test ovs2ovs patchcord' } + let(:facts) { { + :osfamily => 'Debian', + :operatingsystem => 'Ubuntu', + :kernel => 'Linux', + :l23_os => 'ubuntu', + :l3_fqdn_hostname => 'stupid_hostname', + } } + + get_provider_for = {} + before(:each) { + Puppet::Parser::Functions.newfunction(:get_provider_for, :type => :rvalue) { + |args| get_provider_for.call(args[0], args[1]) + } + + get_provider_for.stubs(:call).with('L2_bridge', 'br1').returns('ovs') + get_provider_for.stubs(:call).with('L2_bridge', 'br2').returns('ovs') + } + + let(:pre_condition) { [ + "class {'l23network': }" + ] } + + + context 'Just a ovs2ovs patch between two bridges' do + let(:params) do + { + :bridges => ['br1', 'br2'], + } + end + + it do + should compile.with_all_deps + end + + it do + should_not contain_l23_stored_config('p_39a440c1-0').with({ + 'ipaddr' => nil, + 'gateway' => nil, + 'onboot' => true, + 'bridge' => ['br1', 'br2'], + 'jacks' => ['p_39a440c1-0', 'p_39a440c1-1'] + }) + end + + it do + should contain_l2_patch('patch__br1--br2').with({ + 'ensure' => 'present', + 'bridges' => ['br1', 'br2'], + }) + end + end end -# vim: set ts=2 sw=2 et \ No newline at end of file + + +# vim: set ts=2 sw=2 et