Skip to content

Commit c907d80

Browse files
author
Roman Sysoev
committed
test(vm): add userdata validation unit test
Signed-off-by: Roman Sysoev <roman.sysoev@flant.com>
1 parent 9b93aa5 commit c907d80

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

images/virtualization-artifact/pkg/controller/service/provisioning_service.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ import (
3131

3232
const cloudInitUserMaxLen = 2048
3333

34+
var (
35+
ErrUserDataEmpty = errors.New("provisioning userdata is defined, but it is empty")
36+
ErrUserDataTooLong = fmt.Errorf("userdata exceeds %d byte limit; should use userDataRef for larger data", cloudInitUserMaxLen)
37+
)
38+
3439
type ProvisioningService struct {
3540
reader client.Reader
3641
}
@@ -103,11 +108,11 @@ func (p *ProvisioningService) hasOneOfKeys(secret *corev1.Secret, checkKeys ...s
103108

104109
func (p *ProvisioningService) ValidateUserDataLen(userData string) error {
105110
if userData == "" {
106-
return errors.New("provisioning userdata is defined, but it is empty")
111+
return ErrUserDataEmpty
107112
}
108113

109114
if len(userData) > cloudInitUserMaxLen {
110-
return fmt.Errorf("userdata exceeds %d byte limit; should use userDataRef for larger data", cloudInitUserMaxLen)
115+
return ErrUserDataTooLong
111116
}
112117

113118
return nil
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2026 Flant JSC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package service
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
var _ = Describe("ProvisioningService", func() {
25+
var provisioningService *ProvisioningService
26+
27+
BeforeEach(func() {
28+
provisioningService = NewProvisioningService(nil)
29+
})
30+
31+
DescribeTable("ValidateUserDataLen",
32+
func(userData string, expectedErr error) {
33+
err := provisioningService.ValidateUserDataLen(userData)
34+
if expectedErr != nil {
35+
Expect(err).To(HaveOccurred())
36+
Expect(err).To(MatchError(expectedErr))
37+
} else {
38+
Expect(err).ToNot(HaveOccurred())
39+
}
40+
},
41+
Entry(
42+
"empty userdata",
43+
"",
44+
ErrUserDataEmpty,
45+
),
46+
Entry(
47+
"userdata exceeds max limit",
48+
string(make([]byte, cloudInitUserMaxLen+1)),
49+
ErrUserDataTooLong,
50+
),
51+
Entry(
52+
"userdata is within limit",
53+
string(make([]byte, cloudInitUserMaxLen-1)),
54+
nil,
55+
),
56+
)
57+
})

0 commit comments

Comments
 (0)