Skip to content

Commit

Permalink
tests: don't assume euid != 0
Browse files Browse the repository at this point in the history
If tests rely on the environment being a certain way, they should ensure
it meets its expectations. Update the tests to ensure they're testing
multiple euid conditions, and setting it explicitly where required.

Fix ros-infrastructure#702

Signed-off-by: Kyle Fazzari <[email protected]>
  • Loading branch information
kyrofa authored and nuclearsandwich committed Apr 2, 2020
1 parent 50ee23e commit fdd89e2
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 62 deletions.
13 changes: 8 additions & 5 deletions test/test_rosdep_alpine.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,30 @@ def test_ApkInstaller():
from rosdep2.platforms.alpine import ApkInstaller

@patch.object(ApkInstaller, 'get_packages_to_install')
def test(mock_method):
def test(expected_prefix, mock_method):
installer = ApkInstaller()
mock_method.return_value = []
assert [] == installer.get_install_command(['nonexistingfakepackage'])

mock_method.return_value = ['a-dev', 'b-dev']

expected = [['sudo', '-H', 'apk', 'add', 'a-dev', 'b-dev']]
expected = [expected_prefix + ['apk', 'add', 'a-dev', 'b-dev']]
val = installer.get_install_command(['notused'], interactive=False, quiet=False)
assert val == expected, 'Result was: %s' % val

expected = [['sudo', '-H', 'apk', 'add', '--interactive', 'a-dev', 'b-dev']]
expected = [expected_prefix + ['apk', 'add', '--interactive', 'a-dev', 'b-dev']]
val = installer.get_install_command(['notused'], interactive=True, quiet=False)
assert val == expected, 'Result was: %s' % val

expected = [['sudo', '-H', 'apk', 'add', '--quiet', 'a-dev', 'b-dev']]
expected = [expected_prefix + ['apk', 'add', '--quiet', 'a-dev', 'b-dev']]
val = installer.get_install_command(['notused'], interactive=False, quiet=True)
assert val == expected, 'Result was: %s' % val

try:
test()
with patch('rosdep2.installers.os.geteuid', return_value=1):
test(['sudo', '-H'])
with patch('rosdep2.installers.os.geteuid', return_value=0):
test([])
except AssertionError:
traceback.print_exc()
raise
11 changes: 7 additions & 4 deletions test/test_rosdep_arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,24 @@ def test_PacmanInstaller():
from rosdep2.platforms.arch import PacmanInstaller

@patch.object(PacmanInstaller, 'get_packages_to_install')
def test(mock_method):
def test(expected_prefix, mock_method):
installer = PacmanInstaller()
mock_method.return_value = []
assert [] == installer.get_install_command(['fake'])

