Add validation of Ports
Also do caseless compares for "enum" strings.
This commit is contained in:
@@ -50,6 +50,52 @@ func TestValidateVolumes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatePorts(t *testing.T) {
|
||||
successCase := []Port{
|
||||
{Name: "abc", ContainerPort: 80, HostPort: 80, Protocol: "TCP"},
|
||||
{Name: "123", ContainerPort: 81, HostPort: 81},
|
||||
{Name: "easy", ContainerPort: 82, Protocol: "TCP"},
|
||||
{Name: "as", ContainerPort: 83, Protocol: "UDP"},
|
||||
{Name: "do-re-me", ContainerPort: 84},
|
||||
{Name: "baby-you-and-me", ContainerPort: 82, Protocol: "tcp"},
|
||||
{ContainerPort: 85},
|
||||
}
|
||||
err := validatePorts(successCase)
|
||||
if err != nil {
|
||||
t.Errorf("expected success: %v", err)
|
||||
}
|
||||
|
||||
nonCanonicalCase := []Port{
|
||||
{ContainerPort: 80},
|
||||
}
|
||||
err = validatePorts(nonCanonicalCase)
|
||||
if err != nil {
|
||||
t.Errorf("expected success: %v", err)
|
||||
}
|
||||
if nonCanonicalCase[0].HostPort != 80 || nonCanonicalCase[0].Protocol != "TCP" {
|
||||
t.Errorf("expected default values: %+v", nonCanonicalCase[0])
|
||||
}
|
||||
|
||||
errorCases := map[string][]Port{
|
||||
"name > 63 characters": {{Name: strings.Repeat("a", 64), ContainerPort: 80}},
|
||||
"name not a DNS label": {{Name: "a.b.c", ContainerPort: 80}},
|
||||
"name not unique": {
|
||||
{Name: "abc", ContainerPort: 80},
|
||||
{Name: "abc", ContainerPort: 81},
|
||||
},
|
||||
"zero container port": {{ContainerPort: 0}},
|
||||
"invalid container port": {{ContainerPort: 65536}},
|
||||
"invalid host port": {{ContainerPort: 80, HostPort: 65536}},
|
||||
"invalid protocol": {{ContainerPort: 80, Protocol: "ICMP"}},
|
||||
}
|
||||
for k, v := range errorCases {
|
||||
err := validatePorts(v)
|
||||
if err == nil {
|
||||
t.Errorf("expected failure for %s", k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateEnv(t *testing.T) {
|
||||
successCase := []EnvVar{
|
||||
{Name: "abc", Value: "value"},
|
||||
@@ -139,6 +185,10 @@ func TestValidateContainers(t *testing.T) {
|
||||
{Name: "abc", Image: "image"},
|
||||
},
|
||||
"zero-length image": {{Name: "abc", Image: ""}},
|
||||
"host port not unique": {
|
||||
{Name: "abc", Image: "image", Ports: []Port{{ContainerPort: 80, HostPort: 80}}},
|
||||
{Name: "def", Image: "image", Ports: []Port{{ContainerPort: 81, HostPort: 80}}},
|
||||
},
|
||||
"invalid env var name": {
|
||||
{Name: "abc", Image: "image", Env: []EnvVar{{Name: "ev.1"}}},
|
||||
},
|
||||
@@ -156,8 +206,8 @@ func TestValidateContainers(t *testing.T) {
|
||||
func TestValidateManifest(t *testing.T) {
|
||||
successCases := []ContainerManifest{
|
||||
{Version: "v1beta1", ID: "abc"},
|
||||
{Version: "v1beta1", ID: "123"},
|
||||
{Version: "v1beta1", ID: "abc.123.do-re-mi"},
|
||||
{Version: "v1beta2", ID: "123"},
|
||||
{Version: "V1BETA1", ID: "abc.123.do-re-mi"},
|
||||
{
|
||||
Version: "v1beta1",
|
||||
ID: "abc",
|
||||
@@ -170,6 +220,11 @@ func TestValidateManifest(t *testing.T) {
|
||||
WorkingDir: "/tmp",
|
||||
Memory: 1,
|
||||
CPU: 1,
|
||||
Ports: []Port{
|
||||
{Name: "p1", ContainerPort: 80, HostPort: 8080},
|
||||
{Name: "p2", ContainerPort: 81},
|
||||
{ContainerPort: 82},
|
||||
},
|
||||
Env: []EnvVar{
|
||||
{Name: "ev1", Value: "val1"},
|
||||
{Name: "ev2", Value: "val2"},
|
||||
|
Reference in New Issue
Block a user