-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(load-balancer): add missing Load Balancer unit tests
- Loading branch information
Showing
15 changed files
with
895 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,50 @@ | ||
package loadbalancer | ||
|
||
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 TestAddService(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := AddServiceCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
fx.Client.LoadBalancerClient.EXPECT(). | ||
Get(gomock.Any(), "123"). | ||
Return(&hcloud.LoadBalancer{ID: 123}, nil, nil) | ||
fx.Client.LoadBalancerClient.EXPECT(). | ||
AddService(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, hcloud.LoadBalancerAddServiceOpts{ | ||
Protocol: hcloud.LoadBalancerServiceProtocolHTTP, | ||
ListenPort: hcloud.Ptr(80), | ||
DestinationPort: hcloud.Ptr(8080), | ||
HTTP: &hcloud.LoadBalancerAddServiceOptsHTTP{ | ||
StickySessions: hcloud.Ptr(false), | ||
RedirectHTTP: hcloud.Ptr(false), | ||
}, | ||
Proxyprotocol: hcloud.Ptr(false), | ||
}). | ||
Return(&hcloud.Action{ID: 123}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
ActionProgress(gomock.Any(), &hcloud.Action{ID: 123}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"123", "--protocol", "http", "--listen-port", "80", "--destination-port", "8080"}) | ||
|
||
expOut := "Service was added to Load Balancer 123\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,111 @@ | ||
package loadbalancer | ||
|
||
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 TestAddTargetServer(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := AddTargetCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
fx.Client.LoadBalancerClient.EXPECT(). | ||
Get(gomock.Any(), "123"). | ||
Return(&hcloud.LoadBalancer{ID: 123}, nil, nil) | ||
fx.Client.ServerClient.EXPECT(). | ||
Get(gomock.Any(), "my-server"). | ||
Return(&hcloud.Server{ID: 321}, nil, nil) | ||
fx.Client.LoadBalancerClient.EXPECT(). | ||
AddServerTarget(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, hcloud.LoadBalancerAddServerTargetOpts{ | ||
Server: &hcloud.Server{ID: 321}, | ||
UsePrivateIP: hcloud.Ptr(false), | ||
}). | ||
Return(&hcloud.Action{ID: 123}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
ActionProgress(gomock.Any(), &hcloud.Action{ID: 123}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"123", "--server", "my-server"}) | ||
|
||
expOut := "Target added to Load Balancer 123\n" | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} | ||
|
||
func TestAddTargetLabelSelector(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := AddTargetCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
fx.Client.LoadBalancerClient.EXPECT(). | ||
Get(gomock.Any(), "123"). | ||
Return(&hcloud.LoadBalancer{ID: 123}, nil, nil) | ||
fx.Client.LoadBalancerClient.EXPECT(). | ||
AddLabelSelectorTarget(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, hcloud.LoadBalancerAddLabelSelectorTargetOpts{ | ||
Selector: "my-label", | ||
UsePrivateIP: hcloud.Ptr(false), | ||
}). | ||
Return(&hcloud.Action{ID: 123}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
ActionProgress(gomock.Any(), &hcloud.Action{ID: 123}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"123", "--label-selector", "my-label"}) | ||
|
||
expOut := "Target added to Load Balancer 123\n" | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} | ||
|
||
func TestAddTargetIP(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := AddTargetCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
fx.Client.LoadBalancerClient.EXPECT(). | ||
Get(gomock.Any(), "123"). | ||
Return(&hcloud.LoadBalancer{ID: 123}, nil, nil) | ||
fx.Client.LoadBalancerClient.EXPECT(). | ||
AddIPTarget(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, hcloud.LoadBalancerAddIPTargetOpts{ | ||
IP: net.ParseIP("192.168.2.1"), | ||
}). | ||
Return(&hcloud.Action{ID: 123}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
ActionProgress(gomock.Any(), &hcloud.Action{ID: 123}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"123", "--ip", "192.168.2.1"}) | ||
|
||
expOut := "Target added to Load Balancer 123\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,46 @@ | ||
package loadbalancer | ||
|
||
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 TestAttachToNetwork(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := AttachToNetworkCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
fx.Client.LoadBalancerClient.EXPECT(). | ||
Get(gomock.Any(), "123"). | ||
Return(&hcloud.LoadBalancer{ID: 123}, nil, nil) | ||
fx.Client.NetworkClient.EXPECT(). | ||
Get(gomock.Any(), "my-network"). | ||
Return(&hcloud.Network{ID: 321}, nil, nil) | ||
fx.Client.LoadBalancerClient.EXPECT(). | ||
AttachToNetwork(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, hcloud.LoadBalancerAttachToNetworkOpts{ | ||
Network: &hcloud.Network{ID: 321}, | ||
}). | ||
Return(&hcloud.Action{ID: 123}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
ActionProgress(gomock.Any(), &hcloud.Action{ID: 123}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"123", "--network", "my-network"}) | ||
|
||
expOut := "Load Balancer 123 attached to network 321\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,43 @@ | ||
package loadbalancer | ||
|
||
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 TestChangeAlgorithm(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := ChangeAlgorithmCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
fx.Client.LoadBalancerClient.EXPECT(). | ||
Get(gomock.Any(), "123"). | ||
Return(&hcloud.LoadBalancer{ID: 123}, nil, nil) | ||
fx.Client.LoadBalancerClient.EXPECT(). | ||
ChangeAlgorithm(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, hcloud.LoadBalancerChangeAlgorithmOpts{ | ||
Type: hcloud.LoadBalancerAlgorithmTypeLeastConnections, | ||
}). | ||
Return(&hcloud.Action{ID: 123}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
ActionProgress(gomock.Any(), &hcloud.Action{ID: 123}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"123", "--algorithm-type", "least_connections"}) | ||
|
||
expOut := "Algorithm for Load Balancer 123 was changed\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,48 @@ | ||
package loadbalancer | ||
|
||
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 TestChangeType(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := ChangeTypeCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
lbType := &hcloud.LoadBalancerType{ID: 321, Name: "lb21"} | ||
|
||
fx.Client.LoadBalancerClient.EXPECT(). | ||
Get(gomock.Any(), "123"). | ||
Return(&hcloud.LoadBalancer{ID: 123}, nil, nil) | ||
fx.Client.LoadBalancerTypeClient.EXPECT(). | ||
Get(gomock.Any(), "lb21"). | ||
Return(lbType, nil, nil) | ||
fx.Client.LoadBalancerClient.EXPECT(). | ||
ChangeType(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, hcloud.LoadBalancerChangeTypeOpts{ | ||
LoadBalancerType: lbType, | ||
}). | ||
Return(&hcloud.Action{ID: 123}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
ActionProgress(gomock.Any(), &hcloud.Action{ID: 123}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"123", "lb21"}) | ||
|
||
expOut := "LoadBalancer 123 changed to type lb21\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,38 @@ | ||
package loadbalancer | ||
|
||
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 TestDeleteService(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := DeleteServiceCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
fx.Client.LoadBalancerClient.EXPECT(). | ||
Get(gomock.Any(), "123"). | ||
Return(&hcloud.LoadBalancer{ID: 123}, nil, nil) | ||
fx.Client.LoadBalancerClient.EXPECT(). | ||
DeleteService(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, 80). | ||
Return(&hcloud.Action{ID: 123}, nil, nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"123", "--listen-port", "80"}) | ||
|
||
expOut := "Service on port 80 deleted from Load Balancer 123\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,46 @@ | ||
package loadbalancer | ||
|
||
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 TestDetachFromNetwork(t *testing.T) { | ||
fx := testutil.NewFixture(t) | ||
defer fx.Finish() | ||
|
||
cmd := DetachFromNetworkCmd.CobraCommand( | ||
context.Background(), | ||
fx.Client, | ||
fx.TokenEnsurer, | ||
fx.ActionWaiter) | ||
fx.ExpectEnsureToken() | ||
|
||
fx.Client.LoadBalancerClient.EXPECT(). | ||
Get(gomock.Any(), "123"). | ||
Return(&hcloud.LoadBalancer{ID: 123}, nil, nil) | ||
fx.Client.NetworkClient.EXPECT(). | ||
Get(gomock.Any(), "my-network"). | ||
Return(&hcloud.Network{ID: 321}, nil, nil) | ||
fx.Client.LoadBalancerClient.EXPECT(). | ||
DetachFromNetwork(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, hcloud.LoadBalancerDetachFromNetworkOpts{ | ||
Network: &hcloud.Network{ID: 321}, | ||
}). | ||
Return(&hcloud.Action{ID: 123}, nil, nil) | ||
fx.ActionWaiter.EXPECT(). | ||
ActionProgress(gomock.Any(), &hcloud.Action{ID: 123}). | ||
Return(nil) | ||
|
||
out, _, err := fx.Run(cmd, []string{"123", "--network", "my-network"}) | ||
|
||
expOut := "Load Balancer 123 detached from Network 321\n" | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expOut, out) | ||
} |
Oops, something went wrong.