File: test_python_tf.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (185 lines) | stat: -rw-r--r-- 5,221 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2022 One Identity
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 */

/* this has to come first for modules which include the Python.h header */
#include "python-module.h"

#include <criterion/criterion.h>
#include <criterion/parameterized.h>

#include "python-helpers.h"
#include "python-types.h"
#include "python-tf.h"
#include "python-logmsg.h"
#include "python-logtemplate.h"
#include "python-logtemplate-options.h"
#include "python-integerpointer.h"
#include "python-main.h"
#include "python-startup.h"
#include "apphook.h"
#include "logmsg/logmsg.h"
#include "scratch-buffers.h"
#include "libtest/cr_template.h"


static PyObject *_python_main;
static PyObject *_python_main_dict;

static void
_init_python_main(void)
{
  PyGILState_STATE gstate = PyGILState_Ensure();
  {
    PythonConfig *pc = python_config_get(configuration);
    _python_main = _py_get_main_module(pc);
    _python_main_dict = PyModule_GetDict(_python_main);
  }
  PyGILState_Release(gstate);
}

void
setup(void)
{
  app_startup();
  init_template_tests();

  CfgArgs *args = cfg_args_new();

  cfg_args_set(args, "use-virtualenv", "no");
  cfg_load_module_with_args(configuration, "python", args);
  cfg_args_unref(args);
  _init_python_main();
}

void
teardown(void)
{
  deinit_template_tests();
  scratch_buffers_explicit_gc();
  app_shutdown();
}

TestSuite(python_tf, .init = setup, .fini = teardown);

typedef struct _PyTfTestParams
{
  const gchar *value;
  gssize value_length;
  const gchar *v3_x_expected_value;
  const gchar *v4_x_expected_value;
  LogMessageValueType type;
  LogMessageValueType expected_type;
} PyTfTestParams;

ParameterizedTestParameters(python_tf, test_python_tf)
{
  static PyTfTestParams test_data_list[] =
  {
    {
      .value = "almafa",
      .value_length = -1,
      .type = LM_VT_STRING,
      .expected_type = LM_VT_STRING,
    },
    {
      .value = "42",
      .value_length = -1,
      .type = LM_VT_INTEGER,
      .expected_type = LM_VT_INTEGER,
    },
    {
      .value = "6.900000",
      .value_length = -1,
      .type = LM_VT_DOUBLE,
      .expected_type = LM_VT_DOUBLE
    },
    {
      .value = "true",
      .value_length = -1,
      .type = LM_VT_BOOLEAN,
      .expected_type = LM_VT_BOOLEAN
    },
    {
      .value = "",
      .value_length = -1,
      .type = LM_VT_NULL,
      .expected_type = LM_VT_NULL
    },
    {
      .value = "a,b,c",
      .value_length = -1,
      .type = LM_VT_LIST,
      .expected_type = LM_VT_LIST
    },
    {
      .value = "1680456974",
      .value_length = -1,
      .v4_x_expected_value = "1680456974.000",
      .type = LM_VT_DATETIME,
      .expected_type = LM_VT_DATETIME
    },
    {
      .value = "\0\1\2\3",
      .value_length = 4,
      .v3_x_expected_value = "def",
      .v4_x_expected_value = "def",
      .type = LM_VT_BYTES,
      .expected_type = LM_VT_STRING
    },
    {
      .value = "\4\5\6\7",
      .value_length = 4,
      .v3_x_expected_value = "def",
      .v4_x_expected_value = "def",
      .type = LM_VT_PROTOBUF,
      .expected_type = LM_VT_STRING
    },
  };

  return cr_make_param_array(PyTfTestParams, test_data_list, G_N_ELEMENTS(test_data_list));
}

ParameterizedTest(PyTfTestParams *params, python_tf, test_python_tf)
{
  const gchar *python_tf_implementation = "def test_python_tf(msg):\n"
                                          "    return msg.get('test_key', default='def')\n";
  const gchar *template = "$(python test_python_tf)";

  LogMessage *msg = log_msg_new_empty();
  log_msg_set_value_by_name_with_type(msg, "test_key", params->value, params->value_length, params->type);

  PyGILState_STATE gstate;
  gstate = PyGILState_Ensure();
  {
    cr_assert(PyRun_String(python_tf_implementation, Py_file_input, _python_main_dict, _python_main_dict));

    const gchar *expected_value = params->v3_x_expected_value ? params->v3_x_expected_value : params->value;
    cfg_set_version_without_validation(configuration, VERSION_VALUE_3_38);
    assert_template_format_value_and_type_msg(template, expected_value, LM_VT_STRING, msg);

    expected_value = params->v4_x_expected_value ? params->v4_x_expected_value : params->value;
    cfg_set_version_without_validation(configuration, VERSION_VALUE_4_0);
    assert_template_format_value_and_type_msg(template, expected_value, params->expected_type, msg);
  }
  PyGILState_Release(gstate);

  log_msg_unref(msg);
}