File: test_python_persist.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 (187 lines) | stat: -rw-r--r-- 5,495 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
186
187
/*
 * Copyright (c) 2020 Balabit
 *
 * 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 "libtest/persist_lib.h"

#include "python-persist.h"
#include "python-helpers.h"
#include "python-main.h"
#include "python-startup.h"
#include "python-global.h"
#include "apphook.h"


static PyObject *_python_main;
static PyObject *_python_main_dict;

static GlobalConfig *cfg;

CFG_LTYPE yyltype;
GlobalConfig *empty_cfg;

static void
_init_python_main(void)
{
  PyGILState_STATE gstate = PyGILState_Ensure();
  {
    _python_main = PyImport_AddModule("__main__");
    _python_main_dict = PyModule_GetDict(_python_main);
  }
  PyGILState_Release(gstate);
}

static void
_load_code(const gchar *code)
{
  PyGILState_STATE gstate;
  gstate = PyGILState_Ensure();
  cr_assert(python_evaluate_global_code(cfg, code, &yyltype));
  PyGILState_Release(gstate);
}

void
setup(void)
{
  app_startup();

  _py_init_interpreter(FALSE);
  _init_python_main();

  cfg = cfg_new_snippet();
}

void
teardown(void)
{
  cfg_free(cfg);
  app_shutdown();
}

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

const gchar *simple_persist = "\n\
from _syslogng import Persist\n\
class SubPersist(Persist):\n\
    def __init__(self, persist_name):\n\
        super(SubPersist, self).__init__(persist_name = persist_name)\n\
\n\
persist = SubPersist('foo')\n\
assert persist.persist_name == 'foo', 'wrong persist name'";

void
persist_state_stop(PersistState *state)
{
  gchar *filename = g_strdup(persist_state_get_filename(state));

  persist_state_cancel(state);

  unlink(filename);
  g_free(filename);
};

Test(python_persist, test_python_persist_name)
{
  PersistState *state = clean_and_create_persist_state_for_test("test-python-persist-name.persist");
  cfg->state = state;
  _load_code(simple_persist);
  persist_state_stop(state);
}

Test(python_persist, test_python_persist_basic)
{
  PersistState *state = clean_and_create_persist_state_for_test("test-python.persist");
  cfg->state = state;
  _load_code("from _syslogng import Persist");
  _load_code("persist = Persist('persist_name')");
  _load_code("assert 'key' not in persist");
  _load_code("persist['key'] = 'value'");
  _load_code("assert 'key' in persist");
  cfg->state = restart_persist_state(state);
  _load_code("persist = Persist('persist_name')");
  _load_code("persist['key'] = 'value'");
  _load_code("assert 'key' in persist");
  _load_code("assert persist['key'] == 'value'");
  persist_state_stop(cfg->state);
}

Test(python_persist, test_python_persist_iterator)
{
  PersistState *state = clean_and_create_persist_state_for_test("test-python-iterator.persist");
  cfg->state = state;
  _load_code("from _syslogng import Persist");
  _load_code("persist = Persist('persist_name')");
  _load_code("persist['key1'] = 'value1'");
  _load_code("persist['key2'] = 'value2'");
  _load_code("persist['key3'] = 'value3'");
  _load_code("assert sorted(persist) == ['key1', 'key2', 'key3']");
  persist_state_stop(state);
}

Test(python_persist, test_python_persist_proper_types)
{
  PersistState *state = clean_and_create_persist_state_for_test("test-python-proper_types.persist");
  cfg->state = state;
  _load_code("from _syslogng import Persist");
  _load_code("persist = Persist('persist_name')");
  _load_code("persist['str'] = 'value'");
  _load_code("persist['number'] = 5");
  _load_code("assert persist['str'] == 'value'");
  _load_code("assert persist['number'] == 5");
  persist_state_stop(state);
}

const gchar *should_throw_exception = "\
from _syslogng import Persist\n\
persist = Persist('persist_name')\n\
exception_happened = False\n\
try:\n\
    persist['missing']\n\
except KeyError:\n\
    exception_happened = True\n\
assert exception_happened";

Test(python_persist, test_python_persist_lookup_missing_key)
{
  PersistState *state = clean_and_create_persist_state_for_test("test-python-lookup-missing-key.persist");
  cfg->state = state;
  _load_code(should_throw_exception);
  persist_state_stop(state);
};

const gchar *iter_returns_proper_types = "\n\
from _syslogng import Persist\n\
persist = Persist('persist_name')\n\
persist['integer'] = 1\n\
persist['str'] = 'str'\n\
persist['bytes'] = b'bytes'\n\
assert set([1, 'str', b'bytes']) == set([persist[k] for k in persist])";

Test(python_persist, test_python_persist_iter_returns_proper_types)
{
  PersistState *state = clean_and_create_persist_state_for_test("test-python-iter-returns-proper-types.persist");
  cfg->state = state;
  _load_code(iter_returns_proper_types);
  persist_state_stop(state);
};