| OLD | NEW |
| 1 import re, os, logging, commands, string | 1 import re, os, logging, commands |
| 2 from autotest_lib.client.common_lib import utils, error | 2 from autotest_lib.client.common_lib import utils, error |
| 3 import kvm_vm, kvm_utils, kvm_test_utils, kvm_preprocessing | 3 import kvm_vm, kvm_utils |
| 4 | 4 |
| 5 | 5 |
| 6 def run_qemu_img(test, params, env): | 6 def run_qemu_img(test, params, env): |
| 7 """ | 7 """ |
| 8 'qemu-img' functions test: | 8 'qemu-img' functions test: |
| 9 1) Judge what subcommand is going to be tested | 9 1) Judge what subcommand is going to be tested |
| 10 2) Run subcommand test | 10 2) Run subcommand test |
| 11 | 11 |
| 12 @param test: kvm test object | 12 @param test: kvm test object |
| 13 @param params: Dictionary with the test parameters | 13 @param params: Dictionary with the test parameters |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 delcmd += " -d %s %s" % (sn_name, image_name) | 236 delcmd += " -d %s %s" % (sn_name, image_name) |
| 237 s, o = commands.getstatusoutput(delcmd) | 237 s, o = commands.getstatusoutput(delcmd) |
| 238 if s != 0: | 238 if s != 0: |
| 239 raise error.TestFail("Delete snapshot '%s' failed: %s" % | 239 raise error.TestFail("Delete snapshot '%s' failed: %s" % |
| 240 (sn_name, o)) | 240 (sn_name, o)) |
| 241 | 241 |
| 242 | 242 |
| 243 def commit_test(cmd): | 243 def commit_test(cmd): |
| 244 """ | 244 """ |
| 245 Subcommand 'qemu-img commit' test. | 245 Subcommand 'qemu-img commit' test. |
| 246 1) Create a backing file of the qemu harddisk specified by image_name. | |
| 247 2) Start a VM using the backing file as its harddisk. | |
| 248 3) Touch a file "commit_testfile" in the backing_file, and shutdown the | |
| 249 VM. | |
| 250 4) Make sure touching the file does not affect the original harddisk. | |
| 251 5) Commit the change to the original harddisk by executing | |
| 252 "qemu-img commit" command. | |
| 253 6) Start the VM using the original harddisk. | |
| 254 7) Check if the file "commit_testfile" exists. | |
| 255 | 246 |
| 256 @param cmd: qemu-img base command. | 247 @param cmd: qemu-img base command. |
| 257 """ | 248 """ |
| 258 cmd += " commit" | 249 pass |
| 259 | |
| 260 logging.info("Commit testing started!") | |
| 261 image_name = params.get("image_name", "image") | |
| 262 image_format = params.get("image_format", "qcow2") | |
| 263 backing_file_name = "%s_bak" % (image_name) | |
| 264 | |
| 265 try: | |
| 266 # Remove the existing backing file | |
| 267 backing_file = "%s.%s" % (backing_file_name, image_format) | |
| 268 if os.path.isfile(backing_file): | |
| 269 os.remove(backing_file) | |
| 270 | |
| 271 # Create the new backing file | |
| 272 create_cmd = "qemu-img create -b %s.%s -f %s %s.%s" % (image_name, | |
| 273 image_format, | |
| 274 image_format, | |
| 275 backing_file_name, | |
| 276 image_format) | |
| 277 try: | |
| 278 utils.system(create_cmd) | |
| 279 except error.CmdError, e: | |
| 280 raise error.TestFail("Could not create a backing file!") | |
| 281 logging.info("backing_file created!") | |
| 282 | |
| 283 # Set the qemu harddisk to the backing file | |
| 284 logging.info("Original image_name is: %s", params.get('image_name')) | |
| 285 params['image_name'] = backing_file_name | |
| 286 logging.info("Param image_name changed to: %s", | |
| 287 params.get('image_name')) | |
| 288 | |
| 289 # Start a new VM, using backing file as its harddisk | |
| 290 vm_name = params.get('main_vm') | |
| 291 kvm_preprocessing.preprocess_vm(test, params, env, vm_name) | |
| 292 vm = env.get_vm(vm_name) | |
| 293 vm.create() | |
| 294 timeout = int(params.get("login_timeout", 360)) | |
| 295 session = kvm_test_utils.wait_for_login(vm, timeout=timeout) | |
| 296 | |
| 297 # Do some changes to the backing_file harddisk | |
| 298 try: | |
| 299 output = session.cmd("touch /commit_testfile") | |
| 300 logging.info("Output of touch /commit_testfile: %s", output) | |
| 301 output = session.cmd("ls / | grep commit_testfile") | |
| 302 logging.info("Output of ls / | grep commit_testfile: %s", | |
| 303 output) | |
| 304 except Exception, e: | |
| 305 raise error.TestFail("Could not create commit_testfile in the " | |
| 306 "backing file %s", e) | |
| 307 vm.destroy() | |
| 308 | |
| 309 # Make sure there is no effect on the original harddisk | |
| 310 # First, set the harddisk back to the original one | |
| 311 logging.info("Current image_name is: %s", params.get('image_name')) | |
| 312 params['image_name'] = image_name | |
| 313 logging.info("Param image_name reverted to: %s", | |
| 314 params.get('image_name')) | |
| 315 | |
| 316 # Second, Start a new VM, using image_name as its harddisk | |
| 317 # Here, the commit_testfile should not exist | |
| 318 vm_name = params.get('main_vm') | |
| 319 kvm_preprocessing.preprocess_vm(test, params, env, vm_name) | |
| 320 vm = env.get_vm(vm_name) | |
| 321 vm.create() | |
| 322 timeout = int(params.get("login_timeout", 360)) | |
| 323 session = kvm_test_utils.wait_for_login(vm, timeout=timeout) | |
| 324 try: | |
| 325 output = session.cmd("[ ! -e /commit_testfile ] && echo $?") | |
| 326 logging.info("Output of [ ! -e /commit_testfile ] && echo $?: " | |
| 327 "%s", output) | |
| 328 except: | |
| 329 output = session.cmd("rm -f /commit_testfile") | |
| 330 raise error.TestFail("The commit_testfile exists on the " | |
| 331 "original file") | |
| 332 vm.destroy() | |
| 333 | |
| 334 # Excecute the commit command | |
| 335 logging.info("Commiting image") | |
| 336 cmitcmd = "%s -f %s %s.%s" % (cmd, image_format, backing_file_name, | |
| 337 image_format) | |
| 338 try: | |
| 339 utils.system(cmitcmd) | |
| 340 except error.CmdError, e: | |
| 341 raise error.TestFail("Could not commit the backing file") | |
| 342 | |
| 343 # Start a new VM, using image_name as its harddisk | |
| 344 vm_name = params.get('main_vm') | |
| 345 kvm_preprocessing.preprocess_vm(test, params, env, vm_name) | |
| 346 vm = env.get_vm(vm_name) | |
| 347 vm.create() | |
| 348 timeout = int(params.get("login_timeout", 360)) | |
| 349 session = kvm_test_utils.wait_for_login(vm, timeout=timeout) | |
| 350 try: | |
| 351 output = session.cmd("[ -e /commit_testfile ] && echo $?") | |
| 352 logging.info("Output of [ -e /commit_testfile ] && echo $?: %s", | |
| 353 output) | |
| 354 session.cmd("rm -f /commit_testfile") | |
| 355 except: | |
| 356 raise error.TestFail("Could not find commit_testfile after a " | |
| 357 "commit") | |
| 358 vm.destroy() | |
| 359 | |
| 360 finally: | |
| 361 # Remove the backing file | |
| 362 if os.path.isfile(backing_file): | |
| 363 os.remove(backing_file) | |
| 364 | 250 |
| 365 | 251 |
| 366 def _rebase(cmd, img_name, base_img, backing_fmt, mode="unsafe"): | 252 def _rebase(cmd, img_name, base_img, backing_fmt, mode="unsafe"): |
| 367 """ | 253 """ |
| 368 Simple wrapper of 'qemu-img rebase'. | 254 Simple wrapper of 'qemu-img rebase'. |
| 369 | 255 |
| 370 @param cmd: qemu-img base command. | 256 @param cmd: qemu-img base command. |
| 371 @param img_name: image name to be rebased | 257 @param img_name: image name to be rebased |
| 372 @param base_img: indicates the base image | 258 @param base_img: indicates the base image |
| 373 @param backing_fmt: the format of base image | 259 @param backing_fmt: the format of base image |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 try: | 316 try: |
| 431 os.remove(sn2) | 317 os.remove(sn2) |
| 432 os.remove(sn1) | 318 os.remove(sn1) |
| 433 except: | 319 except: |
| 434 pass | 320 pass |
| 435 | 321 |
| 436 | 322 |
| 437 # Here starts test | 323 # Here starts test |
| 438 subcommand = params.get("subcommand") | 324 subcommand = params.get("subcommand") |
| 439 eval("%s_test(cmd)" % subcommand) | 325 eval("%s_test(cmd)" % subcommand) |
| OLD | NEW |