Skip to content

Commit d24b5f3

Browse files
Cover empty string case
1 parent bcaef92 commit d24b5f3

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

cloud/linode/cloud.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ func newCloud() (cloudprovider.Interface, error) {
201201
return nil, fmt.Errorf("%s", msg)
202202
}
203203

204+
if len(Options.NodeBalancerPrefix) == 0 {
205+
msg := "nodebalancer-prefix cannot be empty string"
206+
klog.Error(msg)
207+
return nil, fmt.Errorf("%s", msg)
208+
}
209+
204210
// create struct that satisfies cloudprovider.Interface
205211
lcloud := &linodeCloud{
206212
client: linodeClient,

cloud/linode/cloud_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,32 @@ func TestNewCloud(t *testing.T) {
152152
require.Error(t, err, "expected error if nodebalancer-prefix is longer than 19 chars")
153153
require.ErrorContains(t, err, "nodebalancer-prefix")
154154
})
155+
156+
t.Run("should fail if nodebalancer-prefix is empty", func(t *testing.T) {
157+
prefix := Options.NodeBalancerPrefix
158+
rtEnabled := Options.EnableRouteController
159+
Options.EnableRouteController = false
160+
Options.LoadBalancerType = "nodebalancer"
161+
Options.VPCNames = "vpc-test1,vpc-test2"
162+
Options.NodeBalancerBackendIPv4SubnetName = "t1"
163+
vpcIDs = map[string]int{"vpc-test1": 1, "vpc-test2": 2, "vpc-test3": 3}
164+
subnetIDs = map[string]int{"t1": 1, "t2": 2, "t3": 3}
165+
Options.NodeBalancerPrefix = strings.Repeat("a", 21)
166+
defer func() {
167+
Options.NodeBalancerPrefix = prefix
168+
Options.LoadBalancerType = ""
169+
Options.EnableRouteController = rtEnabled
170+
Options.VPCNames = ""
171+
Options.NodeBalancerBackendIPv4SubnetID = 0
172+
Options.NodeBalancerBackendIPv4SubnetName = ""
173+
vpcIDs = map[string]int{}
174+
subnetIDs = map[string]int{}
175+
}()
176+
_, err := newCloud()
177+
t.Log(err)
178+
require.Error(t, err, "expected error if nodebalancer-prefix is empty")
179+
require.ErrorContains(t, err, "nodebalancer-prefix cannot be empty")
180+
})
155181
}
156182

157183
func Test_linodeCloud_LoadBalancer(t *testing.T) {

0 commit comments

Comments
 (0)