1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
require 'spec_helper'
describe Specinfra::Backend::Exec do
before :all do
set :backend, :exec
end
describe '#build_command' do
context 'with simple command' do
it 'should escape spaces' do
expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq '/bin/sh -c test\ -f\ /etc/passwd'
end
end
context 'with complex command' do
it 'should escape special chars' do
expect(Specinfra.backend.build_command('test ! -f /etc/selinux/config || (getenforce | grep -i -- disabled && grep -i -- ^SELINUX=disabled$ /etc/selinux/config)')).to eq '/bin/sh -c test\ \!\ -f\ /etc/selinux/config\ \|\|\ \(getenforce\ \|\ grep\ -i\ --\ disabled\ \&\&\ grep\ -i\ --\ \^SELINUX\=disabled\$\ /etc/selinux/config\)'
end
it 'should escape quotes' do
if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.7')
expect(Specinfra.backend.build_command(%Q{find /etc/apt/ -name \*.list | xargs grep -o -E "^deb +[\\"']?http://ppa.launchpad.net/gluster/glusterfs-3.7"})).to eq('/bin/sh -c find\ /etc/apt/\ -name\ \*.list\ \|\ xargs\ grep\ -o\ -E\ \"\^deb\ \+\[\\\\\"\\\'\]\?http://ppa.launchpad.net/gluster/glusterfs-3.7\"')
else
# Since Ruby 2.7, `+` is not escaped.
expect(Specinfra.backend.build_command(%Q{find /etc/apt/ -name \*.list | xargs grep -o -E "^deb +[\\"']?http://ppa.launchpad.net/gluster/glusterfs-3.7"})).to eq('/bin/sh -c find\ /etc/apt/\ -name\ \*.list\ \|\ xargs\ grep\ -o\ -E\ \"\^deb\ +\[\\\\\"\\\'\]\?http://ppa.launchpad.net/gluster/glusterfs-3.7\"')
end
end
end
context 'with custom shell' do
before do
RSpec.configure {|c| c.shell = '/usr/local/bin/tcsh' }
end
after do
RSpec.configure {|c| c.shell = nil }
end
it 'should use custom shell' do
expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq '/usr/local/bin/tcsh -c test\ -f\ /etc/passwd'
end
end
context 'with custom shell that needs escaping' do
before do
RSpec.configure {|c| c.shell = '/usr/test & spec/bin/sh' }
end
after do
RSpec.configure {|c| c.shell = nil }
end
it 'should use custom shell' do
expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq '/usr/test\ \&\ spec/bin/sh -c test\ -f\ /etc/passwd'
end
end
context 'with an interactive shell' do
before do
RSpec.configure {|c| c.interactive_shell = true }
end
after do
RSpec.configure {|c| c.interactive_shell = nil }
end
it 'should emulate an interactive shell' do
expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq '/bin/sh -i -c test\ -f\ /etc/passwd'
end
end
context 'with an login shell' do
before do
RSpec.configure {|c| c.login_shell = true }
end
after do
RSpec.configure {|c| c.login_shell = nil }
end
it 'should emulate an login shell' do
expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq '/bin/sh -l -c test\ -f\ /etc/passwd'
end
end
context 'with custom path' do
before do
RSpec.configure {|c| c.path = '/opt/bin:/opt/foo/bin:$PATH' }
end
after do
RSpec.configure {|c| c.path = nil }
end
it 'should use custom path' do
expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq 'env PATH="/opt/bin:/opt/foo/bin:$PATH" /bin/sh -c test\ -f\ /etc/passwd'
end
end
context 'with custom path that needs escaping' do
before do
RSpec.configure {|c| c.path = '/opt/bin:/opt/test & spec/bin:$PATH' }
end
after do
RSpec.configure {|c| c.path = nil }
end
it 'should use custom path' do
expect(Specinfra.backend.build_command('test -f /etc/passwd')).to eq 'env PATH="/opt/bin:/opt/test & spec/bin:$PATH" /bin/sh -c test\ -f\ /etc/passwd'
end
end
end
end
describe 'os' do
before do
# clear os information cache
property[:os] = nil
Specinfra.configuration.instance_variable_set(:@os, nil)
Specinfra.backend.instance_variable_set(:@os_info, nil)
end
context 'test ubuntu with lsb_release command' do
before do
allow(Specinfra.backend).to receive(:run_command) do |args|
if ['cat /etc/debian_version', 'lsb_release -irc'].include? args
double(
:run_command_response,
:success? => true,
:stdout => "Distributor ID:\tUbuntu\nRelease:\t12.04\nCodename:\tprecise\n"
)
elsif args == 'uname -m'
double :run_command_response, :success? => true, :stdout => "x86_64\n"
else
double :run_command_response, :success? => false, :stdout => nil
end
end
end
subject! { os }
it do
expect(Specinfra.backend).to have_received(:run_command).at_least(1).times
should eq({:family => 'ubuntu', :release => '12.04', :codename => 'precise', :arch => 'x86_64' })
end
end
context 'test ubuntu with /etc/lsb-release' do
before do
allow(Specinfra.backend).to receive(:run_command) do |args|
if ['cat /etc/debian_version', 'cat /etc/lsb-release'].include? args
double(
:run_command_response,
:success? => true,
:stdout => <<-EOF
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
EOF
)
elsif args == 'uname -m'
double :run_command_response, :success? => true, :stdout => "x86_64\n"
else
double :run_command_response, :success? => false, :stdout => nil
end
end
end
subject! { os }
it do
expect(Specinfra.backend).to have_received(:run_command).at_least(1).times
should eq({:family => 'ubuntu', :release => '12.04', :codename => 'precise', :arch => 'x86_64' })
end
end
context 'test debian (no lsb_release or lsb-release)' do
before do
allow(Specinfra.backend).to receive(:run_command) do |args|
if args == 'cat /etc/debian_version'
double :run_command_response, :success? => true, :stdout => "8.5\n"
elsif args == 'uname -m'
double :run_command_response, :success? => true, :stdout => "x86_64\n"
else
double :run_command_response, :success? => false, :stdout => nil
end
end
end
subject! { os }
it do
expect(Specinfra.backend).to have_received(:run_command).at_least(1).times
should eq({:family => 'debian', :release => '8.5', :codename => nil, :arch => 'x86_64' })
end
end
end
|