Exemple #1
0
    def test_context(self):
        """Test that the library creates a workspace when launched as context.

        Approval criteria:
            - The workspace library shall create the workspace main directory when used as context
            - The workspace library shall attempt to compress the workspace directory when closed

        Test steps::
            1. Initialize the workspace library as a context manager.
            2. Verify that the workspace library created the main workspace directory.
            3. Exit the context.
            4. Verify that the workspace library compressed the main workspace directory.
        """
        self.logger.info(
            "STEP: Initialize the workspace library as a context manager.")
        with Workspace(Mock()) as workspace:
            self.workspace = workspace
            self.logger.info(
                "STEP: Verify that the workspace library created the main workspace directory."
            )
            self.assertTrue(workspace.workspace.exists()
                            and workspace.workspace.is_dir())

            self.logger.info("STEP: Exit the context.")
        self.logger.info(
            "STEP: Verify that the workspace library compressed the main workspace directory."
        )
        compressed_workspace = Path.cwd().joinpath("workspace.tar.gz")
        self.assertTrue(compressed_workspace.exists()
                        and compressed_workspace.is_file())
Exemple #2
0
    def test_test_directory(self):
        """Test that the workspace library can create a test directory as a context manager.

        Approval criteria:
            - The workspace library shall create and chdir to a test directory.

        Test steps::
            1. Initialize the workspace.
            2. Enter test directory in a context manager.
            3. Verify that directory was created and changed to.
        """
        self.logger.info("STEP: Initialize the workspace.")
        with Workspace(Mock()) as workspace:
            self.workspace = workspace

            self.logger.info(
                "STEP: Enter test directory in a context manager.")
            with workspace.test_directory("dir") as directory:
                self.logger.info(
                    "STEP: Verify that directory was created and changed to.")
                self.assertTrue(directory.exists() and directory.is_dir())
                try:
                    self.assertEqual(Path.cwd().relative_to(directory),
                                     Path("."))
                except ValueError as exception:
                    raise AssertionError(
                        "Directory is not the cwd") from exception
Exemple #3
0
    def test_test_directory_no_workspace(self):
        """Test that test directory raises exception if not workspace exist.

        Approval criteria:
            - The workspace library shall raise an exception on test directory if workspace
              does not exist.

        Test steps::
            1. Enter a test directory without a workspace.
            2. Verify that an exception was raised.
        """
        self.logger.info("STEP: Enter a test directory without a workspace.")
        self.workspace = Workspace(Mock())
        self.logger.info("STEP: Verify that an exception was raised.")
        with self.assertRaises(Exception):
            with self.workspace.test_directory("dir1"):
                pass
Exemple #4
0
    def test_compress_error_when_no_workspace_exists(self):
        """Test that the compression method raises exception when no workspace exists.

        Approval criteria:
            - The compression method shall raise exception is the workspace directory does not
              exist

        Test steps::
            1. Attempt to compress a directory that does not exist.
            2. Verify that an exception was raised.
        """
        self.logger.info(
            "STEP: Attempt to compress a directory that does not exist.")
        self.workspace = Workspace(Mock())
        with self.assertRaises(Exception):
            self.logger.info("STEP: Verify that an exception was raised.")
            self.workspace.compress()
Exemple #5
0
    def test_init_does_not_create(self):
        """Test that the library does not create workspace when created normally.

        Approval criteria:
            - Workspace library shall not create the workspace main directory
              when created normally.

        Test steps::
            1. Initialize the workspace library.
            2. Verify that the workspace library did not create a workspace directory.
        """
        self.logger.info("STEP: Initialize the workspace library.")
        self.workspace = Workspace(Mock())

        self.logger.info(
            "STEP: Verify that the workspace library did not create a workspace directory."
        )
        self.assertFalse(Path.cwd().joinpath("workspace").exists())
Exemple #6
0
    def test_compress(self):
        """Test that the compression method compresses a folder and all its subdirectories.

        Approval criteria:
            - The compression method shall compress a folder with gztar.

        Test steps::
            1. Create the workspace directory to be compressed.
            2. Compress the directory.
            3. Verify that the directory was compressed using the gztar format.
        """
        self.logger.info(
            "STEP: Create the workspace directory to be compressed.")
        workspace = Workspace(Mock)
        directory = Path.cwd().joinpath("workspace")
        directory.mkdir()
        workspace.workspace = directory

        # Create a file to verify compression.
        directory.joinpath("file.txt").touch()

        test_folder = Path.cwd().joinpath("testfolder")
        test_folder.mkdir()
        self.items.append(test_folder)

        self.logger.info("STEP: Compress the directory.")
        workspace.compress()

        self.logger.info(
            "STEP: Verify that the directory was compressed using the gztar format."
        )
        self.items.append(test_folder)
        compressed_workspace = Path.cwd().joinpath("workspace.tar.gz")
        unpack_archive(compressed_workspace, test_folder, format="gztar")
        compressed_file = test_folder.joinpath("workspace/file.txt")
        self.assertTrue(compressed_file.exists() and compressed_file.is_file())
