Skip to content

Commit cdadd06

Browse files
tests: add tests for InterfaceLAGG model
1 parent aed2e19 commit cdadd06

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace RESTAPI\Tests;
4+
5+
use RESTAPI\Core\Command;
6+
use RESTAPI\Core\TestCase;
7+
use RESTAPI\Models\InterfaceLAGG;
8+
9+
class APIModelsInterfaceLAGGTestCase extends TestCase {
10+
/**
11+
* Checks that the `members` field must be an existing real interface.
12+
*/
13+
public function test_member_field_must_be_real_interface(): void {
14+
# Ensure an error is thrown if the member interface does not exist
15+
$this->assert_throws_response(
16+
response_id: 'INTERFACE_LAGG_MEMBER_DOES_NOT_EXIST',
17+
code: 400,
18+
callable: function () {
19+
$lagg = new InterfaceLAGG();
20+
$lagg->validate_members('nonexistent');
21+
},
22+
);
23+
24+
# Ensure no error is thrown if the member interface exists
25+
$this->assert_does_not_throw(
26+
callable: function () {
27+
$lagg = new InterfaceLAGG();
28+
$lagg->validate_members($this->env['PFREST_OPT1_IF']);
29+
},
30+
);
31+
}
32+
33+
/**
34+
* Checks that the `failovermaster` field must be a member of the LAGG.
35+
*/
36+
public function test_failovermaster_field_must_be_member_of_lagg(): void {
37+
# Ensure an error is thrown if the failover master is not a member
38+
$this->assert_throws_response(
39+
response_id: 'INTERFACE_LAGG_MASTER_NOT_MEMBER',
40+
code: 400,
41+
callable: function () {
42+
$lagg = new InterfaceLAGG();
43+
$lagg->members->value = [$this->env['PFREST_OPT1_IF']];
44+
$lagg->validate_failovermaster('nonexistent');
45+
},
46+
);
47+
48+
# Ensure no error is thrown if the failover master is a member
49+
$this->assert_does_not_throw(
50+
callable: function () {
51+
$lagg = new InterfaceLAGG();
52+
$lagg->members->value = [$this->env['PFREST_OPT1_IF']];
53+
$lagg->validate_failovermaster($this->env['PFREST_OPT1_IF']);
54+
},
55+
);
56+
}
57+
58+
/**
59+
* Ensure that we can create, read, update and delete a LAGG interface.
60+
*/
61+
public function test_crud(): void {
62+
# Create a new LAGG interface
63+
$lagg = new InterfaceLAGG(members: [$this->env['PFREST_OPT1_IF']], proto: 'lacp');
64+
$lagg->create();
65+
$this->assert_is_not_empty($lagg->laggif->value);
66+
$ifconfig_output = new Command("ifconfig {$lagg->laggif->value}");
67+
$this->assert_str_contains($ifconfig_output->output, 'laggproto lacp');
68+
$this->assert_str_contains($ifconfig_output->output, "laggport: {$this->env['PFREST_OPT1_IF']}");
69+
70+
# Update the LAGG interface
71+
$lagg->proto->value = 'failover';
72+
$lagg->update();
73+
$ifconfig_output = new Command("ifconfig {$lagg->laggif->value}");
74+
$this->assert_str_contains($ifconfig_output->output, 'laggproto failover');
75+
$this->assert_str_does_not_contain($ifconfig_output->output, 'laggproto lacp');
76+
77+
# Delete the LAGG interface
78+
$lagg->delete();
79+
$ifconfig_output = new Command("ifconfig {$lagg->laggif->value}");
80+
$this->assert_str_contains($ifconfig_output->output, 'does not exist');
81+
}
82+
83+
/**
84+
* Ensure that we cannot delete a LAGG that is in use.
85+
*/
86+
public function test_cannot_delete_lagg_in_use(): void {
87+
# Create a new LAGG
88+
$lagg = new InterfaceLAGG(members: [$this->env['PFREST_OPT1_IF']], proto: 'lacp');
89+
$lagg->create();
90+
91+
# Mock an interface that is using the LAGG
92+
InterfaceLAGG::set_config('interfaces/opt99', ['if' => $lagg->laggif->value]);
93+
94+
# Ensure we cannot delete the LAGG
95+
$this->assert_throws_response(
96+
response_id: 'INTERFACE_LAGG_CANNOT_BE_DELETED_WHILE_IN_USE',
97+
code: 409,
98+
callable: function () use ($lagg) {
99+
# Try to delete the LAGG
100+
$lagg->delete();
101+
},
102+
);
103+
104+
# Remove the mock interface and actually delete the LAGG
105+
InterfaceLAGG::del_config('interfaces/opt99');
106+
$lagg->delete();
107+
}
108+
}

0 commit comments

Comments
 (0)