Skip to content

Commit cd5e9c3

Browse files
committed
tests
1 parent 3c34ea5 commit cd5e9c3

File tree

8 files changed

+87
-5
lines changed

8 files changed

+87
-5
lines changed

internal/actions/check_node_deleted.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/sirupsen/logrus"
1010
"k8s.io/client-go/kubernetes"
1111

12+
"fmt"
1213
"github.com/castai/cluster-controller/internal/castai"
1314
"github.com/castai/cluster-controller/internal/waitext"
1415
)
@@ -52,6 +53,10 @@ func (h *CheckNodeDeletedHandler) Handle(ctx context.Context, action *castai.Clu
5253
"provider_id": req.ProviderId,
5354
ActionIDLogField: action.ID,
5455
})
56+
if req.NodeName == "" {
57+
return fmt.Errorf("node name is empty %w", errAction)
58+
}
59+
5560
log.Info("checking if node is deleted")
5661

5762
boff := waitext.NewConstantBackoff(h.cfg.retryWait)

internal/actions/check_node_deleted_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66

77
"github.com/google/uuid"
88
"github.com/sirupsen/logrus"
9+
"github.com/stretchr/testify/require"
910
v1 "k8s.io/api/core/v1"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
"k8s.io/apimachinery/pkg/runtime"
1213
"k8s.io/client-go/kubernetes/fake"
1314

1415
"github.com/castai/cluster-controller/internal/castai"
15-
"github.com/stretchr/testify/require"
1616
)
1717

1818
func TestCheckNodeDeletedHandler_Handle(t *testing.T) {
@@ -37,6 +37,13 @@ func TestCheckNodeDeletedHandler_Handle(t *testing.T) {
3737
},
3838
wantErr: errAction,
3939
},
40+
{
41+
name: "empty node name",
42+
args: args{
43+
action: newActionCheckNodeDeleted("", nodeID, providerID),
44+
},
45+
wantErr: errAction,
46+
},
4047
{
4148
name: "nodeID is not matching",
4249
args: args{

internal/actions/check_node_status.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package actions
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"reflect"
78
"time"
@@ -13,7 +14,6 @@ import (
1314
"k8s.io/client-go/kubernetes"
1415
"k8s.io/client-go/kubernetes/typed/core/v1"
1516

16-
"errors"
1717
"github.com/castai/cluster-controller/internal/castai"
1818
"github.com/castai/cluster-controller/internal/waitext"
1919
)
@@ -50,6 +50,9 @@ func (h *CheckNodeStatusHandler) Handle(ctx context.Context, action *castai.Clus
5050
ActionIDLogField: action.ID,
5151
})
5252

53+
if req.NodeName == "" {
54+
return fmt.Errorf("node name is empty %w", errAction)
55+
}
5356
switch req.NodeStatus {
5457
case castai.ActionCheckNodeStatus_READY:
5558
log.Info("checking node ready")

internal/actions/check_node_status_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package actions
22

33
import (
44
"context"
5+
"testing"
6+
57
"github.com/google/uuid"
68
"github.com/samber/lo"
79
"github.com/sirupsen/logrus"
@@ -13,7 +15,6 @@ import (
1315
"k8s.io/apimachinery/pkg/watch"
1416
"k8s.io/client-go/kubernetes/fake"
1517
k8stest "k8s.io/client-go/testing"
16-
"testing"
1718

1819
"github.com/castai/cluster-controller/internal/castai"
1920
)
@@ -52,7 +53,6 @@ func TestCheckNodeStatusHandler_Handle_Deleted(t *testing.T) {
5253
args args
5354
wantErr error
5455
}{
55-
5656
{
5757
name: "action is nil",
5858
wantErr: errAction,
@@ -303,6 +303,13 @@ func TestCheckNodeStatusHandler_Handle_Ready(t *testing.T) {
303303
name: "action is nil",
304304
wantErr: errAction,
305305
},
306+
{
307+
name: "empty node name",
308+
args: args{
309+
action: newActionCheckNodeStatus("", nodeID, providerID, castai.ActionCheckNodeStatus_READY, lo.ToPtr(int32(1))),
310+
},
311+
wantErr: errAction,
312+
},
306313
{
307314
name: "return error when ctx timeout",
308315
args: args{

internal/actions/delete_node_handler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ func (h *DeleteNodeHandler) Handle(ctx context.Context, action *castai.ClusterAc
6565
})
6666
log.Info("deleting kubernetes node")
6767

68+
if req.NodeName == "" {
69+
return fmt.Errorf("node name is empty %w", errAction)
70+
}
71+
6872
b := waitext.NewConstantBackoff(h.cfg.deleteRetryWait)
6973
err := waitext.Retry(
7074
ctx,

internal/actions/delete_node_handler_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,11 @@ func TestDeleteNodeHandler(t *testing.T) {
159159
r.Len(va.Items, 0)
160160
})
161161
}
162+
163+
//{
164+
//name: "empty node name",
165+
//args: args{
166+
//action: newActionCheckNodeStatus("", nodeID, providerID, castai.ActionCheckNodeStatus_READY, lo.ToPtr(int32(1))),
167+
//},
168+
//wantErr: errAction,
169+
//},

internal/actions/drain_node_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (h *DrainNodeHandler) Handle(ctx context.Context, action *castai.ClusterAct
9191
ActionIDLogField: action.ID,
9292
})
9393

94-
if req.NodeID == "" && req.ProviderId == "" {
94+
if req.NodeName == "" || req.NodeID == "" && req.ProviderId == "" {
9595
return fmt.Errorf("node ID and provider ID are empty %w", errAction)
9696
}
9797

internal/actions/kubernetes_helpers_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,54 @@ func Test_isNodeIDProviderIDValid(t *testing.T) {
8585
providerID: "provider-id-456",
8686
},
8787
},
88+
{
89+
name: "node ID and provider ID are empty at Node spec",
90+
args: args{
91+
node: &v1.Node{
92+
ObjectMeta: metav1.ObjectMeta{
93+
Labels: map[string]string{},
94+
},
95+
Spec: v1.NodeSpec{
96+
ProviderID: "",
97+
},
98+
},
99+
nodeID: "node-id-123",
100+
providerID: "provider-id-456",
101+
},
102+
wantErr: errNodeDoesNotMatch,
103+
},
104+
{
105+
name: "node ID is empty at Node spec and Provider is matching",
106+
args: args{
107+
node: &v1.Node{
108+
ObjectMeta: metav1.ObjectMeta{
109+
Labels: map[string]string{},
110+
},
111+
Spec: v1.NodeSpec{
112+
ProviderID: "provider-id-456",
113+
},
114+
},
115+
nodeID: "node-id-123",
116+
providerID: "provider-id-456",
117+
},
118+
},
119+
{
120+
name: "provider ID is empty at Node spec and NodeID is matching",
121+
args: args{
122+
node: &v1.Node{
123+
ObjectMeta: metav1.ObjectMeta{
124+
Labels: map[string]string{
125+
castai.LabelNodeID: "node-id-123",
126+
},
127+
},
128+
Spec: v1.NodeSpec{
129+
ProviderID: "",
130+
},
131+
},
132+
nodeID: "node-id-123",
133+
providerID: "provider-id-456",
134+
},
135+
},
88136
{
89137
name: "node ID and provider ID matches",
90138
args: args{

0 commit comments

Comments
 (0)