Commit af930b8d authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: Test sudo/admin mechanics

parent c9ff8fd1
#!/usr/bin/env python
import os
import os.path as op
import shutil
import subprocess as sp
import sys
import textwrap as tw
# py3
try:
from unittest import mock
# py2
except ImportError:
import mock
import pytest
from . import onpath, server
import fslinstaller as inst
def test_Context_identify_plaform():
tests = [
[('linux', 'x86_64'), 'linux-64'],
[('darwin', 'x86_64'), 'macos-64'],
[('darwin', 'arm64'), 'macos-64'],
]
for info, expected in tests:
sys, cpu = info
with mock.patch('platform.system', return_value=sys), \
mock.patch('platform.machine', return_value=cpu):
assert inst.Context.identify_platform() == expected
def test_Context_identify_cuda():
with inst.tempdir() as cwd:
with onpath(cwd):
nvidia_smi = tw.dedent("""
#!/usr/bin/env bash
echo "{stdout}"
exit {retcode}
""").strip()
# test when nvidia-smi doesn't exist
# (assuming that it won't be present
# in any of the mock paths)
path = op.pathsep.join(('/usr/sbin', '/usr/bin', '/sbin', '/bin'))
with mock.patch.dict(os.environ, PATH=path):
assert inst.Context.identify_cuda() is None
if hasattr(inst.Context.identify_cuda, 'no_cuda'):
delattr(inst.Context.identify_cuda, 'no_cuda')
with open('nvidia-smi', 'wt') as f:
f.write(nvidia_smi.format(stdout='CUDA Version: 10.1', retcode=0))
os.chmod('nvidia-smi', 0o755)
assert inst.Context.identify_cuda() == 10.1
if hasattr(inst.Context.identify_cuda, 'no_cuda'):
delattr(inst.Context.identify_cuda, 'no_cuda')
with open('nvidia-smi', 'wt') as f:
f.write(nvidia_smi.format(stdout='CUDA Version: 11.2', retcode=0))
os.chmod('nvidia-smi', 0o755)
assert inst.Context.identify_cuda() == 11.2
if hasattr(inst.Context.identify_cuda, 'no_cuda'):
delattr(inst.Context.identify_cuda, 'no_cuda')
with open('nvidia-smi', 'wt') as f:
f.write(nvidia_smi.format(stdout='CUDA Version: 11.2', retcode=1))
os.chmod('nvidia-smi', 0o755)
assert inst.Context.identify_cuda() is None
if hasattr(inst.Context.identify_cuda, 'no_cuda'):
delattr(inst.Context.identify_cuda, 'no_cuda')
def test_Context_get_admin_password():
sudo = tw.dedent("""
#!/usr/bin/env bash
echo -n "Password: "
read -e password
if [ "$password" = "password" ]; then exit 0
else exit 1
fi
""").strip()
with inst.tempdir() as cwd:
path = op.pathsep.join((cwd, os.environ['PATH']))
with open('sudo', 'wt') as f:
f.write(sudo)
os.chmod('sudo', 0o755)
# right password first time
with mock.patch.dict(os.environ, PATH=path), \
mock.patch('getpass.getpass', return_value='password'):
assert inst.Context.get_admin_password() == 'password'
# wrong, then right
returnvals = ['wrong', 'password']
def getpass(*a):
return returnvals.pop(0)
with mock.patch.dict(os.environ, PATH=path), \
mock.patch('getpass.getpass', getpass):
assert inst.Context.get_admin_password() == 'password'
# wrong wrong wrong
returnvals = ['wrong', 'bad', 'no']
def getpass(*a):
return returnvals.pop(0)
with mock.patch.dict(os.environ, PATH=path), \
mock.patch('getpass.getpass', getpass):
with pytest.raises(Exception):
inst.Context.get_admin_password()
#!/usr/bin/env python
import os
import os.path as op
import textwrap as tw
import subprocess as sp
try:
from unittest import mock
except ImportError:
import mock
import pytest
from . import onpath, server
import fslinstaller as inst
def test_Process_check_call():
with inst.tempdir() as cwd:
script_template = tw.dedent("""
#!/usr/bin/env sh
touch {semaphore}
exit {retcode}
""").strip()
with open('pass', 'wt') as f:
f.write(script_template.format(semaphore='passed', retcode=0))
with open('fail', 'wt') as f:
f.write(script_template.format(semaphore='failed', retcode=1))
os.chmod('pass', 0o755)
os.chmod('fail', 0o755)
inst.Process.check_call(op.join(cwd, 'pass'))
assert op.exists('passed')
with pytest.raises(Exception):
inst.Process.check_call(op.join(cwd, 'fail'))
assert op.exists('failed')
def test_Process_check_output():
with inst.tempdir() as cwd:
script_template = tw.dedent("""
#!/usr/bin/env sh
echo "{stdout}"
exit {retcode}
""").strip()
# (stdout, retcode)
tests = [
('stdout', 0),
('stdout', 1),
]
for expect, retcode in tests:
script = script_template.format(stdout=expect, retcode=retcode)
with open('script', 'wt') as f:
f.write(script)
os.chmod('script', 0o755)
if retcode == 0:
got = inst.Process.check_output(op.join(cwd, 'script'))
assert got.strip() == expect
else:
with pytest.raises(Exception):
inst.Process.check_output(op.join(cwd, 'script'))
def test_Process_monitor_progress():
with inst.tempdir() as cwd:
script = tw.dedent("""
#!/usr/bin/env bash
for ((i=0;i<10;i++)); do
echo $i
done
touch $1
""").strip()
with open('script', 'wt') as f:
f.write(script)
os.chmod('script', 0o755)
script = op.join(cwd, 'script')
# py2: make sure function accepts string and unicode
scripts = [script, u'{}'.format(script)]
for script in scripts:
inst.Process.monitor_progress( script + ' a')
inst.Process.monitor_progress([script + ' b'])
inst.Process.monitor_progress([script + ' c', script + ' d'])
inst.Process.monitor_progress( script + ' e', 10)
inst.Process.monitor_progress([script + ' f'], 10)
inst.Process.monitor_progress([script + ' g', script + ' h'], 10)
for touched in 'abcdefgh':
assert op.exists(touched)
os.remove(touched)
def test_Process_sudo_popen():
with inst.tempdir() as cwd:
sudo = tw.dedent("""
#!/usr/bin/env bash
s=$1; shift
k=$2; shift
echo -n "Password: "
read password
echo $password > got_password
"$@"
""").strip()
cmd = tw.dedent("""
#!/usr/bin/env bash
echo "Running cmd" > command_output
""")
with open('sudo', 'wt') as f: f.write(sudo)
with open('cmd', 'wt') as f: f.write(cmd)
os.chmod('sudo', 0o755)
os.chmod('cmd', 0o755)
path = op.pathsep.join((cwd, os.environ['PATH']))
with mock.patch.dict(os.environ, PATH=path):
p = inst.Process.sudo_popen(['cmd'], 'password', stdin=sp.PIPE)
p.communicate()
with open('got_password', 'rt') as f:
assert f.read().strip() == 'password'
with open('command_output', 'rt') as f:
assert f.read().strip() == 'Running cmd'
......@@ -32,147 +32,6 @@ def test_Version():
assert inst.Version('1.2.3.0') > inst.Version('1.2.3')
def test_Context_identify_plaform():
tests = [
[('linux', 'x86_64'), 'linux-64'],
[('darwin', 'x86_64'), 'macos-64'],
[('darwin', 'arm64'), 'macos-64'],
]
for info, expected in tests:
sys, cpu = info
with mock.patch('platform.system', return_value=sys), \
mock.patch('platform.machine', return_value=cpu):
assert inst.Context.identify_platform() == expected
def test_Context_identify_cuda():
with inst.tempdir() as cwd:
with onpath(cwd):
nvidia_smi = tw.dedent("""
#!/usr/bin/env bash
echo "{stdout}"
exit {retcode}
""").strip()
# test when nvidia-smi doesn't exist
# (assuming that it won't be present
# in any of the mock paths)
path = op.pathsep.join(('/usr/sbin', '/usr/bin', '/sbin', '/bin'))
with mock.patch.dict(os.environ, PATH=path):
assert inst.Context.identify_cuda() is None
if hasattr(inst.Context.identify_cuda, 'no_cuda'):
delattr(inst.Context.identify_cuda, 'no_cuda')
with open('nvidia-smi', 'wt') as f:
f.write(nvidia_smi.format(stdout='CUDA Version: 10.1', retcode=0))
os.chmod('nvidia-smi', 0o755)
assert inst.Context.identify_cuda() == 10.1
if hasattr(inst.Context.identify_cuda, 'no_cuda'):
delattr(inst.Context.identify_cuda, 'no_cuda')
with open('nvidia-smi', 'wt') as f:
f.write(nvidia_smi.format(stdout='CUDA Version: 11.2', retcode=0))
os.chmod('nvidia-smi', 0o755)
assert inst.Context.identify_cuda() == 11.2
if hasattr(inst.Context.identify_cuda, 'no_cuda'):
delattr(inst.Context.identify_cuda, 'no_cuda')
with open('nvidia-smi', 'wt') as f:
f.write(nvidia_smi.format(stdout='CUDA Version: 11.2', retcode=1))
os.chmod('nvidia-smi', 0o755)
assert inst.Context.identify_cuda() is None
if hasattr(inst.Context.identify_cuda, 'no_cuda'):
delattr(inst.Context.identify_cuda, 'no_cuda')
def test_Process_check_call():
with inst.tempdir() as cwd:
script_template = tw.dedent("""
#!/usr/bin/env sh
touch {semaphore}
exit {retcode}
""").strip()
with open('pass', 'wt') as f:
f.write(script_template.format(semaphore='passed', retcode=0))
with open('fail', 'wt') as f:
f.write(script_template.format(semaphore='failed', retcode=1))
os.chmod('pass', 0o755)
os.chmod('fail', 0o755)
inst.Process.check_call(op.join(cwd, 'pass'))
assert op.exists('passed')
with pytest.raises(Exception):
inst.Process.check_call(op.join(cwd, 'fail'))
assert op.exists('failed')
def test_Process_check_output():
with inst.tempdir() as cwd:
script_template = tw.dedent("""
#!/usr/bin/env sh
echo "{stdout}"
exit {retcode}
""").strip()
# (stdout, retcode)
tests = [
('stdout', 0),
('stdout', 1),
]
for expect, retcode in tests:
script = script_template.format(stdout=expect, retcode=retcode)
with open('script', 'wt') as f:
f.write(script)
os.chmod('script', 0o755)
if retcode == 0:
got = inst.Process.check_output(op.join(cwd, 'script'))
assert got.strip() == expect
else:
with pytest.raises(Exception):
inst.Process.check_output(op.join(cwd, 'script'))
def test_Process_monitor_progress():
with inst.tempdir() as cwd:
script = tw.dedent("""
#!/usr/bin/env bash
for ((i=0;i<10;i++)); do
echo $i
done
touch $1
""").strip()
with open('script', 'wt') as f:
f.write(script)
os.chmod('script', 0o755)
script = op.join(cwd, 'script')
# py2: make sure function accepts string and unicode
scripts = [script, u'{}'.format(script)]
for script in scripts:
inst.Process.monitor_progress( script + ' a')
inst.Process.monitor_progress([script + ' b'])
inst.Process.monitor_progress([script + ' c', script + ' d'])
inst.Process.monitor_progress( script + ' e', 10)
inst.Process.monitor_progress([script + ' f'], 10)
inst.Process.monitor_progress([script + ' g', script + ' h'], 10)
for touched in 'abcdefgh':
assert op.exists(touched)
os.remove(touched)
def test_read_fslversion():
with inst.tempdir() as cwd:
os.mkdir('etc')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment