mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
test: x-caddy extension parsing
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package compose
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
const CaddyExtensionKey = "x-caddy"
|
||||
|
||||
@@ -17,16 +21,20 @@ func (c *Caddy) DecodeMapstructure(value any) error {
|
||||
*c = *v
|
||||
return nil
|
||||
case string:
|
||||
// Handle x-caddy: "caddyfile config"
|
||||
// Handle x-caddy: "Caddyfile config"
|
||||
*c = Caddy{Config: v}
|
||||
case map[string]any:
|
||||
// Handle the long syntax with a config key.
|
||||
if config, ok := v["config"]; ok {
|
||||
configStr, ok := config.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("x-caddy.config must be a string, got %T", config)
|
||||
// Use mapstructure to decode the map directly to the struct.
|
||||
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||
Result: c,
|
||||
ErrorUnused: true, // Error if there are extra keys not in the struct.
|
||||
WeaklyTypedInput: false, // Enforce strict type matching.
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("create decoder for x-caddy extension: %w", err)
|
||||
}
|
||||
*c = Caddy{Config: configStr}
|
||||
if err := decoder.Decode(v); err != nil {
|
||||
return fmt.Errorf("decode x-caddy extension: %w", err)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid type %T for x-caddy extension: expected string or object", value)
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
package compose
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCaddyExtension(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
composeYAML string
|
||||
expectedConfig string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "x-caddy as string",
|
||||
composeYAML: `
|
||||
services:
|
||||
web:
|
||||
image: nginx
|
||||
x-caddy: |
|
||||
example.com {
|
||||
reverse_proxy web:80
|
||||
}
|
||||
`,
|
||||
expectedConfig: `example.com {
|
||||
reverse_proxy web:80
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "x-caddy as object with config field",
|
||||
composeYAML: `
|
||||
services:
|
||||
web:
|
||||
image: nginx
|
||||
x-caddy:
|
||||
config: |
|
||||
example.com {
|
||||
reverse_proxy web:80
|
||||
}
|
||||
`,
|
||||
expectedConfig: `example.com {
|
||||
reverse_proxy web:80
|
||||
}
|
||||
`,
|
||||
},
|
||||
|
||||
{
|
||||
name: "x-caddy with empty object",
|
||||
composeYAML: `
|
||||
services:
|
||||
web:
|
||||
image: nginx
|
||||
x-caddy: {}
|
||||
`,
|
||||
expectedConfig: "",
|
||||
},
|
||||
{
|
||||
name: "x-caddy with empty string",
|
||||
composeYAML: `
|
||||
services:
|
||||
web:
|
||||
image: nginx
|
||||
x-caddy: ""
|
||||
`,
|
||||
expectedConfig: "",
|
||||
},
|
||||
{
|
||||
name: "x-caddy with extra unknown field should fail",
|
||||
composeYAML: `
|
||||
services:
|
||||
web:
|
||||
image: nginx
|
||||
x-caddy:
|
||||
config: |
|
||||
example.com {
|
||||
reverse_proxy web:80
|
||||
}
|
||||
unknown_field: "should cause error"
|
||||
`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "x-caddy with non-string config field should fail",
|
||||
composeYAML: `
|
||||
services:
|
||||
web:
|
||||
image: nginx
|
||||
x-caddy:
|
||||
config: 123
|
||||
`,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
project, err := loadProjectFromContent(t, tt.composeYAML)
|
||||
|
||||
if tt.wantErr {
|
||||
require.Error(t, err, "expected error for test case with invalid extension")
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
service, err := project.GetService("web")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify the x-caddy extension was parsed correctly.
|
||||
caddyExt, ok := service.Extensions[CaddyExtensionKey]
|
||||
require.True(t, ok, "x-caddy extension not found")
|
||||
|
||||
caddy, ok := caddyExt.(Caddy)
|
||||
require.True(t, ok, "x-caddy extension is not Caddy type")
|
||||
|
||||
assert.Equal(t, tt.expectedConfig, caddy.Config)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ func LoadProject(ctx context.Context, paths []string, opts ...composecli.Project
|
||||
composecli.WithConfigFileEnv,
|
||||
// If none was selected, get default Compose file names from current or parent folders.
|
||||
composecli.WithDefaultConfigPath,
|
||||
composecli.WithExtension(CaddyExtensionKey, Caddy{}),
|
||||
composecli.WithExtension(PortsExtensionKey, PortsSource{}),
|
||||
composecli.WithExtension(MachinesExtensionKey, MachinesSource{}),
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ func loadProjectFromContent(t *testing.T, content string) (*types.Project, error
|
||||
if o.KnownExtensions == nil {
|
||||
o.KnownExtensions = map[string]any{}
|
||||
}
|
||||
o.KnownExtensions[CaddyExtensionKey] = Caddy{}
|
||||
o.KnownExtensions[PortsExtensionKey] = PortsSource{}
|
||||
o.KnownExtensions[MachinesExtensionKey] = MachinesSource{}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user