FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
plugin_gpsmap/tests/harness.php at develop · Cacti/plugin_gpsmap · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Cacti
/
plugin_gpsmap
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
3
Code
Issues
29
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
plugin_gpsmap
/
tests
/
harness.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
229 lines (180 loc) · 6.8 KB
Breadcrumbs
plugin_gpsmap
/
tests
/
harness.php
Copy path
File metadata and controls
229 lines (180 loc) · 6.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
+-------------------------------------------------------------------------+
| Shared assertions and Cacti stubs for the standalone test suite. No |
| Composer, no framework: the suite has to run from a plain PHP CLI on a |
| machine that only has the plugin checked out. |
+-------------------------------------------------------------------------+
*/
/* Never reachable over HTTP. Cacti deploys plugins inside the web root, so
* plugins/gpsmap/tests/ would otherwise be a public endpoint that resolves DNS
* and writes to the filesystem. */
if
(
PHP_SAPI
!==
'
cli
'
) {
exit
;
}
$
GLOBALS
[
'
gpsmap_test_pass
'
] =
0
;
$
GLOBALS
[
'
gpsmap_test_fail
'
] =
0
;
/* Rows the db stubs hand back. Tests overwrite these per case. */
$
GLOBALS
[
'
gpsmap_stub_rows
'
] =
array
();
$
GLOBALS
[
'
gpsmap_stub_settings
'
] =
array
(
'
base_url
'
=>
'
https://cacti.example/
'
);
function
assert_equal
(
$
label
,
$
expected
,
$
actual
) {
if
(
$
expected
===
$
actual
) {
echo
"
PASS
$
label
\n"
;
$
GLOBALS
[
'
gpsmap_test_pass
'
]++;
}
else
{
echo
"
FAIL
$
label
\n"
;
echo
'
expected:
'
.
var_export
(
$
expected
,
true
) .
"\n"
;
echo
'
actual:
'
.
var_export
(
$
actual
,
true
) .
"\n"
;
$
GLOBALS
[
'
gpsmap_test_fail
'
]++;
}
}
function
assert_true
(
$
label
,
$
value
) {
assert_equal
(
$
label
,
true
, (
bool
)
$
value
);
}
function
assert_false
(
$
label
,
$
value
) {
assert_equal
(
$
label
,
false
, (
bool
)
$
value
);
}
function
assert_contains
(
$
label
,
string
$
needle
,
string
$
haystack
) {
assert_true
(
$
label
,
str_contains
(
$
haystack
,
$
needle
));
}
function
assert_not_contains
(
$
label
,
string
$
needle
,
string
$
haystack
) {
assert_false
(
$
label
,
str_contains
(
$
haystack
,
$
needle
));
}
function
gpsmap_test_summary
():
int
{
echo
"\n"
;
echo
'
Results:
'
.
$
GLOBALS
[
'
gpsmap_test_pass
'
] .
'
passed,
'
.
$
GLOBALS
[
'
gpsmap_test_fail
'
] .
"
failed
\n"
;
return
$
GLOBALS
[
'
gpsmap_test_fail
'
] >
0
?
1
:
0
;
}
/* ------------------------------------------------------------------ */
/* Cacti stubs */
/* ------------------------------------------------------------------ */
if
(!
isset
(
$
GLOBALS
[
'
config
'
])) {
$
GLOBALS
[
'
config
'
] =
array
(
'
base_path
'
=>
dirname
(
__DIR__
,
3
) .
'
/cacti-stub
'
,
'
url_path
'
=>
'
/cacti/
'
,
);
}
if
(!
function_exists
(
'
cacti_sizeof
'
)) {
function
cacti_sizeof
(
$
var
) {
return
is_array
(
$
var
) ?
count
(
$
var
) :
0
; }
}
if
(!
function_exists
(
'
db_fetch_assoc
'
)) {
function
db_fetch_assoc
(
$
sql
) {
return
gpsmap_stub_query
(
$
sql
); }
}
if
(!
function_exists
(
'
db_fetch_assoc_prepared
'
)) {
function
db_fetch_assoc_prepared
(
$
sql
,
$
params
=
array
()) {
return
gpsmap_stub_query
(
$
sql
); }
}
/* Route each query to a named fixture bucket so a test can set them
* independently rather than guessing call order. */
function
gpsmap_stub_query
(
$
sql
) {
$
GLOBALS
[
'
gpsmap_stub_last_sql
'
] =
$
sql
;
if
(
str_contains
(
$
sql
,
'
FROM `host` AS h
'
)) {
$
GLOBALS
[
'
gpsmap_stub_host_sql
'
] =
$
sql
;
return
$
GLOBALS
[
'
gpsmap_stub_rows
'
][
'
hosts
'
] ??
array
();
}
if
(
str_contains
(
$
sql
,
'
`AP` = 1
'
)) {
return
$
GLOBALS
[
'
gpsmap_stub_rows
'
][
'
towers
'
] ??
array
();
}
if
(
str_contains
(
$
sql
,
'
`host_template`
'
)) {
return
$
GLOBALS
[
'
gpsmap_stub_rows
'
][
'
templates
'
] ??
array
();
}
if
(
str_contains
(
$
sql
,
'
gpsmap_templates
'
)) {
return
$
GLOBALS
[
'
gpsmap_stub_rows
'
][
'
icons
'
] ??
array
();
}
return
array
();
}
if
(!
function_exists
(
'
read_config_option
'
)) {
function
read_config_option
(
$
name
,
$
force
=
false
) {
return
$
GLOBALS
[
'
gpsmap_stub_settings
'
][
$
name
] ??
''
;
}
}
if
(!
function_exists
(
'
cacti_log
'
)) {
function
cacti_log
(
$
message
,
$
stdout
=
false
,
$
env
=
''
) {
$
GLOBALS
[
'
gpsmap_stub_log
'
][] =
$
message
;
}
}
if
(!
function_exists
(
'
is_ipaddress
'
)) {
function
is_ipaddress
(
$
ip
) {
return
filter_var
(
$
ip
,
FILTER_VALIDATE_IP
) !==
false
; }
}
if
(!
function_exists
(
'
html_escape
'
)) {
function
html_escape
(
$
text
) {
return
htmlspecialchars
((
string
)
$
text
,
ENT_QUOTES
,
'
UTF-8
'
); }
}
if
(!
function_exists
(
'
__
'
)) {
function
__
(
$
format
, ...
$
args
) {
/* Cacti's last argument is the text domain when more than one is given. */
if
(
count
(
$
args
) >
1
) {
array_pop
(
$
args
); }
return
$
args
===
array
() ?
$
format
:
vsprintf
(
$
format
,
$
args
);
}
}
if
(!
function_exists
(
'
__esc
'
)) {
function
__esc
(
$
format
, ...
$
args
) {
return
html_escape
(
__
(
$
format
, ...
$
args
)); }
}
/* Scratch Cacti root. class/ and includes/ are symlinked back to the real
* checkout so the code under test loads unmodified, while XML/ and the icon
* folder are real directories the tests can write to. */
function
gpsmap_test_tmpdir
():
string
{
static
$
dir
=
null
;
if
(
$
dir
!==
null
) {
return
$
dir
;
}
$
dir
=
sys_get_temp_dir
() .
'
/gpsmap-tests-
'
.
bin2hex
(
random_bytes
(
8
));
$
plugin
=
$
dir
.
'
/plugins/gpsmap
'
;
$
repo
=
dirname
(
__DIR__
);
@
mkdir
(
$
plugin
.
'
/XML
'
,
0700
,
true
);
@
mkdir
(
$
plugin
.
'
/images/icons
'
,
0700
,
true
);
foreach
(
array
(
'
class
'
,
'
includes
'
)
as
$
link
) {
if
(!
file_exists
(
$
plugin
.
'
/
'
.
$
link
)) {
@
symlink
(
$
repo
.
'
/
'
.
$
link
,
$
plugin
.
'
/
'
.
$
link
);
}
}
register_shutdown_function
(
function
()
use
(
$
dir
) {
gpsmap_test_rmtree
(
$
dir
);
});
return
$
dir
;
}
function
gpsmap_test_rmtree
(
string
$
path
):
void
{
if
(
is_link
(
$
path
) ||
is_file
(
$
path
)) {
@
unlink
(
$
path
);
return
;
}
if
(!
is_dir
(
$
path
)) {
return
;
}
foreach
(
array_diff
(
scandir
(
$
path
),
array
(
'
.
'
,
'
..
'
))
as
$
entry
) {
gpsmap_test_rmtree
(
$
path
.
'
/
'
.
$
entry
);
}
@
rmdir
(
$
path
);
}
/* Replaces the icon fixture folder with exactly the given file names. */
function
gpsmap_test_icons
(
array
$
names
):
string
{
$
dir
=
gpsmap_test_tmpdir
() .
'
/plugins/gpsmap/images/icons
'
;
foreach
(
array_diff
(
scandir
(
$
dir
),
array
(
'
.
'
,
'
..
'
))
as
$
entry
) {
@
unlink
(
$
dir
.
'
/
'
.
$
entry
);
}
foreach
(
$
names
as
$
name
) {
file_put_contents
(
$
dir
.
'
/
'
.
$
name
,
'
x
'
);
}
return
$
dir
;
}
/* Points $config at the scratch root for the duration of a test file. */
function
gpsmap_test_use_tmp_root
():
void
{
$
GLOBALS
[
'
config
'
][
'
base_path
'
] =
gpsmap_test_tmpdir
();
}
/* A stream that accepts fopen then reports a short write, so the truncated
* artefact path can be exercised without /dev/full or a full filesystem. */
class
GpsmapShortWriteStream {
public
$
context
;
public
function
stream_open
(
$
path
,
$
mode
,
$
options
, &
$
opened_path
) {
return
true
; }
public
function
stream_write
(
$
data
) {
return
max
(
0
,
strlen
(
$
data
) -
1
); }
public
function
stream_close
() {
return
true
; }
public
function
stream_flush
() {
return
true
; }
public
function
stream_eof
() {
return
true
; }
public
function
stream_stat
() {
return
array
(); }
public
function
url_stat
(
$
path
,
$
flags
) {
return
array
(); }
public
function
unlink
(
$
path
) {
return
true
; }
public
function
rename
(
$
from
,
$
to
) {
return
false
; }
}
if
(!
in_array
(
'
gpsmapshort
'
,
stream_get_wrappers
(),
true
)) {
stream_wrapper_register
(
'
gpsmapshort
'
,
'
GpsmapShortWriteStream
'
);
}
Back
|
FazBrowse Home
|
New Git URL