Skip to content

Commit bbb1a66

Browse files
committed
combine suggested commit and add unit test
Signed-off-by: Songpon Srisawai <[email protected]>
1 parent a86a1ae commit bbb1a66

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

pkg/limayaml/validate.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,13 +612,14 @@ func warnExperimental(y *LimaYAML) {
612612
}
613613

614614
// ValidateYAMLAgainstLatestConfig validates the values between the latest YAML and the updated(New) YAML.
615+
// This validates configuration rules that disallow certain changes, such as shrinking the disk.
615616
func ValidateYAMLAgainstLatestConfig(yNew, yLatest []byte) error {
616617
var l, n LimaYAML
617-
var err error
618-
if err = Unmarshal(yLatest, &l, "Unmarshal latest YAML bytes"); err != nil {
618+
619+
if err := Unmarshal(yLatest, &l, "Unmarshal latest YAML bytes"); err != nil {
619620
return err
620621
}
621-
if err = Unmarshal(yNew, &n, "Unmarshal new YAML bytes); err != nil {
622+
if err := Unmarshal(yNew, &n, "Unmarshal new YAML bytes"); err != nil {
622623
return err
623624
}
624625

pkg/limayaml/validate_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,28 @@ provision:
264264
"field `provision[0].mode` must one of \"system\", \"user\", \"boot\", \"data\", \"dependency\", or \"ansible\"\n"+
265265
"field `provision[1].path` must not be empty when mode is \"data\"")
266266
}
267+
268+
// TestValidateAgainstLatestConfig ensures the new config is correctly validated against the latest config.
269+
// (e.g., disk shrinking is disallowed).
270+
func TestValidateAgainstLatestConfig(t *testing.T) {
271+
validParam := [][]string{
272+
{`disk: 100G`, `disk: 100G`},
273+
{`disk: 200G`, `disk: 100G`},
274+
{``, ``},
275+
}
276+
for _, param := range validParam {
277+
n, l := param[0], param[1]
278+
err := ValidateYAMLAgainstLatestConfig([]byte(n), []byte(l))
279+
assert.NilError(t, err)
280+
}
281+
282+
invalidParam := [][]string{
283+
{`disk: 50G`, `disk: 100G`, `shrinking`},
284+
}
285+
286+
for _, param := range invalidParam {
287+
n, l := param[0], param[1]
288+
err := ValidateYAMLAgainstLatestConfig([]byte(n), []byte(l))
289+
assert.ErrorContains(t, err, param[2])
290+
}
291+
}

0 commit comments

Comments
 (0)