def setUp(self): self.image_manager = MagicMock() self.image_manager.delete_unused.side_effect = self.fake_delete_unused_images self.image_sweeper = DatastoreImageSweeper(self.image_manager, self.DATASTORE_ID) self.synchronizer = TestSynchronizer() self.timeout = self.TIMEOUT
def setUp(self): self.image_manager = MagicMock() self.vm_manager = MagicMock() self.image_scanner = DatastoreImageScanner(self.image_manager, self.vm_manager, self.DATASTORE_ID) self.synchronizer = TestSynchronizer() self.wait_at_the_end_of_scan = False self.raise_exception = False self.timeout = self.TIMEOUT
def setUp(self): self.image_manager = MagicMock() self.vm_manager = MagicMock() self.image_scanner = DatastoreImageScanner( self.image_manager, self.vm_manager, self.DATASTORE_ID) self.synchronizer = TestSynchronizer() self.wait_at_the_end_of_scan = False self.raise_exception = False self.timeout = self.TIMEOUT
class ImageScannerTestCase(unittest.TestCase): DATASTORE_ID = "DS01" VM_SCAN_RATE = 11 IMAGE_MARK_RATE = 12 TIMEOUT = 6000 # This test uses two threads, the main thread is used to check # the state inside the state machine at different stages. The # second thread, ImageScannerThread, executes the steps in # sequence and calls out the two main methods: # get_active_images() and mark_unused() # These methods have been patched to synchronize with the main # thread. def setUp(self): self.image_manager = MagicMock() self.vm_manager = MagicMock() self.image_scanner = DatastoreImageScanner(self.image_manager, self.vm_manager, self.DATASTORE_ID) self.synchronizer = TestSynchronizer() self.wait_at_the_end_of_scan = False self.raise_exception = False self.timeout = self.TIMEOUT def tearDown(self): self.image_scanner.stop() @patch( "host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_for_unused_images" ) @patch( "host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_vms_for_active_images" ) def test_lifecycle(self, scan_vms_for_active_images, scan_for_unused_images): assert_that( self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) scan_vms_for_active_images.side_effect = self.fake_scan_vms_for_active_images scan_for_unused_images.side_effect = self.fake_scan_for_unused_images self.image_scanner.start(self.timeout, self.VM_SCAN_RATE, self.IMAGE_MARK_RATE) self.image_scanner.wait_for_task_end() assert_that( self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) scan_vms_for_active_images.assert_called_once_with( self.image_scanner, self.DATASTORE_ID) scan_for_unused_images.assert_called_once_with(self.image_scanner, self.DATASTORE_ID) @patch( "host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_for_unused_images" ) @patch( "host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_vms_for_active_images" ) def test_stop(self, scan_vms_for_active_images, scan_for_unused_images): assert_that( self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) scan_vms_for_active_images.side_effect = self.fake_scan_vms_for_active_images scan_for_unused_images.side_effect = self.fake_scan_for_unused_images self.wait_at_the_end_of_scan = True self.image_scanner.start(self.timeout, self.VM_SCAN_RATE, self.IMAGE_MARK_RATE) self.wait_for_runner() self.image_scanner.stop() self.signal_runner() self.image_scanner.wait_for_task_end() assert_that( self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) exception = self.image_scanner.get_exception() assert_that(isinstance(exception, TaskTerminated) is True) scan_vms_for_active_images.assert_called_with(self.image_scanner, self.DATASTORE_ID) assert_that(scan_for_unused_images.called is False) @patch( "host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_for_unused_images" ) @patch( "host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_vms_for_active_images" ) def test_timeout(self, scan_vms_for_active_images, scan_for_unused_images): assert_that( self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) scan_vms_for_active_images.side_effect = self.fake_scan_vms_for_active_images scan_for_unused_images.side_effect = self.fake_scan_for_unused_images self.timeout = 1 self.wait_at_the_end_of_scan = True self.image_scanner.start(self.timeout, self.VM_SCAN_RATE, self.IMAGE_MARK_RATE) self.wait_for_runner() time.sleep(2) self.signal_runner() self.image_scanner.wait_for_task_end() assert_that( self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) exception = self.image_scanner.get_exception() assert_that(isinstance(exception, TaskTimeout) is True) scan_vms_for_active_images.assert_called_with(self.image_scanner, self.DATASTORE_ID) assert_that(scan_for_unused_images.called is False) def test_parse_vmdk(self): base_dir = os.path.dirname(__file__) test_dir = os.path.join(base_dir, "../test_files", "vm_good") vmdk_pathname = os.path.join(test_dir, 'good.vmdk') dictionary = DatastoreImageScannerTaskRunner._parse_vmdk(vmdk_pathname) assert_that(dictionary["version"] == "1") assert_that(dictionary["encoding"] == "UTF-8") assert_that(dictionary["CID"] == "fffffffe") assert_that(dictionary["parentCID"] == "fffffffe") assert_that(dictionary["isNativeSnapshot"] == "no") assert_that(dictionary["createType"] == "vmfsSparse") assert_that(dictionary["parentFileNameHint"] == "/vmfs/volumes/555ca9f8-9f24fa2c-41c1-0025b5414043/" "image_92e62599-6689-4a8f-ba2a-633914b5048e/92e" "62599-6689-4a8f-ba2a-633914b5048e.vmdk") def fake_scan_vms_for_active_images(self, image_scanner, datastore): assert_that(image_scanner.datastore_id is self.DATASTORE_ID) assert_that(datastore is self.DATASTORE_ID) assert_that(self.image_scanner.get_state() is DatastoreImageScanner.State.VM_SCAN) assert_that(image_scanner.vm_scan_rate is self.VM_SCAN_RATE) if self.wait_at_the_end_of_scan: # Wait here until the main thread wants to continue self.signal_main() self.wait_for_main() return dict() def fake_scan_for_unused_images(self, image_scanner, datastore): assert_that(image_scanner.datastore_id is self.DATASTORE_ID) assert_that(datastore is self.DATASTORE_ID) assert_that(self.image_scanner.get_state() is DatastoreImageScanner.State.IMAGE_MARK) assert_that(image_scanner.image_mark_rate is self.IMAGE_MARK_RATE) return dict() def wait_for_runner(self): self.synchronizer.wait_for_runner() def signal_runner(self): self.synchronizer.signal_runner() def wait_for_main(self): self.synchronizer.wait_for_main() def signal_main(self): self.synchronizer.signal_main()
class ImageSweeperTestCase(unittest.TestCase): DATASTORE_ID = "DS01" IMAGE_SWEEP_RATE = 12 TIMEOUT = 6000 # This test uses two threads, the main thread is used to check # the state inside the state machine at different stages. The # second thread, executes the steps in sequence and calls out # the main method: # delete_unused() # These methods have been patched to synchronize with the main # thread. def setUp(self): self.image_manager = MagicMock() self.image_manager.delete_unused.side_effect = self.fake_delete_unused_images self.image_sweeper = DatastoreImageSweeper(self.image_manager, self.DATASTORE_ID) self.synchronizer = TestSynchronizer() self.timeout = self.TIMEOUT def tearDown(self): self.image_sweeper.stop() @patch("host.image.image_sweeper.DatastoreImageSweeperTaskRunner._delete_unused_images") def test_lifecycle(self, delete_unused_images): assert_that(self.image_sweeper.get_state() is DatastoreImageSweeper.State.IDLE) delete_unused_images.side_effect = self.fake_delete_unused_images self.image_sweeper.start(self.timeout, list(), self.IMAGE_SWEEP_RATE) self.image_sweeper.wait_for_task_end() assert_that(self.image_sweeper.get_state() is DatastoreImageSweeper.State.IDLE) delete_unused_images.assert_called_once_with(self.image_sweeper, "/vmfs/volumes/DS01") deleted_images, _ = self.image_sweeper.get_deleted_images() assert_that(len(deleted_images) is 3) def fake_delete_unused_images(self, image_sweeper, datastore_root): assert_that(image_sweeper.datastore_id is self.DATASTORE_ID) assert_that(self.image_sweeper.get_state() is DatastoreImageSweeper.State.IMAGE_SWEEP) assert_that(image_sweeper.image_sweep_rate is self.IMAGE_SWEEP_RATE) deleted_images = list() deleted_images.append("image_id_1") deleted_images.append("image_id_2") deleted_images.append("image_id_3") return deleted_images def wait_for_runner(self): self.synchronizer.wait_for_runner() def signal_runner(self): self.synchronizer.signal_runner() def wait_for_main(self): self.synchronizer.wait_for_main() def signal_main(self): self.synchronizer.signal_main()
class ImageScannerTestCase(unittest.TestCase): DATASTORE_ID = "DS01" VM_SCAN_RATE = 11 IMAGE_MARK_RATE = 12 TIMEOUT = 6000 # This test uses two threads, the main thread is used to check # the state inside the state machine at different stages. The # second thread, ImageScannerThread, executes the steps in # sequence and calls out the two main methods: # get_active_images() and mark_unused() # These methods have been patched to synchronize with the main # thread. def setUp(self): self.image_manager = MagicMock() self.vm_manager = MagicMock() self.image_scanner = DatastoreImageScanner( self.image_manager, self.vm_manager, self.DATASTORE_ID) self.synchronizer = TestSynchronizer() self.wait_at_the_end_of_scan = False self.raise_exception = False self.timeout = self.TIMEOUT def tearDown(self): self.image_scanner.stop() @patch("host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_for_unused_images") @patch("host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_vms_for_active_images") def test_lifecycle(self, scan_vms_for_active_images, scan_for_unused_images): assert_that(self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) scan_vms_for_active_images.side_effect = self.fake_scan_vms_for_active_images scan_for_unused_images.side_effect = self.fake_scan_for_unused_images self.image_scanner.start(self.timeout, self.VM_SCAN_RATE, self.IMAGE_MARK_RATE) self.image_scanner.wait_for_task_end() assert_that(self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) scan_vms_for_active_images.assert_called_once_with(self.image_scanner, self.DATASTORE_ID) scan_for_unused_images.assert_called_once_with(self.image_scanner, self.DATASTORE_ID) @patch("host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_for_unused_images") @patch("host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_vms_for_active_images") def test_stop(self, scan_vms_for_active_images, scan_for_unused_images): assert_that(self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) scan_vms_for_active_images.side_effect = self.fake_scan_vms_for_active_images scan_for_unused_images.side_effect = self.fake_scan_for_unused_images self.wait_at_the_end_of_scan = True self.image_scanner.start(self.timeout, self.VM_SCAN_RATE, self.IMAGE_MARK_RATE) self.wait_for_runner() self.image_scanner.stop() self.signal_runner() self.image_scanner.wait_for_task_end() assert_that(self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) exception = self.image_scanner.get_exception() assert_that(isinstance(exception, TaskTerminated) is True) scan_vms_for_active_images.assert_called_with(self.image_scanner, self.DATASTORE_ID) assert_that(scan_for_unused_images.called is False) @patch("host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_for_unused_images") @patch("host.image.image_scanner.DatastoreImageScannerTaskRunner._scan_vms_for_active_images") def test_timeout(self, scan_vms_for_active_images, scan_for_unused_images): assert_that(self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) scan_vms_for_active_images.side_effect = self.fake_scan_vms_for_active_images scan_for_unused_images.side_effect = self.fake_scan_for_unused_images self.timeout = 1 self.wait_at_the_end_of_scan = True self.image_scanner.start(self.timeout, self.VM_SCAN_RATE, self.IMAGE_MARK_RATE) self.wait_for_runner() time.sleep(2) self.signal_runner() self.image_scanner.wait_for_task_end() assert_that(self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE) exception = self.image_scanner.get_exception() assert_that(isinstance(exception, TaskTimeout) is True) scan_vms_for_active_images.assert_called_with(self.image_scanner, self.DATASTORE_ID) assert_that(scan_for_unused_images.called is False) def test_parse_vmdk(self): base_dir = os.path.dirname(__file__) test_dir = os.path.join(base_dir, "../test_files", "vm_good") vmdk_pathname = os.path.join(test_dir, 'good.vmdk') dictionary = DatastoreImageScannerTaskRunner._parse_vmdk(vmdk_pathname) assert_that(dictionary["version"] == "1") assert_that(dictionary["encoding"] == "UTF-8") assert_that(dictionary["CID"] == "fffffffe") assert_that(dictionary["parentCID"] == "fffffffe") assert_that(dictionary["isNativeSnapshot"] == "no") assert_that(dictionary["createType"] == "vmfsSparse") assert_that(dictionary["parentFileNameHint"] == "/vmfs/volumes/555ca9f8-9f24fa2c-41c1-0025b5414043/" "image_92e62599-6689-4a8f-ba2a-633914b5048e/92e" "62599-6689-4a8f-ba2a-633914b5048e.vmdk") def fake_scan_vms_for_active_images(self, image_scanner, datastore): assert_that(image_scanner.datastore_id is self.DATASTORE_ID) assert_that(datastore is self.DATASTORE_ID) assert_that(self.image_scanner.get_state() is DatastoreImageScanner.State.VM_SCAN) assert_that(image_scanner.vm_scan_rate is self.VM_SCAN_RATE) if self.wait_at_the_end_of_scan: # Wait here until the main thread wants to continue self.signal_main() self.wait_for_main() return dict() def fake_scan_for_unused_images(self, image_scanner, datastore): assert_that(image_scanner.datastore_id is self.DATASTORE_ID) assert_that(datastore is self.DATASTORE_ID) assert_that(self.image_scanner.get_state() is DatastoreImageScanner.State.IMAGE_MARK) assert_that(image_scanner.image_mark_rate is self.IMAGE_MARK_RATE) return dict() def wait_for_runner(self): self.synchronizer.wait_for_runner() def signal_runner(self): self.synchronizer.signal_runner() def wait_for_main(self): self.synchronizer.wait_for_main() def signal_main(self): self.synchronizer.signal_main()