Introduce the simplest RestartPolicy and handling.
This commit is contained in:
@@ -231,19 +231,33 @@ func ValidateManifest(manifest *api.ContainerManifest) errs.ErrorList {
|
||||
allVolumes, errs := validateVolumes(manifest.Volumes)
|
||||
allErrs = append(allErrs, errs.Prefix("volumes")...)
|
||||
allErrs = append(allErrs, validateContainers(manifest.Containers, allVolumes).Prefix("containers")...)
|
||||
allErrs = append(allErrs, validateRestartPolicy(&manifest.RestartPolicy).Prefix("restartPolicy")...)
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func validateRestartPolicy(restartPolicy *api.RestartPolicy) errs.ErrorList {
|
||||
numPolicies := 0
|
||||
allErrors := errs.ErrorList{}
|
||||
if restartPolicy.Always != nil {
|
||||
numPolicies++
|
||||
}
|
||||
if restartPolicy.OnFailure != nil {
|
||||
numPolicies++
|
||||
}
|
||||
if restartPolicy.Never != nil {
|
||||
numPolicies++
|
||||
}
|
||||
if numPolicies == 0 {
|
||||
restartPolicy.Always = &api.RestartPolicyAlways{}
|
||||
}
|
||||
if numPolicies > 1 {
|
||||
allErrors = append(allErrors, errs.NewFieldInvalid("", restartPolicy))
|
||||
}
|
||||
return allErrors
|
||||
}
|
||||
|
||||
func ValidatePodState(podState *api.PodState) errs.ErrorList {
|
||||
allErrs := errs.ErrorList(ValidateManifest(&podState.Manifest)).Prefix("manifest")
|
||||
if podState.RestartPolicy.Type == "" {
|
||||
podState.RestartPolicy.Type = api.RestartAlways
|
||||
} else if podState.RestartPolicy.Type != api.RestartAlways &&
|
||||
podState.RestartPolicy.Type != api.RestartOnFailure &&
|
||||
podState.RestartPolicy.Type != api.RestartNever {
|
||||
allErrs = append(allErrs, errs.NewFieldNotSupported("restartPolicy.type", podState.RestartPolicy.Type))
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
|
@@ -216,6 +216,40 @@ func TestValidateContainers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
t.Errorf("expected failure for %s", k)
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestValidateManifest(t *testing.T) {
|
||||
successCases := []api.ContainerManifest{
|
||||
{Version: "v1beta1", ID: "abc"},
|
||||
@@ -286,8 +320,13 @@ func TestValidatePod(t *testing.T) {
|
||||
"foo": "bar",
|
||||
},
|
||||
DesiredState: api.PodState{
|
||||
Manifest: api.ContainerManifest{Version: "v1beta1", ID: "abc"},
|
||||
RestartPolicy: api.RestartPolicy{Type: "RestartAlways"},
|
||||
Manifest: api.ContainerManifest{
|
||||
Version: "v1beta1",
|
||||
ID: "abc",
|
||||
RestartPolicy: api.RestartPolicy{
|
||||
Always: &api.RestartPolicyAlways{},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if len(errs) != 0 {
|
||||
@@ -312,8 +351,12 @@ func TestValidatePod(t *testing.T) {
|
||||
"foo": "bar",
|
||||
},
|
||||
DesiredState: api.PodState{
|
||||
Manifest: api.ContainerManifest{Version: "v1beta1", ID: "abc"},
|
||||
RestartPolicy: api.RestartPolicy{Type: "WhatEver"},
|
||||
Manifest: api.ContainerManifest{
|
||||
Version: "v1beta1",
|
||||
ID: "abc",
|
||||
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{},
|
||||
Never: &api.RestartPolicyNever{}},
|
||||
},
|
||||
},
|
||||
})
|
||||
if len(errs) != 1 {
|
||||
|
Reference in New Issue
Block a user