@@ -20,23 +20,46 @@ func TestRuleDisableCommand(t *testing.T) {
2020 currentRules := & upcloud.FirewallRules {
2121 FirewallRules : []upcloud.FirewallRule {
2222 {
23- Position : 1 ,
24- Direction : "in" ,
25- Action : "accept" ,
26- Protocol : "tcp" ,
27- Comment : "Test rule" ,
23+ Position : 1 ,
24+ Direction : "in" ,
25+ Action : "accept" ,
26+ Protocol : "tcp" ,
27+ DestinationAddressStart : "0.0.0.0" ,
28+ DestinationAddressEnd : "255.255.255.255" ,
29+ SourceAddressStart : "0.0.0.0" ,
30+ SourceAddressEnd : "255.255.255.255" ,
31+ Comment : "Test rule" ,
2832 },
2933 {
30- Position : 2 ,
31- Direction : "in" ,
32- Action : "accept" ,
33- Protocol : "tcp" ,
34+ Position : 2 ,
35+ Direction : "in" ,
36+ Action : "accept" ,
37+ Protocol : "tcp" ,
38+ DestinationAddressStart : "0.0.0.0" ,
39+ DestinationAddressEnd : "255.255.255.255" ,
40+ SourceAddressStart : "0.0.0.0" ,
41+ SourceAddressEnd : "255.255.255.255" ,
3442 },
3543 {
36- Position : 5 ,
37- Direction : "out" ,
38- Action : "accept" ,
39- Protocol : "udp" ,
44+ Position : 3 ,
45+ Direction : "in" ,
46+ Action : "drop" ,
47+ Protocol : "tcp" ,
48+ DestinationAddressStart : "0.0.0.0" ,
49+ DestinationAddressEnd : "255.255.255.255" ,
50+ SourceAddressStart : "0.0.0.0" ,
51+ SourceAddressEnd : "255.255.255.255" ,
52+ Comment : "Catch-all drop rule" ,
53+ },
54+ {
55+ Position : 4 ,
56+ Direction : "out" ,
57+ Action : "accept" ,
58+ Protocol : "udp" ,
59+ DestinationAddressStart : "0.0.0.0" ,
60+ DestinationAddressEnd : "255.255.255.255" ,
61+ SourceAddressStart : "0.0.0.0" ,
62+ SourceAddressEnd : "255.255.255.255" ,
4063 },
4164 },
4265 }
@@ -52,19 +75,19 @@ func TestRuleDisableCommand(t *testing.T) {
5275 name : "Missing position flag" ,
5376 flags : []string {},
5477 arg : serverUUID ,
55- expectedErr : "at least one filter must be specified " ,
78+ expectedErr : "would disable 2 firewall rules (exceeds skip-confirmation=1) " ,
5679 },
5780 {
5881 name : "Invalid position - too low" ,
5982 flags : []string {"--position" , "0" },
6083 arg : serverUUID ,
61- expectedErr : "at least one filter must be specified " ,
84+ expectedErr : "would disable 2 firewall rules (exceeds skip-confirmation=1) " ,
6285 },
6386 {
6487 name : "Invalid position - too high" ,
6588 flags : []string {"--position" , "1001" },
6689 arg : serverUUID ,
67- expectedErr : "invalid position (1-1000 allowed) " ,
90+ expectedErr : "no enabled firewall rules matched the specified filters " ,
6891 },
6992 {
7093 name : "Successfully disable rule at position 2" ,
@@ -75,20 +98,19 @@ func TestRuleDisableCommand(t *testing.T) {
7598 ServerUUID : serverUUID ,
7699 })
77100
78- mService .AssertCalled (t , "CreateFirewallRules" , mock .MatchedBy (func (req interface {}) bool {
79- r , ok := req .(* request.CreateFirewallRulesRequest )
101+ // Should delete the old rule at position 2
102+ mService .AssertCalled (t , "DeleteFirewallRule" , & request.DeleteFirewallRuleRequest {
103+ ServerUUID : serverUUID ,
104+ Position : 2 ,
105+ })
106+
107+ // Should create the rule after catch-all (position 4)
108+ mService .AssertCalled (t , "CreateFirewallRule" , mock .MatchedBy (func (req interface {}) bool {
109+ r , ok := req .(* request.CreateFirewallRuleRequest )
80110 if ! ok {
81111 return false
82112 }
83- if r .ServerUUID != serverUUID {
84- return false
85- }
86- for _ , rule := range r .FirewallRules {
87- if rule .Position == 2 {
88- return rule .Action == upcloud .FirewallRuleActionDrop
89- }
90- }
91- return false
113+ return r .ServerUUID == serverUUID && r .FirewallRule .Position == 4
92114 }))
93115 },
94116 },
@@ -97,34 +119,37 @@ func TestRuleDisableCommand(t *testing.T) {
97119 flags : []string {"--position" , "1" },
98120 arg : serverUUID ,
99121 checkMocks : func (t * testing.T , mService * smock.Service ) {
100- mService .AssertCalled (t , "CreateFirewallRules" , mock .MatchedBy (func (req interface {}) bool {
101- r , ok := req .(* request.CreateFirewallRulesRequest )
122+ mService .AssertCalled (t , "GetFirewallRules" , & request.GetFirewallRulesRequest {
123+ ServerUUID : serverUUID ,
124+ })
125+
126+ // Should delete the old rule at position 1
127+ mService .AssertCalled (t , "DeleteFirewallRule" , & request.DeleteFirewallRuleRequest {
128+ ServerUUID : serverUUID ,
129+ Position : 1 ,
130+ })
131+
132+ // Should create the rule after catch-all (position 4)
133+ mService .AssertCalled (t , "CreateFirewallRule" , mock .MatchedBy (func (req interface {}) bool {
134+ r , ok := req .(* request.CreateFirewallRuleRequest )
102135 if ! ok {
103136 return false
104137 }
105- if r .ServerUUID != serverUUID {
106- return false
107- }
108- for _ , rule := range r .FirewallRules {
109- if rule .Position == 1 {
110- return rule .Action == upcloud .FirewallRuleActionDrop
111- }
112- }
113- return false
138+ return r .ServerUUID == serverUUID && r .FirewallRule .Position == 4
114139 }))
115140 },
116141 },
117142 {
118143 name : "Rule position not found" ,
119144 flags : []string {"--position" , "99" },
120145 arg : serverUUID ,
121- expectedErr : "firewall rule at position 99 not found on server " ,
146+ expectedErr : "no enabled firewall rules matched the specified filters " ,
122147 },
123148 {
124149 name : "Skip confirmation set to 0 requires confirmation for single rule" ,
125150 flags : []string {"--comment" , "Test" , "--skip-confirmation" , "0" },
126151 arg : serverUUID ,
127- expectedErr : "would disable 1 rules (exceeds skip-confirmation=0)" ,
152+ expectedErr : "would disable 1 firewall rules (exceeds skip-confirmation=0)" ,
128153 },
129154 } {
130155 t .Run (test .name , func (t * testing.T ) {
@@ -135,7 +160,8 @@ func TestRuleDisableCommand(t *testing.T) {
135160 return ok && r .ServerUUID == serverUUID
136161 })).Return (currentRules , nil )
137162
138- mService .On ("CreateFirewallRules" , mock .Anything ).Return (nil )
163+ mService .On ("DeleteFirewallRule" , mock .Anything ).Return (nil ).Maybe ()
164+ mService .On ("CreateFirewallRule" , mock .Anything ).Return (& upcloud.FirewallRule {}, nil ).Maybe ()
139165
140166 conf := config .New ()
141167 cc := commands .BuildCommand (RuleDisableCommand (), nil , conf )
0 commit comments