|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RESTAPI\Tests; |
| 4 | + |
| 5 | +use RESTAPI\Auth\BasicAuth; |
| 6 | +use RESTAPI\Core\Command; |
| 7 | +use RESTAPI\Core\TestCase; |
| 8 | +use RESTAPI\Models\DHCPRelay; |
| 9 | +use RESTAPI\Models\DHCPServer; |
| 10 | + |
| 11 | +class APIModelsDHCPRelayTestCase extends TestCase { |
| 12 | + /** |
| 13 | + * Disables all running DHCPServers. |
| 14 | + */ |
| 15 | + public function disable_dhcp_servers(bool $apply = false): void { |
| 16 | + foreach (DHCPServer::query(enable: true)->model_objects as $dhcp_server) { |
| 17 | + $dhcp_server->enable->value = false; |
| 18 | + $dhcp_server->update(); |
| 19 | + } |
| 20 | + if ($apply) { |
| 21 | + (new DHCPServer())->apply(); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Checks that the DHCPRelay cannot be enabled if any DHCPServer is already enabled. |
| 27 | + */ |
| 28 | + public function test_dhcp_relay_cannot_enable_if_dhcp_server_enabled(): void { |
| 29 | + # Disable all running DHCPServers |
| 30 | + $this->disable_dhcp_servers(); |
| 31 | + |
| 32 | + # Ensure we can enable the DHCPRelay |
| 33 | + $this->assert_does_not_throw( |
| 34 | + callable: function () { |
| 35 | + $dhcp_relay = new DHCPRelay(enable: true); |
| 36 | + $dhcp_relay->update(); |
| 37 | + }, |
| 38 | + ); |
| 39 | + |
| 40 | + # Disable the DHCPRelay |
| 41 | + $dhcp_relay = new DHCPRelay(enable: false); |
| 42 | + $dhcp_relay->update(); |
| 43 | + |
| 44 | + # Enable a DHCPServer |
| 45 | + $dhcp_server = new DHCPServer(id: 'lan', enable: true); |
| 46 | + $dhcp_server->update(); |
| 47 | + |
| 48 | + # Ensure we cannot enable the DHCPRelay |
| 49 | + $this->assert_throws_response( |
| 50 | + response_id: 'DHCP_RELAY_CANNOT_BE_ENABLED_WHILE_DHCP_SERVER_IS_ENABLED', |
| 51 | + code: 409, |
| 52 | + callable: function () use ($dhcp_relay) { |
| 53 | + $dhcp_relay = new DHCPRelay(enable: true); |
| 54 | + $dhcp_relay->update(); |
| 55 | + }, |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Checks that the to_internal() method correctly appends the '_vip' prefix to the 'carpstatusvip' field. |
| 61 | + */ |
| 62 | + public function test_dhcp_relay_to_internal_carpstatusvip(): void { |
| 63 | + $dhcp_relay = new DHCPRelay(carpstatusvip: 'examplecarpvip'); |
| 64 | + $this->assert_equals($dhcp_relay->to_internal()['carpstatusvip'], '_vipexamplecarpvip'); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Checks that the from_internal_carpstatusvip() method correctly removes the '_vip' prefix from the 'carpstatusvip' |
| 69 | + * field when the value is loaded from the internal data. |
| 70 | + */ |
| 71 | + public function test_dhcp_relay_from_internal_carpstatusvip(): void { |
| 72 | + $dhcp_relay = new DHCPRelay(); |
| 73 | + $this->assert_equals($dhcp_relay->from_internal_carpstatusvip('_vipexamplecarpvip'), 'examplecarpvip'); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Checks that the DHCP relay service is running after enabling and disabled after disabling the DHCPRelay. |
| 78 | + */ |
| 79 | + public function test_dhcp_relay_service_running(): void { |
| 80 | + # Disable all running DHCPServers |
| 81 | + $this->disable_dhcp_servers(apply: true); |
| 82 | + |
| 83 | + # Ensure the DHCPRelay is enabled |
| 84 | + $dhcp_relay = new DHCPRelay(enable: true, server: ['1.2.3.4']); |
| 85 | + $dhcp_relay->update(); |
| 86 | + |
| 87 | + # Ensure the DHCP relay service is running with the correct arguments |
| 88 | + $dhcrelay_process = new Command('ps aux | grep dhcrelay'); |
| 89 | + $lan_if = $this->env['PFREST_LAN_IF']; |
| 90 | + $wan_if = $this->env['PFREST_WAN_IF']; |
| 91 | + $this->assert_str_contains( |
| 92 | + $dhcrelay_process->output, |
| 93 | + "/usr/local/sbin/dhcrelay -id $lan_if -iu $wan_if -a -m replace 1.2.3.4", |
| 94 | + ); |
| 95 | + |
| 96 | + # Disable the DHCPRelay |
| 97 | + $dhcp_relay = new DHCPRelay(enable: false); |
| 98 | + $dhcp_relay->update(); |
| 99 | + |
| 100 | + # Ensure the DHCP relay service is not running |
| 101 | + $dhcrelay_pid = new Command('pgrep dhcrelay'); |
| 102 | + $this->assert_equals($dhcrelay_pid->result_code, 1); |
| 103 | + |
| 104 | + # Re-enable the DHCPServer on LAN |
| 105 | + $dhcp_server = new DHCPServer(id: 'lan', enable: true); |
| 106 | + $dhcp_server->update(apply: true); |
| 107 | + } |
| 108 | +} |
0 commit comments