@@ -7,14 +7,41 @@ use RESTAPI\Core\TestCase;
77use RESTAPI \Models \CARP ;
88use RESTAPI \Models \Table ;
99use RESTAPI \Models \VirtualIP ;
10+ use RESTAPI \Responses \ServerError ;
1011
1112class APIModelsTableTestCase extends TestCase {
1213 /**
13- * Checks that we can successful retrieve the list of available table names.
14+ * Adds a new table using pfctl directly and waits until it is readable.
15+ * @param string $table_name The name of the table to add.
16+ * @param array $entries The entries to add to the table.
17+ * @return Command The Command object representing the result of pfctl command.
18+ */
19+ public function add_table (string $ table_name , array $ entries ): Command {
20+ $ entries_str = implode (' ' , $ entries );
21+ $ add_cmd = new Command ("/sbin/pfctl -t $ table_name -T add $ entries_str " );
22+
23+ # Wait until the table is readable by pfctl
24+ foreach (range (1 , 5 ) as $ i ) {
25+ $ show_cmd = new Command ("/sbin/pfctl -t $ table_name -T show " );
26+ if (str_contains ($ show_cmd ->output , $ entries [0 ])) {
27+ return $ add_cmd ;
28+ }
29+ sleep (1 );
30+ }
31+
32+ throw new ServerError (
33+ message: "Failed to add table $ table_name with entries " ,
34+ response_id: "API_MODELS_TABLE_TEST_CASE_ADD_TABLE_FAILED "
35+ );
36+
37+ }
38+
39+ /**
40+ * Checks that we can successfully retrieve the list of available table names.
1441 */
1542 public function test_get_available_table_names (): void {
1643 # Create a new pf table to test with
17- new Command ( ' /sbin/pfctl -t pfrest_test_table -T add 1.2.3.4' );
44+ $ this -> add_table (table_name: " pfrest_test_table " , entries: [ " 1.2.3.4 " ] );
1845
1946 # Ensure get_available_table_names returns the test table
2047 $ table = new Table ();
@@ -38,7 +65,7 @@ class APIModelsTableTestCase extends TestCase {
3865 */
3966 public function test_read (): void {
4067 # Create a new pf table to test with
41- new Command ( ' /sbin/pfctl -t pfrest_test_table -T add 1.2.3.4 4.3.2.1' );
68+ $ this -> add_table (table_name: " pfrest_test_table " , entries: [ " 1.2.3.4 " , " 4.3.2.1 " ] );
4269
4370 # Load the Table model
4471 $ table = new Table (id: 'pfrest_test_table ' );
@@ -55,7 +82,7 @@ class APIModelsTableTestCase extends TestCase {
5582 */
5683 public function test_delete (): void {
5784 # Create a new pf table to test with
58- new Command ( ' /sbin/pfctl -t pfrest_test_table -T add 1.2.3.4 4.3.2.1' );
85+ $ this -> add_table (table_name: " pfrest_test_table " , entries: [ " 1.2.3.4 " , " 4.3.2.1 " ] );
5986 sleep (1 );
6087
6188 # Load the Table model
0 commit comments