Skip to content

Commit 1ee9089

Browse files
committed
Add Python bindings for testing local DBs
We just need to `dlopen("libpromises.so")` and then call the new DB testing API functions with appropriate arguments. Ticket: CFE-4281 Changelog: None
1 parent 1d6f8a6 commit 1ee9089

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ stamp-h1
5252
/libpromises/Makefile
5353
/misc/Makefile
5454
/misc/selinux/Makefile
55+
/python/Makefile
5556
/tests/Makefile
5657
/tests/acceptance/25_cf-execd/Makefile
5758
/tests/acceptance/Makefile

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ SUBDIRS = \
4646
cf-net \
4747
cf-secret \
4848
misc \
49+
python \
4950
ext \
5051
examples \
5152
tests \

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,7 @@ AC_CONFIG_FILES([Makefile
18721872
contrib/vagrant-ci/centos-7-x64/Makefile
18731873
misc/Makefile
18741874
misc/selinux/Makefile
1875+
python/Makefile
18751876
ext/Makefile
18761877
examples/Makefile
18771878
tests/Makefile

python/Makefile.am

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Copyright 2024 Northern.tech AS
3+
#
4+
# This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5+
#
6+
# This program is free software; you can redistribute it and/or modify it
7+
# under the terms of the GNU General Public License as published by the
8+
# Free Software Foundation; version 3.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program; if not, write to the Free Software
17+
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18+
#
19+
# To the extent this program is licensed as part of the Enterprise
20+
# versions of CFEngine, the applicable Commercial Open Source License
21+
# (COSL) may apply to this file if you as a licensee so wish it. See
22+
# included file COSL.txt.
23+
#
24+
25+
pythonbindingsdir = $(libdir)/python
26+
dist_pythonbindings_DATA = README
27+
28+
if !NDEBUG
29+
if LINUX
30+
# The dbm_test_api is only compile in on Linux in debug builds.
31+
dist_pythonbindings_DATA += test_cfe_db.py
32+
endif
33+
endif

python/README

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This directory contains Python packages and modules for working with CFEngine
2+
and its libraries.
3+
4+
If you want to use one of these, PYTHONPATH and LD_LIBRARY_PATH variables can
5+
make things easier when used like this:
6+
7+
PYTHONPATH=/var/cfengine/lib/python LD_LIBRARY_PATH=/var/cfengine/lib python3
8+
9+
That will make sure that the bindings are found by Python and that CFEngine's
10+
libraries are found by dlopen() (used by ctypes.CDLL() etc.).

python/test_cfe_db.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import ctypes
2+
3+
# Based on the dbid enum from libpromises/dbm_api.h
4+
dbs = (
5+
"classes", # Deprecated
6+
"variables", # Deprecated
7+
"performance",
8+
"checksums", # Deprecated
9+
"filestats", # Deprecated
10+
"changes",
11+
"observations",
12+
"state",
13+
"lastseen",
14+
"audit",
15+
"locks",
16+
"history",
17+
"measure",
18+
"static",
19+
"scalars",
20+
"windows_registry",
21+
"cache",
22+
"license",
23+
"value",
24+
"agent_execution",
25+
"bundles", # Deprecated
26+
"packages_installed", # new package promise installed packages list
27+
"packages_updates", # new package promise list of available updates
28+
"cookies", # Enterprise reporting cookies for duplicate host detection
29+
)
30+
31+
def get_db_id(db_name):
32+
return dbs.index(db_name)
33+
34+
35+
class _SimulationStruct(ctypes.Structure):
36+
pass
37+
38+
class _FilamentStruct(ctypes.Structure):
39+
pass
40+
41+
_promises = ctypes.CDLL("libpromises.so.3.0.6")
42+
43+
_promises.SimulateDBLoad.restype = ctypes.POINTER(_SimulationStruct)
44+
_promises.SimulateDBLoad.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_long, ctypes.c_long,
45+
ctypes.c_int, ctypes.c_int, ctypes.c_long, ctypes.c_long,
46+
ctypes.c_long, ctypes.c_long]
47+
def SimulateDBLoad(db_id,
48+
read_keys_refresh_s=5,
49+
read_min_interval_ms=100,
50+
read_max_interval_ms=200,
51+
write_sample_size_pct=50,
52+
write_prune_interval_s=10,
53+
write_min_interval_ms=200,
54+
write_max_interval_ms=400,
55+
iter_min_interval_ms=1000,
56+
iter_max_interval_ms=2000):
57+
return _promises.SimulateDBLoad(db_id,
58+
read_keys_refresh_s, read_min_interval_ms, read_max_interval_ms,
59+
write_sample_size_pct,
60+
write_prune_interval_s, write_min_interval_ms, write_max_interval_ms,
61+
iter_min_interval_ms, iter_max_interval_ms)
62+
63+
_promises.StopSimulation.restype = None # void
64+
_promises.StopSimulation.argtypes = [ctypes.POINTER(_SimulationStruct)]
65+
def StopSimulation(simulation):
66+
_promises.StopSimulation(simulation)
67+
68+
_promises.FillUpDB.restype = ctypes.POINTER(_FilamentStruct)
69+
_promises.FillUpDB.argtypes = [ctypes.c_int, ctypes.c_int]
70+
def FillUpDB(db_id, usage):
71+
return _promises.FillUpDB(db_id, usage)
72+
73+
_promises.RemoveFilament.restype = None # void
74+
_promises.RemoveFilament.argtypes = [ctypes.POINTER(_FilamentStruct)]
75+
def RemoveFilament(filament):
76+
_promises.RemoveFilament(filament)

0 commit comments

Comments
 (0)