← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~smoser/cloud-init:feature/ds-identify-test into cloud-init:master

 

Scott Moser has proposed merging ~smoser/cloud-init:feature/ds-identify-test into cloud-init:master.

Requested reviews:
  cloud init development team (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~smoser/cloud-init/+git/cloud-init/+merge/323059
-- 
Your team cloud init development team is requested to review the proposed merge of ~smoser/cloud-init:feature/ds-identify-test into cloud-init:master.
diff --git a/tests/unittests/test_ds_identify.py b/tests/unittests/test_ds_identify.py
new file mode 100644
index 0000000..4aa91ce
--- /dev/null
+++ b/tests/unittests/test_ds_identify.py
@@ -0,0 +1,114 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
+import os
+
+from cloudinit import util
+from .helpers import CiTestCase, populate_dir
+
+UNAME_MYSYS = ("Linux bart 4.4.0-62-generic #83-Ubuntu "
+               "SMP Wed Jan 18 14:10:15 UTC 2017 x86_64 GNU/Linux")
+BLKID_EFI_ROOT = """
+DEVNAME=/dev/sda1
+UUID=8B36-5390
+TYPE=vfat
+PARTUUID=30d7c715-a6ae-46ee-b050-afc6467fc452
+
+DEVNAME=/dev/sda2
+UUID=19ac97d5-6973-4193-9a09-2e6bbfa38262
+TYPE=ext4
+PARTUUID=30c65c77-e07d-4039-b2fb-88b1fb5fa1fc
+"""
+
+SHELL_MOCK_TMPL = """\
+%(name)s_out='%(out)s'
+%(name)s_err='%(err)s'
+%(name)s_ret='%(ret)s'
+%(name)s() {
+   local out='%(out)s'
+   local err='%(err)s'
+   local ret='%(ret)s'
+   [ "${%(name)s_out}" = "_unset" ] || echo "${%(name)s_out}"
+   [ "${%(name)s_err}" = "_unset" ] || echo "${%(name)s_err}"
+   return ${%(name)s_ret}
+}
+"""
+
+SHELL_MOCK_TMPL = """\
+%(name)s() {
+   local out='%(out)s' err='%(err)s' r=%(ret)s
+   [ "$out" = "_unset" ] || echo "$out"
+   [ "$err" = "_unset" ] || echo "$err" 2>&1
+   return $r
+}
+"""
+
+
+class TestDsIdentify(CiTestCase):
+    dsid_path = os.path.realpath('tools/ds-identify')
+    def call(self, rootd, mocks, args=None):
+        if args is None:
+            args = []
+
+        wrap = self.tmp_path(path="_shwrap", dir=rootd)
+        head = [
+            "DI_MAIN=noop",
+            "DEBUG_LEVEL=2",
+            "DI_LOG=stderr",
+            "PATH_ROOT='%s'" % rootd,
+            ". " + self.dsid_path,
+            ""
+        ]
+
+        unset = '_unset'
+        def write_mock(data):
+            ddata = {'out': unset, 'err': unset, 'ret': "0"}
+            ddata.update(data)
+            return SHELL_MOCK_TMPL % ddata
+
+        mocklines = []
+        defaults = [
+            {'name': 'detect_virt', 'out': 'none', 'ret': '1'},
+            {'name': 'uname', 'out': UNAME_MYSYS},
+            {'name': 'blkid', 'out': BLKID_EFI_ROOT},
+        ]
+
+        written = [d['name'] for d in mocks]
+        for data in mocks:
+            mocklines.append(write_mock(data))
+        for d in defaults:
+            if d['name'] not in written:
+                mocklines.append(write_mock(d))
+
+        endlines = [
+            'main' # %s' % ' '.join(['"%s"' % s for s in args])
+        ]
+
+        with open("/tmp/script", "w") as fp:
+            fp.write('\n'.join(head + mocklines + endlines) + "\n")
+        with open(wrap, "w") as fp:
+            fp.write('\n'.join(head + mocklines + endlines) + "\n")
+        util.subp(['sh', '-c', '. %s' % wrap], capture=False,
+                  rcs=[0,1])
+
+    def test_simple(self):
+        tmp_dir = self.tmp_dir()
+        mocks = [{'name': 'detect_virt', 'out': "none"},
+                 {'name': 'blkid', 'out': BLKID_EFI_ROOT}]
+        r = self.call(tmp_dir, mocks=mocks)
+        raise Exception("FOO")
+
+
+def blkid_out(ret=0, disks=None):
+    if disks is None:
+        disks = []
+    lines = []
+    fields = ("DEVNAME", "UUID", "UUID_SUB", "TYPE", "PARTUUID")
+    for d in disks:
+        for key in [f for f in fields if f in d]:
+            lines.append("%s=%s" % (key, d[key]))
+        lines.append("")
+    done
+    return '\n'.join(lines)
+
+
+# vi: ts=4 expandtab
diff --git a/tools/ds-identify b/tools/ds-identify
index a40b14d..2dab83a 100755
--- a/tools/ds-identify
+++ b/tools/ds-identify
@@ -216,6 +216,10 @@ has_cdrom() {
     [ -e "${PATH_ROOT}/dev/cdrom" ]
 }
 
+detect_virt() {
+    systemd-detect-virt "$@"
+}
+
 read_virt() {
     cached "$DI_VIRT" && return 0
     local out="" r="" virt="${UNAVAILABLE}"

Follow ups