Exemple #7
0
    def test_test_directory_identifer_exists(self):
        """Test that the workspace does not create a directory for an identifer that already exists.

        Approval criteria:
            - The workspace library shall reuse a test directory if identifier already exists.

        Test steps::
            1. Initialize the workspace.
            2. Enter a test directory in a context manager with identifier 'dir1'.
            3. Check that test directory was created and exit the context.
            4. Enter a test directory in a context manager with the same identifer.
            5. Verify that the folder was re-used.
        """
        self.logger.info("STEP: Initialize the workspace.")
        with Workspace(Mock()) as workspace:
            self.workspace = workspace

            self.logger.info(
                "STEP: Enter a test directory in a context manager with identifier 'dir1'."
            )
            with workspace.test_directory("dir1") as directory:
                self.logger.info(
                    "STEP: Check that test directory was created and exit the context."
                )
                if not directory.exists() and directory.is_dir():
                    raise Exception("Test directory was not properly created.")
                first_stat = directory.stat()

            with workspace.test_directory("dir1") as directory:
                self.logger.info(
                    "STEP: Enter a test directory in a context manager with the same identifer."
                )
                if not directory.exists() and directory.is_dir():
                    raise Exception("Test directory was not properly created.")

                self.logger.info("STEP: Verify that the folder was re-used.")
                self.assertEqual(
                    first_stat,
                    directory.stat(),
                    "Second directory is not the same as the first directory.",
                )
Exemple #8
0
    def test_test_directory_function_call(self):
        """Test that the test directory method calls a method when created if supplied.

        Approval criteria:
            - The workspace library shall call method on directory creation if supplied.
            - The workspace library shall not call method if directory was not created.

        Test steps::
            1. Initialize the workspace.
            2. Enter a test directory with a method call registered.
            3. Verify that the method was called.
            4. Enter a test directory with the same identifer and a method call registered.
            5. Verify that the method was not called.
        """
        calls = []

        def callee(calls):
            """Test function for counting calls."""
            calls.append(1)

        self.logger.info("STEP: Initialize the workspace.")
        with Workspace(Mock()) as workspace:
            self.workspace = workspace

            self.logger.info(
                "STEP: Enter a test directory with a method call registered.")
            with workspace.test_directory("dir1", callee, calls):
                self.logger.info("STEP: Verify that the method was called.")
                self.assertEqual(len(calls), 1)

            self.logger.info(
                "STEP: Enter a test directory with the same identifer and a "
                "method call registered.")
            with workspace.test_directory("dir1", callee, calls):
                self.logger.info(
                    "STEP: Verify that the method was not called.")
                self.assertEqual(len(calls), 1)
Exemple #9
0
    def execute(self):  # pylint:disable=too-many-branches,disable=too-many-statements
        """Execute all tests in test suite.

        :return: Result of execution. Linux exit code.
        :rtype: int
        """
        self.logger.info("Send test suite started event.")
        test_suite_started = self.test_suite_started()
        sub_suite_id = test_suite_started.meta.event_id
        main_suite_id = self.main_suite_id(self.etos.config.get("context"))
        self.logger.info("Send test environment events.")
        self.environment(sub_suite_id)
        self.etos.config.set("main_suite_id", main_suite_id)
        self.etos.config.set("sub_suite_id", sub_suite_id)

        result = True
        description = None
        try:
            with Workspace(self.log_area) as workspace:
                self.logger.info("Start IUT monitoring.")
                self.iut_monitoring.start_monitoring()
                self.logger.info("Starting test executor.")
                result = self.run_tests(workspace)
                executed = True
                self.logger.info("Stop IUT monitoring.")
                self.iut_monitoring.stop_monitoring()
        except Exception as exception:  # pylint:disable=broad-except
            result = False
            executed = False
            description = str(exception)
            raise
        finally:
            if self.iut_monitoring.monitoring:
                self.logger.info("Stop IUT monitoring.")
                self.iut_monitoring.stop_monitoring()
            self.logger.info("Figure out test outcome.")
            outcome = self.outcome(result, executed, description)
            pprint(outcome)

            self.logger.info("Send test suite finished event.")
            self.etos.events.send_test_suite_finished(
                test_suite_started,
                links={"CONTEXT": self.etos.config.get("context")},
                outcome=outcome,
                persistentLogs=self.log_area.persistent_logs,
            )

        timeout = time.time() + 600  # 10 minutes
        self.logger.info("Waiting for eiffel publisher to deliver events (600s).")

        previous = 0
        # pylint:disable=protected-access
        current = len(self.etos.publisher._deliveries)
        while current:
            current = len(self.etos.publisher._deliveries)
            self.logger.info("Remaining events to send        : %d", current)
            self.logger.info("Events sent since last iteration: %d", previous - current)
            if time.time() > timeout:
                if current < previous:
                    self.logger.info(
                        "Timeout reached, but events are still being sent. Increase timeout by 10s."
                    )
                    timeout = time.time() + 10
                else:
                    raise Exception("Eiffel publisher did not deliver all eiffel events.")
            previous = current
            time.sleep(1)
        self.logger.info("Tests finished executing.")
        return 0 if result else outcome
