Esempio n. 1
0
    def test_add_referenced_tc_to_ts_where_tc_is_new(self):
        tc_data = {
            YAMLConsts.DESCRIPTION: "Added test",
            YAMLConsts.STEPS: {
                'STEP_A': {},
                'STEP_B': {}
            }
        }
        test_case_name = "NEW_TEST_CASE"

        data_file = get_data_file(
            test_dir_name=self.TESTS_SUBDIR,
            data_dir_name=self.DATA_SUBDIR,
            filename=self.SIMPLE_REF)

        yaml_obj = YamlInputFile(input_file=data_file)
        ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj)

        ref_yaml_obj.add_referenced_tc_to_ts(
            target_ts=self.TEST_SUITE,
            target_tc=test_case_name,
            tc_data=tc_data)

        updated_tc_yaml = ref_yaml_obj.data[0][self.TEST_SUITE][test_case_name]

        logging.info(f"Data added: {tc_data}")
        logging.info(f"Updated YAML Data: {updated_tc_yaml}")

        assert_equals(tc_data.get(YAMLConsts.STEPS, {}),
                      updated_tc_yaml.get(YAMLConsts.STEPS, {}))
Esempio n. 2
0
def setup_state_machine_definitions(
        def_file: str) -> Tuple[str, YamlInputFile, MachineDefinition]:
    """
    Load the configuration from file and creates a state machine definition.

    Returns:
        Tuple of model_definition_file (str), model_cfg (YamlInputFile Obj),
        and MachineDefinition obj.

    """
    # Determine the data directory ad build the config file's absolute path
    model_definition_path = get_data_dir()
    model_definition_filename = os.path.sep.join(
        [model_definition_path, def_file])

    logging.info(f"State Machine Config File: {model_definition_filename}")

    # Read and parse the state machine definition YAML file
    model_cfg = YamlInputFile(input_file=model_definition_filename)
    logging.info(f"Model Definition: {model_cfg.data}")

    # Create the model definition
    model_def = MachineDefinition(data=model_cfg.data)

    return model_definition_filename, model_cfg, model_def
Esempio n. 3
0
    def _test_get_referenced_test_data(
            self, filename: str, testsuite: str, testcase: str) -> dict:
        """
        Call get_referenced_test_data and return results

        Args:
            filename (str): Name of YAML file to read
            testsuite (str): Name of testsuite to retrieve
            testcase (str): Name of testcase to retrieve

        Returns:
            Dict: test case definition that meet the provide parameters

        """
        data_file = get_data_file(
            test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR,
            filename=filename)

        yaml_obj = YamlInputFile(input_file=data_file)
        ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj)

        tc_data = ref_yaml_obj.get_referenced_test_data(
            yaml_input=ref_yaml_obj, testsuite=testsuite, testcase=testcase)

        return tc_data
Esempio n. 4
0
    def _read_and_update_source_yaml_file(
            self, filename: str, testsuite: str, testcase: str,
            element: str, value: typing.Any,
            ref_yaml: ReferentialYAML = None) -> ReferentialYAML:
        """
        Read the specified file, and update the corresponding testsuite/testcase
        element with the provided value.

        Args:
            filename (str): Name of the YAML file
            testsuite (str): Testsuite to update
            testcase (str): Testcase to update
            element (str): Testcase element (dict key) to update
            value (any): Value to update the testcase element
            ref_yaml (ReferentialYAML): Populated Referential Yaml File Obj

        Returns:
            Updated ReferentialYAML Object

        """

        if ref_yaml is None:

            # Read YAML File
            data_file = get_data_file(
                test_dir_name=self.TESTS_SUBDIR,
                data_dir_name=self.DATA_SUBDIR,
                filename=filename)
            yaml_obj = YamlInputFile(input_file=data_file)

        else:
            yaml_obj = ref_yaml.yaml
            ref_yaml_obj = ref_yaml

        # Insert the MOD_STEP data to the YAML structure
        self._update_yaml(
            yaml=yaml_obj, test_suite=testsuite, test_case=testcase,
            element=element, value=value)

        # Build reference YAML object and add the data from
        # the primary reference
        if ref_yaml is None:
            ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj)
            assert_true(ref_yaml_obj.check_if_file_references_another_file())

        ref_yaml_obj._add_referenced_paths()
        logging.info(f"Updated YAML:\n{pprint.pformat(ref_yaml_obj.data)}")

        return ref_yaml_obj
