Exemple #1
0
    def runtest(self):
        yaml_dir = self.fspath.dirname

        with open(str(self.fspath)) as f:
            with pytest.raises(barectf._ConfigurationParseError):
                barectf.configuration_from_file(
                    f, inclusion_directories=[yaml_dir])
def test_everything(request, tmpdir):
    yaml_path = os.path.join(os.path.dirname(request.fspath), 'configs',
                             'pass', 'everything', 'config.yaml')
    yaml_dir = os.path.dirname(yaml_path)

    with open(yaml_path) as f:
        cfg = barectf.configuration_from_file(f,
                                              inclusion_directories=[yaml_dir])

    cg = barectf.CodeGenerator(cfg)
    files = cg.generate_c_headers()
    files += cg.generate_c_sources()

    for file in files:
        with open(os.path.join(tmpdir, file.name), 'w') as f:
            f.write(file.contents)

    cc = os.environ.get('CC', 'cc')
    o_file = 'obj.o'
    subprocess.check_call([cc, '-c', '-o', o_file, files[-1].name], cwd=tmpdir)
    nm = os.environ.get('NM', 'nm')
    syms = subprocess.check_output([nm, o_file],
                                   cwd=tmpdir,
                                   universal_newlines=True)
    syms_to_check = [
        'bctf_init',
        'bctf_my_other_stream_close_packet',
        'bctf_my_other_stream_open_packet',
        'bctf_my_other_stream_trace_context_no_payload',
        'bctf_my_other_stream_trace_evev',
        'bctf_my_other_stream_trace_my_event',
        'bctf_my_other_stream_trace_no_context_no_payload',
        'bctf_my_other_stream_trace_oh_henry_event',
        'bctf_my_other_stream_trace_this_event',
        'bctf_my_stream_close_packet',
        'bctf_my_stream_open_packet',
        'bctf_my_stream_trace_my_event',
        'bctf_packet_buf',
        'bctf_packet_buf_addr',
        'bctf_packet_buf_size',
        'bctf_packet_events_discarded',
        'bctf_discarded_event_records_count',
        'bctf_packet_is_empty',
        'bctf_packet_is_full',
        'bctf_packet_is_open',
        'bctf_packet_set_buf',
        'bctf_packet_size',
    ]

    for sym in syms_to_check:
        assert sym in syms
Exemple #3
0
    def exec(self):
        # create configuration
        try:
            with open(self.cfg.cfg_file_path) as f:
                if self.cfg.dump_config:
                    # print effective configuration file
                    print(
                        barectf.effective_configuration_file(
                            f, True, self.cfg.inclusion_dirs,
                            self.cfg.ignore_inclusion_file_not_found))

                    # barectf.configuration_from_file() reads the file again
                    # below: rewind.
                    f.seek(0)

                config = barectf.configuration_from_file(
                    f, True, self.cfg.inclusion_dirs,
                    self.cfg.ignore_inclusion_file_not_found)
        except barectf._ConfigurationParseError as exc:
            _print_config_error(exc)
        except Exception as exc:
            _print_unknown_exc(exc)

        if self.cfg.v2_prefix is not None:
            # Override prefixes.
            #
            # For historical reasons, the `--prefix` option applies the
            # barectf 2 configuration prefix rules. Therefore, get the
            # equivalent barectf 3 prefixes first.
            v3_prefixes = barectf_config_parse_common._v3_prefixes_from_v2_prefix(
                self.cfg.v2_prefix)
            cg_opts = config.options.code_generation_options
            cg_opts = barectf.ConfigurationCodeGenerationOptions(
                v3_prefixes.identifier, v3_prefixes.file_name,
                cg_opts.default_data_stream_type, cg_opts.header_options,
                cg_opts.clock_type_c_types)
            config = barectf.Configuration(
                config.trace, barectf.ConfigurationOptions(cg_opts))

        # create a barectf code generator
        code_gen = barectf.CodeGenerator(config)

        def write_file(dir, file):
            with open(os.path.join(dir, file.name), 'w') as f:
                f.write(file.contents)

        def write_files(dir, files):
            for file in files:
                write_file(dir, file)

        try:
            # generate and write metadata stream file
            write_file(self.cfg.metadata_stream_dir,
                       code_gen.generate_metadata_stream())

            # generate and write C header files
            write_files(self.cfg.c_header_dir, code_gen.generate_c_headers())

            # generate and write C source files
            write_files(self.cfg.c_source_dir, code_gen.generate_c_sources())
        except Exception as exc:
            # We know `config` is valid, therefore the code generator cannot
            # fail for a reason known to barectf.
            _print_unknown_exc(exc)
Exemple #4
0
    def runtest(self):
        # create a temporary directory
        tmpdir = tempfile.TemporaryDirectory(prefix='pytest-barectf')

        # create barectf configuration
        with open(self.fspath) as f:
            cfg = barectf.configuration_from_file(
                f, inclusion_directories=[self._support_dir_path])

        # generate and write C code files
        cg = barectf.CodeGenerator(cfg)
        files = cg.generate_c_headers()
        files += cg.generate_c_sources()

        for file in files:
            with open(os.path.join(tmpdir.name, file.name), 'w') as f:
                f.write(file.contents)

        # generate metadata stream, stripping the version and date
        file = cg.generate_metadata_stream()
        lines = file.contents.split('\n')
        new_lines = []
        discard_patterns = [
            'Copyright (c)',
            'The following code was generated',
            '* on ',
            'barectf_gen_date =',
            'tracer_major =',
            'tracer_minor =',
            'tracer_patch =',
            'tracer_pre =',
        ]

        for line in lines:
            skip = False

            for pattern in discard_patterns:
                if pattern in line:
                    skip = True

            if skip:
                continue

            new_lines.append(line)

        actual_metadata = '\n'.join(new_lines)

        # copy Makefile to build directory
        shutil.copy(os.path.join(self._support_dir_path, 'Makefile'),
                    tmpdir.name)

        # copy platform files to build directory
        shutil.copy(os.path.join(self._support_dir_path, 'test-platform.c'),
                    tmpdir.name)
        shutil.copy(os.path.join(self._support_dir_path, 'test-platform.h'),
                    tmpdir.name)

        # copy specific source code file to build directory
        shutil.copy(self._src_path, os.path.join(tmpdir.name, 'test.c'))

        # build the test
        subprocess.check_output(['make'], cwd=tmpdir.name)

        # run the test (produce the data stream)
        subprocess.check_output(['./test'], cwd=tmpdir.name)

        # read actual stream
        with open(os.path.join(tmpdir.name, 'stream'), 'rb') as f:
            actual_stream = f.read()

        # read data stream expectation file
        with open(self._data_expect_path, 'rb') as f:
            expected_stream = f.read()

        # read metadata stream expectation file
        with open(self._metadata_expect_path, 'r') as f:
            expected_metadata = f.read()

        # validate streams
        assert actual_metadata == expected_metadata
        assert actual_stream == expected_stream

        # delete temporary directory
        tmpdir.cleanup()