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.vm_manager.get_vm_images.side_effect = \
            self.fake_get_vm_images
        self.image_manager.mark_unused.side_effect = \
            self.fake_mark_unused

        self.raise_exception = False
        self.timeout = self.TIMEOUT

    def tearDown(self):
        self.image_scanner.stop()

    def test_lifecycle(self):
        assert_that(
            self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE)

        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)

        self.vm_manager.get_vm_images.\
            assert_called_once_with(self.image_scanner)
        self.image_manager.mark_unused.\
            assert_called_once_with(self.image_scanner)

    def test_stop(self):
        assert_that(
            self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE)

        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)

        self.vm_manager.get_vm_images.\
            assert_called_with(self.image_scanner)
        assert_that(self.image_manager.mark_unused.called is False)

    def test_timeout(self):
        assert_that(
            self.image_scanner.get_state() is DatastoreImageScanner.State.IDLE)

        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)

        self.vm_manager.get_vm_images.\
            assert_called_with(self.image_scanner)
        assert_that(self.image_manager.mark_unused.called is False)

    def fake_get_vm_images(self, image_scanner):
        assert_that(image_scanner.datastore_id 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_mark_unused(self, image_scanner):
        assert_that(image_scanner.datastore_id 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 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.vm_manager.get_vm_images.side_effect = \
            self.fake_get_vm_images
        self.image_manager.mark_unused.side_effect = \
            self.fake_mark_unused

        self.raise_exception = False
        self.timeout = self.TIMEOUT

    def tearDown(self):
        self.image_scanner.stop()

    def test_lifecycle(self):
        assert_that(self.image_scanner.get_state() is
                    DatastoreImageScanner.State.IDLE)

        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)

        self.vm_manager.get_vm_images.\
            assert_called_once_with(self.image_scanner)
        self.image_manager.mark_unused.\
            assert_called_once_with(self.image_scanner)

    def test_stop(self):
        assert_that(self.image_scanner.get_state() is
                    DatastoreImageScanner.State.IDLE)

        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)

        self.vm_manager.get_vm_images.\
            assert_called_with(self.image_scanner)
        assert_that(self.image_manager.mark_unused.called is False)

    def test_timeout(self):
        assert_that(self.image_scanner.get_state() is
                    DatastoreImageScanner.State.IDLE)

        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)

        self.vm_manager.get_vm_images.\
            assert_called_with(self.image_scanner)
        assert_that(self.image_manager.mark_unused.called is False)

    def fake_get_vm_images(self, image_scanner):
        assert_that(image_scanner.datastore_id 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_mark_unused(self, image_scanner):
        assert_that(image_scanner.datastore_id 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()
Пример #3
0
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.hypervisor.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 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.hypervisor.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()