Exemple #10
0
class TestWorkspace(TestCase):
    """Tests for the workspace library."""

    workspace = None
    logger = logging.getLogger(__name__)

    def setUp(self):
        self.items = []

    def tearDown(self):
        """Attempt to delete all created folders and files."""
        if self.workspace is not None:
            rmtree(self.workspace.workspace, ignore_errors=True)
            Path.cwd().joinpath("workspace.tar.gz").unlink(missing_ok=True)
        for item in self.items:
            if item.is_dir():
                rmtree(item)
            elif item.is_file():
                item.unlink()
        self.workspace = None

    def test_init_does_not_create(self):
        """Test that the library does not create workspace when created normally.

        Approval criteria:
            - Workspace library shall not create the workspace main directory
              when created normally.

        Test steps::
            1. Initialize the workspace library.
            2. Verify that the workspace library did not create a workspace directory.
        """
        self.logger.info("STEP: Initialize the workspace library.")
        self.workspace = Workspace(Mock())

        self.logger.info(
            "STEP: Verify that the workspace library did not create a workspace directory."
        )
        self.assertFalse(Path.cwd().joinpath("workspace").exists())

    def test_context(self):
        """Test that the library creates a workspace when launched as context.

        Approval criteria:
            - The workspace library shall create the workspace main directory when used as context
            - The workspace library shall attempt to compress the workspace directory when closed

        Test steps::
            1. Initialize the workspace library as a context manager.
            2. Verify that the workspace library created the main workspace directory.
            3. Exit the context.
            4. Verify that the workspace library compressed the main workspace directory.
        """
        self.logger.info(
            "STEP: Initialize the workspace library as a context manager.")
        with Workspace(Mock()) as workspace:
            self.workspace = workspace
            self.logger.info(
                "STEP: Verify that the workspace library created the main workspace directory."
            )
            self.assertTrue(workspace.workspace.exists()
                            and workspace.workspace.is_dir())

            self.logger.info("STEP: Exit the context.")
        self.logger.info(
            "STEP: Verify that the workspace library compressed the main workspace directory."
        )
        compressed_workspace = Path.cwd().joinpath("workspace.tar.gz")
        self.assertTrue(compressed_workspace.exists()
                        and compressed_workspace.is_file())

    def test_compress(self):
        """Test that the compression method compresses a folder and all its subdirectories.

        Approval criteria:
            - The compression method shall compress a folder with gztar.

        Test steps::
            1. Create the workspace directory to be compressed.
            2. Compress the directory.
            3. Verify that the directory was compressed using the gztar format.
        """
        self.logger.info(
            "STEP: Create the workspace directory to be compressed.")
        workspace = Workspace(Mock)
        directory = Path.cwd().joinpath("workspace")
        directory.mkdir()
        workspace.workspace = directory

        # Create a file to verify compression.
        directory.joinpath("file.txt").touch()

        test_folder = Path.cwd().joinpath("testfolder")
        test_folder.mkdir()
        self.items.append(test_folder)

        self.logger.info("STEP: Compress the directory.")
        workspace.compress()

        self.logger.info(
            "STEP: Verify that the directory was compressed using the gztar format."
        )
        self.items.append(test_folder)
        compressed_workspace = Path.cwd().joinpath("workspace.tar.gz")
        unpack_archive(compressed_workspace, test_folder, format="gztar")
        compressed_file = test_folder.joinpath("workspace/file.txt")
        self.assertTrue(compressed_file.exists() and compressed_file.is_file())

    def test_compress_error_when_no_workspace_exists(self):
        """Test that the compression method raises exception when no workspace exists.

        Approval criteria:
            - The compression method shall raise exception is the workspace directory does not
              exist

        Test steps::
            1. Attempt to compress a directory that does not exist.
            2. Verify that an exception was raised.
        """
        self.logger.info(
            "STEP: Attempt to compress a directory that does not exist.")
        self.workspace = Workspace(Mock())
        with self.assertRaises(Exception):
            self.logger.info("STEP: Verify that an exception was raised.")
            self.workspace.compress()

    def test_test_directory(self):
        """Test that the workspace library can create a test directory as a context manager.

        Approval criteria:
            - The workspace library shall create and chdir to a test directory.

        Test steps::
            1. Initialize the workspace.
            2. Enter test directory in a context manager.
            3. Verify that directory was created and changed to.
        """
        self.logger.info("STEP: Initialize the workspace.")
        with Workspace(Mock()) as workspace:
            self.workspace = workspace

            self.logger.info(
                "STEP: Enter test directory in a context manager.")
            with workspace.test_directory("dir") as directory:
                self.logger.info(
                    "STEP: Verify that directory was created and changed to.")
                self.assertTrue(directory.exists() and directory.is_dir())
                try:
                    self.assertEqual(Path.cwd().relative_to(directory),
                                     Path("."))
                except ValueError as exception:
                    raise AssertionError(
                        "Directory is not the cwd") from exception

    def test_test_directory_identifer_exists(self):
        """Test that the workspace does not create a directory for an identifer that already exists.

        Approval criteria:
            - The workspace library shall reuse a test directory if identifier already exists.

        Test steps::
            1. Initialize the workspace.
            2. Enter a test directory in a context manager with identifier 'dir1'.
            3. Check that test directory was created and exit the context.
            4. Enter a test directory in a context manager with the same identifer.
            5. Verify that the folder was re-used.
        """
        self.logger.info("STEP: Initialize the workspace.")
        with Workspace(Mock()) as workspace:
            self.workspace = workspace

            self.logger.info(
                "STEP: Enter a test directory in a context manager with identifier 'dir1'."
            )
            with workspace.test_directory("dir1") as directory:
                self.logger.info(
                    "STEP: Check that test directory was created and exit the context."
                )
                if not directory.exists() and directory.is_dir():
                    raise Exception("Test directory was not properly created.")
                first_stat = directory.stat()

            with workspace.test_directory("dir1") as directory:
                self.logger.info(
                    "STEP: Enter a test directory in a context manager with the same identifer."
                )
                if not directory.exists() and directory.is_dir():
                    raise Exception("Test directory was not properly created.")

                self.logger.info("STEP: Verify that the folder was re-used.")
                self.assertEqual(
                    first_stat,
                    directory.stat(),
                    "Second directory is not the same as the first directory.",
                )

    def test_test_directory_function_call(self):
        """Test that the test directory method calls a method when created if supplied.

        Approval criteria:
            - The workspace library shall call method on directory creation if supplied.
            - The workspace library shall not call method if directory was not created.

        Test steps::
            1. Initialize the workspace.
            2. Enter a test directory with a method call registered.
            3. Verify that the method was called.
            4. Enter a test directory with the same identifer and a method call registered.
            5. Verify that the method was not called.
        """
        calls = []

        def callee(calls):
            """Test function for counting calls."""
            calls.append(1)

        self.logger.info("STEP: Initialize the workspace.")
        with Workspace(Mock()) as workspace:
            self.workspace = workspace

            self.logger.info(
                "STEP: Enter a test directory with a method call registered.")
            with workspace.test_directory("dir1", callee, calls):
                self.logger.info("STEP: Verify that the method was called.")
                self.assertEqual(len(calls), 1)

            self.logger.info(
                "STEP: Enter a test directory with the same identifer and a "
                "method call registered.")
            with workspace.test_directory("dir1", callee, calls):
                self.logger.info(
                    "STEP: Verify that the method was not called.")
                self.assertEqual(len(calls), 1)

    def test_test_directory_no_workspace(self):
        """Test that test directory raises exception if not workspace exist.

        Approval criteria:
            - The workspace library shall raise an exception on test directory if workspace
              does not exist.

        Test steps::
            1. Enter a test directory without a workspace.
            2. Verify that an exception was raised.
        """
        self.logger.info("STEP: Enter a test directory without a workspace.")
        self.workspace = Workspace(Mock())
        self.logger.info("STEP: Verify that an exception was raised.")
        with self.assertRaises(Exception):
            with self.workspace.test_directory("dir1"):
                pass