Skip to content

Commit

Permalink
Merge pull request #5524 from Stavrospanakakis/compose-ports-validation
Browse files Browse the repository at this point in the history
cli/compose: implement the ports validation method
  • Loading branch information
thaJeztah authored Oct 10, 2024
2 parents 305985c + 0319795 commit 6a78e92
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 4 deletions.
18 changes: 14 additions & 4 deletions cli/compose/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package schema
import (
"embed"
"fmt"
"math/big"
"strings"
"time"

"github.com/docker/go-connections/nat"
"github.com/pkg/errors"
"github.com/xeipuuv/gojsonschema"
)
Expand All @@ -20,9 +22,18 @@ const (

type portsFormatChecker struct{}

func (checker portsFormatChecker) IsFormat(_ any) bool {
// TODO: implement this
return true
func (checker portsFormatChecker) IsFormat(input any) bool {
var portSpec string

switch p := input.(type) {
case string:
portSpec = p
case *big.Rat:
portSpec = strings.Split(p.String(), "/")[0]
}

_, err := nat.ParsePortSpec(portSpec)
return err == nil
}

type durationFormatChecker struct{}
Expand All @@ -37,7 +48,6 @@ func (checker durationFormatChecker) IsFormat(input any) bool {
}

func init() {
gojsonschema.FormatCheckers.Add("expose", portsFormatChecker{})
gojsonschema.FormatCheckers.Add("ports", portsFormatChecker{})
gojsonschema.FormatCheckers.Add("duration", durationFormatChecker{})
}
Expand Down
105 changes: 105 additions & 0 deletions cli/compose/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,111 @@ func TestValidate(t *testing.T) {
assert.ErrorContains(t, Validate(config, "12345"), "unsupported Compose file version: 12345")
}

func TestValidatePorts(t *testing.T) {
testcases := []struct {
ports any
hasError bool
}{
{
ports: []int{8000},
hasError: false,
},
{
ports: []string{"8000:8000"},
hasError: false,
},
{
ports: []string{"8001-8005"},
hasError: false,
},
{
ports: []string{"8001-8005:8001-8005"},
hasError: false,
},
{
ports: []string{"8000"},
hasError: false,
},
{
ports: []string{"8000-9000:80"},
hasError: false,
},
{
ports: []string{"[::1]:8080:8000"},
hasError: false,
},
{
ports: []string{"[::1]:8080-8085:8000"},
hasError: false,
},
{
ports: []string{"127.0.0.1:8000:8000"},
hasError: false,
},
{
ports: []string{"127.0.0.1:8000-8005:8000-8005"},
hasError: false,
},
{
ports: []string{"127.0.0.1:8000:8000/udp"},
hasError: false,
},
{
ports: []string{"8000:8000/udp"},
hasError: false,
},
{
ports: []string{"8000:8000/http"},
hasError: true,
},
{
ports: []string{"-1"},
hasError: true,
},
{
ports: []string{"65536"},
hasError: true,
},
{
ports: []string{"-1:65536/http"},
hasError: true,
},
{
ports: []string{"invalid"},
hasError: true,
},
{
ports: []string{"12345678:8000:8000/tcp"},
hasError: true,
},
{
ports: []string{"8005-8000:8005-8000"},
hasError: true,
},
{
ports: []string{"8006-8000:8005-8000"},
hasError: true,
},
}

for _, tc := range testcases {
config := dict{
"version": "3.0",
"services": dict{
"foo": dict{
"image": "busybox",
"ports": tc.ports,
},
},
}
if tc.hasError {
assert.ErrorContains(t, Validate(config, "3"), "services.foo.ports.0 Does not match format 'ports'")
} else {
assert.NilError(t, Validate(config, "3"))
}
}
}

func TestValidateUndefinedTopLevelOption(t *testing.T) {
config := dict{
"version": "3.0",
Expand Down

0 comments on commit 6a78e92

Please sign in to comment.