Commit a75cda0d authored by Johan Ström's avatar Johan Ström

Rewrote the only python unittest we have to pytest, and moved to unittest/python/

Also fix so it actually runs when doing make check.
parent 6cfe8c43
import unittest
import agoclient.config as config
from agoclient.agoapp import AgoApp
import tempfile
import os, os.path, shutil
# Find checkedout code dir:
# We're in subdir shared/agoclient/
devdir = os.path.realpath(os.path.dirname(os.path.realpath(__file__)) + "/../..")
class AgoTest(AgoApp):
pass
class ConfigTestBase(unittest.TestCase):
def setUp(self):
# Create dummy conf
self.tempdir = tempfile.mkdtemp()
os.mkdir("%s/conf.d" % self.tempdir)
# Custom dir, we must place agocontrol.aug into this dir
# TODO: or add some dir in loadpath?
shutil.copyfile("%s/conf/agocontrol.aug" % devdir, "%s/agocontrol.aug" % self.tempdir)
shutil.copyfile("%s/conf/conf.d/system.conf" % devdir, "%s/conf.d/system.conf" % self.tempdir)
config.set_config_dir(self.tempdir)
with open(config.get_config_path('conf.d/test.conf'), 'w') as f:
f.write("[test]\n"+
"test_value_0=100\n"+
"test_value_blank=\n"+
"local=4\n"+
"units=inv\n"+
"[system]\n"+
"password=testpwd\n"+
"[sec2]\n"+
"some=value\n"+
"test_value_blank=not blank\n")
# Kill augeas
config.augeas = None
def tearDown(self):
shutil.rmtree(self.tempdir)
class ConfigTest(ConfigTestBase):
def setUp(self):
super(ConfigTest, self).setUp()
global gco, sco
gco = config.get_config_option
sco = config.set_config_option
def test_get_path(self):
self.assertEqual(config.get_config_path("conf.d"), "%s/conf.d" % self.tempdir)
self.assertEqual(config.get_config_path("conf.d/"), "%s/conf.d/" % self.tempdir)
self.assertEqual(config.get_config_path("/conf.d/"), "%s/conf.d/" % self.tempdir)
def test_get_basic(self):
self.assertEqual(gco('system', 'broker'), 'localhost')
self.assertEqual(gco('system', 'not_set'), None)
self.assertEqual(gco('test', 'test_value_blank'), None)
self.assertEqual(gco('test', 'local'), '4')
# Test with defaults
self.assertEqual(gco('system', 'broker', 'def'), 'localhost')
self.assertEqual(gco('system', 'not_set', 'def'), 'def')
def test_get_multiapp(self):
self.assertEqual(gco('system', 'broker', app="test"), None)
self.assertEqual(gco('system', 'broker', app=["test", "system"]), 'localhost')
self.assertEqual(gco('system', 'password', app=["test", "system"]), 'testpwd')
def test_get_multisection(self):
# Must explicitly tell app=test, with differnet section name
self.assertEqual(gco('sec2', 'some', app='test'), 'value')
self.assertEqual(gco(['sec2', 'test'], 'test_value_blank', app='test'), 'not blank')
self.assertEqual(gco(['system'], 'units'), 'SI')
self.assertEqual(gco(['test', 'system'], 'units'), 'inv')
def test_set_basic(self):
self.assertEqual(sco('system', 'new_value', '666'), True)
self.assertEqual(gco('system', 'new_value'), '666')
self.assertEqual(sco('system', 'test_string', 'goodbye'), True)
self.assertEqual(gco('system', 'test_string'), 'goodbye')
def test_set_new(self):
self.assertFalse(os.path.exists(config.get_config_path("conf.d/newfile.conf")))
self.assertEqual(sco('newfile', 'new_value', '666'), True)
self.assertTrue(os.path.exists(config.get_config_path("conf.d/newfile.conf")))
self.assertEqual(gco('newfile', 'new_value'), '666')
def test_unwritable(self):
if os.getuid() == 0:
# TODO: Fix builders so they dont run as root, then change to self.fail instead.
#self.fail("You cannot run this test as. Also, do not develop as root!")
print("You cannot run this test as. Also, do not develop as root!")
return
with open(config.get_config_path('conf.d/blocked.conf'), 'w') as f:
f.write("[blocked]\nnop=nop\n")
os.chmod(config.get_config_path('conf.d/blocked.conf'), 0o444)
self.assertEqual(gco('blocked', 'nop'), 'nop')
# This shall fail, and write log msg
self.assertEqual(sco('blocked', 'new_value', '666'), False)
os.chmod(config.get_config_path('conf.d/blocked.conf'), 0)
# reload
config.augeas = None
self.assertEqual(gco('blocked', 'nop'), None)
self.assertEqual(gco('blocked', 'nop', 'def'), 'def')
class AppConfigTest(ConfigTestBase):
def setUp(self):
super(AppConfigTest, self).setUp()
self.app = AgoTest()
global gco, sco
gco = self.app.get_config_option
sco = self.app.set_config_option
def test_get_basic(self):
self.assertEqual(gco('test_value_0'), '100')
self.assertEqual(gco('test_value_1'), None)
self.assertEqual(gco('units'), 'inv')
def test_get_section(self):
# from system.conf:
self.assertEqual(gco('broker', section='system'), 'localhost')
# from test.conf:
self.assertEqual(gco('password', section='system'), 'testpwd')
def test_get_app(self):
# Will give None, since we're looking in section test, which is not in system.conf
self.assertEqual(gco('test_value_0', app=['other', 'system']), None)
# Will give system.confs password
self.assertEqual(gco('password', app=['other', 'system'], section='system'), 'letmein')
def test_set_basic(self):
self.assertEqual(sco('new_value', '666'), True)
self.assertEqual(gco('new_value'), '666')
self.assertEqual(sco('test_string', 'goodbye'), True)
self.assertEqual(gco('test_string'), 'goodbye')
def test_set_section(self):
self.assertEqual(sco('some_value', '0', 'sec2'), True)
self.assertEqual(gco('some_value'), None)
self.assertEqual(gco('some_value', section='sec2'), '0')
def test_set_new_file(self):
self.assertFalse(os.path.exists(config.get_config_path("conf.d/newfile.conf")))
self.assertEqual(sco('new_value', '666', section='newfile'), True)
self.assertTrue(os.path.exists(config.get_config_path("conf.d/newfile.conf")))
self.assertEqual(gco('new_value', section='newfile'), '666')
......@@ -17,7 +17,9 @@ target_link_libraries (testrunner ${TEST_LIBRARIES})
add_test( cpp-test testrunner )
add_test( python-test python -m unittest discover -s ../shared/ )
add_test( python-test pytest -v ${CMAKE_CURRENT_SOURCE_DIR}/python/)
set_tests_properties(python-test PROPERTIES
ENVIRONMENT PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}/../python/)
# Shortcut to build and run all tests
add_custom_target(check
......
import pytest
import agoclient.config as config
from agoclient.agoapp import AgoApp
import os, os.path, shutil
# Find checkedout code dir:
# We're in subdir python/agoclient/
devdir = os.path.realpath(os.path.dirname(os.path.realpath(__file__)) + "/../..")
class AgoTest(AgoApp):
pass
@pytest.fixture
def setup_config(tmpdir):
# Create dummy conf
conf_d = tmpdir.mkdir('conf.d')
# Custom dir, we must place agocontrol.aug into this dir
# TODO: or add some dir in loadpath?
shutil.copyfile("%s/conf/agocontrol.aug" % devdir, str(tmpdir.join("agocontrol.aug")))
shutil.copyfile("%s/conf/conf.d/system.conf" % devdir, str(conf_d.join("system.conf")))
config.set_config_dir(str(tmpdir))
with open(config.get_config_path('conf.d/test.conf'), 'w') as f:
f.write("[test]\n"+
"test_value_0=100\n"+
"test_value_blank=\n"+
"local=4\n"+
"units=inv\n"+
"[system]\n"+
"password=testpwd\n"+
"[sec2]\n"+
"some=value\n"+
"test_value_blank=not blank\n")
# Kill augeas
config.augeas = None
yield str(tmpdir)
tmpdir.remove()
@pytest.fixture
def app_sut(setup_config):
app = AgoTest()
yield app
gco = config.get_config_option
sco = config.set_config_option
class TestConfig:
def test_get_path(self, setup_config):
assert config.get_config_path("conf.d") == "%s/conf.d" % setup_config
assert config.get_config_path("conf.d/") == "%s/conf.d/" % setup_config
assert config.get_config_path("/conf.d/") == "%s/conf.d/" % setup_config
def test_get_basic(self, setup_config):
assert gco('system', 'broker') == 'localhost'
assert gco('system', 'not_set') is None
assert gco('test', 'test_value_blank') is None
assert gco('test', 'local') == '4'
# Test with defaults
assert gco('system', 'broker', 'def') == 'localhost'
assert gco('system', 'not_set', 'def') == 'def'
def test_get_multiapp(self, setup_config):
assert gco('system', 'broker', app="test") is None
assert gco('system', 'broker', app=["test", "system"]) == 'localhost'
assert gco('system', 'password', app=["test", "system"]) == 'testpwd'
def test_get_multisection(self, setup_config):
# Must explicitly tell app=test, with differnet section name
assert gco('sec2', 'some', app='test') == 'value'
assert gco(['sec2', 'test'], 'test_value_blank', app='test') == 'not blank'
assert gco(['system'], 'units') == 'SI'
assert gco(['test', 'system'], 'units') == 'inv'
def test_set_basic(self, setup_config):
assert sco('system', 'new_value', '666')
assert gco('system', 'new_value') == '666'
assert sco('system', 'test_string', 'goodbye')
assert gco('system', 'test_string') == 'goodbye'
def test_set_new(self, setup_config):
assert not os.path.exists(config.get_config_path("conf.d/newfile.conf"))
assert sco('newfile', 'new_value', '666') == True
assert os.path.exists(config.get_config_path("conf.d/newfile.conf"))
assert gco('newfile', 'new_value') == '666'
def test_unwritable(self, setup_config):
if os.getuid() == 0:
# TODO: Fix builders so they dont run as root, then change to self.fail instead.
#self.fail("You cannot run this test as. Also, do not develop as root!")
print("You cannot run this test as. Also, do not develop as root!")
return
with open(config.get_config_path('conf.d/blocked.conf'), 'w') as f:
f.write("[blocked]\nnop=nop\n")
os.chmod(config.get_config_path('conf.d/blocked.conf'), 0o444)
assert gco('blocked', 'nop') == 'nop'
# This shall fail, and write log msg
assert not sco('blocked', 'new_value', '666')
os.chmod(config.get_config_path('conf.d/blocked.conf'), 0)
# reload
config.augeas = None
assert gco('blocked', 'nop') == None
assert gco('blocked', 'nop', 'def') == 'def'
class TestAppConfig:
def test_get_basic(self, app_sut):
assert app_sut.get_config_option('test_value_0') == '100'
assert app_sut.get_config_option('test_value_1') is None
assert app_sut.get_config_option('units') == 'inv'
def test_get_section(self, app_sut):
# from system.conf:
assert app_sut.get_config_option('broker', section='system') == 'localhost'
# from test.conf:
assert app_sut.get_config_option('password', section='system') == 'testpwd'
def test_get_app(self, app_sut):
# Will give None, since we're looking in section test, which is not in system.conf
assert app_sut.get_config_option('test_value_0', app=['other', 'system']) is None
# Will give system.confs password
assert app_sut.get_config_option('password', app=['other', 'system'], section='system') == 'letmein'
def test_set_basic(self, app_sut):
assert app_sut.set_config_option('new_value', '666') == True
assert app_sut.get_config_option('new_value') == '666'
assert app_sut.set_config_option('test_string', 'goodbye') == True
assert app_sut.get_config_option('test_string') == 'goodbye'
def test_set_section(self, app_sut):
assert app_sut.set_config_option('some_value', '0', 'sec2') == True
assert app_sut.get_config_option('some_value') is None
assert app_sut.get_config_option('some_value', section='sec2') == '0'
def test_set_new_file(self, app_sut):
assert not os.path.exists(config.get_config_path("conf.d/newfile.conf"))
assert app_sut.set_config_option('new_value', '666', section='newfile')
assert os.path.exists(config.get_config_path("conf.d/newfile.conf"))
assert app_sut.get_config_option('new_value', section='newfile') == '666'
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