Esempio n. 5
0
    def test_check_if_file_references_empty_string(self):
        data_file = get_data_file(
            test_dir_name=self.TESTS_SUBDIR,
            data_dir_name=self.DATA_SUBDIR,
            filename=self.SIMPLE_REF)

        yaml_obj = YamlInputFile(input_file=data_file)

        self._update_yaml(yaml=yaml_obj,
                          test_suite=self.TEST_SUITE,
                          test_case=self.TEST_CASE,
                          element=SMConsts.REFERENCE,
                          value='')

        ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj)

        # Verify the reference is found but fails due to being unable to parse
        ref_yaml_obj.check_if_file_references_another_file()
Esempio n. 6
0
    def test_malformed_yaml_file(self) -> NoReturn:
        # """
        # A malformed YAML generates Parser exception and returns '{}'
        #
        # Returns:
        #     None
        #
        # """
        # Build file path
        data_path = get_data_dir(
            test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR)
        data_file = os.path.sep.join(
            [data_path, self.MALFORMED_YAML_FILE])

        # Read in data file
        test_file_obj = YamlInputFile(input_file=data_file)

        assert_equals(test_file_obj.data, {})
Esempio n. 7
0
    def test_input_file_does_not_exist(self) -> NoReturn:
        # """
        # A non-existent YAML file returns '{}'
        #
        # Returns:
        #     None
        # """

        # Build file path
        data_path = get_data_dir(
            test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR)
        data_file = os.path.sep.join(
            [data_path, self.NON_EXISTING_YAML_FILE])

        # Read in data file
        test_file_obj = YamlInputFile(input_file=data_file)

        assert_equals(test_file_obj.data, {})
Esempio n. 8
0
    def test_input_file_exists(self) -> NoReturn:
        # """
        # A well defined YAML file returns a populated dictionary
        #
        # Returns:
        #     None
        #
        # """
        # Build file path
        data_path = get_data_dir(
            test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR)
        data_file = os.path.sep.join(
            [data_path, self.EXISTING_YAML_FILE])

        # Read in data file
        test_file_obj = YamlInputFile(input_file=data_file)

        assert_not_equals(test_file_obj.data, {})
        assert_greater_equal(len(list(test_file_obj.data.keys())), 1)
Esempio n. 9
0
    def test_check_if_file_references_another_file_invalid_reference(self):
        data_file = get_data_file(
            test_dir_name=self.TESTS_SUBDIR,
            data_dir_name=self.DATA_SUBDIR,
            filename=self.SIMPLE_REF)

        yaml_obj = YamlInputFile(input_file=data_file)

        illegal_value = (f"{self.BASE_REF_FILE}{ReferentialYAML.DELIMITER}"
                         f"{self.TEST_SUITE}{ReferentialYAML.DELIMITER}")

        self._update_yaml(yaml=yaml_obj,
                          test_suite=self.TEST_SUITE,
                          test_case=self.TEST_CASE,
                          element=SMConsts.REFERENCE,
                          value=illegal_value)

        ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj)

        # Verify the reference is found but fails due to being unable to parse
        ref_yaml_obj.check_if_file_references_another_file()
Esempio n. 10
0
    def test_check_if_file_references_another_valid_file_and_validate_ref(self):

        expected_ref_obj = ReferentialYAML.REFERENCE_DATA(
            target_test_suite=self.TEST_SUITE,
            target_test_case=self.TEST_CASE,
            reference_file=self.BASE_REF_FILE,
            reference_test_suite=self.TEST_SUITE,
            reference_test_case=self.TEST_CASE)

        data_file = get_data_file(
            test_dir_name=self.TESTS_SUBDIR,
            data_dir_name=self.DATA_SUBDIR,
            filename=self.SIMPLE_REF)

        yaml_obj = YamlInputFile(input_file=data_file)
        ref_yaml_obj = ReferentialYAML(yaml_input=yaml_obj)

        # Verify the reference is found
        assert_true(ref_yaml_obj.check_if_file_references_another_file())

        # Verify the reference was parsed correctly
        for ref_obj in ref_yaml_obj.references:
            self._validate_reference_data(expected_ref_obj, ref_obj)
