|
| 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