2016-05-12 19:55:51 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-05-12 19:55:51 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-11-01 22:46:23 +00:00
|
|
|
package cronjob
|
2016-05-12 19:55:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-17 10:38:25 +00:00
|
|
|
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
2017-02-27 03:19:47 +00:00
|
|
|
"k8s.io/apiserver/pkg/registry/rest"
|
2016-05-12 19:55:51 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/apis/batch"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newBool(a bool) *bool {
|
|
|
|
r := new(bool)
|
|
|
|
*r = a
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-11-01 22:46:23 +00:00
|
|
|
func TestCronJobStrategy(t *testing.T) {
|
2017-01-02 14:07:36 +00:00
|
|
|
ctx := genericapirequest.NewDefaultContext()
|
2016-05-12 19:55:51 +00:00
|
|
|
if !Strategy.NamespaceScoped() {
|
2016-11-01 22:46:23 +00:00
|
|
|
t.Errorf("CronJob must be namespace scoped")
|
2016-05-12 19:55:51 +00:00
|
|
|
}
|
|
|
|
if Strategy.AllowCreateOnUpdate() {
|
2016-11-01 22:46:23 +00:00
|
|
|
t.Errorf("CronJob should not allow create on update")
|
2016-05-12 19:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
validPodTemplateSpec := api.PodTemplateSpec{
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
RestartPolicy: api.RestartPolicyOnFailure,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2017-01-16 04:56:22 +00:00
|
|
|
Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent", TerminationMessagePolicy: api.TerminationMessageReadFile}},
|
2016-05-12 19:55:51 +00:00
|
|
|
},
|
|
|
|
}
|
2016-11-01 22:46:23 +00:00
|
|
|
scheduledJob := &batch.CronJob{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-11-01 22:46:23 +00:00
|
|
|
Name: "mycronjob",
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2016-05-12 19:55:51 +00:00
|
|
|
},
|
2016-11-01 22:46:23 +00:00
|
|
|
Spec: batch.CronJobSpec{
|
2016-08-08 13:41:20 +00:00
|
|
|
Schedule: "* * * * ?",
|
2016-05-12 19:55:51 +00:00
|
|
|
ConcurrencyPolicy: batch.AllowConcurrent,
|
|
|
|
JobTemplate: batch.JobTemplateSpec{
|
|
|
|
Spec: batch.JobSpec{
|
2016-05-23 21:10:30 +00:00
|
|
|
Template: validPodTemplateSpec,
|
2016-05-12 19:55:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-08-08 20:15:33 +00:00
|
|
|
Strategy.PrepareForCreate(ctx, scheduledJob)
|
2016-05-12 19:55:51 +00:00
|
|
|
if len(scheduledJob.Status.Active) != 0 {
|
2016-11-01 22:46:23 +00:00
|
|
|
t.Errorf("CronJob does not allow setting status on create")
|
2016-05-12 19:55:51 +00:00
|
|
|
}
|
|
|
|
errs := Strategy.Validate(ctx, scheduledJob)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("Unexpected error validating %v", errs)
|
|
|
|
}
|
2016-12-03 18:57:26 +00:00
|
|
|
now := metav1.Now()
|
2016-11-01 22:46:23 +00:00
|
|
|
updatedCronJob := &batch.CronJob{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "bar", ResourceVersion: "4"},
|
2016-11-01 22:46:23 +00:00
|
|
|
Spec: batch.CronJobSpec{
|
2016-08-08 13:41:20 +00:00
|
|
|
Schedule: "5 5 5 * ?",
|
2016-05-12 19:55:51 +00:00
|
|
|
},
|
2016-11-01 22:46:23 +00:00
|
|
|
Status: batch.CronJobStatus{
|
2016-05-12 19:55:51 +00:00
|
|
|
LastScheduleTime: &now,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure we do not change status
|
2016-11-01 22:46:23 +00:00
|
|
|
Strategy.PrepareForUpdate(ctx, updatedCronJob, scheduledJob)
|
|
|
|
if updatedCronJob.Status.Active != nil {
|
2016-05-12 19:55:51 +00:00
|
|
|
t.Errorf("PrepareForUpdate should have preserved prior version status")
|
|
|
|
}
|
2016-11-01 22:46:23 +00:00
|
|
|
errs = Strategy.ValidateUpdate(ctx, updatedCronJob, scheduledJob)
|
2016-05-12 19:55:51 +00:00
|
|
|
if len(errs) == 0 {
|
|
|
|
t.Errorf("Expected a validation error")
|
|
|
|
}
|
2017-02-27 03:19:47 +00:00
|
|
|
|
|
|
|
// Make sure we correctly implement the interface.
|
|
|
|
// Otherwise a typo could silently change the default.
|
|
|
|
var gcds rest.GarbageCollectionDeleteStrategy = Strategy
|
|
|
|
if got, want := gcds.DefaultGarbageCollectionPolicy(), rest.OrphanDependents; got != want {
|
|
|
|
t.Errorf("DefaultGarbageCollectionPolicy() = %#v, want %#v", got, want)
|
|
|
|
}
|
2016-05-12 19:55:51 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 22:46:23 +00:00
|
|
|
func TestCronJobStatusStrategy(t *testing.T) {
|
2017-01-02 14:07:36 +00:00
|
|
|
ctx := genericapirequest.NewDefaultContext()
|
2016-05-12 19:55:51 +00:00
|
|
|
if !StatusStrategy.NamespaceScoped() {
|
2016-11-01 22:46:23 +00:00
|
|
|
t.Errorf("CronJob must be namespace scoped")
|
2016-05-12 19:55:51 +00:00
|
|
|
}
|
|
|
|
if StatusStrategy.AllowCreateOnUpdate() {
|
2016-11-01 22:46:23 +00:00
|
|
|
t.Errorf("CronJob should not allow create on update")
|
2016-05-12 19:55:51 +00:00
|
|
|
}
|
|
|
|
validPodTemplateSpec := api.PodTemplateSpec{
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
RestartPolicy: api.RestartPolicyOnFailure,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2017-01-16 04:56:22 +00:00
|
|
|
Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent", TerminationMessagePolicy: api.TerminationMessageReadFile}},
|
2016-05-12 19:55:51 +00:00
|
|
|
},
|
|
|
|
}
|
2016-08-08 13:41:20 +00:00
|
|
|
oldSchedule := "* * * * ?"
|
2016-11-01 22:46:23 +00:00
|
|
|
oldCronJob := &batch.CronJob{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-11-01 22:46:23 +00:00
|
|
|
Name: "mycronjob",
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2016-05-12 19:55:51 +00:00
|
|
|
ResourceVersion: "10",
|
|
|
|
},
|
2016-11-01 22:46:23 +00:00
|
|
|
Spec: batch.CronJobSpec{
|
2016-05-12 19:55:51 +00:00
|
|
|
Schedule: oldSchedule,
|
|
|
|
ConcurrencyPolicy: batch.AllowConcurrent,
|
|
|
|
JobTemplate: batch.JobTemplateSpec{
|
|
|
|
Spec: batch.JobSpec{
|
2016-05-23 21:10:30 +00:00
|
|
|
Template: validPodTemplateSpec,
|
2016-05-12 19:55:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-12-03 18:57:26 +00:00
|
|
|
now := metav1.Now()
|
2016-11-01 22:46:23 +00:00
|
|
|
newCronJob := &batch.CronJob{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-11-01 22:46:23 +00:00
|
|
|
Name: "mycronjob",
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2016-05-12 19:55:51 +00:00
|
|
|
ResourceVersion: "9",
|
|
|
|
},
|
2016-11-01 22:46:23 +00:00
|
|
|
Spec: batch.CronJobSpec{
|
2016-08-08 13:41:20 +00:00
|
|
|
Schedule: "5 5 * * ?",
|
2016-05-12 19:55:51 +00:00
|
|
|
ConcurrencyPolicy: batch.AllowConcurrent,
|
|
|
|
JobTemplate: batch.JobTemplateSpec{
|
|
|
|
Spec: batch.JobSpec{
|
2016-05-23 21:10:30 +00:00
|
|
|
Template: validPodTemplateSpec,
|
2016-05-12 19:55:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-11-01 22:46:23 +00:00
|
|
|
Status: batch.CronJobStatus{
|
2016-05-12 19:55:51 +00:00
|
|
|
LastScheduleTime: &now,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-01 22:46:23 +00:00
|
|
|
StatusStrategy.PrepareForUpdate(ctx, newCronJob, oldCronJob)
|
|
|
|
if newCronJob.Status.LastScheduleTime == nil {
|
|
|
|
t.Errorf("CronJob status updates must allow changes to scheduledJob status")
|
2016-05-12 19:55:51 +00:00
|
|
|
}
|
2016-11-01 22:46:23 +00:00
|
|
|
if newCronJob.Spec.Schedule != oldSchedule {
|
|
|
|
t.Errorf("CronJob status updates must now allow changes to scheduledJob spec")
|
2016-05-12 19:55:51 +00:00
|
|
|
}
|
2016-11-01 22:46:23 +00:00
|
|
|
errs := StatusStrategy.ValidateUpdate(ctx, newCronJob, oldCronJob)
|
2016-05-12 19:55:51 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("Unexpected error %v", errs)
|
|
|
|
}
|
2016-11-01 22:46:23 +00:00
|
|
|
if newCronJob.ResourceVersion != "9" {
|
2016-05-12 19:55:51 +00:00
|
|
|
t.Errorf("Incoming resource version on update should not be mutated")
|
|
|
|
}
|
|
|
|
}
|