diff --git a/pkg/api/validation.go b/pkg/api/validation.go index 3da4bdeda5b..2946444795b 100644 --- a/pkg/api/validation.go +++ b/pkg/api/validation.go @@ -281,6 +281,16 @@ func ValidateManifest(manifest *ContainerManifest) []error { return []error(allErrs) } +// Pod tests if required fields in the pod are set. +func ValidatePod(pod *Pod) []error { + allErrs := errorList{} + if pod.ID == "" { + allErrs.Append(makeInvalidError("Pod.ID", pod.ID)) + } + allErrs.Append(ValidateManifest(&pod.DesiredState.Manifest)...) + return []error(allErrs) +} + // ValidateService tests if required fields in the service are set. func ValidateService(service *Service) []error { allErrs := errorList{} diff --git a/pkg/registry/pod_registry.go b/pkg/registry/pod_registry.go index 53849f1956d..59c3df1c887 100644 --- a/pkg/registry/pod_registry.go +++ b/pkg/registry/pod_registry.go @@ -213,6 +213,10 @@ func (storage *PodRegistryStorage) Create(obj interface{}) (<-chan interface{}, } pod.DesiredState.Manifest.ID = pod.ID + if errs := api.ValidatePod(&pod); len(errs) > 0 { + return nil, fmt.Errorf("Validation errors: %v", errs) + } + return apiserver.MakeAsync(func() (interface{}, error) { err := storage.scheduleAndCreatePod(pod) if err != nil { @@ -224,10 +228,9 @@ func (storage *PodRegistryStorage) Create(obj interface{}) (<-chan interface{}, func (storage *PodRegistryStorage) Update(obj interface{}) (<-chan interface{}, error) { pod := obj.(api.Pod) - if len(pod.ID) == 0 { - return nil, fmt.Errorf("ID should not be empty: %#v", pod) + if errs := api.ValidatePod(&pod); len(errs) > 0 { + return nil, fmt.Errorf("Validation errors: %v", errs) } - return apiserver.MakeAsync(func() (interface{}, error) { err := storage.registry.UpdatePod(pod) if err != nil { diff --git a/pkg/registry/pod_registry_test.go b/pkg/registry/pod_registry_test.go index 160bc374f5e..65faac7e0bf 100644 --- a/pkg/registry/pod_registry_test.go +++ b/pkg/registry/pod_registry_test.go @@ -65,7 +65,12 @@ func TestCreatePodRegistryError(t *testing.T) { scheduler: &MockScheduler{}, registry: mockRegistry, } - pod := api.Pod{} + desiredState := api.PodState{ + Manifest: api.ContainerManifest{ + Version: "v1beta1", + }, + } + pod := api.Pod{DesiredState: desiredState} ch, err := storage.Create(pod) if err != nil { t.Errorf("Expected %#v, Got %#v", nil, err) @@ -91,7 +96,12 @@ func TestCreatePodSchedulerError(t *testing.T) { storage := PodRegistryStorage{ scheduler: &mockScheduler, } - pod := api.Pod{} + desiredState := api.PodState{ + Manifest: api.ContainerManifest{ + Version: "v1beta1", + }, + } + pod := api.Pod{DesiredState: desiredState} ch, err := storage.Create(pod) if err != nil { t.Errorf("Expected %#v, Got %#v", nil, err) @@ -118,7 +128,12 @@ func TestCreatePodSetsIds(t *testing.T) { scheduler: &MockScheduler{machine: "test"}, registry: mockRegistry, } - pod := api.Pod{} + desiredState := api.PodState{ + Manifest: api.ContainerManifest{ + Version: "v1beta1", + }, + } + pod := api.Pod{DesiredState: desiredState} ch, err := storage.Create(pod) if err != nil { t.Errorf("Expected %#v, Got %#v", nil, err) @@ -254,6 +269,7 @@ func TestGetPodCloud(t *testing.T) { func TestMakePodStatus(t *testing.T) { desiredState := api.PodState{ Manifest: api.ContainerManifest{ + Version: "v1beta1", Containers: []api.Container{ {Name: "containerA"}, {Name: "containerB"}, @@ -337,6 +353,43 @@ func TestMakePodStatus(t *testing.T) { } } + +func TestPodStorageValidatesCreate(t *testing.T) { + mockRegistry := &MockPodStorageRegistry{ + MockPodRegistry: MockPodRegistry{err: fmt.Errorf("test error")}, + } + storage := PodRegistryStorage{ + scheduler: &MockScheduler{machine: "test"}, + registry: mockRegistry, + } + pod := api.Pod{} + c, err := storage.Create(pod) + if c != nil { + t.Errorf("Expected nil channel") + } + if err == nil { + t.Errorf("Expected to get an error") + } +} + +func TestPodStorageValidatesUpdate(t *testing.T) { + mockRegistry := &MockPodStorageRegistry{ + MockPodRegistry: MockPodRegistry{err: fmt.Errorf("test error")}, + } + storage := PodRegistryStorage{ + scheduler: &MockScheduler{machine: "test"}, + registry: mockRegistry, + } + pod := api.Pod{} + c, err := storage.Update(pod) + if c != nil { + t.Errorf("Expected nil channel") + } + if err == nil { + t.Errorf("Expected to get an error") + } +} + func TestCreatePod(t *testing.T) { mockRegistry := MockPodRegistry{ pod: &api.Pod{ @@ -352,8 +405,14 @@ func TestCreatePod(t *testing.T) { scheduler: scheduler.MakeRoundRobinScheduler(), minionLister: MakeMinionRegistry([]string{"machine"}), } + desiredState := api.PodState{ + Manifest: api.ContainerManifest{ + Version: "v1beta1", + }, + } pod := api.Pod{ JSONBase: api.JSONBase{ID: "foo"}, + DesiredState: desiredState, } channel, err := storage.Create(pod) expectNoError(t, err)