forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestmod_python.c
More file actions
183 lines (146 loc) · 5.92 KB
/
Copy pathtestmod_python.c
File metadata and controls
183 lines (146 loc) · 5.92 KB
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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <kdbmacros.h>
#include <stdlib.h>
#ifdef HAVE_KDBCONFIG_H
#include "kdbconfig.h"
#endif
#include <tests_plugin.h>
static void init_env (void)
{
setenv ("PYTHONDONTWRITEBYTECODE", "1", 1);
}
char filebuf[KDB_MAX_PATH_LENGTH + 1];
static char * srcdir_rewrite = NULL;
static char * python_file (const char * filename)
{
if (!srcdir_rewrite)
{
/* no rewrite. just append our plugin name */
strcpy (filebuf, ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
strcat (strcat (filebuf, "/"), filename);
return srcdir_file (filebuf);
}
/* wipe old value */
*srcdir_rewrite = '\0';
/* append plugin name and delete last character */
strcat (strcat (filebuf, "/"), ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
*(filebuf + strlen (filebuf) - 1) = '\0';
strcat (strcat (filebuf, "/"), filename);
return filebuf;
}
// test simple variable passing
static void test_variable_passing (void)
{
printf ("Testing simple variable passing...\n");
KeySet * conf = ksNew (1, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin.py"), KEY_END),
keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/print", KEY_END),
keyNew ("user/python/path", KEY_VALUE, ".", KEY_END), KS_END);
PLUGIN_OPEN (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
Key * parentKey = keyNew ("user/from_c", KEY_END);
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
exit_if_fail (ksGetSize (ks) == 1, "keyset size is still 0");
succeed_if_same_string (keyName (ksHead (ks)), "user/from_python");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
// test loading python twice
static void test_two_scripts (void)
{
printf ("Testing loading of two active python plugins...\n");
KeySet * modules = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin.py"), KEY_END),
keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/python/path", KEY_VALUE, ".", KEY_END),
keyNew ("user/print", KEY_END), KS_END);
KeySet * conf2 = ksNew (2, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin2.py"), KEY_END),
keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/python/path", KEY_VALUE, ".", KEY_END),
keyNew ("user/print", KEY_END), KS_END);
Key * errorKey = keyNew ("", KEY_END);
Plugin * plugin = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf, errorKey);
succeed_if (output_warnings (errorKey), "warnings in kdbOpen");
succeed_if (output_error (errorKey), "errors in kdbOpen");
exit_if_fail (plugin != NULL, "unable to load python plugin");
keyDel (errorKey);
Key * errorKey2 = keyNew ("", KEY_END);
Plugin * plugin2 = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf2, errorKey2);
succeed_if (output_warnings (errorKey2), "warnings in kdbOpen");
succeed_if (output_error (errorKey2), "errors in kdbOpen");
exit_if_fail (plugin2 != NULL, "unable to load python plugin again");
keyDel (errorKey2);
elektraPluginClose (plugin2, 0);
elektraPluginClose (plugin, 0);
elektraModulesClose (modules, 0);
ksDel (modules);
}
// simple return value test
static void test_fail (void)
{
printf ("Testing return values from python functions...\n");
KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin_fail.py"), KEY_END),
keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/python/path", KEY_VALUE, ".", KEY_END),
keyNew ("user/print", KEY_END), KS_END);
PLUGIN_OPEN (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
Key * parentKey = keyNew ("user/tests/from_c", KEY_END);
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == -1, "call to kdbGet didn't fail");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == -1, "call to kdbSet didn't fail");
succeed_if (plugin->kdbError (plugin, ks, parentKey) == -1, "call to kdbError didn't fail");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
// test script with wrong class name
static void test_wrong (void)
{
printf ("Testing python script with wrong class name...\n");
KeySet * modules = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin_wrong.py"), KEY_END),
keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/python/path", KEY_VALUE, ".", KEY_END),
keyNew ("user/print", KEY_END), KS_END);
Key * errorKey = keyNew ("", KEY_END);
Plugin * plugin = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf, errorKey);
succeed_if (!output_warnings (errorKey), "we expect some warnings");
succeed_if (!output_error (errorKey), "we expect some errors");
succeed_if (plugin == NULL, "python plugin shouldn't be loadable");
keyDel (errorKey);
elektraModulesClose (modules, 0);
ksDel (modules);
}
int main (int argc, char ** argv)
{
printf ("PYTHON TESTS\n");
printf ("==================\n\n");
init (argc, argv);
if (argc > 1)
{
strncpy (filebuf, argv[1], sizeof (filebuf) - 1);
/* our files are in pythons plugin directory
* -> rewrite srcdir from xxx/python2 to xxx/python
*/
if (strlen (filebuf) > strlen ("python2") && !strcmp (filebuf + strlen (filebuf) - strlen ("python2"), "python2"))
{
srcdir_rewrite = filebuf + strlen (filebuf) - 1;
*srcdir_rewrite = '\0';
}
}
init_env ();
test_variable_passing ();
test_two_scripts ();
printf ("\n");
printf ("========================================================================\n");
printf ("NOTE: The following errors are intended. We're testing error conditions!\n");
printf ("========================================================================\n");
test_fail ();
test_wrong ();
print_result ("test_python");
return nbError;
}