Esempio n. 11
0
    # ERROR: no test suite or test case specified
    elif (args.args.test_suite_name is None
          or args.args.test_case_name is None):
        logging.error("Need to specify test_suite_name AND test_case_name.")
        exit(1)

    # List the input files for reference
    display_input_files(log=logging,
                        model_file=machine_cfg_file,
                        test_file=test_file_name,
                        ts_name=test_suite_name,
                        tc_name=test_case_name,
                        log_file=logfile)

    # Parse the state machine definitions
    model_data = MachineDefinition(YamlInputFile(machine_cfg_file).data)

    # Validate that the state machine configuration is valid (static assessment)
    validation = ValidateData(model_data)
    if not (validation.validate_all_transitions()
            and validation.validate_initial_state()):
        logging.error("State Machine definitions are not correct.")
        exit()

    # Instantiate the Object model
    server_model = VmModel()

    # Instantiate and build the state machine
    machine = StateMachine(data_model=model_data, object_model=server_model)
    machine.BORDER_LEN = step_log_border_num_stars
    machine.NUM_ELEMS_PER_GRAPH_LINE = graph_steps_per_line
Esempio n. 12
0
    def test_evaluate_yaml_file(self):

        filename = self.SIMPLE_REF
        testsuite = self.TEST_SUITE
        testcase = self.TEST_CASE
        element = YAMLConsts.DEL_STEPS

        added_step_name_1 = 'STEP_1A'
        insert_before_id_1 = '2'
        added_test_case_id_1 = 'ADDED_1'

        added_step_name_2 = 'STEP_2A'
        insert_after_id_2 = 'ADDED_1'
        added_test_case_id_2 = 'ADDED_2'

        add_tc_data = [
            {
                added_step_name_1:
                {
                    YAMLConsts.BEFORE_ID: insert_before_id_1,
                    YAMLConsts.ID: added_test_case_id_1,
                    YAMLConsts.DATA: None,
                    YAMLConsts.EXPECTATIONS: {'test_me': False}
                }
            },
            {
                added_step_name_2:
                {
                    YAMLConsts.AFTER_ID: insert_after_id_2,
                    YAMLConsts.ID: added_test_case_id_2,
                    YAMLConsts.DATA: None,
                    YAMLConsts.EXPECTATIONS: {'test_me': False}
                }
            }
        ]

        modify_tc_data = [
            {
                "STEP_1":
                    {
                        YAMLConsts.ID: 'DNE',
                        YAMLConsts.DATA: {'params': 1, 'value': 7},
                        YAMLConsts.EXPECTATIONS:
                            {
                                "expectations_10": False,
                                "expectations_20": True,
                            }
                    }
            }
        ]

        delete_step_ids = ['3', '4']

        data_file = get_data_file(
            test_dir_name=self.TESTS_SUBDIR, data_dir_name=self.DATA_SUBDIR,
            filename=filename)
        yaml_obj = YamlInputFile(input_file=data_file)
        test_yaml_obj = ReferentialYAML(yaml_input=yaml_obj)
        test_yaml_obj.evaluate_yaml_file()

        ref_yaml_obj = self._read_and_update_source_yaml_file(
            filename=filename, testsuite=testsuite, testcase=testcase,
            element=YAMLConsts.ADD_STEPS, value=add_tc_data)

        ref_yaml_obj = self._read_and_update_source_yaml_file(
            filename=filename, testsuite=testsuite, testcase=testcase,
            element=YAMLConsts.MOD_STEPS, value=modify_tc_data,
            ref_yaml=ref_yaml_obj)

        ref_yaml_obj = self._read_and_update_source_yaml_file(
            filename=filename, testsuite=testsuite, testcase=testcase,
            element=YAMLConsts.DEL_STEPS, value=delete_step_ids,
            ref_yaml=ref_yaml_obj)

        ref_ts = list(ref_yaml_obj.data[0].values())[0]
        ref_tc = list(ref_ts.values())[0]
        del ref_tc[YAMLConsts.ADD_STEPS]
        del ref_tc[YAMLConsts.MOD_STEPS]
        del ref_tc[YAMLConsts.DEL_STEPS]

        assert_equals.__self__.maxDiff = None
        assert_equals(test_yaml_obj.data, ref_yaml_obj.data)