Skip to content

Commit e4a677e

Browse files
committed
chore: address PR feedback
1 parent 075ee91 commit e4a677e

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

internal/projectconfig/image.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ type ImageConfig struct {
3030

3131
// Capabilities describes the runtime capabilities of this image.
3232
Capabilities ImageCapabilities `toml:"capabilities,omitempty" json:"capabilities,omitempty" jsonschema:"title=Capabilities,description=Runtime capabilities of this image"`
33-
3433
}
3534

3635
// ImagePublishConfig holds publishing settings for an image.
@@ -59,7 +58,7 @@ const (
5958
// about supported deployment and runtime scenarios.
6059
type ImageCapabilities struct {
6160
// SupportedHosts lists the host types on which this image can be deployed or run.
62-
SupportedHosts []SupportedHost `toml:"supported-hosts,omitempty" json:"supportedHosts,omitempty" jsonschema:"title=Supported hosts,description=Host types on which this image can be deployed or run,enum=azure-vm-gen1,enum=azure-vm-gen2,enum=oci-container"`
61+
SupportedHosts []SupportedHost `toml:"supported-hosts,omitempty" json:"supportedHosts,omitempty" validate:"unique,dive,oneof=azure-vm-gen1 azure-vm-gen2 oci-container" jsonschema:"title=Supported hosts,description=Host types on which this image can be deployed or run,enum=azure-vm-gen1,enum=azure-vm-gen2,enum=oci-container"`
6362

6463
// RuntimePackageManagement indicates the image supports runtime package management
6564
// (e.g., installing or removing packages after first boot).
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
package projectconfig_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/go-playground/validator/v10"
10+
"github.com/microsoft/azure-linux-dev-tools/internal/projectconfig"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestImageCapabilities_SupportedHosts_Validation(t *testing.T) {
16+
t.Run("empty is valid", func(t *testing.T) {
17+
caps := projectconfig.ImageCapabilities{}
18+
require.NoError(t, validator.New().Struct(&caps))
19+
})
20+
21+
t.Run("single valid host", func(t *testing.T) {
22+
caps := projectconfig.ImageCapabilities{
23+
SupportedHosts: []projectconfig.SupportedHost{
24+
projectconfig.SupportedHostAzureVMGen2,
25+
},
26+
}
27+
require.NoError(t, validator.New().Struct(&caps))
28+
})
29+
30+
t.Run("all valid hosts", func(t *testing.T) {
31+
caps := projectconfig.ImageCapabilities{
32+
SupportedHosts: []projectconfig.SupportedHost{
33+
projectconfig.SupportedHostAzureVMGen1,
34+
projectconfig.SupportedHostAzureVMGen2,
35+
projectconfig.SupportedHostOCIContainer,
36+
},
37+
}
38+
require.NoError(t, validator.New().Struct(&caps))
39+
})
40+
41+
t.Run("invalid host is rejected", func(t *testing.T) {
42+
caps := projectconfig.ImageCapabilities{
43+
SupportedHosts: []projectconfig.SupportedHost{"hyperv"},
44+
}
45+
err := validator.New().Struct(&caps)
46+
require.Error(t, err)
47+
assert.Contains(t, err.Error(), "oneof")
48+
})
49+
50+
t.Run("duplicate hosts are rejected", func(t *testing.T) {
51+
caps := projectconfig.ImageCapabilities{
52+
SupportedHosts: []projectconfig.SupportedHost{
53+
projectconfig.SupportedHostAzureVMGen1,
54+
projectconfig.SupportedHostAzureVMGen1,
55+
},
56+
}
57+
err := validator.New().Struct(&caps)
58+
require.Error(t, err)
59+
assert.Contains(t, err.Error(), "unique")
60+
})
61+
}

0 commit comments

Comments
 (0)