OLD | NEW |
| (Empty) |
1 import os, sys, commands | |
2 | |
3 | |
4 class ImageCheckError(Exception): | |
5 """ | |
6 Simple wrapper for the builtin Exception class. | |
7 """ | |
8 pass | |
9 | |
10 | |
11 class ImageCheck(object): | |
12 """ | |
13 Check qcow2 image by qemu-img info/check command. | |
14 """ | |
15 def __init__(self): | |
16 """ | |
17 Gets params from environment variables and sets class attributes. | |
18 """ | |
19 self.image_path_list = [] | |
20 client_dir = os.environ['AUTODIR'] | |
21 self.kvm_dir = os.path.join(client_dir, 'tests/kvm') | |
22 img_to_check = os.environ['KVM_TEST_images'].split() | |
23 | |
24 for img in img_to_check: | |
25 img_name_str = "KVM_TEST_image_name_%s" % img | |
26 if not os.environ.has_key(img_name_str): | |
27 img_name_str = "KVM_TEST_image_name" | |
28 img_format_str = "KVM_TEST_image_format_%s" % img | |
29 if os.environ.has_key(img_format_str): | |
30 image_format = os.environ[img_format_str] | |
31 else: | |
32 image_format = os.environ['KVM_TEST_image_format'] | |
33 if image_format != "qcow2": | |
34 continue | |
35 image_name = os.environ[img_name_str] | |
36 image_filename = "%s.%s" % (image_name, image_format) | |
37 image_filename = os.path.join(self.kvm_dir, image_filename) | |
38 self.image_path_list.append(image_filename) | |
39 if os.environ.has_key('KVM_TEST_qemu_img_binary'): | |
40 self.qemu_img_path = os.environ['KVM_TEST_qemu_img_binary'] | |
41 else: | |
42 self.qemu_img_path = os.path.join(self.kvm_dir, 'qemu-img') | |
43 self.qemu_img_check = True | |
44 cmd = "%s |grep check" % self.qemu_img_path | |
45 (s1, output) = commands.getstatusoutput(cmd) | |
46 if s1: | |
47 self.qemu_img_check = False | |
48 print "Command qemu-img check not available, not checking..." | |
49 cmd = "%s |grep info" % self.qemu_img_path | |
50 (s2, output) = commands.getstatusoutput(cmd) | |
51 if s2: | |
52 self.qemu_img_check = False | |
53 print "Command qemu-img info not available, not checking..." | |
54 | |
55 def exec_img_cmd(self, cmd_type, image_path): | |
56 """ | |
57 Run qemu-img info/check on given image. | |
58 | |
59 @param cmd_type: Sub command used together with qemu. | |
60 @param image_path: Real path of the image. | |
61 """ | |
62 cmd = ' '.join([self.qemu_img_path, cmd_type, image_path]) | |
63 print "Checking image with command %s" % cmd | |
64 (status, output) = commands.getstatusoutput(cmd) | |
65 print output | |
66 if status or (cmd_type == "check" and not "No errors" in output): | |
67 msg = "Command %s failed" % cmd | |
68 return False, msg | |
69 else: | |
70 return True, '' | |
71 | |
72 | |
73 def check_image(self): | |
74 """ | |
75 Run qemu-img info/check to check the image in list. | |
76 | |
77 If the image checking is failed, raise an exception. | |
78 """ | |
79 # Check all the image in list. | |
80 errmsg = [] | |
81 for image_path in self.image_path_list: | |
82 if not os.path.exists(image_path): | |
83 print "Image %s does not exist!" % image_path | |
84 continue | |
85 s, o = self.exec_img_cmd('info', image_path) | |
86 if not s: | |
87 errmsg.append(o) | |
88 s, o = self.exec_img_cmd('check', image_path) | |
89 if not s: | |
90 errmsg.append(o) | |
91 | |
92 if len(errmsg) > 0: | |
93 raise ImageCheckError('Errors were found, please check log!') | |
94 | |
95 | |
96 if __name__ == "__main__": | |
97 image_check = ImageCheck() | |
98 if image_check.qemu_img_check: | |
99 image_check.check_image() | |
OLD | NEW |