Skip to content

Commit 025c3a5

Browse files
tests: added better testing for HAProxy frontends and backends
1 parent 48d8185 commit 025c3a5

File tree

2 files changed

+323
-0
lines changed

2 files changed

+323
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
namespace RESTAPI\Tests;
4+
5+
use RESTAPI\Core\TestCase;
6+
use RESTAPI\Models\HAProxyBackend;
7+
use RESTAPI\Models\HAProxyBackendACL;
8+
use RESTAPI\Models\HAProxyBackendAction;
9+
10+
class APIModelsHAProxyBackendTestCase extends TestCase
11+
{
12+
public array $required_packages = ["pfSense-pkg-haproxy"];
13+
14+
/**
15+
* Ensure that the HAProxyBackend model can be created, updated, and deleted.
16+
*/
17+
public function test_crud(): void {
18+
# Create a new HAProxyBackend model
19+
$backend = new HAProxyBackend(
20+
name: "example_backend",
21+
balance: "",
22+
acls: [
23+
[
24+
"name" => "example_acl",
25+
"expression" => "host_starts_with",
26+
"value" => "example",
27+
"casesensitive" => false,
28+
"not" => false
29+
]
30+
],
31+
actions: [
32+
[
33+
"action" => "http-response_lua",
34+
"acl" => "example_acl",
35+
"lua_function" => "example function"
36+
]
37+
],
38+
check_type: "HTTP",
39+
log_health_checks: false,
40+
httpcheck_method: "OPTIONS",
41+
monitor_uri: "",
42+
monitor_httpversion: "",
43+
agent_checks: false,
44+
agent_port: "",
45+
persist_cookie_enabled: false,
46+
persist_cookie_name: "",
47+
persist_cookie_mode: "passive",
48+
haproxy_cookie_domains: [],
49+
haproxy_cookie_dynamic_cookie_key: "",
50+
persist_sticky_type: "none",
51+
persist_stick_expire: "",
52+
persist_stick_tablesize: "",
53+
persist_stick_cookiename: "",
54+
email_level: "",
55+
email_to: "",
56+
stats_enabled: true,
57+
stats_uri: "/",
58+
stats_scope: [ "." ],
59+
stats_realm: "",
60+
stats_username: "",
61+
stats_admin: "",
62+
stats_node: "NODE1",
63+
stats_desc: "",
64+
stats_refresh: 10,
65+
cookie_attribute_secure: false,
66+
advanced: "",
67+
advanced_backend: "",
68+
transparent_clientip: true,
69+
transparent_interface: "lan"
70+
);
71+
$backend->create();
72+
73+
# Ensure the model was created
74+
$read_backend = HAProxyBackend::query(id: $backend->id);
75+
$this->assert_is_true($read_backend->exists());
76+
$read_backend = $read_backend->first();
77+
$this->assert_equals($read_backend->name->value, "example_backend");
78+
$this->assert_equals($read_backend->balance->value, "");
79+
$this->assert_equals($read_backend->check_type->value, "HTTP");
80+
$this->assert_equals($read_backend->log_health_checks->value, false);
81+
$this->assert_equals($read_backend->httpcheck_method->value, "OPTIONS");
82+
$this->assert_equals($read_backend->monitor_uri->value, "");
83+
$this->assert_equals($read_backend->monitor_httpversion->value, "");
84+
$this->assert_is_empty($read_backend->monitor_username->value, "");
85+
$this->assert_is_empty($read_backend->monitor_domain->value, "");
86+
$this->assert_equals($read_backend->agent_checks->value, false);
87+
$this->assert_is_empty($read_backend->agent_port->value, "");
88+
$this->assert_is_empty($read_backend->persist_cookie_enabled->value, false);
89+
$this->assert_is_empty($read_backend->persist_cookie_name->value, "");
90+
$this->assert_is_empty($read_backend->persist_cookie_mode->value, "passive");
91+
$this->assert_is_empty($read_backend->haproxy_cookie_dynamic_cookie_key->value, "");
92+
$this->assert_equals($read_backend->persist_sticky_type->value, "none");
93+
$this->assert_is_empty($read_backend->persist_stick_expire->value, "");
94+
$this->assert_is_empty($read_backend->persist_stick_tablesize->value, "");
95+
$this->assert_is_empty($read_backend->persist_stick_cookiename->value, "");
96+
$this->assert_is_empty($read_backend->email_level->value, "");
97+
$this->assert_is_empty($read_backend->email_to->value, "");
98+
$this->assert_equals($read_backend->stats_enabled->value, true);
99+
$this->assert_equals($read_backend->stats_uri->value, "/");
100+
$this->assert_equals($read_backend->stats_scope->value, [ "." ]);
101+
$this->assert_is_empty($read_backend->stats_realm->value, "");
102+
$this->assert_is_empty($read_backend->stats_username->value, "");
103+
$this->assert_is_empty($read_backend->stats_admin->value, "");
104+
$this->assert_equals($read_backend->stats_node->value, "NODE1");
105+
$this->assert_is_empty($read_backend->stats_desc->value, "");
106+
$this->assert_equals($read_backend->stats_refresh->value, 10);
107+
$this->assert_equals($read_backend->cookie_attribute_secure->value, false);
108+
$this->assert_is_empty($read_backend->advanced->value, "");
109+
$this->assert_is_empty($read_backend->advanced_backend->value, "");
110+
$this->assert_equals($read_backend->transparent_clientip->value, true);
111+
$this->assert_equals($read_backend->transparent_interface->value, "lan");
112+
113+
# Ensure the HAProxyBackendACL model was created
114+
$this->assert_is_true(HAProxyBackendACL::query(parent_id: $backend->id, id: 0)->exists());
115+
$this->assert_equals($read_backend->acls->value, [
116+
[
117+
"parent_id" => $backend->id,
118+
"id" => 0,
119+
"name" => "example_acl",
120+
"expression" => "host_starts_with",
121+
"value" => "example",
122+
"casesensitive" => false,
123+
"not" => false
124+
]
125+
]);
126+
127+
# Ensure the HAProxyBackendAction model was created
128+
$this->assert_is_true(HAProxyBackendAction::query(parent_id: $backend->id, id: 0)->exists());
129+
$this->assert_equals($read_backend->actions->value[0]["action"], "http-response_lua");
130+
$this->assert_equals($read_backend->actions->value[0]["acl"], "example_acl");
131+
$this->assert_equals($read_backend->actions->value[0]["lua_function"], "example function");
132+
133+
# Update the HAProxyBackend model
134+
$backend->balance->value = "roundrobin";
135+
$backend->update();
136+
$read_backend = HAProxyBackend::query(id: $backend->id)->first();
137+
$this->assert_equals($read_backend->balance->value, "roundrobin");
138+
139+
# Delete the HAProxyBackend model
140+
$backend->delete();
141+
$this->assert_is_false(HAProxyBackend::query(id: $backend->id)->exists());
142+
}
143+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<?php
2+
3+
namespace RESTAPI\Tests;
4+
5+
use RESTAPI\Core\TestCase;
6+
use RESTAPI\Models\HAProxyBackend;
7+
use RESTAPI\Models\HAProxyBackendAction;
8+
use RESTAPI\Models\HAProxyFrontend;
9+
use RESTAPI\Models\HAProxyFrontendACL;
10+
use RESTAPI\Models\HAProxyFrontendAction;
11+
use RESTAPI\Models\HAProxyFrontendAddress;
12+
13+
class APIModelsHAProxyFrontendTestCase extends TestCase
14+
{
15+
private HAProxyBackend $backend;
16+
public array $required_packages = ["pfSense-pkg-haproxy"];
17+
18+
/**
19+
* Setup the test case.
20+
*/
21+
public function setup(): void {
22+
$this->backend = new HAProxyBackend(
23+
name: "example_backend",
24+
balance: "",
25+
acls: [
26+
[
27+
"name" => "example_acl",
28+
"expression" => "host_starts_with",
29+
"value" => "example",
30+
"casesensitive" => false,
31+
"not" => false
32+
]
33+
],
34+
actions: [
35+
[
36+
"action" => "http-response_lua",
37+
"acl" => "example_acl",
38+
"lua_function" => "example function"
39+
]
40+
],
41+
check_type: "HTTP",
42+
log_health_checks: false,
43+
httpcheck_method: "OPTIONS",
44+
monitor_uri: "",
45+
monitor_httpversion: "",
46+
monitor_username: "",
47+
monitor_domain: "",
48+
agent_checks: false,
49+
agent_port: "",
50+
persist_cookie_enabled: false,
51+
persist_cookie_name: "",
52+
persist_cookie_mode: "passive",
53+
haproxy_cookie_domains: [],
54+
haproxy_cookie_dynamic_cookie_key: "",
55+
persist_sticky_type: "none",
56+
persist_stick_expire: "",
57+
persist_stick_tablesize: "",
58+
persist_stick_cookiename: "",
59+
email_level: "",
60+
email_to: "",
61+
stats_enabled: true,
62+
stats_uri: "/",
63+
stats_scope: [ "." ],
64+
stats_realm: "",
65+
stats_username: "",
66+
stats_admin: "",
67+
stats_node: "NODE1",
68+
stats_desc: "",
69+
stats_refresh: 10,
70+
cookie_attribute_secure: false,
71+
advanced: "",
72+
advanced_backend: "",
73+
transparent_clientip: false,
74+
transparent_interface: "lan"
75+
);
76+
$this->backend->create();
77+
}
78+
79+
/**
80+
* Tear down the test case.
81+
*/
82+
public function teardown(): void
83+
{
84+
$this->backend->delete();
85+
}
86+
87+
/**
88+
* Check that we can create, update and deleted HAProxy Frontend objects.
89+
*/
90+
public function test_crud(): void {
91+
$frontend = new HAProxyFrontend(
92+
name: "example_multipledomains",
93+
status: "active",
94+
a_extaddr: [
95+
[
96+
"extaddr" => "any_ipv4",
97+
"extaddr_port" => "80",
98+
"extaddr_ssl" => false
99+
]
100+
],
101+
type: "http",
102+
ha_acls: [
103+
[
104+
"name" => "mail_acl",
105+
"expression" => "host_matches",
106+
"value" => "mail.domain.tld",
107+
"casesensitive" => false,
108+
"not" => false
109+
]
110+
],
111+
a_actionitems: [
112+
[
113+
"action" => "tcp-request_content_lua",
114+
"acl" => "mail_acl",
115+
"lua_function" => "example function"
116+
]
117+
],
118+
backend_serverpool: "example_backend",
119+
socket_stats: false,
120+
dontlognull: false,
121+
dontlog_normal: false,
122+
log_separate_errors: false,
123+
log_detailed: false,
124+
client_timeout: 30000,
125+
forwardfor: false,
126+
httpclose: "http-keep-alive",
127+
advanced: ""
128+
);
129+
$frontend->create();
130+
131+
# Ensure the object was created successful and we can read it and its child objects
132+
$read_frontend = HAProxyFrontend::query(id: $frontend->id);
133+
$this->assert_is_true($read_frontend->exists());
134+
$read_frontend = $read_frontend->first();
135+
$this->assert_equals($read_frontend->name->value, "example_multipledomains");
136+
$this->assert_equals($read_frontend->status->value, "active");
137+
$this->assert_equals($read_frontend->type->value, "http");
138+
$this->assert_equals($read_frontend->backend_serverpool->value, "example_backend");
139+
$this->assert_equals($read_frontend->client_timeout->value, 30000);
140+
$this->assert_equals($read_frontend->httpclose->value, "http-keep-alive");
141+
142+
# Ensure the nested HAProxyBackend object linked up with the foreign object correctly
143+
$this->assert_is_true($read_frontend->backend_serverpool->get_related_model() instanceof HAProxyBackend);
144+
145+
# Ensure the nested HAProxyFrontendAddress object was created successful
146+
$this->assert_is_not_empty($read_frontend->a_extaddr->value);
147+
$this->assert_is_true(HAProxyFrontendAddress::query(parent_id: $frontend->id, id: 0)->exists());
148+
$this->assert_equals($read_frontend->a_extaddr->value[0]["extaddr"], "any_ipv4");
149+
$this->assert_equals($read_frontend->a_extaddr->value[0]["extaddr_port"], "80");
150+
$this->assert_equals($read_frontend->a_extaddr->value[0]["extaddr_ssl"], false);
151+
152+
# Ensure the nested HAProxyFrontendACL object was created successful
153+
$this->assert_is_true(HAProxyFrontendACL::query(parent_id: $frontend->id, id: 0)->exists());
154+
$this->assert_is_not_empty($read_frontend->ha_acls->value);
155+
$this->assert_equals($read_frontend->ha_acls->value[0]["name"], "mail_acl");
156+
$this->assert_equals($read_frontend->ha_acls->value[0]["expression"], "host_matches");
157+
$this->assert_equals($read_frontend->ha_acls->value[0]["value"], "mail.domain.tld");
158+
$this->assert_equals($read_frontend->ha_acls->value[0]["casesensitive"], false);
159+
$this->assert_equals($read_frontend->ha_acls->value[0]["not"], false);
160+
161+
# Ensure the nested HAProxyFrontendAction object was created successful, and the dynamic internal_name was set
162+
$this->assert_is_not_empty($read_frontend->a_actionitems->value);
163+
$action = HAProxyFrontendAction::query(parent_id: $frontend->id, id: 0);
164+
$this->assert_is_true($action->exists());
165+
$this->assert_equals($action->first()->lua_function->internal_name, "tcp-request_content_lualua-function");
166+
$this->assert_equals($read_frontend->a_actionitems->value[0]["action"], "tcp-request_content_lua");
167+
$this->assert_equals($read_frontend->a_actionitems->value[0]["acl"], "mail_acl");
168+
$this->assert_equals($read_frontend->a_actionitems->value[0]["lua_function"], "example function");
169+
170+
# Update the object and ensure the changes were successful
171+
$frontend->status->value = "disabled";
172+
$frontend->update();
173+
$read_frontend = HAProxyFrontend::query(id: $frontend->id)->first();
174+
$this->assert_equals($read_frontend->status->value, "disabled");
175+
176+
# Delete the object and ensure it was deleted
177+
$frontend->delete();
178+
$this->assert_is_false(HAProxyFrontend::query(id: $frontend)->exists());
179+
}
180+
}

0 commit comments

Comments
 (0)