# vim: syntax=ruby tabstop=2 softtabstop=2 shiftwidth=2 fdm=marker require 'spec_helper' describe Facter::Util::Fact do before do Facter.clear end # {{{ Instance variables let(:gluster_binary) { '/usr/sbin/gluster' } let(:gluster_volume_one) { 'volume1' } let(:gluster_brick_path) { "/data/glusterfs/#{gluster_volume_one}/brick1/brick" } let(:gluster_peer_one) { 'peer1' } # localhost let(:gluster_peer_one_uuid) { '7d1148a2-f19e-4f18-818f-3396ddf38c30' } let(:gluster_peer_one_port) { 49_153 } let(:gluster_peer_two) { 'peer2' } let(:gluster_peer_two_uuid) { 'b8a91151-9d32-43a1-8067-136ec855cb1f' } let(:gluster_peer_two_port) { 49_153 } let(:gluster_peer_three) { 'peer3' } let(:gluster_peer_three_uuid) { '35f53c52-83dc-4100-a1f7-4a7cdeee074d' } let(:gluster_peer_three_port) { 49_152 } let(:gluster_peer_shd_port) { 'N/A' } # {{{ Gluster peers let(:gluster_peer_count) { 2 } let(:gluster_peer_list) { "#{gluster_peer_two},#{gluster_peer_three}" } let(:gluster_peers) do { gluster_peer_two => { 'uuid' => gluster_peer_two_uuid, 'connected' => 1, 'state' => 3, 'status' => 'Peer in Cluster' }, gluster_peer_three => { 'uuid' => gluster_peer_three_uuid, 'connected' => 1, 'state' => 3, 'status' => 'Peer in Cluster' } } end # }}} # {{{ Gluster volumes let(:gluster_volume_list) { gluster_volume_one.to_s } let(:gluster_volumes) do { gluster_volume_one => { 'status' => 'Started', 'bricks' => [ "#{gluster_peer_one}:#{gluster_brick_path}", "#{gluster_peer_two}:#{gluster_brick_path}", "#{gluster_peer_three}:#{gluster_brick_path}" ], 'features' => { 'features.cache-invalidation' => 'true' }, 'options' => { 'nfs.disable' => 'on', 'performance.readdir-ahead' => 'on', 'auth.allow' => '10.10.0.21,10.10.0.22,10.10.0.23' }, 'ports' => [ gluster_peer_one_port, gluster_peer_two_port, gluster_peer_three_port, gluster_peer_shd_port.to_i, # Self-heal Daemon gluster_peer_shd_port.to_i, # Self-heal Daemon gluster_peer_shd_port.to_i # Self-heal Daemon ] } } end # }}} # {{{ Volume options let(:gluster_volume_options) do { gluster_volume_one => [ "features.cache-invalidation: #{gluster_volumes[gluster_volume_one]['features']['features.cache-invalidation']}", "nfs.disable: #{gluster_volumes[gluster_volume_one]['options']['nfs.disable']}", "performance.readdir-ahead: #{gluster_volumes[gluster_volume_one]['options']['performance.readdir-ahead']}", "auth.allow: #{gluster_volumes[gluster_volume_one]['options']['auth.allow']}" ] } end # }}} # {{{ Volume ports let(:gluster_volume_ports) do { gluster_volume_one => { 'ports' => [ gluster_peer_one_port, gluster_peer_two_port, gluster_peer_three_port, gluster_peer_shd_port, # Self-heal Daemon gluster_peer_shd_port, # Self-heal Daemon gluster_peer_shd_port # Self-heal Daemon ] } } end # }}} # {{{ Xml # {{{ No peer let(:gluster_no_peer) do ' 0 0 ' end # }}} # {{{ No volume let(:gluster_no_volume) do ' 0 0 0 ' end # }}} # {{{ Peer status let(:gluster_peer_status_xml) do " 0 0 #{gluster_peers[gluster_peer_two]['uuid']} #{gluster_peer_two} #{gluster_peer_two} #{gluster_peers[gluster_peer_two]['connected']} #{gluster_peers[gluster_peer_two]['state']} #{gluster_peers[gluster_peer_two]['status']} #{gluster_peers[gluster_peer_three]['uuid']} #{gluster_peer_three} #{gluster_peer_three} #{gluster_peers[gluster_peer_three]['connected']} #{gluster_peers[gluster_peer_three]['state']} #{gluster_peers[gluster_peer_three]['status']} " end # }}} # {{{ Volume info let(:gluster_volume_info_xml) do " 0 0 #{gluster_volume_one} 208c58eb-44da-467c-b73d-3e52a1d9d544 1 #{gluster_volumes[gluster_volume_one]['status']} 0 3 3 1 1 0 3 1 4 Disperse 0 #{gluster_peer_one}:#{gluster_brick_path}#{gluster_peer_one}:#{gluster_brick_path}#{gluster_peer_one_uuid}0 #{gluster_peer_two}:#{gluster_brick_path}#{gluster_peer_two}:#{gluster_brick_path}#{gluster_peer_two_uuid}0 #{gluster_peer_three}:#{gluster_brick_path}#{gluster_peer_three}:#{gluster_brick_path}#{gluster_peer_three_uuid}0 4 1 " end # }}} # {{{ Volume status let(:gluster_volume_one_status_xml) do " 0 0 #{gluster_volume_one} 6 #{gluster_peer_one} #{gluster_brick_path} #{gluster_peer_one_uuid} 1 #{gluster_peer_one_port} #{gluster_peer_one_port} N/A 1773 #{gluster_peer_two} #{gluster_brick_path} #{gluster_peer_two_uuid} 1 #{gluster_peer_two_port} #{gluster_peer_two_port} N/A 1732 #{gluster_peer_three} #{gluster_brick_path} #{gluster_peer_three_uuid} 1 #{gluster_peer_three_port} #{gluster_peer_three_port} N/A 2175 Self-heal Daemon localhost #{gluster_peer_one_uuid} 1 #{gluster_peer_shd_port} #{gluster_peer_shd_port} N/A 12189 Self-heal Daemon #{gluster_peer_three} #{gluster_peer_three_uuid} 1 #{gluster_peer_shd_port} #{gluster_peer_shd_port} N/A 22521 Self-heal Daemon #{gluster_peer_two} #{gluster_peer_two_uuid} 1 #{gluster_peer_shd_port} #{gluster_peer_shd_port} N/A 31403 " end # }}} # }}} # }}} # {{{ Gluster not running context 'gluster not running' do before do allow(Facter).to receive(:value) # Stub all other calls allow(Facter).to receive(:value).with('gluster_custom_binary').and_return(gluster_binary) allow(File).to receive(:executable?).with(gluster_binary).and_return(true) allow(Facter::Util::Resolution).to receive(:exec).with("#{gluster_binary} peer status --xml") { 'Connection failed. Please check if gluster daemon is operational.' } allow(Facter::Util::Resolution).to receive(:exec).with("#{gluster_binary} volume info --xml") { 'Connection failed. Please check if gluster daemon is operational.' } end it 'detect gluster binary' do expect(Facter.fact(:gluster_binary).value).to eq(gluster_binary) end it 'null peer count' do expect(Facter.fact(:gluster_peer_count).value).to eq(0) end it 'empty peer list' do expect(Facter.fact(:gluster_peer_list).value).to eq('') end it 'empty peers hash' do expect(Facter.fact(:gluster_peers).value).to eq({}) end it 'empty volumes hash' do expect(Facter.fact(:gluster_volumes).value).to eq({}) end it 'nil gluster_volume_list' do expect(Facter.fact(:gluster_volume_list)).to eq(nil) end it 'nil gluster_volume_volume_bricks' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_bricks")).to eq(nil) end it 'nil gluster_volume_volume_options' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_options")).to eq(nil) end it 'nil gluster_volume_volume_ports' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_ports")).to eq(nil) end end # }}} # {{{ No peers and no volumes context 'no peers and no volumes' do before do allow(Facter).to receive(:value) # Stub all other calls allow(Facter).to receive(:value).with('gluster_custom_binary').and_return(gluster_binary) allow(File).to receive(:executable?).with(gluster_binary).and_return(true) allow(Facter::Util::Resolution).to receive(:exec).with("#{gluster_binary} peer status --xml") { gluster_no_peer } allow(Facter::Util::Resolution).to receive(:exec).with("#{gluster_binary} volume info --xml") { gluster_no_volume } end it 'detect gluster binary' do expect(Facter.fact(:gluster_binary).value).to eq(gluster_binary) end it 'null peer count' do expect(Facter.fact(:gluster_peer_count).value).to eq(0) end it 'empty peer list' do expect(Facter.fact(:gluster_peer_list).value).to eq('') end it 'empty peers hash' do expect(Facter.fact(:gluster_peers).value).to eq({}) end it 'empty volumes hash' do expect(Facter.fact(:gluster_volumes).value).to eq({}) end it 'nil gluster_volume_list' do expect(Facter.fact(:gluster_volume_list)).to eq(nil) end it 'nil gluster_volume_volume_bricks' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_bricks")).to eq(nil) end it 'nil gluster_volume_volume_options' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_options")).to eq(nil) end it 'nil gluster_volume_volume_ports' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_ports")).to eq(nil) end end # }}} # {{{ Two peers and no volumes context 'two peers and no volumes' do before do allow(Facter).to receive(:value) # Stub all other calls allow(Facter).to receive(:value).with('gluster_custom_binary').and_return(gluster_binary) allow(File).to receive(:executable?).with(gluster_binary).and_return(true) allow(Facter::Util::Resolution).to receive(:exec).with("#{gluster_binary} peer status --xml") { gluster_peer_status_xml } allow(Facter::Util::Resolution).to receive(:exec).with("#{gluster_binary} volume info --xml") { gluster_no_volume } end it 'detect gluster binary' do expect(Facter.fact(:gluster_binary).value).to eq(gluster_binary) end it 'check gluster_peer_count integer' do expect(Facter.fact(:gluster_peer_count).value).to eq(gluster_peer_count) end it 'check gluster_peer_list string' do expect(Facter.fact(:gluster_peer_list).value).to eq(gluster_peer_list) end it 'check gluster_peers hash' do expect(Facter.fact(:gluster_peers).value).to eq(gluster_peers) end it 'empty volumes hash' do expect(Facter.fact(:gluster_volumes).value).to eq({}) end it 'nil gluster_volume_list' do expect(Facter.fact(:gluster_volume_list)).to eq(nil) end it 'nil gluster_volume_volume_bricks' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_bricks")).to eq(nil) end it 'nil gluster_volume_volume_options' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_options")).to eq(nil) end it 'nil gluster_volume_volume_ports' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_ports")).to eq(nil) end end # }}} # {{{ Two peers and one volume context 'two peers and one volumes' do before do allow(Facter).to receive(:value) # Stub all other calls allow(Facter).to receive(:value).with('gluster_custom_binary').and_return(gluster_binary) allow(File).to receive(:executable?).with(gluster_binary).and_return(true) allow(Facter::Util::Resolution).to receive(:exec).with("#{gluster_binary} peer status --xml") { gluster_peer_status_xml } allow(Facter::Util::Resolution).to receive(:exec).with("#{gluster_binary} volume info --xml") { gluster_volume_info_xml } allow(Facter::Util::Resolution).to receive(:exec).with("#{gluster_binary} volume status #{gluster_volume_one} --xml") { gluster_volume_one_status_xml } end it 'detect gluster binary' do expect(Facter.fact(:gluster_binary).value).to eq(gluster_binary) end it 'check gluster_peer_count integer' do expect(Facter.fact(:gluster_peer_count).value).to eq(gluster_peer_count) end it 'check gluster_peer_list string' do expect(Facter.fact(:gluster_peer_list).value).to eq(gluster_peer_list) end it 'check gluster_peers hash' do expect(Facter.fact(:gluster_peers).value).to eq(gluster_peers) end it 'check gluster_volumes hash' do expect(Facter.fact(:gluster_volumes).value).to eq(gluster_volumes) end it 'check gluster_volume_list string' do expect(Facter.fact(:gluster_volume_list).value).to eq(gluster_volume_list) end it 'check gluster_volume_volume_bricks (comma separated string)' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_bricks").value).to eq(gluster_volumes[gluster_volume_one]['bricks'].join(',')) end it 'check gluster_volume_volume_options (comma separated string)' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_options").value).to eq(gluster_volume_options[gluster_volume_one].join(',')) end it 'check gluster_volume_volume_ports (comma separated string)' do expect(Facter.fact(:"gluster_volume_#{gluster_volume_one}_ports").value).to eq(gluster_volume_ports[gluster_volume_one]['ports'].join(',')) end end # }}} end