Skip to content

Commit

Permalink
fix nil shared_cluster; add test cases for cluster_id
Browse files Browse the repository at this point in the history
  • Loading branch information
olegy89 committed Oct 22, 2024
1 parent 6281021 commit 1d7273a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions serverscom/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (l *loadBalancers) EnsureLoadBalancer(ctx context.Context, clusterName stri
input.Name = &name
input.ClusterID = lbClusterID
if lbClusterID == nil {
input.SharedCluster = new(bool)
*input.SharedCluster = true
}

Expand Down
57 changes: 44 additions & 13 deletions serverscom/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func TestLoadBalancers_EnsureLoadBalancer(t *testing.T) {

balancerName := "service-cluster-a123"
locationID := int64(1)
sharedCluster := true

balancer := cli.LoadBalancer{
ID: "a",
Expand All @@ -193,7 +194,8 @@ func TestLoadBalancers_EnsureLoadBalancer(t *testing.T) {
ctx := context.TODO()

input := cli.L4LoadBalancerUpdateInput{
Name: &balancerName,
Name: &balancerName,
SharedCluster: &sharedCluster,
VHostZones: []cli.L4VHostZoneInput{
{
ID: "k8s-nodes-80-tcp",
Expand Down Expand Up @@ -351,12 +353,12 @@ func TestLoadBalancers_EnsureLoadBalancerWithCreate(t *testing.T) {
},
}

collection.EXPECT().SetPerPage(100).Return(collection)
collection.EXPECT().SetParam("search_pattern", balancerName).Return(collection)
collection.EXPECT().SetParam("type", "l4").Return(collection)
collection.EXPECT().Collect(ctx).Return([]cli.LoadBalancer{}, nil)
collection.EXPECT().SetPerPage(100).Return(collection).Times(2)
collection.EXPECT().SetParam("search_pattern", balancerName).Return(collection).Times(2)
collection.EXPECT().SetParam("type", "l4").Return(collection).Times(2)
collection.EXPECT().Collect(ctx).Return([]cli.LoadBalancer{}, nil).Times(2)

service.EXPECT().Collection().Return(collection)
service.EXPECT().Collection().Return(collection).Times(2)
service.EXPECT().CreateL4LoadBalancer(ctx, input).Return(&l4Balancer, nil)

client := cli.NewClient("some")
Expand All @@ -381,6 +383,19 @@ func TestLoadBalancers_EnsureLoadBalancerWithCreate(t *testing.T) {

g.Expect(err).To(BeNil())
g.Expect(status).NotTo(BeNil())

// with cluster-id annotation
clusterID := "some-hash-id"
srv.Annotations = map[string]string{
"servers.com/cluster-id": clusterID,
}

input.ClusterID = &clusterID
service.EXPECT().CreateL4LoadBalancer(ctx, input).Return(&l4Balancer, nil)
status, err = balancerInterface.EnsureLoadBalancer(ctx, "cluster", &srv, []*v1.Node{&node})

g.Expect(err).To(BeNil())
g.Expect(status).NotTo(BeNil())
}

func TestLoadBalancers_UpdateLoadBalancer(t *testing.T) {
Expand All @@ -394,6 +409,7 @@ func TestLoadBalancers_UpdateLoadBalancer(t *testing.T) {

balancerName := "service-cluster-a123"
locationID := int64(1)
sharedCluster := true

balancer := cli.LoadBalancer{
ID: "a",
Expand All @@ -410,7 +426,8 @@ func TestLoadBalancers_UpdateLoadBalancer(t *testing.T) {
ctx := context.TODO()

input := cli.L4LoadBalancerUpdateInput{
Name: &balancerName,
Name: &balancerName,
SharedCluster: &sharedCluster,
VHostZones: []cli.L4VHostZoneInput{
{
ID: "k8s-nodes-80-tcp",
Expand Down Expand Up @@ -461,13 +478,13 @@ func TestLoadBalancers_UpdateLoadBalancer(t *testing.T) {
},
}

collection.EXPECT().SetPerPage(100).Return(collection)
collection.EXPECT().SetParam("search_pattern", balancerName).Return(collection)
collection.EXPECT().SetParam("type", "l4").Return(collection)
collection.EXPECT().Collect(ctx).Return([]cli.LoadBalancer{balancer}, nil)
collection.EXPECT().SetPerPage(100).Return(collection).Times(2)
collection.EXPECT().SetParam("search_pattern", balancerName).Return(collection).Times(2)
collection.EXPECT().SetParam("type", "l4").Return(collection).Times(2)
collection.EXPECT().Collect(ctx).Return([]cli.LoadBalancer{balancer}, nil).Times(2)

service.EXPECT().Collection().Return(collection)
service.EXPECT().GetL4LoadBalancer(ctx, "a").Return(&l4Balancer, nil)
service.EXPECT().Collection().Return(collection).Times(2)
service.EXPECT().GetL4LoadBalancer(ctx, "a").Return(&l4Balancer, nil).Times(2)
service.EXPECT().UpdateL4LoadBalancer(ctx, "a", input).Return(&l4Balancer, nil)

client := cli.NewClient("some")
Expand All @@ -492,6 +509,20 @@ func TestLoadBalancers_UpdateLoadBalancer(t *testing.T) {

g.Expect(err).To(BeNil())
g.Expect(status).NotTo(BeNil())

// with cluster-id annotation
clusterID := "some-hash-id"
srv.Annotations = map[string]string{
"servers.com/cluster-id": clusterID,
}

input.ClusterID = &clusterID
input.SharedCluster = nil
service.EXPECT().UpdateL4LoadBalancer(ctx, "a", input).Return(&l4Balancer, nil)
status, err = balancerInterface.EnsureLoadBalancer(ctx, "cluster", &srv, []*v1.Node{&node})

g.Expect(err).To(BeNil())
g.Expect(status).NotTo(BeNil())
}

func TestLoadBalancers_EnsureLoadBalancerDeleted(t *testing.T) {
Expand Down

0 comments on commit 1d7273a

Please sign in to comment.