2014-07-01 20:01:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-08-29 22:48:41 +00:00
|
|
|
package validation
|
2014-07-01 20:01:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-07-01 22:14:25 +00:00
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-08-20 03:54:20 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2014-09-16 14:04:12 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/capabilities"
|
2014-07-01 22:14:25 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-07-01 20:01:39 +00:00
|
|
|
)
|
|
|
|
|
2014-08-20 03:54:20 +00:00
|
|
|
func expectPrefix(t *testing.T, prefix string, errs errors.ErrorList) {
|
|
|
|
for i := range errs {
|
2014-10-07 20:54:41 +00:00
|
|
|
if f, p := errs[i].(errors.ValidationError).Field, prefix; !strings.HasPrefix(f, p) {
|
|
|
|
t.Errorf("expected prefix '%s' for field '%s' (%v)", p, f, errs[i])
|
2014-08-20 03:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 21:40:36 +00:00
|
|
|
func TestValidateVolumes(t *testing.T) {
|
2014-08-30 01:20:27 +00:00
|
|
|
successCase := []api.Volume{
|
2014-07-16 19:32:59 +00:00
|
|
|
{Name: "abc"},
|
2014-10-01 20:35:21 +00:00
|
|
|
{Name: "123", Source: &api.VolumeSource{HostDir: &api.HostDir{"/mnt/path2"}}},
|
|
|
|
{Name: "abc-123", Source: &api.VolumeSource{HostDir: &api.HostDir{"/mnt/path3"}}},
|
|
|
|
{Name: "empty", Source: &api.VolumeSource{EmptyDir: &api.EmptyDir{}}},
|
2014-08-05 17:58:43 +00:00
|
|
|
{Name: "gcepd", Source: &api.VolumeSource{GCEPersistentDisk: &api.GCEPersistentDisk{"my-PD", "ext4", 1, false}}},
|
2014-07-01 21:40:36 +00:00
|
|
|
}
|
2014-07-08 06:20:30 +00:00
|
|
|
names, errs := validateVolumes(successCase)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
2014-07-01 21:40:36 +00:00
|
|
|
}
|
2014-08-05 17:58:43 +00:00
|
|
|
if len(names) != 5 || !names.HasAll("abc", "123", "abc-123", "empty", "gcepd") {
|
2014-07-01 21:40:36 +00:00
|
|
|
t.Errorf("wrong names result: %v", names)
|
|
|
|
}
|
|
|
|
|
2014-08-20 03:54:20 +00:00
|
|
|
errorCases := map[string]struct {
|
2014-08-30 01:20:27 +00:00
|
|
|
V []api.Volume
|
2014-08-20 03:54:20 +00:00
|
|
|
T errors.ValidationErrorType
|
|
|
|
F string
|
|
|
|
}{
|
2014-08-30 01:20:27 +00:00
|
|
|
"zero-length name": {[]api.Volume{{Name: ""}}, errors.ValidationErrorTypeRequired, "[0].name"},
|
|
|
|
"name > 63 characters": {[]api.Volume{{Name: strings.Repeat("a", 64)}}, errors.ValidationErrorTypeInvalid, "[0].name"},
|
|
|
|
"name not a DNS label": {[]api.Volume{{Name: "a.b.c"}}, errors.ValidationErrorTypeInvalid, "[0].name"},
|
|
|
|
"name not unique": {[]api.Volume{{Name: "abc"}, {Name: "abc"}}, errors.ValidationErrorTypeDuplicate, "[1].name"},
|
2014-07-01 21:40:36 +00:00
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
2014-08-20 03:54:20 +00:00
|
|
|
_, errs := validateVolumes(v.V)
|
|
|
|
if len(errs) == 0 {
|
2014-10-10 22:34:48 +00:00
|
|
|
t.Errorf("expected failure %s for %v", k, v.V)
|
2014-08-20 03:54:20 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i := range errs {
|
|
|
|
if errs[i].(errors.ValidationError).Type != v.T {
|
|
|
|
t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
|
|
|
|
}
|
|
|
|
if errs[i].(errors.ValidationError).Field != v.F {
|
|
|
|
t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i])
|
|
|
|
}
|
2014-07-01 22:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 04:32:56 +00:00
|
|
|
func TestValidatePorts(t *testing.T) {
|
2014-08-30 01:20:27 +00:00
|
|
|
successCase := []api.Port{
|
2014-07-08 04:32:56 +00:00
|
|
|
{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},
|
|
|
|
}
|
2014-07-08 06:20:30 +00:00
|
|
|
if errs := validatePorts(successCase); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
nonCanonicalCase := []api.Port{
|
2014-07-08 04:32:56 +00:00
|
|
|
{ContainerPort: 80},
|
|
|
|
}
|
2014-07-08 06:20:30 +00:00
|
|
|
if errs := validatePorts(nonCanonicalCase); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
2014-08-19 22:18:49 +00:00
|
|
|
if nonCanonicalCase[0].HostPort != 0 || nonCanonicalCase[0].Protocol != "TCP" {
|
2014-07-08 04:32:56 +00:00
|
|
|
t.Errorf("expected default values: %+v", nonCanonicalCase[0])
|
|
|
|
}
|
|
|
|
|
2014-08-20 03:54:20 +00:00
|
|
|
errorCases := map[string]struct {
|
2014-08-30 01:20:27 +00:00
|
|
|
P []api.Port
|
2014-08-20 03:54:20 +00:00
|
|
|
T errors.ValidationErrorType
|
|
|
|
F string
|
|
|
|
}{
|
2014-08-30 01:20:27 +00:00
|
|
|
"name > 63 characters": {[]api.Port{{Name: strings.Repeat("a", 64), ContainerPort: 80}}, errors.ValidationErrorTypeInvalid, "[0].name"},
|
|
|
|
"name not a DNS label": {[]api.Port{{Name: "a.b.c", ContainerPort: 80}}, errors.ValidationErrorTypeInvalid, "[0].name"},
|
|
|
|
"name not unique": {[]api.Port{
|
2014-07-08 04:32:56 +00:00
|
|
|
{Name: "abc", ContainerPort: 80},
|
|
|
|
{Name: "abc", ContainerPort: 81},
|
2014-08-20 03:54:20 +00:00
|
|
|
}, errors.ValidationErrorTypeDuplicate, "[1].name"},
|
2014-08-30 01:20:27 +00:00
|
|
|
"zero container port": {[]api.Port{{ContainerPort: 0}}, errors.ValidationErrorTypeRequired, "[0].containerPort"},
|
|
|
|
"invalid container port": {[]api.Port{{ContainerPort: 65536}}, errors.ValidationErrorTypeInvalid, "[0].containerPort"},
|
|
|
|
"invalid host port": {[]api.Port{{ContainerPort: 80, HostPort: 65536}}, errors.ValidationErrorTypeInvalid, "[0].hostPort"},
|
|
|
|
"invalid protocol": {[]api.Port{{ContainerPort: 80, Protocol: "ICMP"}}, errors.ValidationErrorTypeNotSupported, "[0].protocol"},
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
2014-08-20 03:54:20 +00:00
|
|
|
errs := validatePorts(v.P)
|
|
|
|
if len(errs) == 0 {
|
2014-07-08 04:32:56 +00:00
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
2014-08-20 03:54:20 +00:00
|
|
|
for i := range errs {
|
|
|
|
if errs[i].(errors.ValidationError).Type != v.T {
|
|
|
|
t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
|
|
|
|
}
|
|
|
|
if errs[i].(errors.ValidationError).Field != v.F {
|
|
|
|
t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i])
|
|
|
|
}
|
|
|
|
}
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 22:56:30 +00:00
|
|
|
func TestValidateEnv(t *testing.T) {
|
2014-08-30 01:20:27 +00:00
|
|
|
successCase := []api.EnvVar{
|
2014-07-01 22:56:30 +00:00
|
|
|
{Name: "abc", Value: "value"},
|
|
|
|
{Name: "ABC", Value: "value"},
|
|
|
|
{Name: "AbC_123", Value: "value"},
|
|
|
|
{Name: "abc", Value: ""},
|
|
|
|
}
|
2014-07-08 06:20:30 +00:00
|
|
|
if errs := validateEnv(successCase); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
2014-07-01 22:56:30 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
errorCases := map[string][]api.EnvVar{
|
2014-07-01 22:56:30 +00:00
|
|
|
"zero-length name": {{Name: ""}},
|
|
|
|
"name not a C identifier": {{Name: "a.b.c"}},
|
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
2014-07-08 06:20:30 +00:00
|
|
|
if errs := validateEnv(v); len(errs) == 0 {
|
2014-07-01 21:40:36 +00:00
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 02:46:56 +00:00
|
|
|
func TestValidateVolumeMounts(t *testing.T) {
|
|
|
|
volumes := util.NewStringSet("abc", "123", "abc-123")
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
successCase := []api.VolumeMount{
|
2014-07-05 02:46:56 +00:00
|
|
|
{Name: "abc", MountPath: "/foo"},
|
|
|
|
{Name: "123", MountPath: "/foo"},
|
|
|
|
{Name: "abc-123", MountPath: "/bar"},
|
|
|
|
}
|
2014-07-08 06:20:30 +00:00
|
|
|
if errs := validateVolumeMounts(successCase, volumes); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
2014-07-05 02:46:56 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
errorCases := map[string][]api.VolumeMount{
|
2014-07-05 02:46:56 +00:00
|
|
|
"empty name": {{Name: "", MountPath: "/foo"}},
|
|
|
|
"name not found": {{Name: "", MountPath: "/foo"}},
|
|
|
|
"empty mountpath": {{Name: "abc", MountPath: ""}},
|
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
2014-07-08 06:20:30 +00:00
|
|
|
if errs := validateVolumeMounts(v, volumes); len(errs) == 0 {
|
2014-07-05 02:46:56 +00:00
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 22:14:25 +00:00
|
|
|
func TestValidateContainers(t *testing.T) {
|
|
|
|
volumes := util.StringSet{}
|
2014-09-16 22:18:33 +00:00
|
|
|
capabilities.SetForTests(capabilities.Capabilities{
|
2014-09-16 14:04:12 +00:00
|
|
|
AllowPrivileged: true,
|
|
|
|
})
|
2014-07-01 22:14:25 +00:00
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
successCase := []api.Container{
|
2014-07-01 22:14:25 +00:00
|
|
|
{Name: "abc", Image: "image"},
|
|
|
|
{Name: "123", Image: "image"},
|
|
|
|
{Name: "abc-123", Image: "image"},
|
2014-09-12 23:04:10 +00:00
|
|
|
{
|
|
|
|
Name: "life-123",
|
|
|
|
Image: "image",
|
|
|
|
Lifecycle: &api.Lifecycle{
|
|
|
|
PreStop: &api.Handler{
|
|
|
|
Exec: &api.ExecAction{Command: []string{"ls", "-l"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-09-16 14:04:12 +00:00
|
|
|
{Name: "abc-1234", Image: "image", Privileged: true},
|
2014-07-01 22:14:25 +00:00
|
|
|
}
|
2014-07-08 06:20:30 +00:00
|
|
|
if errs := validateContainers(successCase, volumes); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
2014-07-01 22:14:25 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 22:18:33 +00:00
|
|
|
capabilities.SetForTests(capabilities.Capabilities{
|
2014-09-16 14:04:12 +00:00
|
|
|
AllowPrivileged: false,
|
|
|
|
})
|
2014-08-30 01:20:27 +00:00
|
|
|
errorCases := map[string][]api.Container{
|
2014-07-01 22:14:25 +00:00
|
|
|
"zero-length name": {{Name: "", Image: "image"}},
|
|
|
|
"name > 63 characters": {{Name: strings.Repeat("a", 64), Image: "image"}},
|
|
|
|
"name not a DNS label": {{Name: "a.b.c", Image: "image"}},
|
|
|
|
"name not unique": {
|
|
|
|
{Name: "abc", Image: "image"},
|
|
|
|
{Name: "abc", Image: "image"},
|
|
|
|
},
|
|
|
|
"zero-length image": {{Name: "abc", Image: ""}},
|
2014-07-08 04:32:56 +00:00
|
|
|
"host port not unique": {
|
2014-08-30 01:20:27 +00:00
|
|
|
{Name: "abc", Image: "image", Ports: []api.Port{{ContainerPort: 80, HostPort: 80}}},
|
|
|
|
{Name: "def", Image: "image", Ports: []api.Port{{ContainerPort: 81, HostPort: 80}}},
|
2014-07-08 04:32:56 +00:00
|
|
|
},
|
2014-07-01 22:56:30 +00:00
|
|
|
"invalid env var name": {
|
2014-08-30 01:20:27 +00:00
|
|
|
{Name: "abc", Image: "image", Env: []api.EnvVar{{Name: "ev.1"}}},
|
2014-07-01 22:56:30 +00:00
|
|
|
},
|
2014-07-05 02:46:56 +00:00
|
|
|
"unknown volume name": {
|
2014-08-30 01:20:27 +00:00
|
|
|
{Name: "abc", Image: "image", VolumeMounts: []api.VolumeMount{{Name: "anything", MountPath: "/foo"}}},
|
2014-07-05 02:46:56 +00:00
|
|
|
},
|
2014-09-12 23:04:10 +00:00
|
|
|
"invalid lifecycle, no exec command.": {
|
|
|
|
{
|
|
|
|
Name: "life-123",
|
|
|
|
Image: "image",
|
|
|
|
Lifecycle: &api.Lifecycle{
|
|
|
|
PreStop: &api.Handler{
|
|
|
|
Exec: &api.ExecAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"invalid lifecycle, no http path.": {
|
|
|
|
{
|
|
|
|
Name: "life-123",
|
|
|
|
Image: "image",
|
|
|
|
Lifecycle: &api.Lifecycle{
|
|
|
|
PreStop: &api.Handler{
|
|
|
|
HTTPGet: &api.HTTPGetAction{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"invalid lifecycle, no action.": {
|
|
|
|
{
|
|
|
|
Name: "life-123",
|
|
|
|
Image: "image",
|
|
|
|
Lifecycle: &api.Lifecycle{
|
|
|
|
PreStop: &api.Handler{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-09-16 14:04:12 +00:00
|
|
|
"privilege disabled": {
|
|
|
|
{Name: "abc", Image: "image", Privileged: true},
|
|
|
|
},
|
2014-07-01 22:14:25 +00:00
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
2014-07-08 06:20:30 +00:00
|
|
|
if errs := validateContainers(v, volumes); len(errs) == 0 {
|
2014-07-01 22:14:25 +00:00
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:25:17 +00:00
|
|
|
func TestValidateRestartPolicy(t *testing.T) {
|
|
|
|
successCases := []api.RestartPolicy{
|
|
|
|
{},
|
|
|
|
{Always: &api.RestartPolicyAlways{}},
|
|
|
|
{OnFailure: &api.RestartPolicyOnFailure{}},
|
|
|
|
{Never: &api.RestartPolicyNever{}},
|
|
|
|
}
|
|
|
|
for _, policy := range successCases {
|
|
|
|
if errs := validateRestartPolicy(&policy); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorCases := []api.RestartPolicy{
|
|
|
|
{Always: &api.RestartPolicyAlways{}, Never: &api.RestartPolicyNever{}},
|
|
|
|
{Never: &api.RestartPolicyNever{}, OnFailure: &api.RestartPolicyOnFailure{}},
|
|
|
|
}
|
|
|
|
for k, policy := range errorCases {
|
|
|
|
if errs := validateRestartPolicy(&policy); len(errs) == 0 {
|
2014-10-10 00:06:32 +00:00
|
|
|
t.Errorf("expected failure for %d", k)
|
2014-08-26 18:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
noPolicySpecified := api.RestartPolicy{}
|
|
|
|
errs := validateRestartPolicy(&noPolicySpecified)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
|
|
|
}
|
|
|
|
if noPolicySpecified.Always == nil {
|
|
|
|
t.Errorf("expected Always policy specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-01 20:01:39 +00:00
|
|
|
func TestValidateManifest(t *testing.T) {
|
2014-08-30 01:20:27 +00:00
|
|
|
successCases := []api.ContainerManifest{
|
2014-07-01 20:01:39 +00:00
|
|
|
{Version: "v1beta1", ID: "abc"},
|
2014-07-08 04:32:56 +00:00
|
|
|
{Version: "v1beta2", ID: "123"},
|
|
|
|
{Version: "V1BETA1", ID: "abc.123.do-re-mi"},
|
2014-07-01 21:40:36 +00:00
|
|
|
{
|
|
|
|
Version: "v1beta1",
|
|
|
|
ID: "abc",
|
2014-10-01 20:35:21 +00:00
|
|
|
Volumes: []api.Volume{{Name: "vol1", Source: &api.VolumeSource{HostDir: &api.HostDir{"/mnt/vol1"}}},
|
|
|
|
{Name: "vol2", Source: &api.VolumeSource{HostDir: &api.HostDir{"/mnt/vol2"}}}},
|
2014-08-30 01:20:27 +00:00
|
|
|
Containers: []api.Container{
|
2014-07-01 22:14:25 +00:00
|
|
|
{
|
|
|
|
Name: "abc",
|
|
|
|
Image: "image",
|
|
|
|
Command: []string{"foo", "bar"},
|
|
|
|
WorkingDir: "/tmp",
|
|
|
|
Memory: 1,
|
|
|
|
CPU: 1,
|
2014-08-30 01:20:27 +00:00
|
|
|
Ports: []api.Port{
|
2014-07-08 04:32:56 +00:00
|
|
|
{Name: "p1", ContainerPort: 80, HostPort: 8080},
|
|
|
|
{Name: "p2", ContainerPort: 81},
|
|
|
|
{ContainerPort: 82},
|
|
|
|
},
|
2014-08-30 01:20:27 +00:00
|
|
|
Env: []api.EnvVar{
|
2014-07-01 22:56:30 +00:00
|
|
|
{Name: "ev1", Value: "val1"},
|
|
|
|
{Name: "ev2", Value: "val2"},
|
2014-07-28 22:13:17 +00:00
|
|
|
{Name: "EV3", Value: "val3"},
|
2014-07-01 22:56:30 +00:00
|
|
|
},
|
2014-08-30 01:20:27 +00:00
|
|
|
VolumeMounts: []api.VolumeMount{
|
2014-07-05 02:46:56 +00:00
|
|
|
{Name: "vol1", MountPath: "/foo"},
|
2014-08-27 00:28:36 +00:00
|
|
|
{Name: "vol1", MountPath: "/bar"},
|
2014-07-05 02:46:56 +00:00
|
|
|
},
|
2014-07-01 22:14:25 +00:00
|
|
|
},
|
|
|
|
},
|
2014-07-01 21:40:36 +00:00
|
|
|
},
|
2014-07-01 20:01:39 +00:00
|
|
|
}
|
|
|
|
for _, manifest := range successCases {
|
2014-07-08 06:20:30 +00:00
|
|
|
if errs := ValidateManifest(&manifest); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
2014-07-01 20:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
errorCases := map[string]api.ContainerManifest{
|
2014-07-15 14:52:39 +00:00
|
|
|
"empty version": {Version: "", ID: "abc"},
|
|
|
|
"invalid version": {Version: "bogus", ID: "abc"},
|
2014-07-01 21:40:36 +00:00
|
|
|
"invalid volume name": {
|
|
|
|
Version: "v1beta1",
|
|
|
|
ID: "abc",
|
2014-08-30 01:20:27 +00:00
|
|
|
Volumes: []api.Volume{{Name: "vol.1"}},
|
2014-07-01 21:40:36 +00:00
|
|
|
},
|
2014-07-01 22:14:25 +00:00
|
|
|
"invalid container name": {
|
|
|
|
Version: "v1beta1",
|
|
|
|
ID: "abc",
|
2014-08-30 01:20:27 +00:00
|
|
|
Containers: []api.Container{{Name: "ctr.1", Image: "image"}},
|
2014-07-01 22:14:25 +00:00
|
|
|
},
|
2014-07-01 20:01:39 +00:00
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
2014-07-08 06:20:30 +00:00
|
|
|
if errs := ValidateManifest(&v); len(errs) == 0 {
|
2014-07-01 20:01:39 +00:00
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-10 19:45:01 +00:00
|
|
|
|
2014-07-22 18:45:12 +00:00
|
|
|
func TestValidatePod(t *testing.T) {
|
2014-08-30 01:20:27 +00:00
|
|
|
errs := ValidatePod(&api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo", Namespace: api.NamespaceDefault,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
2014-07-22 18:45:12 +00:00
|
|
|
},
|
2014-08-30 01:20:27 +00:00
|
|
|
DesiredState: api.PodState{
|
2014-08-26 18:25:17 +00:00
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Version: "v1beta1",
|
|
|
|
ID: "abc",
|
|
|
|
RestartPolicy: api.RestartPolicy{
|
|
|
|
Always: &api.RestartPolicyAlways{},
|
|
|
|
},
|
|
|
|
},
|
2014-07-22 18:45:12 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("Unexpected non-zero error list: %#v", errs)
|
|
|
|
}
|
2014-08-30 01:20:27 +00:00
|
|
|
errs = ValidatePod(&api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
2014-07-22 18:45:12 +00:00
|
|
|
},
|
2014-08-30 01:20:27 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{Version: "v1beta1", ID: "abc"},
|
2014-07-22 18:45:12 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("Unexpected non-zero error list: %#v", errs)
|
|
|
|
}
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
errs = ValidatePod(&api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo", Namespace: api.NamespaceDefault,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
2014-07-22 18:45:12 +00:00
|
|
|
},
|
2014-08-30 01:20:27 +00:00
|
|
|
DesiredState: api.PodState{
|
2014-08-26 18:25:17 +00:00
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Version: "v1beta1",
|
|
|
|
ID: "abc",
|
|
|
|
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{},
|
|
|
|
Never: &api.RestartPolicyNever{}},
|
|
|
|
},
|
2014-07-22 18:45:12 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if len(errs) != 1 {
|
|
|
|
t.Errorf("Unexpected error list: %#v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 03:30:34 +00:00
|
|
|
func TestValidatePodUpdate(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
a api.Pod
|
|
|
|
b api.Pod
|
|
|
|
isValid bool
|
|
|
|
test string
|
|
|
|
}{
|
|
|
|
{api.Pod{}, api.Pod{}, true, "nothing"},
|
|
|
|
{
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "bar"},
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
"ids",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"bar": "foo",
|
|
|
|
},
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
"labels",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
2014-10-10 03:30:34 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-10-10 03:30:34 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Image: "bar:V2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
"more containers",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-10-10 03:30:34 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-10-10 03:30:34 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
"image change",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-10-10 03:30:34 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V1",
|
|
|
|
CPU: 100,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-10-10 03:30:34 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V2",
|
|
|
|
CPU: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
"cpu change",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-10-10 03:30:34 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V1",
|
|
|
|
Ports: []api.Port{
|
|
|
|
{HostPort: 8080, ContainerPort: 80},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-10-10 03:30:34 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V2",
|
|
|
|
Ports: []api.Port{
|
|
|
|
{HostPort: 8000, ContainerPort: 80},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
"port change",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
errs := ValidatePodUpdate(&test.a, &test.b)
|
|
|
|
if test.isValid {
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("unexpected invalid: %s %v, %v", test.test, test.a, test.b)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(errs) == 0 {
|
|
|
|
t.Errorf("unexpected valid: %s %v, %v", test.test, test.a, test.b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 19:45:01 +00:00
|
|
|
func TestValidateService(t *testing.T) {
|
2014-09-10 16:41:31 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
svc api.Service
|
|
|
|
numErrs int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing id",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault},
|
|
|
|
Port: 8675,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
2014-09-10 16:41:31 +00:00
|
|
|
},
|
|
|
|
// Should fail because the ID is missing.
|
|
|
|
numErrs: 1,
|
2014-07-10 19:45:01 +00:00
|
|
|
},
|
2014-09-29 21:18:18 +00:00
|
|
|
{
|
|
|
|
name: "missing namespace",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
|
|
Port: 8675,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
2014-09-29 21:18:18 +00:00
|
|
|
},
|
|
|
|
// Should fail because the Namespace is missing.
|
|
|
|
numErrs: 1,
|
|
|
|
},
|
2014-09-10 16:41:31 +00:00
|
|
|
{
|
|
|
|
name: "invalid id",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "123abc", Namespace: api.NamespaceDefault},
|
|
|
|
Port: 8675,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
2014-09-10 16:41:31 +00:00
|
|
|
},
|
|
|
|
// Should fail because the ID is invalid.
|
|
|
|
numErrs: 1,
|
2014-08-22 21:44:21 +00:00
|
|
|
},
|
2014-09-10 16:41:31 +00:00
|
|
|
{
|
|
|
|
name: "missing port",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
2014-09-10 16:41:31 +00:00
|
|
|
},
|
|
|
|
// Should fail because the port number is missing/invalid.
|
|
|
|
numErrs: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid port",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
|
|
|
Port: 65536,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
2014-09-10 16:41:31 +00:00
|
|
|
},
|
|
|
|
// Should fail because the port number is invalid.
|
|
|
|
numErrs: 1,
|
|
|
|
},
|
2014-09-10 16:53:40 +00:00
|
|
|
{
|
|
|
|
name: "invalid protocol",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
|
|
|
Port: 8675,
|
|
|
|
Protocol: "INVALID",
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
2014-09-10 16:53:40 +00:00
|
|
|
},
|
|
|
|
// Should fail because the protocol is invalid.
|
|
|
|
numErrs: 1,
|
|
|
|
},
|
2014-09-10 16:41:31 +00:00
|
|
|
{
|
|
|
|
name: "missing selector",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: api.NamespaceDefault},
|
|
|
|
Port: 8675,
|
2014-09-10 16:41:31 +00:00
|
|
|
},
|
|
|
|
// Should fail because the selector is missing.
|
|
|
|
numErrs: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid 1",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
|
|
|
Port: 1,
|
|
|
|
Protocol: "TCP",
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
2014-09-10 16:41:31 +00:00
|
|
|
},
|
|
|
|
numErrs: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid 2",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
|
|
|
Port: 65535,
|
|
|
|
Protocol: "UDP",
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
2014-09-10 16:41:31 +00:00
|
|
|
},
|
|
|
|
numErrs: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid 3",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
|
|
|
Port: 80,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
2014-09-10 16:41:31 +00:00
|
|
|
},
|
|
|
|
numErrs: 0,
|
2014-07-10 19:45:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-10 16:41:31 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
errs := ValidateService(&tc.svc)
|
|
|
|
if len(errs) != tc.numErrs {
|
|
|
|
t.Errorf("Unexpected error list for case %q: %+v", tc.name, errs)
|
|
|
|
}
|
2014-07-10 19:45:01 +00:00
|
|
|
}
|
2014-09-10 16:53:40 +00:00
|
|
|
|
|
|
|
svc := api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
Port: 6502,
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: api.NamespaceDefault},
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
2014-09-10 16:53:40 +00:00
|
|
|
}
|
|
|
|
errs := ValidateService(&svc)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("Unexpected non-zero error list: %#v", errs)
|
|
|
|
}
|
|
|
|
if svc.Protocol != "TCP" {
|
|
|
|
t.Errorf("Expected default protocol of 'TCP': %#v", errs)
|
|
|
|
}
|
2014-07-10 19:45:01 +00:00
|
|
|
}
|
2014-07-25 16:15:17 +00:00
|
|
|
|
|
|
|
func TestValidateReplicationController(t *testing.T) {
|
|
|
|
validSelector := map[string]string{"a": "b"}
|
2014-08-30 01:20:27 +00:00
|
|
|
validPodTemplate := api.PodTemplate{
|
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
2014-07-25 16:15:17 +00:00
|
|
|
Version: "v1beta1",
|
|
|
|
},
|
|
|
|
},
|
2014-08-22 00:02:39 +00:00
|
|
|
Labels: validSelector,
|
2014-07-25 16:15:17 +00:00
|
|
|
}
|
2014-08-05 17:58:43 +00:00
|
|
|
invalidVolumePodTemplate := api.PodTemplate{
|
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Version: "v1beta1",
|
|
|
|
Volumes: []api.Volume{{Name: "gcepd", Source: &api.VolumeSource{GCEPersistentDisk: &api.GCEPersistentDisk{"my-PD", "ext4", 1, false}}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-08-30 01:20:27 +00:00
|
|
|
successCases := []api.ReplicationController{
|
2014-07-25 16:15:17 +00:00
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault},
|
2014-08-30 01:20:27 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
2014-07-25 16:15:17 +00:00
|
|
|
ReplicaSelector: validSelector,
|
|
|
|
PodTemplate: validPodTemplate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc-123", Namespace: api.NamespaceDefault},
|
2014-08-30 01:20:27 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
2014-07-25 16:15:17 +00:00
|
|
|
ReplicaSelector: validSelector,
|
|
|
|
PodTemplate: validPodTemplate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, successCase := range successCases {
|
|
|
|
if errs := ValidateReplicationController(&successCase); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
errorCases := map[string]api.ReplicationController{
|
2014-07-25 16:15:17 +00:00
|
|
|
"zero-length ID": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "", Namespace: api.NamespaceDefault},
|
2014-09-29 21:18:18 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
ReplicaSelector: validSelector,
|
|
|
|
PodTemplate: validPodTemplate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"missing-namespace": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc-123"},
|
2014-08-30 01:20:27 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
2014-07-25 16:15:17 +00:00
|
|
|
ReplicaSelector: validSelector,
|
|
|
|
PodTemplate: validPodTemplate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"empty selector": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault},
|
2014-08-30 01:20:27 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
2014-07-25 16:15:17 +00:00
|
|
|
PodTemplate: validPodTemplate,
|
|
|
|
},
|
|
|
|
},
|
2014-08-22 00:02:39 +00:00
|
|
|
"selector_doesnt_match": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault},
|
2014-08-30 01:20:27 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
2014-08-22 00:02:39 +00:00
|
|
|
ReplicaSelector: map[string]string{"foo": "bar"},
|
|
|
|
PodTemplate: validPodTemplate,
|
|
|
|
},
|
|
|
|
},
|
2014-07-25 16:15:17 +00:00
|
|
|
"invalid manifest": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault},
|
2014-08-30 01:20:27 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
2014-07-25 16:15:17 +00:00
|
|
|
ReplicaSelector: validSelector,
|
|
|
|
},
|
|
|
|
},
|
2014-08-05 17:58:43 +00:00
|
|
|
"read-write presistent disk": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc"},
|
2014-08-05 17:58:43 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
|
|
|
ReplicaSelector: validSelector,
|
|
|
|
PodTemplate: invalidVolumePodTemplate,
|
|
|
|
},
|
|
|
|
},
|
2014-08-04 19:02:51 +00:00
|
|
|
"negative_replicas": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault},
|
2014-08-30 01:20:27 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
2014-08-13 00:18:07 +00:00
|
|
|
Replicas: -1,
|
2014-08-04 19:02:51 +00:00
|
|
|
ReplicaSelector: validSelector,
|
|
|
|
},
|
|
|
|
},
|
2014-07-25 16:15:17 +00:00
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
2014-08-20 03:54:20 +00:00
|
|
|
errs := ValidateReplicationController(&v)
|
|
|
|
if len(errs) == 0 {
|
2014-07-25 16:15:17 +00:00
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
2014-08-20 03:54:20 +00:00
|
|
|
for i := range errs {
|
|
|
|
field := errs[i].(errors.ValidationError).Field
|
|
|
|
if !strings.HasPrefix(field, "desiredState.podTemplate.") &&
|
2014-10-22 18:46:28 +00:00
|
|
|
field != "name" &&
|
2014-10-03 03:41:02 +00:00
|
|
|
field != "namespace" &&
|
2014-08-20 03:54:20 +00:00
|
|
|
field != "desiredState.replicaSelector" &&
|
2014-08-05 17:58:43 +00:00
|
|
|
field != "GCEPersistentDisk.ReadOnly" &&
|
2014-08-20 03:54:20 +00:00
|
|
|
field != "desiredState.replicas" {
|
|
|
|
t.Errorf("%s: missing prefix for: %v", k, errs[i])
|
|
|
|
}
|
|
|
|
}
|
2014-07-25 16:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-08 19:56:02 +00:00
|
|
|
|
|
|
|
func TestValidateBoundPodNoName(t *testing.T) {
|
|
|
|
errorCases := map[string]api.BoundPod{
|
|
|
|
// manifest is tested in api/validation_test.go, ensure it is invoked
|
2014-10-23 20:51:34 +00:00
|
|
|
"empty version": {ObjectMeta: api.ObjectMeta{Name: "test"}, Spec: api.PodSpec{Containers: []api.Container{{Name: ""}}}},
|
2014-10-08 19:56:02 +00:00
|
|
|
|
|
|
|
// Name
|
2014-10-23 20:51:34 +00:00
|
|
|
"zero-length name": {ObjectMeta: api.ObjectMeta{Name: ""}},
|
|
|
|
"name > 255 characters": {ObjectMeta: api.ObjectMeta{Name: strings.Repeat("a", 256)}},
|
|
|
|
"name not a DNS subdomain": {ObjectMeta: api.ObjectMeta{Name: "a.b.c."}},
|
|
|
|
"name with underscore": {ObjectMeta: api.ObjectMeta{Name: "a_b_c"}},
|
2014-10-08 19:56:02 +00:00
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
|
|
|
if errs := ValidateBoundPod(&v); len(errs) == 0 {
|
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|