Commit 2b90fc01 authored by Tomáš Peterka's avatar Tomáš Peterka

Merge remote-tracking branch 'origin/master' into amarisoft

Conflicts:
	slapos/tests/slapformat.py
parents d393206c 889711ed
...@@ -47,11 +47,11 @@ documentation. ...@@ -47,11 +47,11 @@ documentation.
This allows to request instances in a few words, i.e:: This allows to request instances in a few words, i.e::
request("mykvm", "http://www.url.com/path/to/current/best/known/kvm/software.cfg") request("http://www.url.com/path/to/current/best/known/kvm/software.cfg", "mykvm")
can be simplified into :: can be simplified into ::
request("mykvm", product.kvm) request(product.kvm, "mykvm")
* "slap" is an instance of the SLAP library. It is only used for advanced usages. * "slap" is an instance of the SLAP library. It is only used for advanced usages.
"slap" instance is obtained by doing :: "slap" instance is obtained by doing ::
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# #
############################################################################## ##############################################################################
from __future__ import print_function
import logging import logging
import slapos.format import slapos.format
...@@ -82,7 +83,7 @@ class FakeCallAndRead: ...@@ -82,7 +83,7 @@ class FakeCallAndRead:
retval = 0, 'UP' retval = 0, 'UP'
global INTERFACE_DICT global INTERFACE_DICT
if 'useradd' in argument_list: if 'useradd' in argument_list:
print argument_list print(argument_list)
global USER_LIST global USER_LIST
username = argument_list[-1] username = argument_list[-1]
if username == '-r': if username == '-r':
...@@ -254,6 +255,33 @@ class SlapformatMixin(unittest.TestCase): ...@@ -254,6 +255,33 @@ class SlapformatMixin(unittest.TestCase):
setattr(slapos.util, name, original_value) setattr(slapos.util, name, original_value)
del self.saved_slapos_util del self.saved_slapos_util
def assertEqualCommands(self, seq1, seq2):
"""Assert that all items in seq1 are present in right order in seq2.
This is useful for ``logger.bucket_list`` where we save executed commands
which mix together with logging messages.
"""
if len(seq1) == 0:
return
assert len(seq1) <= len(seq2), "First argument should contain mandatory items\n"
if len(seq1) != len(seq2):
logging.getLogger('test.slap.format').warning("Comparing uneven sequences!\n")
print("\n".join(map(str, seq1)))
print("\n".join(map(str, seq2)))
# we cannot do simple filter s2 in s1 because commands might repeat
iter1, iter2 = iter(seq1), iter(seq2)
item1, item2 = next(iter1), next(iter2)
try:
while item1 != item2:
item2 = next(iter2)
item1 = next(iter1)
except StopIteration as e:
raise AssertionError(
"Following sequences differs at line \"{!s}\"\n{}\n!=\n{}\n".format(
item1,
"\n".join(map(str, seq1)),
"\n".join(map(str, seq2))))
def setUp(self): def setUp(self):
config = FakeConfig() config = FakeConfig()
config.dry_run = True config.dry_run = True
...@@ -263,6 +291,9 @@ class SlapformatMixin(unittest.TestCase): ...@@ -263,6 +291,9 @@ class SlapformatMixin(unittest.TestCase):
self.test_result = TestLoggerHandler() self.test_result = TestLoggerHandler()
logger.addHandler(self.test_result) logger.addHandler(self.test_result)
config.logger = logger config.logger = logger
if hasattr(self, "logger"):
raise ValueError("{} already has logger attached".format(self.__class__.__name__))
self.logger = logger
self.partition = slapos.format.Partition('partition', '/part_path', self.partition = slapos.format.Partition('partition', '/part_path',
slapos.format.User('testuser'), [], None) slapos.format.User('testuser'), [], None)
global USER_LIST global USER_LIST
...@@ -311,14 +342,14 @@ class TestComputer(SlapformatMixin): ...@@ -311,14 +342,14 @@ class TestComputer(SlapformatMixin):
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'),
partition_list=[]) partition_list=[])
computer.format() computer.format()
self.assertEqual([ self.assertEqualCommands([
"makedirs('/instance_root', 493)", "makedirs('/instance_root', 493)",
"makedirs('/software_root', 493)", "makedirs('/software_root', 493)",
"chown('/software_root', 0, 0)", "chown('/software_root', 0, 0)",
"chmod('/software_root', 493)"], "chmod('/software_root', 493)"],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqual([
'ip addr list bridge', # 'ip addr list bridge', # don't track non-functional commands
'groupadd slapsoft', 'groupadd slapsoft',
'useradd -d /software_root -g slapsoft slapsoft -r' 'useradd -d /software_root -g slapsoft slapsoft -r'
], ],
...@@ -332,7 +363,7 @@ class TestComputer(SlapformatMixin): ...@@ -332,7 +363,7 @@ class TestComputer(SlapformatMixin):
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'),
partition_list=[]) partition_list=[])
computer.format(alter_user=False) computer.format(alter_user=False)
self.assertEqual([ self.assertEqualCommands([
"makedirs('/instance_root', 493)", "makedirs('/instance_root', 493)",
"makedirs('/software_root', 493)", "makedirs('/software_root', 493)",
"chmod('/software_root', 493)"], "chmod('/software_root', 493)"],
...@@ -349,13 +380,13 @@ class TestComputer(SlapformatMixin): ...@@ -349,13 +380,13 @@ class TestComputer(SlapformatMixin):
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'),
partition_list=[]) partition_list=[])
computer.format(alter_network=False) computer.format(alter_network=False)
self.assertEqual([ self.assertEqualCommands([
"makedirs('/instance_root', 493)", "makedirs('/instance_root', 493)",
"makedirs('/software_root', 493)", "makedirs('/software_root', 493)",
"chown('/software_root', 0, 0)", "chown('/software_root', 0, 0)",
"chmod('/software_root', 493)"], "chmod('/software_root', 493)"],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqualCommands([
'ip addr list bridge', 'ip addr list bridge',
'groupadd slapsoft', 'groupadd slapsoft',
'useradd -d /software_root -g slapsoft slapsoft -r' 'useradd -d /software_root -g slapsoft slapsoft -r'
...@@ -370,15 +401,12 @@ class TestComputer(SlapformatMixin): ...@@ -370,15 +401,12 @@ class TestComputer(SlapformatMixin):
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'),
partition_list=[]) partition_list=[])
computer.format(alter_network=False, alter_user=False) computer.format(alter_network=False, alter_user=False)
self.assertEqual([ self.assertEqualCommands([
"makedirs('/instance_root', 493)", "makedirs('/instance_root', 493)",
"makedirs('/software_root', 493)", "makedirs('/software_root', 493)",
"chmod('/software_root', 493)"], "chmod('/software_root', 493)"],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqualCommands([], self.fakeCallAndRead.external_command_list)
'ip addr list bridge',
],
self.fakeCallAndRead.external_command_list)
@unittest.skip("Not implemented") @unittest.skip("Not implemented")
def test_construct_prepared(self): def test_construct_prepared(self):
...@@ -391,7 +419,6 @@ class TestComputer(SlapformatMixin): ...@@ -391,7 +419,6 @@ class TestComputer(SlapformatMixin):
slapos.format.Partition( slapos.format.Partition(
'partition', '/part_path', slapos.format.User('testuser'), [], tap=slapos.format.Tap('tap')), 'partition', '/part_path', slapos.format.User('testuser'), [], tap=slapos.format.Tap('tap')),
]) ])
global INTERFACE_DICT global INTERFACE_DICT
INTERFACE_DICT['bridge'] = { INTERFACE_DICT['bridge'] = {
socket.AF_INET: [{'addr': '192.168.242.77', 'broadcast': '127.0.0.1', socket.AF_INET: [{'addr': '192.168.242.77', 'broadcast': '127.0.0.1',
...@@ -400,7 +427,7 @@ class TestComputer(SlapformatMixin): ...@@ -400,7 +427,7 @@ class TestComputer(SlapformatMixin):
} }
computer.format() computer.format()
self.assertEqual([ self.assertEqualCommands([
"makedirs('/instance_root', 493)", "makedirs('/instance_root', 493)",
"makedirs('/software_root', 493)", "makedirs('/software_root', 493)",
"chown('/software_root', 0, 0)", "chown('/software_root', 0, 0)",
...@@ -410,7 +437,7 @@ class TestComputer(SlapformatMixin): ...@@ -410,7 +437,7 @@ class TestComputer(SlapformatMixin):
"chmod('/instance_root/partition', 488)" "chmod('/instance_root/partition', 488)"
], ],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqualCommands([
'ip addr list bridge', 'ip addr list bridge',
'groupadd slapsoft', 'groupadd slapsoft',
'useradd -d /software_root -g slapsoft slapsoft -r', 'useradd -d /software_root -g slapsoft slapsoft -r',
...@@ -418,12 +445,12 @@ class TestComputer(SlapformatMixin): ...@@ -418,12 +445,12 @@ class TestComputer(SlapformatMixin):
'useradd -d /instance_root/partition -g testuser -G slapsoft testuser -r', 'useradd -d /instance_root/partition -g testuser -G slapsoft testuser -r',
'tunctl -t tap -u testuser', 'tunctl -t tap -u testuser',
'ip link set tap up', 'ip link set tap up',
'brctl show', # 'brctl show', # don't track non-functional commands
'brctl addif bridge tap', 'brctl addif bridge tap',
'ip addr add ip/255.255.255.255 dev bridge', 'ip addr add ip/255.255.255.255 dev bridge',
'ip addr list bridge', # 'ip addr list bridge', # don't track non-functional commands
'ip addr add ip/ffff:ffff:ffff:ffff:: dev bridge', 'ip addr add ip/ffff:ffff:ffff:ffff:: dev bridge',
'ip addr list bridge', # 'ip addr list bridge', # don't track non-functional commands
], ],
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
...@@ -437,7 +464,6 @@ class TestComputer(SlapformatMixin): ...@@ -437,7 +464,6 @@ class TestComputer(SlapformatMixin):
slapos.format.Partition( slapos.format.Partition(
'partition', '/part_path', slapos.format.User('testuser'), [], tap=None), 'partition', '/part_path', slapos.format.User('testuser'), [], tap=None),
]) ])
global USER_LIST global USER_LIST
USER_LIST = ['testuser'] USER_LIST = ['testuser']
partition.tap = slapos.format.Tap('tap') partition.tap = slapos.format.Tap('tap')
...@@ -450,7 +476,7 @@ class TestComputer(SlapformatMixin): ...@@ -450,7 +476,7 @@ class TestComputer(SlapformatMixin):
} }
computer.format(alter_user=False) computer.format(alter_user=False)
self.assertEqual([ self.assertEqualCommands([
"makedirs('/instance_root', 493)", "makedirs('/instance_root', 493)",
"makedirs('/software_root', 493)", "makedirs('/software_root', 493)",
"chmod('/software_root', 493)", "chmod('/software_root', 493)",
...@@ -458,16 +484,16 @@ class TestComputer(SlapformatMixin): ...@@ -458,16 +484,16 @@ class TestComputer(SlapformatMixin):
"chmod('/instance_root/partition', 488)" "chmod('/instance_root/partition', 488)"
], ],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqualCommands([
'ip addr list bridge', 'ip addr list bridge',
'tunctl -t tap -u testuser', 'tunctl -t tap -u testuser',
'ip link set tap up', 'ip link set tap up',
'brctl show', # 'brctl show', # don't track non-functional commands
'brctl addif bridge tap', 'brctl addif bridge tap',
'ip addr add ip/255.255.255.255 dev bridge', 'ip addr add ip/255.255.255.255 dev bridge',
# 'ip addr list bridge', # 'ip addr list bridge', # don't track non-functional commands
'ip addr add ip/ffff:ffff:ffff:ffff:: dev bridge', 'ip addr add ip/ffff:ffff:ffff:ffff:: dev bridge',
'ip -6 addr list bridge', # 'ip -6 addr list bridge', # don't track non-functional commands
], ],
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
...@@ -497,7 +523,7 @@ class TestComputer(SlapformatMixin): ...@@ -497,7 +523,7 @@ class TestComputer(SlapformatMixin):
} }
computer.format(alter_user=False) computer.format(alter_user=False)
self.assertEqual([ self.assertEqualCommands([
"makedirs('/instance_root', 493)", "makedirs('/instance_root', 493)",
"makedirs('/software_root', 493)", "makedirs('/software_root', 493)",
"chmod('/software_root', 493)", "chmod('/software_root', 493)",
...@@ -505,7 +531,7 @@ class TestComputer(SlapformatMixin): ...@@ -505,7 +531,7 @@ class TestComputer(SlapformatMixin):
"chmod('/instance_root/partition', 488)" "chmod('/instance_root/partition', 488)"
], ],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqualCommands([
'ip addr list iface', 'ip addr list iface',
'tunctl -t tap -u testuser', 'tunctl -t tap -u testuser',
'ip link set tap up', 'ip link set tap up',
...@@ -513,7 +539,7 @@ class TestComputer(SlapformatMixin): ...@@ -513,7 +539,7 @@ class TestComputer(SlapformatMixin):
'route add -host 10.8.0.2 dev tap', 'route add -host 10.8.0.2 dev tap',
'ip addr add ip/255.255.255.255 dev iface', 'ip addr add ip/255.255.255.255 dev iface',
'ip addr add ip/ffff:ffff:ffff:ffff:: dev iface', 'ip addr add ip/ffff:ffff:ffff:ffff:: dev iface',
'ip -6 addr list iface' # 'ip -6 addr list iface' # don't track non-functional commands
], ],
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
self.assertEqual(partition.tap.ipv4_addr, '10.8.0.2') self.assertEqual(partition.tap.ipv4_addr, '10.8.0.2')
...@@ -541,7 +567,7 @@ class TestComputer(SlapformatMixin): ...@@ -541,7 +567,7 @@ class TestComputer(SlapformatMixin):
} }
computer.format(alter_network=False) computer.format(alter_network=False)
self.assertEqual([ self.assertEqualCommands([
"makedirs('/instance_root', 493)", "makedirs('/instance_root', 493)",
"makedirs('/software_root', 493)", "makedirs('/software_root', 493)",
"chown('/software_root', 0, 0)", "chown('/software_root', 0, 0)",
...@@ -551,8 +577,8 @@ class TestComputer(SlapformatMixin): ...@@ -551,8 +577,8 @@ class TestComputer(SlapformatMixin):
"chmod('/instance_root/partition', 488)" "chmod('/instance_root/partition', 488)"
], ],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqualCommands([
# 'ip addr list bridge', # 'ip addr list bridge', # don't track non-functional commands
'groupadd slapsoft', 'groupadd slapsoft',
'useradd -d /software_root -g slapsoft slapsoft -r', 'useradd -d /software_root -g slapsoft slapsoft -r',
'groupadd testuser', 'groupadd testuser',
...@@ -583,7 +609,7 @@ class TestComputer(SlapformatMixin): ...@@ -583,7 +609,7 @@ class TestComputer(SlapformatMixin):
} }
computer.format(alter_network=False, alter_user=False) computer.format(alter_network=False, alter_user=False)
self.assertEqual([ self.assertEqualCommands([
"makedirs('/instance_root', 493)", "makedirs('/instance_root', 493)",
"makedirs('/software_root', 493)", "makedirs('/software_root', 493)",
"chmod('/software_root', 493)", "chmod('/software_root', 493)",
...@@ -591,12 +617,12 @@ class TestComputer(SlapformatMixin): ...@@ -591,12 +617,12 @@ class TestComputer(SlapformatMixin):
"chmod('/instance_root/partition', 488)" "chmod('/instance_root/partition', 488)"
], ],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqualCommands([
'ip addr list bridge', # 'ip addr list bridge', # don't track non-functional commands
'ip addr add ip/255.255.255.255 dev bridge', 'ip addr add ip/255.255.255.255 dev bridge',
# 'ip addr list bridge', # 'ip addr list bridge', # don't track non-functional commands
'ip addr add ip/ffff:ffff:ffff:ffff:: dev bridge', 'ip addr add ip/ffff:ffff:ffff:ffff:: dev bridge',
'ip -6 addr list bridge', # 'ip -6 addr list bridge', # don't track non-functional commands
], ],
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
...@@ -624,7 +650,7 @@ class TestComputer(SlapformatMixin): ...@@ -624,7 +650,7 @@ class TestComputer(SlapformatMixin):
} }
computer.format(use_unique_local_address_block=True, alter_user=False, create_tap=False) computer.format(use_unique_local_address_block=True, alter_user=False, create_tap=False)
self.assertEqual([ self.assertEqualCommands([
"makedirs('/instance_root', 493)", "makedirs('/instance_root', 493)",
"makedirs('/software_root', 493)", "makedirs('/software_root', 493)",
"chmod('/software_root', 493)", "chmod('/software_root', 493)",
...@@ -632,12 +658,12 @@ class TestComputer(SlapformatMixin): ...@@ -632,12 +658,12 @@ class TestComputer(SlapformatMixin):
"chmod('/instance_root/partition', 488)" "chmod('/instance_root/partition', 488)"
], ],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqualCommands([
'ip addr list myinterface', # 'ip addr list myinterface', # don't track non-functional commands
'ip address add dev myinterface fd00::1/64', 'ip address add dev myinterface fd00::1/64',
'ip addr add ip/255.255.255.255 dev myinterface', 'ip addr add ip/255.255.255.255 dev myinterface',
'ip addr add ip/ffff:ffff:ffff:ffff:: dev myinterface', 'ip addr add ip/ffff:ffff:ffff:ffff:: dev myinterface',
'ip -6 addr list myinterface' # 'ip -6 addr list myinterface' # don't track non-functional commands
], ],
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
...@@ -707,7 +733,7 @@ class TestPartition(SlapformatMixin): ...@@ -707,7 +733,7 @@ class TestPartition(SlapformatMixin):
def test_createPath_no_alter_user(self): def test_createPath_no_alter_user(self):
self.partition.createPath(False) self.partition.createPath(False)
self.assertEqual( self.assertEqualCommands(
[ [
"mkdir('/part_path', 488)", "mkdir('/part_path', 488)",
"chmod('/part_path', 488)" "chmod('/part_path', 488)"
......
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