Skip to content

Commit

Permalink
pod: Add test for restart policy
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <[email protected]>
  • Loading branch information
gaocegege committed Jun 13, 2018
1 parent ffbdbac commit 6c77fca
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
20 changes: 12 additions & 8 deletions pkg/controller.v2/controller_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,7 @@ func (tc *TFJobController) createNewPod(tfjob *tfv1alpha2.TFJob, rt, index strin
loggerForReplica(tfjob, rt).Warning(errMsg)
tc.recorder.Event(tfjob, v1.EventTypeWarning, podTemplateRestartPolicyReason, errMsg)
}
if spec.RestartPolicy == tfv1alpha2.RestartPolicyExitCode {
podTemplate.Spec.RestartPolicy = v1.RestartPolicyNever
} else if spec.RestartPolicy == tfv1alpha2.RestartPolicy("") {
// Set default to Never.
podTemplate.Spec.RestartPolicy = v1.RestartPolicyNever
} else {
podTemplate.Spec.RestartPolicy = v1.RestartPolicy(spec.RestartPolicy)
}
setRestartPolicy(podTemplate, spec)

err = tc.podControl.CreatePodsWithControllerRef(tfjob.Namespace, podTemplate, tfjob, controllerRef)
if err != nil && errors.IsTimeout(err) {
Expand Down Expand Up @@ -212,6 +205,17 @@ func setClusterSpec(podTemplateSpec *v1.PodTemplateSpec, tfjob *tfv1alpha2.TFJob
return nil
}

func setRestartPolicy(podTemplateSpec *v1.PodTemplateSpec, spec *tfv1alpha2.TFReplicaSpec) {
if spec.RestartPolicy == tfv1alpha2.RestartPolicyExitCode {
podTemplateSpec.Spec.RestartPolicy = v1.RestartPolicyNever
} else if spec.RestartPolicy == tfv1alpha2.RestartPolicy("") {
// Set default to Never.
podTemplateSpec.Spec.RestartPolicy = v1.RestartPolicyNever
} else {
podTemplateSpec.Spec.RestartPolicy = v1.RestartPolicy(spec.RestartPolicy)
}
}

// getPodsForTFJob returns the set of pods that this tfjob should manage.
// It also reconciles ControllerRef by adopting/orphaning.
// Note that the returned Pods are pointers into the cache.
Expand Down
68 changes: 68 additions & 0 deletions pkg/controller.v2/controller_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,71 @@ func TestClusterSpec(t *testing.T) {
}
}
}

func TestRestartPolicy(t *testing.T) {
type tc struct {
tfJob *tfv1alpha2.TFJob
expectedRestartPolicy v1.RestartPolicy
expectedType tfv1alpha2.TFReplicaType
}
testCase := []tc{
func() tc {
tfJob := newTFJob(1, 0)
specRestartPolicy := tfv1alpha2.RestartPolicyExitCode
tfJob.Spec.TFReplicaSpecs[tfv1alpha2.TFReplicaTypeWorker].RestartPolicy = specRestartPolicy
return tc{
tfJob: tfJob,
expectedRestartPolicy: v1.RestartPolicyNever,
expectedType: tfv1alpha2.TFReplicaTypeWorker,
}
}(),
func() tc {
tfJob := newTFJob(1, 0)
specRestartPolicy := tfv1alpha2.RestartPolicyNever
tfJob.Spec.TFReplicaSpecs[tfv1alpha2.TFReplicaTypeWorker].RestartPolicy = specRestartPolicy
return tc{
tfJob: tfJob,
expectedRestartPolicy: v1.RestartPolicyNever,
expectedType: tfv1alpha2.TFReplicaTypeWorker,
}
}(),
func() tc {
tfJob := newTFJob(1, 0)
specRestartPolicy := tfv1alpha2.RestartPolicyAlways
tfJob.Spec.TFReplicaSpecs[tfv1alpha2.TFReplicaTypeWorker].RestartPolicy = specRestartPolicy
return tc{
tfJob: tfJob,
expectedRestartPolicy: v1.RestartPolicyAlways,
expectedType: tfv1alpha2.TFReplicaTypeWorker,
}
}(),
func() tc {
tfJob := newTFJob(1, 0)
specRestartPolicy := tfv1alpha2.RestartPolicyOnFailure
tfJob.Spec.TFReplicaSpecs[tfv1alpha2.TFReplicaTypeWorker].RestartPolicy = specRestartPolicy
return tc{
tfJob: tfJob,
expectedRestartPolicy: v1.RestartPolicyOnFailure,
expectedType: tfv1alpha2.TFReplicaTypeWorker,
}
}(),
func() tc {
tfJob := newTFJob(1, 0)
specRestartPolicy := tfv1alpha2.RestartPolicy("")
tfJob.Spec.TFReplicaSpecs[tfv1alpha2.TFReplicaTypeWorker].RestartPolicy = specRestartPolicy
return tc{
tfJob: tfJob,
expectedRestartPolicy: v1.RestartPolicyNever,
expectedType: tfv1alpha2.TFReplicaTypeWorker,
}
}(),
}
for _, c := range testCase {
spec := c.tfJob.Spec.TFReplicaSpecs[c.expectedType]
podTemplate := spec.Template
setRestartPolicy(&podTemplate, spec)
if podTemplate.Spec.RestartPolicy != c.expectedRestartPolicy {
t.Errorf("Expected %s, got %s", c.expectedRestartPolicy, podTemplate.Spec.RestartPolicy)
}
}
}

0 comments on commit 6c77fca

Please sign in to comment.