Skip to content

Commit

Permalink
test(load-balancer): add missing Load Balancer unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Nov 21, 2023
1 parent 18a8ee4 commit 4daa1dd
Show file tree
Hide file tree
Showing 15 changed files with 895 additions and 0 deletions.
50 changes: 50 additions & 0 deletions internal/cmd/loadbalancer/add_service_test.go
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)
}
111 changes: 111 additions & 0 deletions internal/cmd/loadbalancer/add_target_test.go
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)
}
46 changes: 46 additions & 0 deletions internal/cmd/loadbalancer/attach_to_network_test.go
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)
}
43 changes: 43 additions & 0 deletions internal/cmd/loadbalancer/change_algorithm_test.go
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)
}
48 changes: 48 additions & 0 deletions internal/cmd/loadbalancer/change_type_test.go
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)
}
38 changes: 38 additions & 0 deletions internal/cmd/loadbalancer/delete_service_test.go
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)
}
46 changes: 46 additions & 0 deletions internal/cmd/loadbalancer/detach_from_network_test.go
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)
}
Loading

0 comments on commit 4daa1dd

Please sign in to comment.