summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSoren Hansen <soren@canonical.com>2009-08-25 14:46:16 +0200
committerSoren Hansen <soren@canonical.com>2009-08-25 14:46:16 +0200
commit76d5d79c0ec119cf5e87e71578125081c40c291d (patch)
tree91baa01c1772c55c00eb2d4ba5800f79c9a796dc
parentc4f3133dc99b68a41353f763a65dee1b323f8868 (diff)
downloadvyos-cloud-init-76d5d79c0ec119cf5e87e71578125081c40c291d.tar.gz
vyos-cloud-init-76d5d79c0ec119cf5e87e71578125081c40c291d.zip
Added script handling to appliance config handling.
Added test cases for this.
-rwxr-xr-xec2-run-user-data.py15
-rw-r--r--tests.py42
2 files changed, 57 insertions, 0 deletions
diff --git a/ec2-run-user-data.py b/ec2-run-user-data.py
index 784a1b3f..2917b2fe 100755
--- a/ec2-run-user-data.py
+++ b/ec2-run-user-data.py
@@ -107,6 +107,21 @@ class ApplianceConfig(object):
remove_package(pkg)
else:
install_package(pkg)
+ elif node.tagName == 'script':
+ script = ''
+ for subnode in node.childNodes:
+ # If someone went through the trouble of wrapping it in CDATA,
+ # it's probably the script we want to run..
+ if subnode.nodeType == root.CDATA_SECTION_NODE:
+ script = subnode.nodeValue
+ # ..however, fall back to whatever TEXT_NODE stuff is between
+ # the <script> tags.
+ if subnode.nodeType == root.TEXT_NODE and not script:
+ script = subnode.nodeValue
+ if not script:
+ # An empty script?
+ continue
+ content_type_handlers['text/x-shellscript'](script)
def main():
ec2 = ec2init.EC2Init()
diff --git a/tests.py b/tests.py
index fa3383a2..33c45ec7 100644
--- a/tests.py
+++ b/tests.py
@@ -20,6 +20,48 @@
import unittest
+class RunUserDataApplianceConfigScript(unittest.TestCase):
+ def setUp(self):
+ self.ec2_run_user_data = __import__('ec2-run-user-data')
+ self.fake_handle_shell_script_counter = 0
+ self.expected_scripts = []
+ # Override install_remove_package
+ self.ec2_run_user_data.content_type_handlers['text/x-shellscript'] = self.fake_handle_shell_script
+
+ def fake_handle_shell_script(self, txt):
+ self.fake_handle_shell_script_counter += 1
+ self.assertEqual(self.expected_scripts.pop(0), txt)
+
+ def handle_xml(self, xml):
+ msg = self.ec2_run_user_data.parse_user_data(xml)
+ self.ec2_run_user_data.handle_part(msg)
+
+ def testApplianceConfigPackageScriptSingle(self):
+ script = '''#!/bin/sh
+echo hey'''
+ xml = '<appliance><script>%s</script></appliance>' % script
+ self.expected_scripts += [script]
+ self.handle_xml(xml)
+ self.assertEqual(self.fake_handle_shell_script_counter, 1)
+
+ def testApplianceConfigPackageScriptMultiple(self):
+ script1 = '''#!/bin/sh
+echo hey'''
+ script2 = '''#!/usr/bin/python
+print "hey"'''
+ xml = '<appliance><script>%s</script><script>%s</script></appliance>' % (script1, script2)
+ self.expected_scripts += [script1, script2]
+ self.handle_xml(xml)
+ self.assertEqual(self.fake_handle_shell_script_counter, 2)
+
+ def testApplianceConfigPackageScriptCDATA(self):
+ script = '''#!/bin/sh
+echo hey'''
+ xml = '<appliance><script><![CDATA[%s]]></script></appliance>' % (script, )
+ self.expected_scripts += [script]
+ self.handle_xml(xml)
+ self.assertEqual(self.fake_handle_shell_script_counter, 1)
+
class RunUserDataApplianceConfigPackageHandling(unittest.TestCase):
def setUp(self):
self.fake_install_remove_package_counter = 0