Set Job's labels from PodTemplateSpec if none are specified
This commit is contained in:
@@ -280,15 +280,39 @@ func TestSetDefaultDeployment(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaultJobParallelismAndCompletions(t *testing.T) {
|
||||
tests := []struct {
|
||||
original *Job
|
||||
expected *Job
|
||||
func TestSetDefaultJob(t *testing.T) {
|
||||
defaultLabels := map[string]string{"default": "default"}
|
||||
tests := map[string]struct {
|
||||
original *Job
|
||||
expected *Job
|
||||
expectLabels bool
|
||||
}{
|
||||
// both unspecified -> sets both to 1
|
||||
{
|
||||
"both unspecified -> sets both to 1": {
|
||||
original: &Job{
|
||||
Spec: JobSpec{},
|
||||
Spec: JobSpec{
|
||||
Template: v1.PodTemplateSpec{
|
||||
ObjectMeta: v1.ObjectMeta{Labels: defaultLabels},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: &Job{
|
||||
Spec: JobSpec{
|
||||
Completions: newInt32(1),
|
||||
Parallelism: newInt32(1),
|
||||
},
|
||||
},
|
||||
expectLabels: true,
|
||||
},
|
||||
"both unspecified -> sets both to 1 and no default labels": {
|
||||
original: &Job{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Labels: map[string]string{"mylabel": "myvalue"},
|
||||
},
|
||||
Spec: JobSpec{
|
||||
Template: v1.PodTemplateSpec{
|
||||
ObjectMeta: v1.ObjectMeta{Labels: defaultLabels},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: &Job{
|
||||
Spec: JobSpec{
|
||||
@@ -297,11 +321,13 @@ func TestSetDefaultJobParallelismAndCompletions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
// WQ: Parallelism explicitly 0 and completions unset -> no change
|
||||
{
|
||||
"WQ: Parallelism explicitly 0 and completions unset -> no change": {
|
||||
original: &Job{
|
||||
Spec: JobSpec{
|
||||
Parallelism: newInt32(0),
|
||||
Template: v1.PodTemplateSpec{
|
||||
ObjectMeta: v1.ObjectMeta{Labels: defaultLabels},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: &Job{
|
||||
@@ -309,12 +335,15 @@ func TestSetDefaultJobParallelismAndCompletions(t *testing.T) {
|
||||
Parallelism: newInt32(0),
|
||||
},
|
||||
},
|
||||
expectLabels: true,
|
||||
},
|
||||
// WQ: Parallelism explicitly 2 and completions unset -> no change
|
||||
{
|
||||
"WQ: Parallelism explicitly 2 and completions unset -> no change": {
|
||||
original: &Job{
|
||||
Spec: JobSpec{
|
||||
Parallelism: newInt32(2),
|
||||
Template: v1.PodTemplateSpec{
|
||||
ObjectMeta: v1.ObjectMeta{Labels: defaultLabels},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: &Job{
|
||||
@@ -322,12 +351,15 @@ func TestSetDefaultJobParallelismAndCompletions(t *testing.T) {
|
||||
Parallelism: newInt32(2),
|
||||
},
|
||||
},
|
||||
expectLabels: true,
|
||||
},
|
||||
// Completions explicitly 2 and parallelism unset -> parallelism is defaulted
|
||||
{
|
||||
"Completions explicitly 2 and parallelism unset -> parallelism is defaulted": {
|
||||
original: &Job{
|
||||
Spec: JobSpec{
|
||||
Completions: newInt32(2),
|
||||
Template: v1.PodTemplateSpec{
|
||||
ObjectMeta: v1.ObjectMeta{Labels: defaultLabels},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: &Job{
|
||||
@@ -336,28 +368,37 @@ func TestSetDefaultJobParallelismAndCompletions(t *testing.T) {
|
||||
Parallelism: newInt32(1),
|
||||
},
|
||||
},
|
||||
expectLabels: true,
|
||||
},
|
||||
// Both set -> no change
|
||||
{
|
||||
"Both set -> no change": {
|
||||
original: &Job{
|
||||
Spec: JobSpec{
|
||||
Completions: newInt32(10),
|
||||
Parallelism: newInt32(11),
|
||||
Template: v1.PodTemplateSpec{
|
||||
ObjectMeta: v1.ObjectMeta{Labels: defaultLabels},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: &Job{
|
||||
Spec: JobSpec{
|
||||
Completions: newInt32(10),
|
||||
Parallelism: newInt32(11),
|
||||
Template: v1.PodTemplateSpec{
|
||||
ObjectMeta: v1.ObjectMeta{Labels: defaultLabels},
|
||||
},
|
||||
},
|
||||
},
|
||||
expectLabels: true,
|
||||
},
|
||||
// Both set, flipped -> no change
|
||||
{
|
||||
"Both set, flipped -> no change": {
|
||||
original: &Job{
|
||||
Spec: JobSpec{
|
||||
Completions: newInt32(11),
|
||||
Parallelism: newInt32(10),
|
||||
Template: v1.PodTemplateSpec{
|
||||
ObjectMeta: v1.ObjectMeta{Labels: defaultLabels},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: &Job{
|
||||
@@ -366,32 +407,40 @@ func TestSetDefaultJobParallelismAndCompletions(t *testing.T) {
|
||||
Parallelism: newInt32(10),
|
||||
},
|
||||
},
|
||||
expectLabels: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
original := tc.original
|
||||
expected := tc.expected
|
||||
for name, test := range tests {
|
||||
original := test.original
|
||||
expected := test.expected
|
||||
obj2 := roundTrip(t, runtime.Object(original))
|
||||
got, ok := obj2.(*Job)
|
||||
actual, ok := obj2.(*Job)
|
||||
if !ok {
|
||||
t.Errorf("unexpected object: %v", got)
|
||||
t.Errorf("%s: unexpected object: %v", name, actual)
|
||||
t.FailNow()
|
||||
}
|
||||
if (got.Spec.Completions == nil) != (expected.Spec.Completions == nil) {
|
||||
t.Errorf("got different *completions than expected: %v %v", got.Spec.Completions, expected.Spec.Completions)
|
||||
if (actual.Spec.Completions == nil) != (expected.Spec.Completions == nil) {
|
||||
t.Errorf("%s: got different *completions than expected: %v %v", name, actual.Spec.Completions, expected.Spec.Completions)
|
||||
}
|
||||
if got.Spec.Completions != nil && expected.Spec.Completions != nil {
|
||||
if *got.Spec.Completions != *expected.Spec.Completions {
|
||||
t.Errorf("got different completions than expected: %d %d", *got.Spec.Completions, *expected.Spec.Completions)
|
||||
if actual.Spec.Completions != nil && expected.Spec.Completions != nil {
|
||||
if *actual.Spec.Completions != *expected.Spec.Completions {
|
||||
t.Errorf("%s: got different completions than expected: %d %d", name, *actual.Spec.Completions, *expected.Spec.Completions)
|
||||
}
|
||||
}
|
||||
if (got.Spec.Parallelism == nil) != (expected.Spec.Parallelism == nil) {
|
||||
t.Errorf("got different *Parallelism than expected: %v %v", got.Spec.Parallelism, expected.Spec.Parallelism)
|
||||
if (actual.Spec.Parallelism == nil) != (expected.Spec.Parallelism == nil) {
|
||||
t.Errorf("%s: got different *Parallelism than expected: %v %v", name, actual.Spec.Parallelism, expected.Spec.Parallelism)
|
||||
}
|
||||
if got.Spec.Parallelism != nil && expected.Spec.Parallelism != nil {
|
||||
if *got.Spec.Parallelism != *expected.Spec.Parallelism {
|
||||
t.Errorf("got different parallelism than expected: %d %d", *got.Spec.Parallelism, *expected.Spec.Parallelism)
|
||||
if actual.Spec.Parallelism != nil && expected.Spec.Parallelism != nil {
|
||||
if *actual.Spec.Parallelism != *expected.Spec.Parallelism {
|
||||
t.Errorf("%s: got different parallelism than expected: %d %d", name, *actual.Spec.Parallelism, *expected.Spec.Parallelism)
|
||||
}
|
||||
}
|
||||
if test.expectLabels != reflect.DeepEqual(actual.Labels, actual.Spec.Template.Labels) {
|
||||
if test.expectLabels {
|
||||
t.Errorf("%s: expected: %v, got: %v", name, actual.Spec.Template.Labels, actual.Labels)
|
||||
} else {
|
||||
t.Errorf("%s: unexpected equality: %v", name, actual.Labels)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user