|
| 1 | +package serverfirewall |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" |
| 7 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/config" |
| 8 | + smock "github.com/UpCloudLtd/upcloud-cli/v3/internal/mock" |
| 9 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/mockexecute" |
| 10 | + |
| 11 | + "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud" |
| 12 | + "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/mock" |
| 15 | +) |
| 16 | + |
| 17 | +func TestRuleDisableCommand(t *testing.T) { |
| 18 | + serverUUID := "1fdfda29-ead1-4855-b71f-1e33eb2ca9de" |
| 19 | + |
| 20 | + currentRules := &upcloud.FirewallRules{ |
| 21 | + FirewallRules: []upcloud.FirewallRule{ |
| 22 | + { |
| 23 | + Position: 1, |
| 24 | + Direction: "in", |
| 25 | + Action: "accept", |
| 26 | + Protocol: "tcp", |
| 27 | + }, |
| 28 | + { |
| 29 | + Position: 2, |
| 30 | + Direction: "in", |
| 31 | + Action: "accept", |
| 32 | + Protocol: "tcp", |
| 33 | + }, |
| 34 | + { |
| 35 | + Position: 5, |
| 36 | + Direction: "out", |
| 37 | + Action: "accept", |
| 38 | + Protocol: "udp", |
| 39 | + }, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + for _, test := range []struct { |
| 44 | + name string |
| 45 | + flags []string |
| 46 | + arg string |
| 47 | + expectedErr string |
| 48 | + checkMocks func(*testing.T, *smock.Service) |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "Missing position flag", |
| 52 | + flags: []string{}, |
| 53 | + arg: serverUUID, |
| 54 | + expectedErr: `required flag(s) "position" not set`, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Invalid position - too low", |
| 58 | + flags: []string{"--position", "0"}, |
| 59 | + arg: serverUUID, |
| 60 | + expectedErr: "invalid position (1-1000 allowed)", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "Invalid position - too high", |
| 64 | + flags: []string{"--position", "1001"}, |
| 65 | + arg: serverUUID, |
| 66 | + expectedErr: "invalid position (1-1000 allowed)", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "Successfully disable rule at position 2", |
| 70 | + flags: []string{"--position", "2"}, |
| 71 | + arg: serverUUID, |
| 72 | + checkMocks: func(t *testing.T, mService *smock.Service) { |
| 73 | + mService.AssertCalled(t, "GetFirewallRules", &request.GetFirewallRulesRequest{ |
| 74 | + ServerUUID: serverUUID, |
| 75 | + }) |
| 76 | + |
| 77 | + mService.AssertCalled(t, "CreateFirewallRules", mock.MatchedBy(func(req interface{}) bool { |
| 78 | + r, ok := req.(*request.CreateFirewallRulesRequest) |
| 79 | + if !ok { |
| 80 | + return false |
| 81 | + } |
| 82 | + if r.ServerUUID != serverUUID { |
| 83 | + return false |
| 84 | + } |
| 85 | + for _, rule := range r.FirewallRules { |
| 86 | + if rule.Position == 2 { |
| 87 | + return rule.Action == upcloud.FirewallRuleActionDrop |
| 88 | + } |
| 89 | + } |
| 90 | + return false |
| 91 | + })) |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "Successfully disable rule at position 1", |
| 96 | + flags: []string{"--position", "1"}, |
| 97 | + arg: serverUUID, |
| 98 | + checkMocks: func(t *testing.T, mService *smock.Service) { |
| 99 | + mService.AssertCalled(t, "CreateFirewallRules", mock.MatchedBy(func(req interface{}) bool { |
| 100 | + r, ok := req.(*request.CreateFirewallRulesRequest) |
| 101 | + if !ok { |
| 102 | + return false |
| 103 | + } |
| 104 | + if r.ServerUUID != serverUUID { |
| 105 | + return false |
| 106 | + } |
| 107 | + for _, rule := range r.FirewallRules { |
| 108 | + if rule.Position == 1 { |
| 109 | + return rule.Action == upcloud.FirewallRuleActionDrop |
| 110 | + } |
| 111 | + } |
| 112 | + return false |
| 113 | + })) |
| 114 | + }, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "Rule position not found", |
| 118 | + flags: []string{"--position", "99"}, |
| 119 | + arg: serverUUID, |
| 120 | + expectedErr: "firewall rule at position 99 not found on server", |
| 121 | + }, |
| 122 | + } { |
| 123 | + t.Run(test.name, func(t *testing.T) { |
| 124 | + mService := smock.Service{} |
| 125 | + |
| 126 | + mService.On("GetFirewallRules", mock.MatchedBy(func(req interface{}) bool { |
| 127 | + r, ok := req.(*request.GetFirewallRulesRequest) |
| 128 | + return ok && r.ServerUUID == serverUUID |
| 129 | + })).Return(currentRules, nil) |
| 130 | + |
| 131 | + mService.On("CreateFirewallRules", mock.Anything).Return(nil) |
| 132 | + |
| 133 | + conf := config.New() |
| 134 | + cc := commands.BuildCommand(RuleDisableCommand(), nil, conf) |
| 135 | + |
| 136 | + cc.Cobra().SetArgs(append(test.flags, test.arg)) |
| 137 | + _, err := mockexecute.MockExecute(cc, &mService, conf) |
| 138 | + |
| 139 | + if test.expectedErr != "" { |
| 140 | + assert.ErrorContains(t, err, test.expectedErr) |
| 141 | + } else { |
| 142 | + assert.NoError(t, err) |
| 143 | + if test.checkMocks != nil { |
| 144 | + test.checkMocks(t, &mService) |
| 145 | + } |
| 146 | + } |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
0 commit comments