summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVlastimil Holer <vlastimil.holer@gmail.com>2013-09-04 16:19:54 +0200
committerVlastimil Holer <vlastimil.holer@gmail.com>2013-09-04 16:19:54 +0200
commit1adc68b643b2f73a2a08ca7d19c3fc8e759f06c2 (patch)
tree2cb86908340b09b962890b3dfdf898f60b3bf82d
parent77c8798388637fcb2f7dbc057cad81e8fd5fbe58 (diff)
downloadvyos-cloud-init-1adc68b643b2f73a2a08ca7d19c3fc8e759f06c2.tar.gz
vyos-cloud-init-1adc68b643b2f73a2a08ca7d19c3fc8e759f06c2.zip
Fix RE matching context variables. Test cleanups.
-rw-r--r--cloudinit/sources/DataSourceOpenNebula.py26
-rw-r--r--tests/unittests/test_datasource/test_opennebula.py12
2 files changed, 26 insertions, 12 deletions
diff --git a/cloudinit/sources/DataSourceOpenNebula.py b/cloudinit/sources/DataSourceOpenNebula.py
index d2ab348f..0ab23b25 100644
--- a/cloudinit/sources/DataSourceOpenNebula.py
+++ b/cloudinit/sources/DataSourceOpenNebula.py
@@ -277,16 +277,28 @@ def read_context_disk_dir(source_dir):
text = f.read()
f.close()
- context_reg = re.compile(r"^([\w_]+)=['\"](.*?[^\\])['\"]$",
- re.MULTILINE | re.DOTALL)
- variables = context_reg.findall(text)
+ # lame matching:
+ # 1. group = key
+ # 2. group = single quoted value, respect '\''
+ # 3. group = old double quoted value, but doesn't end with \"
+ context_reg = re.compile(
+ r"^([\w_]+)=(?:'((?:[^']|'\\'')*?)'|\"(.*?[^\\])\")$",
+ re.MULTILINE | re.DOTALL)
+ variables = context_reg.findall(text)
if not variables:
raise NonContextDiskDir("No variables in context")
- context_sh = {}
- for k, v in variables:
- context_sh[k.lower()] = v
+ for k, v1, v2 in variables:
+ k = k.lower()
+ if v1:
+ # take single quoted variable 'xyz'
+ # (ON>=4) and unquote '\'' -> '
+ context_sh[k] = v1.replace(r"'\''", r"'")
+ elif v2:
+ # take double quoted variable "xyz"
+ # (old ON<4) and unquote \" -> "
+ context_sh[k] = v2.replace(r'\"', r'"')
except (IOError, NonContextDiskDir) as e:
raise NonContextDiskDir("Error reading context.sh: %s" % (e))
@@ -326,7 +338,7 @@ def read_context_disk_dir(source_dir):
# http://opennebula.org/documentation:rel3.8:cong#network_configuration
for k in context_sh.keys():
if re.match(r'^eth\d+_ip$', k):
- (out, _) = util.subp(['/sbin/ip', '-o', 'link'])
+ (out, _) = util.subp(['/sbin/ip', 'link'])
net = OpenNebulaNetwork(out, context_sh)
results['network-interfaces'] = net.gen_conf()
break
diff --git a/tests/unittests/test_datasource/test_opennebula.py b/tests/unittests/test_datasource/test_opennebula.py
index 27725930..66a38e31 100644
--- a/tests/unittests/test_datasource/test_opennebula.py
+++ b/tests/unittests/test_datasource/test_opennebula.py
@@ -1,7 +1,8 @@
import os
-from mocker import MockerTestCase
-from cloudinit import util
+
from cloudinit.sources import DataSourceOpenNebula as ds
+from cloudinit import util
+from mocker import MockerTestCase
TEST_VARS = {
'var1': 'single',
@@ -19,7 +20,7 @@ SSH_KEY = 'ssh-rsa AAAAB3NzaC1....sIkJhq8wdX+4I3A4cYbYP ubuntu@server-460-%i'
HOSTNAME = 'foo.example.com'
PUBLIC_IP = '10.0.0.3'
-CMD_IP_OUT = '''
+CMD_IP_OUT = '''\
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
@@ -91,6 +92,7 @@ class TestOpenNebulaDataSource(MockerTestCase):
devs_with_answers = {
"TYPE=iso9660": ["/dev/vdb"],
"LABEL=CDROM": ["/dev/sr0"],
+ "LABEL=CONTEXT": ["/dev/sdb"],
}
def my_devs_with(criteria):
@@ -99,7 +101,7 @@ class TestOpenNebulaDataSource(MockerTestCase):
try:
orig_find_devs_with = util.find_devs_with
util.find_devs_with = my_devs_with
- self.assertEqual(["/dev/sr0", "/dev/vdb"],
+ self.assertEqual(["/dev/sdb", "/dev/sr0", "/dev/vdb"],
ds.find_candidate_devs())
finally:
util.find_devs_with = orig_find_devs_with
@@ -161,7 +163,7 @@ def populate_dir(seed_dir, files):
with open(os.path.join(seed_dir, "context.sh"), "w") as fp:
fp.write("# Context variables generated by OpenNebula\n")
for (name, content) in files.iteritems():
- fp.write('%s="%s"\n' % (name.upper(), content))
+ fp.write("%s='%s'\n" % (name.upper(), content.replace(r"'", r"'\''")))
fp.close()
# vi: ts=4 expandtab