mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
refactor: print warnings for all detected unsupported Compose features, not only first one
This commit is contained in:
@@ -67,7 +67,7 @@ func LoadProject(ctx context.Context, paths []string, opts ...composecli.Project
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = validateServicesFeatures(project); err != nil {
|
for _, err = range validateServicesFeatures(project) {
|
||||||
tui.PrintWarning(err.Error())
|
tui.PrintWarning(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -188,18 +189,31 @@ REDIS_URL=redis://localhost:6379
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestLoadProject_Unsupported checks that unsupported features lead to an error.
|
// TestLoadProject_Unsupported checks that unsupported features lead to warnings.
|
||||||
func TestLoadProject_Unsupported(t *testing.T) {
|
func TestLoadProject_Unsupported(t *testing.T) {
|
||||||
requireWarning := func(t *testing.T, r io.Reader) {
|
// captureStderr runs fn while capturing stderr output and returns what was written.
|
||||||
p := make([]byte, 15) // also room for the (color) escapes
|
captureStderr := func(t *testing.T, fn func()) string {
|
||||||
io.ReadFull(r, p)
|
t.Helper()
|
||||||
require.Contains(t, string(p), "WARNING:")
|
old := os.Stderr
|
||||||
|
r, w, err := os.Pipe()
|
||||||
|
require.NoError(t, err)
|
||||||
|
os.Stderr = w
|
||||||
|
defer func() { os.Stderr = old }()
|
||||||
|
|
||||||
|
fn()
|
||||||
|
|
||||||
|
w.Close()
|
||||||
|
out, err := io.ReadAll(r)
|
||||||
|
require.NoError(t, err)
|
||||||
|
r.Close()
|
||||||
|
return string(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
composeYAML string
|
composeYAML string
|
||||||
verify func(t *testing.T, projectDir string)
|
warnCount int
|
||||||
|
warnContains []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "unsupported dns",
|
name: "unsupported dns",
|
||||||
@@ -208,18 +222,8 @@ func TestLoadProject_Unsupported(t *testing.T) {
|
|||||||
image: myapp:latest
|
image: myapp:latest
|
||||||
dns: 8.8.8.8
|
dns: 8.8.8.8
|
||||||
`,
|
`,
|
||||||
verify: func(t *testing.T, projectDir string) {
|
warnCount: 1,
|
||||||
old := os.Stderr
|
warnContains: []string{"dns"},
|
||||||
var r io.ReadCloser
|
|
||||||
defer func() { os.Stderr = old }()
|
|
||||||
r, os.Stderr, _ = os.Pipe()
|
|
||||||
defer r.Close()
|
|
||||||
defer os.Stderr.Close()
|
|
||||||
|
|
||||||
_, err := LoadProject(context.Background(), []string{filepath.Join(projectDir, "compose.yaml")})
|
|
||||||
require.NoError(t, err)
|
|
||||||
requireWarning(t, r)
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "unsupported networks",
|
name: "unsupported networks",
|
||||||
@@ -232,46 +236,65 @@ func TestLoadProject_Unsupported(t *testing.T) {
|
|||||||
networks:
|
networks:
|
||||||
frontend:
|
frontend:
|
||||||
`,
|
`,
|
||||||
verify: func(t *testing.T, projectDir string) {
|
warnCount: 1,
|
||||||
old := os.Stderr
|
warnContains: []string{"networks"},
|
||||||
var r io.ReadCloser
|
},
|
||||||
defer func() { os.Stderr = old }()
|
{
|
||||||
r, os.Stderr, _ = os.Pipe()
|
name: "multiple unsupported features",
|
||||||
defer r.Close()
|
composeYAML: `services:
|
||||||
defer os.Stderr.Close()
|
app:
|
||||||
|
image: myapp:latest
|
||||||
|
dns: 8.8.8.8
|
||||||
|
links:
|
||||||
|
- db
|
||||||
|
db:
|
||||||
|
image: postgres:latest
|
||||||
|
secrets:
|
||||||
|
- db_password
|
||||||
|
|
||||||
_, err := LoadProject(context.Background(), []string{filepath.Join(projectDir, "compose.yaml")})
|
secrets:
|
||||||
require.NoError(t, err)
|
db_password:
|
||||||
requireWarning(t, r)
|
file: ./secret.txt
|
||||||
},
|
`,
|
||||||
|
warnCount: 3,
|
||||||
|
warnContains: []string{"dns", "links", "secrets"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "supported networks",
|
name: "supported networks",
|
||||||
composeYAML: `services:
|
composeYAML: `services:
|
||||||
app:
|
app:
|
||||||
image: myapp:latest
|
image: myapp:latest
|
||||||
|
networks:
|
||||||
|
default: {}
|
||||||
|
|
||||||
|
web:
|
||||||
|
image: nginx
|
||||||
networks:
|
networks:
|
||||||
- default
|
- default
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
default: {}
|
default:
|
||||||
`,
|
`,
|
||||||
verify: func(t *testing.T, projectDir string) {
|
warnCount: 0,
|
||||||
_, err := LoadProject(context.Background(), []string{filepath.Join(projectDir, "compose.yaml")})
|
|
||||||
require.NoError(t, err)
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
tempDir := t.TempDir()
|
stderr := captureStderr(t, func() {
|
||||||
|
_, err := LoadProjectFromContent(context.Background(), tt.composeYAML)
|
||||||
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
composeFile := filepath.Join(tempDir, "compose.yaml")
|
if tt.warnCount == 0 {
|
||||||
err := os.WriteFile(composeFile, []byte(tt.composeYAML), 0o644)
|
assert.Empty(t, stderr)
|
||||||
require.NoError(t, err)
|
} else {
|
||||||
|
assert.Equal(t, tt.warnCount, strings.Count(stderr, "WARNING:"),
|
||||||
tt.verify(t, tempDir)
|
"expected %d warnings, got stderr: %s", tt.warnCount, stderr)
|
||||||
|
for _, substr := range tt.warnContains {
|
||||||
|
assert.Contains(t, stderr, substr)
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -423,51 +423,52 @@ func validateServicesExtensions(project *types.Project) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateServicesFeatures checks the service for unsupported features and returns an error for this first one found.
|
// validateServicesFeatures checks services for unsupported features and returns all found.
|
||||||
func validateServicesFeatures(project *types.Project) error {
|
func validateServicesFeatures(project *types.Project) []error {
|
||||||
err := func(service, feature string) error {
|
err := func(service, feature string) error {
|
||||||
return fmt.Errorf("service: '%s': unsupported feature: '%s', see %s", service, feature, "https://uncloud.run/docs/compose-file-reference/support-matrix")
|
return fmt.Errorf("service '%s': unsupported feature '%s', see %s",
|
||||||
|
service, feature, "https://uncloud.run/docs/compose-file-reference/support-matrix")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: check other commonly used but unsupported features.
|
||||||
|
var errs []error
|
||||||
for _, service := range project.Services {
|
for _, service := range project.Services {
|
||||||
if service.SecurityOpt != nil {
|
if service.SecurityOpt != nil {
|
||||||
return err(service.Name, "security_opt")
|
errs = append(errs, err(service.Name, "security_opt"))
|
||||||
}
|
}
|
||||||
if service.DNS != nil {
|
if service.DNS != nil {
|
||||||
return err(service.Name, "dns")
|
errs = append(errs, err(service.Name, "dns"))
|
||||||
}
|
}
|
||||||
if service.DNSSearch != nil {
|
if service.DNSSearch != nil {
|
||||||
return err(service.Name, "dns_search")
|
errs = append(errs, err(service.Name, "dns_search"))
|
||||||
}
|
}
|
||||||
if service.Labels != nil {
|
if service.Labels != nil {
|
||||||
return err(service.Name, "labels")
|
errs = append(errs, err(service.Name, "labels"))
|
||||||
}
|
}
|
||||||
if service.Links != nil {
|
if service.Links != nil {
|
||||||
return err(service.Name, "links")
|
errs = append(errs, err(service.Name, "links"))
|
||||||
}
|
}
|
||||||
if service.MemSwappiness > 0 {
|
if service.MemSwappiness > 0 {
|
||||||
return err(service.Name, "mem_swappiness")
|
errs = append(errs, err(service.Name, "mem_swappiness"))
|
||||||
}
|
}
|
||||||
if service.MemSwapLimit > 0 {
|
if service.MemSwapLimit > 0 {
|
||||||
return err(service.Name, "memswap_limit")
|
errs = append(errs, err(service.Name, "memswap_limit"))
|
||||||
}
|
}
|
||||||
if service.Secrets != nil {
|
if service.Secrets != nil {
|
||||||
return err(service.Name, "secrets")
|
errs = append(errs, err(service.Name, "secrets"))
|
||||||
}
|
}
|
||||||
if service.StorageOpt != nil {
|
if service.StorageOpt != nil {
|
||||||
return err(service.Name, "storage_opt")
|
errs = append(errs, err(service.Name, "storage_opt"))
|
||||||
}
|
}
|
||||||
// we only allow the 'default' network, nothing else.
|
// we only allow the 'default' network, nothing else.
|
||||||
if x := service.Networks; x != nil {
|
if x := service.Networks; x != nil {
|
||||||
if len(x) != 1 {
|
if len(x) != 1 {
|
||||||
return err(service.Name, "networks")
|
errs = append(errs, err(service.Name, "networks"))
|
||||||
}
|
} else if _, ok := x["default"]; !ok {
|
||||||
config, ok := x["default"]
|
errs = append(errs, err(service.Name, "networks"))
|
||||||
if !ok || config != nil {
|
|
||||||
return err(service.Name, "networks")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return errs
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user