# no interactive option implemented yet
mock_method.return_value = ['a', 'b']
expected = [['sudo', '-H', 'pacman', '-S', '--noconfirm', '--needed', 'a', 'b']]
expected = [expected_prefix + ['pacman', '-S', '--noconfirm', '--needed', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=False)
assert val == expected, val
expected = [['sudo', '-H', 'pacman', '-S', '--needed', 'a', 'b']]
expected = [expected_prefix + ['pacman', '-S', '--needed', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=True)
assert val == expected, val
try:
test()
with patch('rosdep2.installers.os.geteuid', return_value=1):
test(['sudo', '-H'])
with patch('rosdep2.installers.os.geteuid', return_value=0):
test([])
except AssertionError:
traceback.print_exc()
raise
15 changes: 9 additions & 6 deletions test/test_rosdep_debian.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,27 @@ def test_AptInstaller():

@patch('rosdep2.platforms.debian.read_stdout')
@patch.object(AptInstaller, 'get_packages_to_install')
def test(mock_get_packages_to_install, mock_read_stdout):
def test(expected_prefix, mock_get_packages_to_install, mock_read_stdout):
installer = AptInstaller()
mock_get_packages_to_install.return_value = []
mock_read_stdout.return_value = ''
assert [] == installer.get_install_command(['fake'])

mock_get_packages_to_install.return_value = ['a', 'b']
expected = [['sudo', '-H', 'apt-get', 'install', '-y', 'a'],
['sudo', '-H', 'apt-get', 'install', '-y', 'b']]
expected = [expected_prefix + ['apt-get', 'install', '-y', 'a'],
expected_prefix + ['apt-get', 'install', '-y', 'b']]
val = installer.get_install_command(['whatever'], interactive=False)
print('VAL', val)
assert val == expected, val
expected = [['sudo', '-H', 'apt-get', 'install', 'a'],
['sudo', '-H', 'apt-get', 'install', 'b']]
expected = [expected_prefix + ['apt-get', 'install', 'a'],
expected_prefix + ['apt-get', 'install', 'b']]
val = installer.get_install_command(['whatever'], interactive=True)
assert val == expected, val
try:
test()
with patch('rosdep2.installers.os.geteuid', return_value=1):
test(['sudo', '-H'])
with patch('rosdep2.installers.os.geteuid', return_value=0):
test([])
except AssertionError:
traceback.print_exc()
raise
11 changes: 7 additions & 4 deletions test/test_rosdep_freebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,24 @@ def test_PkgInstaller():
from rosdep2.platforms.freebsd import PkgInstaller

@patch.object(PkgInstaller, 'get_packages_to_install')
def test(mock_method):
def test(expected_prefix, mock_method):
installer = PkgInstaller()
mock_method.return_value = []
assert [] == installer.get_install_command(['fake'])

# no interactive option with YUM
mock_method.return_value = ['a', 'b']
expected = [['sudo', '-H', '/usr/sbin/pkg', 'install', '-y', 'a', 'b']]
expected = [expected_prefix + ['/usr/sbin/pkg', 'install', '-y', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=False)
assert val == expected, val
expected = [['sudo', '-H', '/usr/sbin/pkg', 'install', '-y', 'a', 'b']]
expected = [expected_prefix + ['/usr/sbin/pkg', 'install', '-y', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=True)
assert val == expected, val
try:
test()
with patch('rosdep2.installers.os.geteuid', return_value=1):
test(['sudo', '-H'])
with patch('rosdep2.installers.os.geteuid', return_value=0):
test([])
except AssertionError:
traceback.print_exc()
raise
15 changes: 9 additions & 6 deletions test/test_rosdep_gem.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,27 @@ def test_no_gem(mock_method):

@patch('rosdep2.platforms.gem.is_gem_installed')
@patch.object(GemInstaller, 'get_packages_to_install')
def test(mock_method, mock_is_gem_installed):
def test(expected_prefix, mock_method, mock_is_gem_installed):
mock_is_gem_installed.return_value = True
installer = GemInstaller()
mock_method.return_value = []
assert [] == installer.get_install_command(['fake'])

# no interactive option with GEM
mock_method.return_value = ['a', 'b']
expected = [['sudo', '-H', 'gem', 'install', 'a'],
['sudo', '-H', 'gem', 'install', 'b']]
expected = [expected_prefix + ['gem', 'install', 'a'],
expected_prefix + ['gem', 'install', 'b']]
val = installer.get_install_command(['whatever'], interactive=False)
assert val == expected, val
expected = [['sudo', '-H', 'gem', 'install', 'a'],
['sudo', '-H', 'gem', 'install', 'b']]
expected = [expected_prefix + ['gem', 'install', 'a'],
expected_prefix + ['gem', 'install', 'b']]
val = installer.get_install_command(['whatever'], interactive=True)
assert val == expected, val
try:
test()
with patch('rosdep2.installers.os.geteuid', return_value=1):
test(['sudo', '-H'])
with patch('rosdep2.installers.os.geteuid', return_value=0):
test([])
except AssertionError:
traceback.print_exc()
raise
4 changes: 3 additions & 1 deletion test/test_rosdep_installers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from __future__ import print_function

from contextlib import contextmanager
from mock import patch
import os
import sys
try:
Expand Down Expand Up @@ -583,7 +584,8 @@ def fakeout():
sys.stderr = realstderr


def test_RosdepInstaller_install_resolved():
@patch('rosdep2.installers.os.geteuid', return_value=1)
def test_RosdepInstaller_install_resolved(mock_geteuid):
from rosdep2 import create_default_installer_context
from rosdep2.lookup import RosdepLookup
from rosdep2.installers import RosdepInstaller
Expand Down
3 changes: 2 additions & 1 deletion test/test_rosdep_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def test_check(self):
pass

@patch('rosdep2.platforms.debian.read_stdout')
def test_install(self, mock_read_stdout):
@patch('rosdep2.installers.os.geteuid', return_value=1)
def test_install(self, mock_geteuid, mock_read_stdout):
sources_cache = get_cache_dir()
cmd_extras = ['-c', sources_cache]
catkin_tree = get_test_catkin_tree_dir()
Expand Down
13 changes: 8 additions & 5 deletions test/test_rosdep_opensuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,24 @@ def test_ZypperInstaller():
from rosdep2.platforms.opensuse import ZypperInstaller

@patch.object(ZypperInstaller, 'get_packages_to_install')
def test(mock_method):
def test(expected_prefix, mock_method):
installer = ZypperInstaller()
mock_method.return_value = []
assert [] == installer.get_install_command(['fake'])
mock_method.return_value = ['a', 'b']

# no interactive option with YUM
mock_method.return_value = ['a', 'b']
expected = [['sudo', '-H', 'zypper', 'install', '-yl', 'a', 'b']]
expected = [expected_prefix + ['zypper', 'install', '-yl', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=False)
assert val == expected, val
expected = [['sudo', '-H', 'zypper', 'install', 'a', 'b']]
expected = [expected_prefix + ['zypper', 'install', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=True)
assert val == expected, val
try:
test()
with patch('rosdep2.installers.os.geteuid', return_value=1):
test(['sudo', '-H'])
with patch('rosdep2.installers.os.geteuid', return_value=0):
test([])
except AssertionError:
traceback.print_exc()
raise
15 changes: 9 additions & 6 deletions test/test_rosdep_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,27 @@ def test_no_pip(mock_method):

@patch('rosdep2.platforms.pip.get_pip_command')
@patch.object(PipInstaller, 'get_packages_to_install')
def test(mock_method, mock_get_pip_command):
def test(expected_prefix, mock_method, mock_get_pip_command):
mock_get_pip_command.return_value = ['mock-pip']
installer = PipInstaller()
mock_method.return_value = []
assert [] == installer.get_install_command(['fake'])

# no interactive option with PIP
mock_method.return_value = ['a', 'b']
expected = [['sudo', '-H', 'mock-pip', 'install', '-U', 'a'],
['sudo', '-H', 'mock-pip', 'install', '-U', 'b']]
expected = [expected_prefix + ['mock-pip', 'install', '-U', 'a'],
expected_prefix + ['mock-pip', 'install', '-U', 'b']]
val = installer.get_install_command(['whatever'], interactive=False)
assert val == expected, val
expected = [['sudo', '-H', 'mock-pip', 'install', '-U', 'a'],
['sudo', '-H', 'mock-pip', 'install', '-U', 'b']]
expected = [expected_prefix + ['mock-pip', 'install', '-U', 'a'],
expected_prefix + ['mock-pip', 'install', '-U', 'b']]
val = installer.get_install_command(['whatever'], interactive=True)
assert val == expected, val
try:
test()
with patch('rosdep2.installers.os.geteuid', return_value=1):
test(['sudo', '-H'])
with patch('rosdep2.installers.os.geteuid', return_value=0):
test([])
except AssertionError:
traceback.print_exc()
raise
Expand Down
30 changes: 18 additions & 12 deletions test/test_rosdep_redhat.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,30 @@ def test_DnfInstaller():
from rosdep2.platforms.redhat import DnfInstaller

@patch.object(DnfInstaller, 'get_packages_to_install')
def test(mock_method):
def test(expected_prefix, mock_method):
installer = DnfInstaller()
mock_method.return_value = []
assert [] == installer.get_install_command(['fake'])

# no interactive option with YUM
mock_method.return_value = ['a', 'b']
expected = [['sudo', '-H', 'dnf', '--assumeyes', '--quiet', '--setopt=strict=0', 'install', 'a', 'b']]
expected = [expected_prefix + ['dnf', '--assumeyes', '--quiet', '--setopt=strict=0', 'install', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=False, quiet=True)
assert val == expected, val + expected
expected = [['sudo', '-H', 'dnf', '--quiet', '--setopt=strict=0', 'install', 'a', 'b']]
expected = [expected_prefix + ['dnf', '--quiet', '--setopt=strict=0', 'install', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=True, quiet=True)
assert val == expected, val + expected
expected = [['sudo', '-H', 'dnf', '--assumeyes', '--setopt=strict=0', 'install', 'a', 'b']]
expected = [expected_prefix + ['dnf', '--assumeyes', '--setopt=strict=0', 'install', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=False, quiet=False)
assert val == expected, val + expected
expected = [['sudo', '-H', 'dnf', '--setopt=strict=0', 'install', 'a', 'b']]
expected = [expected_prefix + ['dnf', '--setopt=strict=0', 'install', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=True, quiet=False)
assert val == expected, val + expected
try:
test()
with patch('rosdep2.installers.os.geteuid', return_value=1):
test(['sudo', '-H'])
with patch('rosdep2.installers.os.geteuid', return_value=0):
test([])
except AssertionError:
traceback.print_exc()
raise
Expand All @@ -110,27 +113,30 @@ def test_YumInstaller():
from rosdep2.platforms.redhat import YumInstaller

@patch.object(YumInstaller, 'get_packages_to_install')
def test(mock_method):
def test(expected_prefix, mock_method):
installer = YumInstaller()
mock_method.return_value = []
assert [] == installer.get_install_command(['fake'])

# no interactive option with YUM
mock_method.return_value = ['a', 'b']
expected = [['sudo', '-H', 'yum', '--assumeyes', '--quiet', '--skip-broken', 'install', 'a', 'b']]
expected = [expected_prefix + ['yum', '--assumeyes', '--quiet', '--skip-broken', 'install', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=False, quiet=True)
assert val == expected, val + expected
expected = [['sudo', '-H', 'yum', '--quiet', '--skip-broken', 'install', 'a', 'b']]
expected = [expected_prefix + ['yum', '--quiet', '--skip-broken', 'install', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=True, quiet=True)
assert val == expected, val + expected
expected = [['sudo', '-H', 'yum', '--assumeyes', '--skip-broken', 'install', 'a', 'b']]
expected = [expected_prefix + ['yum', '--assumeyes', '--skip-broken', 'install', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=False, quiet=False)
assert val == expected, val + expected
expected = [['sudo', '-H', 'yum', '--skip-broken', 'install', 'a', 'b']]
expected = [expected_prefix + ['yum', '--skip-broken', 'install', 'a', 'b']]
val = installer.get_install_command(['whatever'], interactive=True, quiet=False)
assert val == expected, val + expected
try:
test()
with patch('rosdep2.installers.os.geteuid', return_value=1):
test(['sudo', '-H'])
with patch('rosdep2.installers.os.geteuid', return_value=0):
test([])
except AssertionError:
traceback.print_exc()
raise
Expand Down
30 changes: 18 additions & 12 deletions test/test_rosdep_slackware.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,28 @@ def test_SbotoolsInstaller():
from rosdep2.platforms.slackware import SbotoolsInstaller

@patch.object(SbotoolsInstaller, 'get_packages_to_install')
def test(mock_method):
def test(expected_prefix, mock_method):
installer = SbotoolsInstaller()
mock_method.return_value = []
assert [] == installer.get_install_command(['fake'])

mock_method.return_value = ['a', 'b']

expected = [['sudo', '-H', 'sboinstall', '-r', 'a'],
['sudo', '-H', 'sboinstall', '-r', 'b']]
expected = [expected_prefix + ['sboinstall', '-r', 'a'],
expected_prefix + ['sboinstall', '-r', 'b']]
val = installer.get_install_command(['whatever'], interactive=False)
assert val == expected, val

expected = [['sudo', '-H', 'sboinstall', 'a'],
['sudo', '-H', 'sboinstall', 'b']]
expected = [expected_prefix + ['sboinstall', 'a'],
expected_prefix + ['sboinstall', 'b']]
val = installer.get_install_command(['whatever'], interactive=True)
assert val == expected, val

try:
test()
with patch('rosdep2.installers.os.geteuid', return_value=1):
test(['sudo', '-H'])
with patch('rosdep2.installers.os.geteuid', return_value=0):
test([])
except AssertionError:
traceback.print_exc()
raise
Expand Down Expand Up @@ -159,25 +162,28 @@ def test_SlackpkgInstaller():
from rosdep2.platforms.slackware import SlackpkgInstaller

@patch.object(SlackpkgInstaller, 'get_packages_to_install')
def test(mock_method):
def test(expected_prefix, mock_method):
installer = SlackpkgInstaller()
mock_method.return_value = []
assert [] == installer.get_install_command(['fake'])

mock_method.return_value = ['a', 'b']

expected = [['sudo', '-H', 'slackpkg', 'install', 'a'],
['sudo', '-H', 'slackpkg', 'install', 'b']]
expected = [expected_prefix + ['slackpkg', 'install', 'a'],
expected_prefix + ['slackpkg', 'install', 'b']]
val = installer.get_install_command(['whatever'], interactive=False)
assert val == expected, val

expected = [['sudo', '-H', 'slackpkg', 'install', 'a'],
['sudo', '-H', 'slackpkg', 'install', 'b']]
expected = [expected_prefix + ['slackpkg', 'install', 'a'],
expected_prefix + ['slackpkg', 'install', 'b']]
val = installer.get_install_command(['whatever'], interactive=True)
assert val == expected, val

try:
test()
with patch('rosdep2.installers.os.geteuid', return_value=1):
test(['sudo', '-H'])
with patch('rosdep2.installers.os.geteuid', return_value=0):
test([])
except AssertionError:
traceback.print_exc()
raise

0 comments on commit fdd89e2

Please sign in to comment.