|
19 | 19 | import os, os.path, glob |
20 | 20 | import tests |
21 | 21 |
|
22 | | -vmx2virtimage_dir = "tests/virtconv-files/vmx2virtimage" |
23 | | -vmx2virtimage_files = [ "test" ] |
| 22 | +BASE = "tests/virtconv-files" |
24 | 23 |
|
25 | | -virtimage2vmx_dir = "tests/virtconv-files/virtimage2vmx" |
26 | | -virtimage2vmx_files = [ "image" ] |
| 24 | +vmx_input = BASE + "/vmx_input" |
| 25 | +vmx_output = BASE + "/vmx_output" |
27 | 26 |
|
28 | | -# For x2x conversion, we want to use already tested output, since ideally |
29 | | -# we should be able to run a generated config continually through the |
30 | | -# converter and it will generate the same result |
31 | | -vmx2vmx_dirs = [ virtimage2vmx_dir ] |
32 | | -virtimage2virtimage_dirs = [ vmx2virtimage_dir ] |
| 27 | +virtimage_input = BASE + "/virtimage_input" |
| 28 | +virtimage_output = BASE + "/virtimage_output" |
33 | 29 |
|
34 | 30 | class TestVirtConv(unittest.TestCase): |
35 | 31 |
|
36 | 32 | def setUp(self): |
37 | 33 | pass |
38 | 34 |
|
39 | | - def _convert_helper(self, dirname, filebase, input_type, output_type): |
40 | | - infile = os.path.join(dirname, filebase + "." + input_type) |
41 | | - outfile = os.path.join(dirname, filebase + "." + output_type) |
42 | | - |
| 35 | + def _convert_helper(self, infile, outfile, in_type, out_type): |
43 | 36 | inp = virtconv.formats.find_parser_by_file(infile) |
44 | | - outp = virtconv.formats.parser_by_name(output_type) |
| 37 | + outp = virtconv.formats.parser_by_name(out_type) |
45 | 38 |
|
46 | | - if not inp or inp.name != input_type: |
| 39 | + if not inp or inp.name != in_type: |
47 | 40 | raise AssertionError("find_parser_by_file for '%s' returned " |
48 | 41 | "wrong parser type.\n" |
49 | 42 | "Expected: %s\n" |
50 | 43 | "Received: %s\n" % \ |
51 | | - (infile, input_type, |
| 44 | + (infile, in_type, |
52 | 45 | str((not inp) and str(inp) or inp.name))) |
53 | 46 |
|
54 | 47 | vmdef = inp.import_file(infile) |
55 | 48 | out_expect = outp.export(vmdef) |
56 | 49 | tests.diff_compare(out_expect, outfile) |
57 | 50 |
|
| 51 | + def _build_compare_path(self, base, in_path, out_dir, out_type): |
| 52 | + out_path = os.path.basename(in_path).rsplit(".", 1)[0] |
| 53 | + return "%s/%s_%s.%s" % (out_dir, base, out_path, out_type) |
| 54 | + |
| 55 | + def _compare_files(self, base, in_type, out_type, in_dir, out_dir): |
| 56 | + for in_path in glob.glob(os.path.join(in_dir, "*." + in_type)): |
| 57 | + if in_type != out_type: |
| 58 | + out_path = self._build_compare_path(base, in_path, |
| 59 | + out_dir, out_type) |
| 60 | + else: |
| 61 | + out_path = in_path |
| 62 | + self._convert_helper(in_path, out_path, in_type, out_type) |
| 63 | + |
58 | 64 | def testVMX2VirtImage(self): |
59 | | - for filename in vmx2virtimage_files: |
60 | | - self._convert_helper(vmx2virtimage_dir, filename, |
61 | | - "vmx", "virt-image") |
| 65 | + base = "vmx2virtimage" |
| 66 | + in_type = "vmx" |
| 67 | + out_type = "virt-image" |
| 68 | + in_dir = vmx_input |
| 69 | + out_dir = virtimage_output |
62 | 70 |
|
63 | | - def testVMX2VMX(self): |
64 | | - for dirname in vmx2vmx_dirs: |
65 | | - for filepath in glob.glob(os.path.join(dirname, "*.vmx")): |
66 | | - filename = os.path.splitext(os.path.basename(filepath))[0] |
67 | | - self._convert_helper(dirname, filename, "vmx", "vmx") |
| 71 | + self._compare_files(base, in_type, out_type, in_dir, out_dir) |
68 | 72 |
|
69 | 73 | def testVirtImage2VMX(self): |
70 | | - for filename in virtimage2vmx_files: |
71 | | - self._convert_helper(virtimage2vmx_dir, filename, |
72 | | - "virt-image", "vmx") |
| 74 | + base = "virtimage2vmx" |
| 75 | + in_type = "virt-image" |
| 76 | + out_type = "vmx" |
| 77 | + in_dir = virtimage_input |
| 78 | + out_dir = vmx_output |
| 79 | + |
| 80 | + self._compare_files(base, in_type, out_type, in_dir, out_dir) |
| 81 | + |
| 82 | + # For x2x conversion, we want to use already tested output, since ideally |
| 83 | + # we should be able to run a generated config continually through the |
| 84 | + # converter and it will generate the same result |
| 85 | + def testVMX2VMX(self): |
| 86 | + base = None |
| 87 | + in_type = out_type = "vmx" |
| 88 | + in_dir = out_dir = vmx_output |
| 89 | + |
| 90 | + self._compare_files(base, in_type, out_type, in_dir, out_dir) |
73 | 91 |
|
74 | 92 | def testVirtImage2VirtImage(self): |
75 | | - for dirname in virtimage2virtimage_dirs: |
76 | | - for filepath in glob.glob(os.path.join(dirname, "*.virt-image")): |
77 | | - filename = os.path.splitext(os.path.basename(filepath))[0] |
78 | | - self._convert_helper(dirname, filename, "virt-image", |
79 | | - "virt-image") |
| 93 | + base = None |
| 94 | + in_type = out_type = "virt-image" |
| 95 | + in_dir = out_dir = virtimage_output |
80 | 96 |
|
| 97 | + self._compare_files(base, in_type, out_type, in_dir, out_dir) |
0 commit comments