-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
1,592 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package firewall | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/hetznercloud/cli/internal/testutil" | ||
"github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
) | ||
|
||
func TestAddRule(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := AddRuleCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
firewall := &hcloud.Firewall{ | ||
ID: 123, | ||
Name: "test", | ||
} | ||
|
||
fx.Client.FirewallClient.EXPECT(). | ||
Get(gomock.Any(), "test"). | ||
Return(firewall, nil, nil) | ||
fx.Client.FirewallClient.EXPECT(). | ||
SetRules(gomock.Any(), firewall, hcloud.FirewallSetRulesOpts{ | ||
Rules: []hcloud.FirewallRule{{ | ||
Direction: hcloud.FirewallRuleDirectionIn, | ||
SourceIPs: []net.IPNet{{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}}, | ||
DestinationIPs: nil, | ||
Protocol: hcloud.FirewallRuleProtocolTCP, | ||
Port: hcloud.Ptr("80"), | ||
Description: hcloud.Ptr("http"), | ||
}}, | ||
}). | ||
Return([]*hcloud.Action{{ID: 123}, {ID: 321}}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 123}, {ID: 321}}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"--direction", "in", "--protocol", "tcp", "--source-ips", "0.0.0.0/0", "--port", "80", "--description", "http", "test"}) | ||
|
||
expOut := "Firewall Rules for Firewall 123 updated\n" | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package firewall | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/hetznercloud/cli/internal/testutil" | ||
"github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
) | ||
|
||
func TestApplyToServer(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := ApplyToResourceCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
firewall := &hcloud.Firewall{ | ||
ID: 123, | ||
Name: "test", | ||
} | ||
|
||
fx.Client.FirewallClient.EXPECT(). | ||
Get(gomock.Any(), "test"). | ||
Return(firewall, nil, nil) | ||
fx.Client.ServerClient.EXPECT(). | ||
Get(gomock.Any(), "my-server"). | ||
Return(&hcloud.Server{ID: 456}, nil, nil) | ||
fx.Client.FirewallClient.EXPECT(). | ||
ApplyResources(gomock.Any(), firewall, []hcloud.FirewallResource{{ | ||
Type: hcloud.FirewallResourceTypeServer, | ||
Server: &hcloud.FirewallResourceServer{ | ||
ID: 456, | ||
}, | ||
}}). | ||
Return([]*hcloud.Action{{ID: 123}, {ID: 321}}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 123}, {ID: 321}}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"--type", "server", "--server", "my-server", "test"}) | ||
|
||
expOut := "Firewall 123 applied\n" | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} | ||
|
||
func TestApplyToLabelSelector(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := ApplyToResourceCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
firewall := &hcloud.Firewall{ | ||
ID: 123, | ||
Name: "test", | ||
} | ||
|
||
fx.Client.FirewallClient.EXPECT(). | ||
Get(gomock.Any(), "test"). | ||
Return(firewall, nil, nil) | ||
fx.Client.FirewallClient.EXPECT(). | ||
ApplyResources(gomock.Any(), firewall, []hcloud.FirewallResource{{ | ||
Type: hcloud.FirewallResourceTypeLabelSelector, | ||
LabelSelector: &hcloud.FirewallResourceLabelSelector{ | ||
Selector: "my-label", | ||
}, | ||
}}). | ||
Return([]*hcloud.Action{{ID: 123}, {ID: 321}}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 123}, {ID: 321}}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"--type", "label_selector", "--label-selector", "my-label", "test"}) | ||
|
||
expOut := "Firewall 123 applied\n" | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package firewall | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/hetznercloud/cli/internal/testutil" | ||
"github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
) | ||
|
||
func TestDeleteRule(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := DeleteRuleCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
firewall := &hcloud.Firewall{ | ||
ID: 123, | ||
Name: "test", | ||
Rules: []hcloud.FirewallRule{{ | ||
Direction: hcloud.FirewallRuleDirectionIn, | ||
SourceIPs: []net.IPNet{{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}}, | ||
DestinationIPs: []net.IPNet{}, | ||
Protocol: hcloud.FirewallRuleProtocolTCP, | ||
Port: hcloud.Ptr("80"), | ||
Description: hcloud.Ptr("http"), | ||
}}, | ||
} | ||
|
||
fx.Client.FirewallClient.EXPECT(). | ||
Get(gomock.Any(), "test"). | ||
Return(firewall, nil, nil) | ||
fx.Client.FirewallClient.EXPECT(). | ||
SetRules(gomock.Any(), firewall, hcloud.FirewallSetRulesOpts{Rules: nil}). | ||
Return([]*hcloud.Action{{ID: 123}, {ID: 321}}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 123}, {ID: 321}}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"--direction", "in", "--protocol", "tcp", "--source-ips", "0.0.0.0/0", "--port", "80", "--description", "http", "test"}) | ||
|
||
expOut := "Firewall Rules for Firewall 123 updated\n" | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package firewall | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/hetznercloud/cli/internal/testutil" | ||
"github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
) | ||
|
||
func TestRemoveFromServer(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := RemoveFromResourceCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
firewall := &hcloud.Firewall{ | ||
ID: 123, | ||
Name: "test", | ||
} | ||
|
||
fx.Client.FirewallClient.EXPECT(). | ||
Get(gomock.Any(), "test"). | ||
Return(firewall, nil, nil) | ||
fx.Client.ServerClient.EXPECT(). | ||
Get(gomock.Any(), "my-server"). | ||
Return(&hcloud.Server{ID: 456}, nil, nil) | ||
fx.Client.FirewallClient.EXPECT(). | ||
RemoveResources(gomock.Any(), firewall, []hcloud.FirewallResource{{ | ||
Type: hcloud.FirewallResourceTypeServer, | ||
Server: &hcloud.FirewallResourceServer{ | ||
ID: 456, | ||
}, | ||
}}). | ||
Return([]*hcloud.Action{{ID: 123}, {ID: 321}}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 123}, {ID: 321}}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"--type", "server", "--server", "my-server", "test"}) | ||
|
||
expOut := "Firewall 123 applied\n" | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} | ||
|
||
func TestRemoveFromLabelSelector(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := RemoveFromResourceCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
firewall := &hcloud.Firewall{ | ||
ID: 123, | ||
Name: "test", | ||
} | ||
|
||
fx.Client.FirewallClient.EXPECT(). | ||
Get(gomock.Any(), "test"). | ||
Return(firewall, nil, nil) | ||
fx.Client.FirewallClient.EXPECT(). | ||
RemoveResources(gomock.Any(), firewall, []hcloud.FirewallResource{{ | ||
Type: hcloud.FirewallResourceTypeLabelSelector, | ||
LabelSelector: &hcloud.FirewallResourceLabelSelector{ | ||
Selector: "my-label", | ||
}, | ||
}}). | ||
Return([]*hcloud.Action{{ID: 123}, {ID: 321}}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 123}, {ID: 321}}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"--type", "label_selector", "--label-selector", "my-label", "test"}) | ||
|
||
expOut := "Firewall 123 applied\n" | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package firewall | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/hetznercloud/cli/internal/testutil" | ||
"github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
) | ||
|
||
func TestReplaceRules(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := ReplaceRulesCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
firewall := &hcloud.Firewall{ | ||
ID: 123, | ||
Name: "test", | ||
} | ||
|
||
fx.Client.FirewallClient.EXPECT(). | ||
Get(gomock.Any(), "test"). | ||
Return(firewall, nil, nil) | ||
fx.Client.FirewallClient.EXPECT(). | ||
SetRules(gomock.Any(), firewall, hcloud.FirewallSetRulesOpts{ | ||
Rules: []hcloud.FirewallRule{ | ||
{ | ||
Direction: hcloud.FirewallRuleDirectionIn, | ||
SourceIPs: []net.IPNet{ | ||
{IP: net.IP{28, 239, 13, 1}, Mask: net.IPMask{255, 255, 255, 255}}, | ||
{IP: net.IP{28, 239, 14, 0}, Mask: net.IPMask{255, 255, 255, 0}}, | ||
{ | ||
IP: net.IP{0xff, 0x21, 0x1e, 0xac, 0x9a, 0x3b, 0xee, 0x58, 0x05, 0xca, 0x99, 0x0c, 0x8b, 0xc9, 0xc0, 0x3b}, | ||
Mask: net.IPMask{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, | ||
}, | ||
}, | ||
DestinationIPs: nil, | ||
Protocol: hcloud.FirewallRuleProtocolTCP, | ||
Port: hcloud.Ptr("80"), | ||
Description: hcloud.Ptr("Allow port 80"), | ||
}, | ||
{ | ||
Direction: hcloud.FirewallRuleDirectionIn, | ||
SourceIPs: []net.IPNet{ | ||
{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}, | ||
{ | ||
IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | ||
Mask: net.IPMask{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | ||
}, | ||
}, | ||
DestinationIPs: nil, | ||
Protocol: hcloud.FirewallRuleProtocolTCP, | ||
Port: hcloud.Ptr("443"), | ||
Description: hcloud.Ptr("Allow port 443"), | ||
}, | ||
}, | ||
}). | ||
Return([]*hcloud.Action{{ID: 123}, {ID: 321}}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 123}, {ID: 321}}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"--rules-file", "testdata/rules.json", "test"}) | ||
|
||
expOut := "Firewall Rules for Firewall 123 updated\n" | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} |
Oops, something went wrong.