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"
|
2015-01-05 21:16:18 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
|
2014-09-16 14:04:12 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/capabilities"
|
2014-10-31 06:03:52 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
2014-07-01 22:14:25 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2015-01-09 06:10:03 +00:00
|
|
|
utilerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
|
2014-07-01 20:01:39 +00:00
|
|
|
)
|
|
|
|
|
2014-10-24 16:43:14 +00:00
|
|
|
func expectPrefix(t *testing.T, prefix string, errs errors.ValidationErrorList) {
|
2014-08-20 03:54:20 +00:00
|
|
|
for i := range errs {
|
2014-11-20 22:11:23 +00:00
|
|
|
if f, p := errs[i].(*errors.ValidationError).Field, prefix; !strings.HasPrefix(f, p) {
|
2014-10-07 20:54:41 +00:00
|
|
|
t.Errorf("expected prefix '%s' for field '%s' (%v)", p, f, errs[i])
|
2014-08-20 03:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 23:56:38 +00:00
|
|
|
// Ensure custom name functions are allowed
|
|
|
|
func TestValidateObjectMetaCustomName(t *testing.T) {
|
|
|
|
errs := ValidateObjectMeta(&api.ObjectMeta{Name: "test", GenerateName: "foo"}, false, func(s string, prefix bool) (bool, string) {
|
|
|
|
if s == "test" {
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
return false, "name-gen"
|
|
|
|
})
|
|
|
|
if len(errs) != 1 {
|
|
|
|
t.Fatalf("unexpected errors: %v", errs)
|
|
|
|
}
|
|
|
|
if !strings.Contains(errs[0].Error(), "name-gen") {
|
|
|
|
t.Errorf("unexpected error message: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure trailing slash is allowed in generate name
|
|
|
|
func TestValidateObjectMetaTrimsTrailingSlash(t *testing.T) {
|
|
|
|
errs := ValidateObjectMeta(&api.ObjectMeta{Name: "test", GenerateName: "foo-"}, false, nameIsDNSSubdomain)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Fatalf("unexpected errors: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 06:27:11 +00:00
|
|
|
func TestValidateLabels(t *testing.T) {
|
|
|
|
successCases := []map[string]string{
|
|
|
|
{"simple": "bar"},
|
|
|
|
{"now-with-dashes": "bar"},
|
2014-11-20 06:27:11 +00:00
|
|
|
{"1-starts-with-num": "bar"},
|
|
|
|
{"1234": "bar"},
|
2014-11-20 06:27:11 +00:00
|
|
|
{"simple/simple": "bar"},
|
|
|
|
{"now-with-dashes/simple": "bar"},
|
|
|
|
{"now-with-dashes/now-with-dashes": "bar"},
|
|
|
|
{"now.with.dots/simple": "bar"},
|
|
|
|
{"now-with.dashes-and.dots/simple": "bar"},
|
2014-11-20 06:27:11 +00:00
|
|
|
{"1-num.2-num/3-num": "bar"},
|
|
|
|
{"1234/5678": "bar"},
|
|
|
|
{"1.2.3.4/5678": "bar"},
|
2014-11-20 06:27:11 +00:00
|
|
|
}
|
|
|
|
for i := range successCases {
|
2015-01-20 03:32:39 +00:00
|
|
|
errs := ValidateLabels(successCases[i], "field")
|
2014-11-20 06:27:11 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("case[%d] expected success, got %#v", i, errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorCases := []map[string]string{
|
2014-11-20 06:27:11 +00:00
|
|
|
{"NoUppercase123": "bar"},
|
|
|
|
{"nospecialchars^=@": "bar"},
|
|
|
|
{"cantendwithadash-": "bar"},
|
2014-11-20 06:27:11 +00:00
|
|
|
{"only/one/slash": "bar"},
|
2014-11-20 06:27:11 +00:00
|
|
|
{strings.Repeat("a", 254): "bar"},
|
2014-11-20 06:27:11 +00:00
|
|
|
}
|
|
|
|
for i := range errorCases {
|
2015-01-20 03:32:39 +00:00
|
|
|
errs := ValidateLabels(errorCases[i], "field")
|
2014-11-20 06:27:11 +00:00
|
|
|
if len(errs) != 1 {
|
|
|
|
t.Errorf("case[%d] expected failure", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 00:03:04 +00:00
|
|
|
func TestValidateAnnotations(t *testing.T) {
|
|
|
|
successCases := []map[string]string{
|
|
|
|
{"simple": "bar"},
|
|
|
|
{"now-with-dashes": "bar"},
|
|
|
|
{"1-starts-with-num": "bar"},
|
|
|
|
{"1234": "bar"},
|
|
|
|
{"simple/simple": "bar"},
|
|
|
|
{"now-with-dashes/simple": "bar"},
|
|
|
|
{"now-with-dashes/now-with-dashes": "bar"},
|
|
|
|
{"now.with.dots/simple": "bar"},
|
|
|
|
{"now-with.dashes-and.dots/simple": "bar"},
|
|
|
|
{"1-num.2-num/3-num": "bar"},
|
|
|
|
{"1234/5678": "bar"},
|
|
|
|
{"1.2.3.4/5678": "bar"},
|
|
|
|
{"UpperCase123": "bar"},
|
|
|
|
}
|
|
|
|
for i := range successCases {
|
|
|
|
errs := ValidateAnnotations(successCases[i], "field")
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("case[%d] expected success, got %#v", i, errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorCases := []map[string]string{
|
|
|
|
{"nospecialchars^=@": "bar"},
|
|
|
|
{"cantendwithadash-": "bar"},
|
|
|
|
{"only/one/slash": "bar"},
|
|
|
|
{strings.Repeat("a", 254): "bar"},
|
|
|
|
}
|
|
|
|
for i := range errorCases {
|
|
|
|
errs := ValidateAnnotations(errorCases[i], "field")
|
|
|
|
if len(errs) != 1 {
|
|
|
|
t.Errorf("case[%d] expected failure", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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"},
|
2015-01-21 01:40:43 +00:00
|
|
|
{Name: "123", Source: api.VolumeSource{HostPath: &api.HostPath{"/mnt/path2"}}},
|
|
|
|
{Name: "abc-123", Source: api.VolumeSource{HostPath: &api.HostPath{"/mnt/path3"}}},
|
|
|
|
{Name: "empty", Source: api.VolumeSource{EmptyDir: &api.EmptyDir{}}},
|
|
|
|
{Name: "gcepd", Source: api.VolumeSource{GCEPersistentDisk: &api.GCEPersistentDisk{"my-PD", "ext4", 1, false}}},
|
|
|
|
{Name: "gitrepo", Source: api.VolumeSource{GitRepo: &api.GitRepo{"my-repo", "hashstring"}}},
|
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-11-24 04:03:11 +00:00
|
|
|
if len(names) != 6 || !names.HasAll("abc", "123", "abc-123", "empty", "gcepd", "gitrepo") {
|
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 {
|
2014-11-20 22:11:23 +00:00
|
|
|
if errs[i].(*errors.ValidationError).Type != v.T {
|
2014-08-20 03:54:20 +00:00
|
|
|
t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
|
|
|
|
}
|
2014-11-20 22:11:23 +00:00
|
|
|
if errs[i].(*errors.ValidationError).Field != v.F {
|
2014-08-20 03:54:20 +00:00
|
|
|
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 {
|
2014-11-20 22:11:23 +00:00
|
|
|
if errs[i].(*errors.ValidationError).Type != v.T {
|
2014-08-20 03:54:20 +00:00
|
|
|
t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
|
|
|
|
}
|
2014-11-20 22:11:23 +00:00
|
|
|
if errs[i].(*errors.ValidationError).Field != v.F {
|
2014-08-20 03:54:20 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 23:02:36 +00:00
|
|
|
func TestValidatePullPolicy(t *testing.T) {
|
|
|
|
type T struct {
|
|
|
|
Container api.Container
|
|
|
|
ExpectedPolicy api.PullPolicy
|
|
|
|
}
|
|
|
|
testCases := map[string]T{
|
|
|
|
"NotPresent1": {
|
2015-01-21 04:30:42 +00:00
|
|
|
api.Container{Name: "abc", Image: "image:latest", ImagePullPolicy: "IfNotPresent"},
|
2015-01-16 23:02:36 +00:00
|
|
|
api.PullIfNotPresent,
|
|
|
|
},
|
|
|
|
"NotPresent2": {
|
2015-01-21 04:30:42 +00:00
|
|
|
api.Container{Name: "abc1", Image: "image", ImagePullPolicy: "IfNotPresent"},
|
2015-01-16 23:02:36 +00:00
|
|
|
api.PullIfNotPresent,
|
|
|
|
},
|
|
|
|
"Always1": {
|
2015-01-21 04:30:42 +00:00
|
|
|
api.Container{Name: "123", Image: "image:latest", ImagePullPolicy: "Always"},
|
2015-01-16 23:02:36 +00:00
|
|
|
api.PullAlways,
|
|
|
|
},
|
|
|
|
"Always2": {
|
2015-01-21 04:30:42 +00:00
|
|
|
api.Container{Name: "1234", Image: "image", ImagePullPolicy: "Always"},
|
2015-01-16 23:02:36 +00:00
|
|
|
api.PullAlways,
|
|
|
|
},
|
|
|
|
"Never1": {
|
2015-01-21 04:30:42 +00:00
|
|
|
api.Container{Name: "abc-123", Image: "image:latest", ImagePullPolicy: "Never"},
|
2015-01-16 23:02:36 +00:00
|
|
|
api.PullNever,
|
|
|
|
},
|
|
|
|
"Never2": {
|
2015-01-21 04:30:42 +00:00
|
|
|
api.Container{Name: "abc-1234", Image: "image", ImagePullPolicy: "Never"},
|
2015-01-16 23:02:36 +00:00
|
|
|
api.PullNever,
|
|
|
|
},
|
|
|
|
"DefaultToNotPresent": {api.Container{Name: "notPresent", Image: "image"}, api.PullIfNotPresent},
|
|
|
|
"DefaultToNotPresent2": {api.Container{Name: "notPresent1", Image: "image:sometag"}, api.PullIfNotPresent},
|
|
|
|
"DefaultToAlways1": {api.Container{Name: "always", Image: "image:latest"}, api.PullAlways},
|
|
|
|
"DefaultToAlways2": {api.Container{Name: "always", Image: "foo.bar.com:5000/my/image:latest"}, api.PullAlways},
|
|
|
|
}
|
|
|
|
for k, v := range testCases {
|
|
|
|
ctr := &v.Container
|
|
|
|
errs := validatePullPolicyWithDefault(ctr)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("case[%s] expected success, got %#v", k, errs)
|
|
|
|
}
|
|
|
|
if ctr.ImagePullPolicy != v.ExpectedPolicy {
|
|
|
|
t.Errorf("case[%s] expected policy %v, got %v", k, v.ExpectedPolicy, ctr.ImagePullPolicy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-25 04:19:36 +00:00
|
|
|
func getResourceLimits(cpu, memory string) api.ResourceList {
|
|
|
|
res := api.ResourceList{}
|
|
|
|
res[api.ResourceCPU] = resource.MustParse(cpu)
|
|
|
|
res[api.ResourceMemory] = resource.MustParse(memory)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
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"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-01-25 04:19:36 +00:00
|
|
|
{
|
|
|
|
Name: "resources-test",
|
|
|
|
Image: "image",
|
|
|
|
Resources: api.ResourceRequirementSpec{
|
|
|
|
Limits: api.ResourceList{
|
|
|
|
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
|
|
|
|
api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
|
|
|
|
api.ResourceName("my.org/resource"): resource.MustParse("10m"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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},
|
|
|
|
},
|
2015-01-25 04:19:36 +00:00
|
|
|
"invalid compute resource": {
|
|
|
|
{
|
|
|
|
Name: "abc-123",
|
|
|
|
Image: "image",
|
|
|
|
Resources: api.ResourceRequirementSpec{
|
|
|
|
Limits: api.ResourceList{
|
|
|
|
"disk": resource.MustParse("10G"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Resource CPU invalid": {
|
|
|
|
{
|
|
|
|
Name: "abc-123",
|
|
|
|
Image: "image",
|
|
|
|
Resources: api.ResourceRequirementSpec{
|
|
|
|
Limits: getResourceLimits("-10", "0"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Resource Memory invalid": {
|
|
|
|
{
|
|
|
|
Name: "abc-123",
|
|
|
|
Image: "image",
|
|
|
|
Resources: api.ResourceRequirementSpec{
|
|
|
|
Limits: getResourceLimits("0", "-10"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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")
|
|
|
|
}
|
2015-01-06 19:11:52 +00:00
|
|
|
}
|
2014-08-26 18:25:17 +00:00
|
|
|
|
2015-01-06 19:11:52 +00:00
|
|
|
func TestValidateDNSPolicy(t *testing.T) {
|
|
|
|
successCases := []api.DNSPolicy{api.DNSClusterFirst, api.DNSDefault, api.DNSPolicy("")}
|
|
|
|
for _, policy := range successCases {
|
|
|
|
if errs := validateDNSPolicy(&policy); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorCases := []api.DNSPolicy{api.DNSPolicy("invalid")}
|
|
|
|
for _, policy := range errorCases {
|
|
|
|
if errs := validateDNSPolicy(&policy); len(errs) == 0 {
|
|
|
|
t.Errorf("expected failure for %v", policy)
|
|
|
|
}
|
|
|
|
}
|
2014-08-26 18:25:17 +00:00
|
|
|
}
|
|
|
|
|
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",
|
2015-01-21 01:40:43 +00:00
|
|
|
Volumes: []api.Volume{{Name: "vol1", Source: api.VolumeSource{HostPath: &api.HostPath{"/mnt/vol1"}}},
|
|
|
|
{Name: "vol2", Source: api.VolumeSource{HostPath: &api.HostPath{"/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",
|
2015-01-25 04:19:36 +00:00
|
|
|
Resources: api.ResourceRequirementSpec{
|
|
|
|
Limits: api.ResourceList{
|
|
|
|
"cpu": resource.MustParse("1"),
|
|
|
|
"memory": resource.MustParse("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
|
|
|
|
2015-01-06 19:11:52 +00:00
|
|
|
func TestValidatePodSpec(t *testing.T) {
|
|
|
|
successCases := []api.PodSpec{
|
|
|
|
{}, // empty is valid, if not very useful */
|
|
|
|
{ // Populate basic fields, leave defaults for most.
|
|
|
|
Volumes: []api.Volume{{Name: "vol"}},
|
|
|
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
|
|
|
},
|
|
|
|
{ // Populate all fields.
|
|
|
|
Volumes: []api.Volume{
|
|
|
|
{Name: "vol"},
|
|
|
|
},
|
|
|
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
|
|
|
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
NodeSelector: map[string]string{
|
|
|
|
"key": "value",
|
2014-10-23 20:51:34 +00:00
|
|
|
},
|
2015-01-06 19:11:52 +00:00
|
|
|
Host: "foobar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i := range successCases {
|
|
|
|
if errs := ValidatePodSpec(&successCases[i]); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
failureCases := map[string]api.PodSpec{
|
|
|
|
"bad volume": {
|
|
|
|
Volumes: []api.Volume{{}},
|
2014-07-22 18:45:12 +00:00
|
|
|
},
|
2015-01-06 19:11:52 +00:00
|
|
|
"bad container": {
|
|
|
|
Containers: []api.Container{{}},
|
|
|
|
},
|
|
|
|
"bad DNS policy": {
|
|
|
|
DNSPolicy: api.DNSPolicy("invalid"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for k, v := range failureCases {
|
|
|
|
if errs := ValidatePodSpec(&v); len(errs) == 0 {
|
|
|
|
t.Errorf("expected failure for %q", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultPod := api.PodSpec{} // all empty fields
|
|
|
|
if errs := ValidatePodSpec(&defaultPod); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
|
|
|
}
|
|
|
|
if util.AllPtrFieldsNil(defaultPod.RestartPolicy) {
|
|
|
|
t.Errorf("expected a default RestartPolicy")
|
|
|
|
}
|
|
|
|
if defaultPod.DNSPolicy == "" {
|
|
|
|
t.Errorf("expected a default DNSPolicy")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatePod(t *testing.T) {
|
|
|
|
successCases := []api.Pod{
|
|
|
|
{ // Mostly empty.
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "ns"},
|
|
|
|
},
|
|
|
|
{ // Basic fields.
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: "ns"},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Volumes: []api.Volume{{Name: "vol"}},
|
|
|
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
2014-08-26 18:25:17 +00:00
|
|
|
},
|
2014-07-22 18:45:12 +00:00
|
|
|
},
|
2015-01-06 19:11:52 +00:00
|
|
|
{ // Just about everything.
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc.123.do-re-mi", Namespace: "ns"},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Volumes: []api.Volume{
|
|
|
|
{Name: "vol"},
|
|
|
|
},
|
|
|
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
|
|
|
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
NodeSelector: map[string]string{
|
|
|
|
"key": "value",
|
|
|
|
},
|
|
|
|
Host: "foobar",
|
2014-10-23 20:51:34 +00:00
|
|
|
},
|
2014-07-22 18:45:12 +00:00
|
|
|
},
|
2015-01-06 19:11:52 +00:00
|
|
|
}
|
|
|
|
for _, pod := range successCases {
|
|
|
|
if errs := ValidatePod(&pod); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
|
|
|
}
|
2014-07-22 18:45:12 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 19:11:52 +00:00
|
|
|
errorCases := map[string]api.Pod{
|
|
|
|
"bad name": {ObjectMeta: api.ObjectMeta{Name: "", Namespace: "ns"}},
|
|
|
|
"bad namespace": {ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: ""}},
|
|
|
|
"bad spec": {
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "ns"},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{{}},
|
2014-10-23 20:14:13 +00:00
|
|
|
},
|
|
|
|
},
|
2015-01-24 14:36:22 +00:00
|
|
|
"bad label": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc",
|
|
|
|
Namespace: "ns",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"NoUppercaseOrSpecialCharsLike=Equals": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bad annotation": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc",
|
|
|
|
Namespace: "ns",
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"NoUppercaseOrSpecialCharsLike=Equals": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-01-06 19:11:52 +00:00
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
|
|
|
if errs := ValidatePod(&v); len(errs) == 0 {
|
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
2014-10-23 20:14:13 +00:00
|
|
|
}
|
2014-07-22 18:45:12 +00:00
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
},
|
2015-01-24 14:36:22 +00:00
|
|
|
{
|
|
|
|
api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"bar": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
"annotations",
|
|
|
|
},
|
2014-10-10 03:30:34 +00:00
|
|
|
{
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
2014-11-13 15:52:13 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V1",
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-11-13 15:52:13 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Image: "bar:V2",
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
"more containers",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-11-13 15:52:13 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V1",
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-11-13 15:52:13 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V2",
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
"image change",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-11-13 15:52:13 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V1",
|
2015-01-25 04:19:36 +00:00
|
|
|
Resources: api.ResourceRequirementSpec{
|
|
|
|
Limits: getResourceLimits("100m", "0"),
|
|
|
|
},
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-11-13 15:52:13 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V2",
|
2015-01-25 04:19:36 +00:00
|
|
|
Resources: api.ResourceRequirementSpec{
|
|
|
|
Limits: getResourceLimits("1000m", "0"),
|
|
|
|
},
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
"cpu change",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-11-13 15:52:13 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V1",
|
|
|
|
Ports: []api.Port{
|
|
|
|
{HostPort: 8080, ContainerPort: 80},
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-11-13 15:52:13 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo:V2",
|
|
|
|
Ports: []api.Port{
|
|
|
|
{HostPort: 8000, ContainerPort: 80},
|
2014-10-10 03:30:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 19:11:52 +00:00
|
|
|
func TestValidateBoundPods(t *testing.T) {
|
|
|
|
successCases := []api.BoundPod{
|
|
|
|
{ // Mostly empty.
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "ns"},
|
|
|
|
},
|
|
|
|
{ // Basic fields.
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: "ns"},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Volumes: []api.Volume{{Name: "vol"}},
|
|
|
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // Just about everything.
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc.123.do-re-mi", Namespace: "ns"},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Volumes: []api.Volume{
|
|
|
|
{Name: "vol"},
|
|
|
|
},
|
|
|
|
Containers: []api.Container{{Name: "ctr", Image: "image"}},
|
|
|
|
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
NodeSelector: map[string]string{
|
|
|
|
"key": "value",
|
|
|
|
},
|
|
|
|
Host: "foobar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, pod := range successCases {
|
|
|
|
if errs := ValidateBoundPod(&pod); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorCases := map[string]api.Pod{
|
|
|
|
"bad name": {ObjectMeta: api.ObjectMeta{Name: "", Namespace: "ns"}},
|
|
|
|
"bad namespace": {ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: ""}},
|
|
|
|
"bad spec": {
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "ns"},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{{}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
|
|
|
if errs := ValidatePod(&v); len(errs) == 0 {
|
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 19:45:01 +00:00
|
|
|
func TestValidateService(t *testing.T) {
|
2014-09-10 16:41:31 +00:00
|
|
|
testCases := []struct {
|
2014-10-31 06:03:52 +00:00
|
|
|
name string
|
|
|
|
svc api.Service
|
|
|
|
existing api.ServiceList
|
|
|
|
numErrs int
|
2014-09-10 16:41:31 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing id",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
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"},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
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{
|
2015-01-27 23:56:38 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "-123abc", Namespace: api.NamespaceDefault},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
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
|
|
|
},
|
2015-01-27 23:56:38 +00:00
|
|
|
{
|
|
|
|
name: "invalid generate.base",
|
|
|
|
svc: api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "valid",
|
|
|
|
GenerateName: "-123abc",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 8675,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Should fail because the Base value for generation is invalid
|
|
|
|
numErrs: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid generateName",
|
|
|
|
svc: api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "valid",
|
|
|
|
GenerateName: "abc1234567abc1234567abc1234567abc1234567abc1234567abc1234567",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 8675,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Should fail because the generate name type is invalid.
|
|
|
|
numErrs: 1,
|
|
|
|
},
|
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},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
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},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 66536,
|
|
|
|
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},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 8675,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
Protocol: "INVALID",
|
|
|
|
},
|
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},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 8675,
|
|
|
|
},
|
2014-09-10 16:41:31 +00:00
|
|
|
},
|
2014-11-18 17:49:00 +00:00
|
|
|
// Should be ok because the selector is missing.
|
|
|
|
numErrs: 0,
|
2014-09-10 16:41:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid 1",
|
|
|
|
svc: api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 8675,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
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},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 8675,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
Protocol: "UDP",
|
|
|
|
},
|
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},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 8675,
|
|
|
|
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-10-31 06:03:52 +00:00
|
|
|
{
|
2015-01-26 21:19:27 +00:00
|
|
|
name: "external port in use",
|
2014-10-31 06:03:52 +00:00
|
|
|
svc: api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
2014-11-05 06:52:06 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 80,
|
|
|
|
CreateExternalLoadBalancer: true,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
},
|
2014-10-31 06:03:52 +00:00
|
|
|
},
|
|
|
|
existing: api.ServiceList{
|
|
|
|
Items: []api.Service{
|
2015-01-08 15:25:14 +00:00
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "def123", Namespace: api.NamespaceDefault},
|
|
|
|
Spec: api.ServiceSpec{Port: 80, CreateExternalLoadBalancer: true},
|
|
|
|
},
|
2014-10-31 06:03:52 +00:00
|
|
|
},
|
|
|
|
},
|
2015-01-26 21:19:27 +00:00
|
|
|
numErrs: 0,
|
2014-10-31 06:03:52 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "same port in use, but not external",
|
|
|
|
svc: api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
2014-11-05 06:52:06 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 80,
|
|
|
|
CreateExternalLoadBalancer: true,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
},
|
2014-10-31 06:03:52 +00:00
|
|
|
},
|
|
|
|
existing: api.ServiceList{
|
|
|
|
Items: []api.Service{
|
2015-01-08 15:25:14 +00:00
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "def123", Namespace: api.NamespaceDefault},
|
|
|
|
Spec: api.ServiceSpec{Port: 80},
|
|
|
|
},
|
2014-10-31 06:03:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
numErrs: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "same port in use, but not external on input",
|
|
|
|
svc: api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
2014-11-05 06:52:06 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 80,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
},
|
2014-10-31 06:03:52 +00:00
|
|
|
},
|
|
|
|
existing: api.ServiceList{
|
|
|
|
Items: []api.Service{
|
2015-01-08 15:25:14 +00:00
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "def123", Namespace: api.NamespaceDefault},
|
|
|
|
Spec: api.ServiceSpec{Port: 80, CreateExternalLoadBalancer: true},
|
|
|
|
},
|
2014-10-31 06:03:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
numErrs: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "same port in use, but neither external",
|
|
|
|
svc: api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc123", Namespace: api.NamespaceDefault},
|
2014-11-05 06:52:06 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 80,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
},
|
2014-10-31 06:03:52 +00:00
|
|
|
},
|
|
|
|
existing: api.ServiceList{
|
|
|
|
Items: []api.Service{
|
2015-01-08 15:25:14 +00:00
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "def123", Namespace: api.NamespaceDefault},
|
|
|
|
Spec: api.ServiceSpec{Port: 80},
|
|
|
|
},
|
2014-10-31 06:03:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
numErrs: 0,
|
|
|
|
},
|
2014-10-23 20:14:13 +00:00
|
|
|
{
|
|
|
|
name: "invalid label",
|
|
|
|
svc: api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc123",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"NoUppercaseOrSpecialCharsLike=Equals": "bar",
|
|
|
|
},
|
|
|
|
},
|
2014-11-18 17:49:00 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 8675,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
numErrs: 1,
|
|
|
|
},
|
2015-01-24 14:36:22 +00:00
|
|
|
{
|
|
|
|
name: "invalid annotation",
|
|
|
|
svc: api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc123",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"NoUppercaseOrSpecialCharsLike=Equals": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 8675,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
numErrs: 1,
|
|
|
|
},
|
2014-11-18 17:49:00 +00:00
|
|
|
{
|
|
|
|
name: "invalid selector",
|
|
|
|
svc: api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc123",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 8675,
|
|
|
|
Selector: map[string]string{"foo": "bar", "NoUppercaseOrSpecialCharsLike=Equals": "bar"},
|
|
|
|
},
|
2014-10-23 20:14:13 +00:00
|
|
|
},
|
2014-11-18 17:49:00 +00:00
|
|
|
numErrs: 1,
|
2014-10-23 20:14:13 +00:00
|
|
|
},
|
2014-07-10 19:45:01 +00:00
|
|
|
}
|
|
|
|
|
2014-09-10 16:41:31 +00:00
|
|
|
for _, tc := range testCases {
|
2014-10-31 06:03:52 +00:00
|
|
|
registry := registrytest.NewServiceRegistry()
|
|
|
|
registry.List = tc.existing
|
2015-01-27 23:55:54 +00:00
|
|
|
errs := ValidateService(&tc.svc)
|
2014-09-10 16:41:31 +00:00
|
|
|
if len(errs) != tc.numErrs {
|
2015-01-09 06:10:03 +00:00
|
|
|
t.Errorf("Unexpected error list for case %q: %v", tc.name, utilerrors.NewAggregate(errs))
|
2014-09-10 16:41:31 +00:00
|
|
|
}
|
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
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: api.NamespaceDefault},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: 8675,
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
},
|
2014-09-10 16:53:40 +00:00
|
|
|
}
|
2015-01-27 23:55:54 +00:00
|
|
|
errs := ValidateService(&svc)
|
2014-09-10 16:53:40 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("Unexpected non-zero error list: %#v", errs)
|
|
|
|
}
|
2014-10-30 13:29:11 +00:00
|
|
|
if svc.Spec.Protocol != "TCP" {
|
2014-09-10 16:53:40 +00:00
|
|
|
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{
|
2014-11-07 02:08:46 +00:00
|
|
|
Spec: api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: validSelector,
|
2014-07-25 16:15:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-08-05 17:58:43 +00:00
|
|
|
invalidVolumePodTemplate := api.PodTemplate{
|
2014-11-07 02:08:46 +00:00
|
|
|
Spec: api.PodTemplateSpec{
|
|
|
|
Spec: api.PodSpec{
|
2015-01-21 01:40:43 +00:00
|
|
|
Volumes: []api.Volume{{Name: "gcepd", Source: api.VolumeSource{GCEPersistentDisk: &api.GCEPersistentDisk{"my-PD", "ext4", 1, false}}}},
|
2014-08-05 17:58:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-10-23 20:14:13 +00:00
|
|
|
invalidSelector := map[string]string{"NoUppercaseOrSpecialCharsLike=Equals": "b"}
|
|
|
|
invalidPodTemplate := api.PodTemplate{
|
2014-11-07 02:08:46 +00:00
|
|
|
Spec: api.PodTemplateSpec{
|
2014-11-18 04:08:23 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
RestartPolicy: api.RestartPolicy{
|
|
|
|
Always: &api.RestartPolicyAlways{},
|
|
|
|
},
|
|
|
|
},
|
2014-11-07 02:08:46 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: invalidSelector,
|
2014-10-23 20:14:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
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-11-07 02:08:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: validSelector,
|
|
|
|
Template: &validPodTemplate.Spec,
|
2014-07-25 16:15:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc-123", Namespace: api.NamespaceDefault},
|
2014-11-07 02:08:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: validSelector,
|
|
|
|
Template: &validPodTemplate.Spec,
|
2014-07-25 16:15:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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-11-07 02:08:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: validSelector,
|
|
|
|
Template: &validPodTemplate.Spec,
|
2014-09-29 21:18:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"missing-namespace": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc-123"},
|
2014-11-07 02:08:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: validSelector,
|
|
|
|
Template: &validPodTemplate.Spec,
|
2014-07-25 16:15:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"empty selector": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault},
|
2014-11-07 02:08:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Template: &validPodTemplate.Spec,
|
2014-07-25 16:15:17 +00:00
|
|
|
},
|
|
|
|
},
|
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-11-07 02:08:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: map[string]string{"foo": "bar"},
|
|
|
|
Template: &validPodTemplate.Spec,
|
2014-08-22 00:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
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-11-07 02:08:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: validSelector,
|
2014-07-25 16:15:17 +00:00
|
|
|
},
|
|
|
|
},
|
2015-01-18 07:32:34 +00:00
|
|
|
"read-write persistent disk": {
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "abc"},
|
2014-11-07 02:08:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: validSelector,
|
|
|
|
Template: &invalidVolumePodTemplate.Spec,
|
2014-08-05 17:58:43 +00:00
|
|
|
},
|
|
|
|
},
|
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-11-07 02:08:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: -1,
|
|
|
|
Selector: validSelector,
|
2014-08-04 19:02:51 +00:00
|
|
|
},
|
|
|
|
},
|
2014-10-23 20:14:13 +00:00
|
|
|
"invalid_label": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc-123",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"NoUppercaseOrSpecialCharsLike=Equals": "bar",
|
|
|
|
},
|
|
|
|
},
|
2014-11-07 02:08:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: validSelector,
|
|
|
|
Template: &validPodTemplate.Spec,
|
2014-10-23 20:14:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"invalid_label 2": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc-123",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"NoUppercaseOrSpecialCharsLike=Equals": "bar",
|
|
|
|
},
|
|
|
|
},
|
2014-11-07 02:08:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Template: &invalidPodTemplate.Spec,
|
2014-10-23 20:14:13 +00:00
|
|
|
},
|
|
|
|
},
|
2015-01-24 14:36:22 +00:00
|
|
|
"invalid_annotation": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc-123",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"NoUppercaseOrSpecialCharsLike=Equals": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: validSelector,
|
|
|
|
Template: &validPodTemplate.Spec,
|
|
|
|
},
|
|
|
|
},
|
2014-11-18 04:08:23 +00:00
|
|
|
"invalid restart policy 1": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc-123",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: validSelector,
|
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
RestartPolicy: api.RestartPolicy{
|
|
|
|
OnFailure: &api.RestartPolicyOnFailure{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: validSelector,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"invalid restart policy 2": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc-123",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Selector: validSelector,
|
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
RestartPolicy: api.RestartPolicy{
|
|
|
|
Never: &api.RestartPolicyNever{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: 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 {
|
2014-11-20 22:11:23 +00:00
|
|
|
field := errs[i].(*errors.ValidationError).Field
|
2014-11-07 02:08:46 +00:00
|
|
|
if !strings.HasPrefix(field, "spec.template.") &&
|
2015-01-27 23:55:54 +00:00
|
|
|
field != "metadata.name" &&
|
|
|
|
field != "metadata.namespace" &&
|
2014-11-07 02:08:46 +00:00
|
|
|
field != "spec.selector" &&
|
|
|
|
field != "spec.template" &&
|
2014-08-05 17:58:43 +00:00
|
|
|
field != "GCEPersistentDisk.ReadOnly" &&
|
2014-11-07 02:08:46 +00:00
|
|
|
field != "spec.replicas" &&
|
2014-11-20 05:55:45 +00:00
|
|
|
field != "spec.template.labels" &&
|
2015-01-27 23:55:54 +00:00
|
|
|
field != "metadata.annotations" &&
|
|
|
|
field != "metadata.labels" {
|
2014-08-20 03:54:20 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-12 17:38:15 +00:00
|
|
|
|
|
|
|
func TestValidateMinion(t *testing.T) {
|
|
|
|
validSelector := map[string]string{"a": "b"}
|
|
|
|
invalidSelector := map[string]string{"NoUppercaseOrSpecialCharsLike=Equals": "b"}
|
2014-12-08 03:44:27 +00:00
|
|
|
successCases := []api.Node{
|
2014-11-12 17:38:15 +00:00
|
|
|
{
|
2014-11-19 22:39:10 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc",
|
|
|
|
Labels: validSelector,
|
|
|
|
},
|
|
|
|
Status: api.NodeStatus{
|
|
|
|
HostIP: "something",
|
|
|
|
},
|
2014-11-12 17:38:15 +00:00
|
|
|
},
|
|
|
|
{
|
2015-01-27 23:56:38 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc",
|
|
|
|
},
|
2014-11-19 22:39:10 +00:00
|
|
|
Status: api.NodeStatus{
|
|
|
|
HostIP: "something",
|
|
|
|
},
|
2014-11-12 17:38:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, successCase := range successCases {
|
|
|
|
if errs := ValidateMinion(&successCase); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 03:44:27 +00:00
|
|
|
errorCases := map[string]api.Node{
|
2014-11-12 17:38:15 +00:00
|
|
|
"zero-length Name": {
|
2014-11-19 22:39:10 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "",
|
|
|
|
Labels: validSelector,
|
|
|
|
},
|
|
|
|
Status: api.NodeStatus{
|
|
|
|
HostIP: "something",
|
|
|
|
},
|
2014-11-12 17:38:15 +00:00
|
|
|
},
|
|
|
|
"invalid-labels": {
|
2014-11-19 22:39:10 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc-123",
|
|
|
|
Labels: invalidSelector,
|
|
|
|
},
|
2014-11-12 17:38:15 +00:00
|
|
|
},
|
2015-01-24 14:36:22 +00:00
|
|
|
"invalid-annotations": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc-123",
|
|
|
|
Annotations: invalidSelector,
|
|
|
|
},
|
|
|
|
},
|
2014-11-12 17:38:15 +00:00
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
|
|
|
errs := ValidateMinion(&v)
|
|
|
|
if len(errs) == 0 {
|
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
|
|
|
for i := range errs {
|
2014-11-20 22:11:23 +00:00
|
|
|
field := errs[i].(*errors.ValidationError).Field
|
2015-01-27 23:55:54 +00:00
|
|
|
if field != "metadata.name" &&
|
|
|
|
field != "metadata.labels" &&
|
|
|
|
field != "metadata.annotations" &&
|
|
|
|
field != "metadata.namespace" {
|
2014-11-12 17:38:15 +00:00
|
|
|
t.Errorf("%s: missing prefix for: %v", k, errs[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 18:22:27 +00:00
|
|
|
|
|
|
|
func TestValidateMinionUpdate(t *testing.T) {
|
|
|
|
tests := []struct {
|
2014-12-08 03:44:27 +00:00
|
|
|
oldMinion api.Node
|
|
|
|
minion api.Node
|
2014-11-17 18:22:27 +00:00
|
|
|
valid bool
|
|
|
|
}{
|
2014-12-08 03:44:27 +00:00
|
|
|
{api.Node{}, api.Node{}, true},
|
|
|
|
{api.Node{
|
2014-11-17 18:22:27 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo"}},
|
2014-12-08 03:44:27 +00:00
|
|
|
api.Node{
|
2014-11-17 18:22:27 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "bar"},
|
|
|
|
}, false},
|
2014-12-08 03:44:27 +00:00
|
|
|
{api.Node{
|
2014-11-17 18:22:27 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"foo": "bar"},
|
|
|
|
},
|
2014-12-08 03:44:27 +00:00
|
|
|
}, api.Node{
|
2014-11-17 18:22:27 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"foo": "baz"},
|
|
|
|
},
|
|
|
|
}, true},
|
2014-12-08 03:44:27 +00:00
|
|
|
{api.Node{
|
2014-11-17 18:22:27 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
2014-12-08 03:44:27 +00:00
|
|
|
}, api.Node{
|
2014-11-17 18:22:27 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"foo": "baz"},
|
|
|
|
},
|
|
|
|
}, true},
|
2014-12-08 03:44:27 +00:00
|
|
|
{api.Node{
|
2014-11-17 18:22:27 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "foo"},
|
|
|
|
},
|
2014-12-08 03:44:27 +00:00
|
|
|
}, api.Node{
|
2014-11-17 18:22:27 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"foo": "baz"},
|
|
|
|
},
|
|
|
|
}, true},
|
2014-12-12 05:39:56 +00:00
|
|
|
{api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.NodeSpec{
|
|
|
|
Capacity: api.ResourceList{
|
2015-01-07 01:20:01 +00:00
|
|
|
api.ResourceCPU: resource.MustParse("10000"),
|
|
|
|
api.ResourceMemory: resource.MustParse("100"),
|
2014-12-12 05:39:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.NodeSpec{
|
|
|
|
Capacity: api.ResourceList{
|
2015-01-07 01:20:01 +00:00
|
|
|
api.ResourceCPU: resource.MustParse("100"),
|
|
|
|
api.ResourceMemory: resource.MustParse("10000"),
|
2014-12-12 05:39:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, true},
|
|
|
|
{api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "foo"},
|
|
|
|
},
|
|
|
|
Spec: api.NodeSpec{
|
|
|
|
Capacity: api.ResourceList{
|
2015-01-07 01:20:01 +00:00
|
|
|
api.ResourceCPU: resource.MustParse("10000"),
|
|
|
|
api.ResourceMemory: resource.MustParse("100"),
|
2014-12-12 05:39:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "fooobaz"},
|
|
|
|
},
|
|
|
|
Spec: api.NodeSpec{
|
|
|
|
Capacity: api.ResourceList{
|
2015-01-07 01:20:01 +00:00
|
|
|
api.ResourceCPU: resource.MustParse("100"),
|
|
|
|
api.ResourceMemory: resource.MustParse("10000"),
|
2014-12-12 05:39:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, true},
|
2014-12-22 19:04:57 +00:00
|
|
|
{api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "foo"},
|
|
|
|
},
|
|
|
|
Status: api.NodeStatus{
|
|
|
|
HostIP: "1.2.3.4",
|
|
|
|
},
|
|
|
|
}, api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "fooobaz"},
|
|
|
|
},
|
|
|
|
}, true},
|
2014-11-17 18:22:27 +00:00
|
|
|
}
|
2015-01-27 23:55:54 +00:00
|
|
|
for i, test := range tests {
|
2014-11-17 18:22:27 +00:00
|
|
|
errs := ValidateMinionUpdate(&test.oldMinion, &test.minion)
|
|
|
|
if test.valid && len(errs) > 0 {
|
2015-01-27 23:55:54 +00:00
|
|
|
t.Errorf("%d: Unexpected error: %v", i, errs)
|
2014-11-17 18:22:27 +00:00
|
|
|
t.Logf("%#v vs %#v", test.oldMinion.ObjectMeta, test.minion.ObjectMeta)
|
|
|
|
}
|
|
|
|
if !test.valid && len(errs) == 0 {
|
2015-01-27 23:55:54 +00:00
|
|
|
t.Errorf("%d: Unexpected non-error", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateServiceUpdate(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
oldService api.Service
|
|
|
|
service api.Service
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{ // 0
|
|
|
|
api.Service{},
|
|
|
|
api.Service{},
|
|
|
|
true},
|
|
|
|
{ // 1
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo"}},
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "bar"},
|
|
|
|
}, false},
|
|
|
|
{ // 2
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"foo": "baz"},
|
|
|
|
},
|
|
|
|
}, true},
|
|
|
|
{ // 3
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"foo": "baz"},
|
|
|
|
},
|
|
|
|
}, true},
|
|
|
|
{ // 4
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"foo": "baz"},
|
|
|
|
},
|
|
|
|
}, true},
|
|
|
|
{ // 5
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Annotations: map[string]string{"bar": "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Annotations: map[string]string{"foo": "baz"},
|
|
|
|
},
|
|
|
|
}, true},
|
|
|
|
{ // 6
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{"foo": "baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{"foo": "baz"},
|
|
|
|
},
|
|
|
|
}, true},
|
|
|
|
{ // 7
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "foo"},
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
PortalIP: "127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "fooobaz"},
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
PortalIP: "new",
|
|
|
|
},
|
|
|
|
}, false},
|
|
|
|
{ // 8
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "foo"},
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
PortalIP: "127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "fooobaz"},
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
PortalIP: "",
|
|
|
|
},
|
|
|
|
}, false},
|
|
|
|
{ // 9
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "foo"},
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
PortalIP: "127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"bar": "fooobaz"},
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
PortalIP: "127.0.0.2",
|
|
|
|
},
|
|
|
|
}, false},
|
|
|
|
}
|
|
|
|
for i, test := range tests {
|
|
|
|
errs := ValidateServiceUpdate(&test.oldService, &test.service)
|
|
|
|
if test.valid && len(errs) > 0 {
|
|
|
|
t.Errorf("%d: Unexpected error: %v", i, errs)
|
|
|
|
t.Logf("%#v vs %#v", test.oldService.ObjectMeta, test.service.ObjectMeta)
|
|
|
|
}
|
|
|
|
if !test.valid && len(errs) == 0 {
|
|
|
|
t.Errorf("%d: Unexpected non-error", i)
|
2014-11-17 18:22:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-17 00:34:47 +00:00
|
|
|
|
|
|
|
func TestValidateResourceNames(t *testing.T) {
|
|
|
|
longString := "a"
|
|
|
|
for i := 0; i < 6; i++ {
|
|
|
|
longString += longString
|
|
|
|
}
|
|
|
|
table := []struct {
|
|
|
|
input string
|
|
|
|
success bool
|
|
|
|
}{
|
|
|
|
{"memory", true},
|
|
|
|
{"cpu", true},
|
|
|
|
{"network", false},
|
|
|
|
{"disk", false},
|
|
|
|
{"", false},
|
|
|
|
{".", false},
|
|
|
|
{"..", false},
|
|
|
|
{"my.favorite.app.co/12345", true},
|
|
|
|
{"my.favorite.app.co/_12345", false},
|
|
|
|
{"my.favorite.app.co/12345_", false},
|
|
|
|
{"kubernetes.io/..", false},
|
|
|
|
{"kubernetes.io/" + longString, false},
|
|
|
|
{"kubernetes.io//", false},
|
|
|
|
{"kubernetes.io", false},
|
|
|
|
{"kubernetes.io/will/not/work/", false},
|
|
|
|
}
|
|
|
|
for _, item := range table {
|
2015-01-25 04:19:36 +00:00
|
|
|
err := validateResourceName(item.input)
|
2015-01-17 00:34:47 +00:00
|
|
|
if len(err) != 0 && item.success {
|
|
|
|
t.Errorf("expected no failure for input %q", item.input)
|
|
|
|
} else if len(err) == 0 && !item.success {
|
|
|
|
t.Errorf("expected failure for input %q", item.input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 21:52:40 +00:00
|
|
|
|
|
|
|
func TestValidateLimitRange(t *testing.T) {
|
|
|
|
successCases := []api.LimitRange{
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.LimitRangeSpec{
|
|
|
|
Limits: []api.LimitRangeItem{
|
|
|
|
{
|
2015-01-23 03:21:13 +00:00
|
|
|
Type: api.LimitTypePod,
|
2015-01-22 21:52:40 +00:00
|
|
|
Max: api.ResourceList{
|
|
|
|
api.ResourceCPU: resource.MustParse("100"),
|
|
|
|
api.ResourceMemory: resource.MustParse("10000"),
|
|
|
|
},
|
|
|
|
Min: api.ResourceList{
|
|
|
|
api.ResourceCPU: resource.MustParse("0"),
|
|
|
|
api.ResourceMemory: resource.MustParse("100"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-01-23 17:38:30 +00:00
|
|
|
|
2015-01-22 21:52:40 +00:00
|
|
|
for _, successCase := range successCases {
|
|
|
|
if errs := ValidateLimitRange(&successCase); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorCases := map[string]api.LimitRange{
|
|
|
|
"zero-length Name": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.LimitRangeSpec{
|
|
|
|
Limits: []api.LimitRangeItem{
|
|
|
|
{
|
2015-01-23 03:21:13 +00:00
|
|
|
Type: api.LimitTypePod,
|
2015-01-22 21:52:40 +00:00
|
|
|
Max: api.ResourceList{
|
|
|
|
api.ResourceCPU: resource.MustParse("100"),
|
|
|
|
api.ResourceMemory: resource.MustParse("10000"),
|
|
|
|
},
|
|
|
|
Min: api.ResourceList{
|
|
|
|
api.ResourceCPU: resource.MustParse("0"),
|
|
|
|
api.ResourceMemory: resource.MustParse("100"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"zero-length-namespace": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc",
|
|
|
|
Namespace: "",
|
|
|
|
},
|
|
|
|
Spec: api.LimitRangeSpec{
|
|
|
|
Limits: []api.LimitRangeItem{
|
|
|
|
{
|
2015-01-23 03:21:13 +00:00
|
|
|
Type: api.LimitTypePod,
|
2015-01-22 21:52:40 +00:00
|
|
|
Max: api.ResourceList{
|
|
|
|
api.ResourceCPU: resource.MustParse("100"),
|
|
|
|
api.ResourceMemory: resource.MustParse("10000"),
|
|
|
|
},
|
|
|
|
Min: api.ResourceList{
|
|
|
|
api.ResourceCPU: resource.MustParse("0"),
|
|
|
|
api.ResourceMemory: resource.MustParse("100"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
|
|
|
errs := ValidateLimitRange(&v)
|
|
|
|
if len(errs) == 0 {
|
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
|
|
|
for i := range errs {
|
|
|
|
field := errs[i].(*errors.ValidationError).Field
|
|
|
|
if field != "name" &&
|
|
|
|
field != "namespace" {
|
|
|
|
t.Errorf("%s: missing prefix for: %v", k, errs[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 17:38:30 +00:00
|
|
|
|
|
|
|
func TestValidateResourceQuota(t *testing.T) {
|
|
|
|
successCases := []api.ResourceQuota{
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.ResourceQuotaSpec{
|
|
|
|
Hard: api.ResourceList{
|
|
|
|
api.ResourceCPU: resource.MustParse("100"),
|
|
|
|
api.ResourceMemory: resource.MustParse("10000"),
|
|
|
|
api.ResourcePods: resource.MustParse("10"),
|
|
|
|
api.ResourceServices: resource.MustParse("10"),
|
|
|
|
api.ResourceReplicationControllers: resource.MustParse("10"),
|
|
|
|
api.ResourceQuotas: resource.MustParse("10"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, successCase := range successCases {
|
|
|
|
if errs := ValidateResourceQuota(&successCase); len(errs) != 0 {
|
|
|
|
t.Errorf("expected success: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorCases := map[string]api.ResourceQuota{
|
|
|
|
"zero-length Name": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.ResourceQuotaSpec{
|
|
|
|
Hard: api.ResourceList{
|
|
|
|
api.ResourceCPU: resource.MustParse("100"),
|
|
|
|
api.ResourceMemory: resource.MustParse("10000"),
|
|
|
|
api.ResourcePods: resource.MustParse("10"),
|
|
|
|
api.ResourceServices: resource.MustParse("10"),
|
|
|
|
api.ResourceReplicationControllers: resource.MustParse("10"),
|
|
|
|
api.ResourceQuotas: resource.MustParse("10"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"zero-length-namespace": {
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "abc",
|
|
|
|
Namespace: "",
|
|
|
|
},
|
|
|
|
Spec: api.ResourceQuotaSpec{
|
|
|
|
Hard: api.ResourceList{
|
|
|
|
api.ResourceCPU: resource.MustParse("100"),
|
|
|
|
api.ResourceMemory: resource.MustParse("10000"),
|
|
|
|
api.ResourcePods: resource.MustParse("10"),
|
|
|
|
api.ResourceServices: resource.MustParse("10"),
|
|
|
|
api.ResourceReplicationControllers: resource.MustParse("10"),
|
|
|
|
api.ResourceQuotas: resource.MustParse("10"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for k, v := range errorCases {
|
|
|
|
errs := ValidateResourceQuota(&v)
|
|
|
|
if len(errs) == 0 {
|
|
|
|
t.Errorf("expected failure for %s", k)
|
|
|
|
}
|
|
|
|
for i := range errs {
|
|
|
|
field := errs[i].(*errors.ValidationError).Field
|
|
|
|
if field != "name" &&
|
|
|
|
field != "namespace" {
|
|
|
|
t.Errorf("%s: missing prefix for: %v", k, errs[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|