mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-13 22:05:59 +00:00
Test now has coverage!
This commit is contained in:
parent
0bc0256093
commit
e9be1d7438
@ -469,7 +469,8 @@ func (jm *Controller) updateJob(logger klog.Logger, old, cur interface{}) {
|
|||||||
jm.enqueueSyncJobImmediately(logger, curJob)
|
jm.enqueueSyncJobImmediately(logger, curJob)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if curJob is finished, remove the finalizer as a backup check.
|
// The job shouldn't be marked as finished until all pod finalizers are removed.
|
||||||
|
// This is a backup operation
|
||||||
if IsJobFinished(curJob) {
|
if IsJobFinished(curJob) {
|
||||||
jm.backupRemovePodFinalizers(curJob)
|
jm.backupRemovePodFinalizers(curJob)
|
||||||
}
|
}
|
||||||
|
@ -5174,52 +5174,48 @@ func TestFinalizersRemovedExpectations(t *testing.T) {
|
|||||||
|
|
||||||
func TestBackupFinalizerRemoval(t *testing.T) {
|
func TestBackupFinalizerRemoval(t *testing.T) {
|
||||||
_, ctx := ktesting.NewTestContext(t)
|
_, ctx := ktesting.NewTestContext(t)
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
clientset := fake.NewSimpleClientset()
|
clientset := fake.NewSimpleClientset()
|
||||||
sharedInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
|
sharedInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
|
||||||
manager := NewController(ctx, sharedInformers.Core().V1().Pods(), sharedInformers.Batch().V1().Jobs(), clientset)
|
manager := NewController(ctx, sharedInformers.Core().V1().Pods(), sharedInformers.Batch().V1().Jobs(), clientset)
|
||||||
manager.podStoreSynced = alwaysReady
|
manager.podStoreSynced = alwaysReady
|
||||||
manager.jobStoreSynced = alwaysReady
|
manager.jobStoreSynced = alwaysReady
|
||||||
|
|
||||||
stopCh := make(chan struct{})
|
go manager.Run(context.TODO(), 0)
|
||||||
defer close(stopCh)
|
|
||||||
podInformer := sharedInformers.Core().V1().Pods().Informer()
|
|
||||||
go podInformer.Run(stopCh)
|
|
||||||
cache.WaitForCacheSync(stopCh, podInformer.HasSynced)
|
|
||||||
|
|
||||||
// 1. Create the controller but do not start the workers.
|
// Start the Pod and Job informers.
|
||||||
// This is done above by initializing the manager and not calling manager.Run() yet.
|
sharedInformers.Start(ctx.Done())
|
||||||
|
sharedInformers.WaitForCacheSync(ctx.Done())
|
||||||
|
|
||||||
// 2. Create a job.
|
// Create a simple Job
|
||||||
job := newJob(1, 1, 1, batch.NonIndexedCompletion)
|
job := newJob(1, 1, 1, batch.NonIndexedCompletion)
|
||||||
|
|
||||||
job, err := clientset.BatchV1().Jobs(job.GetNamespace()).Create(ctx, job, metav1.CreateOptions{})
|
job, err := clientset.BatchV1().Jobs(job.GetNamespace()).Create(ctx, job, metav1.CreateOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Creating job: %v", err)
|
t.Fatalf("Creating job: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a Pod with the job tracking finalizer
|
||||||
pod := newPod("test-pod", job)
|
pod := newPod("test-pod", job)
|
||||||
pod.Finalizers = append(pod.Finalizers, batch.JobTrackingFinalizer)
|
pod.Finalizers = append(pod.Finalizers, batch.JobTrackingFinalizer)
|
||||||
|
|
||||||
pod, err = clientset.CoreV1().Pods(pod.GetNamespace()).Create(ctx, pod, metav1.CreateOptions{})
|
pod, err = clientset.CoreV1().Pods(pod.GetNamespace()).Create(ctx, pod, metav1.CreateOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Creating pod: %v", err)
|
t.Fatalf("Creating pod: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mark Job as complete.
|
||||||
job.Status.Conditions = append(job.Status.Conditions, batch.JobCondition{
|
job.Status.Conditions = append(job.Status.Conditions, batch.JobCondition{
|
||||||
Type: batch.JobComplete,
|
Type: batch.JobComplete,
|
||||||
Status: v1.ConditionTrue,
|
Status: v1.ConditionTrue,
|
||||||
})
|
})
|
||||||
|
|
||||||
_, err = clientset.BatchV1().Jobs(job.GetNamespace()).UpdateStatus(ctx, job, metav1.UpdateOptions{})
|
_, err = clientset.BatchV1().Jobs(job.GetNamespace()).UpdateStatus(ctx, job, metav1.UpdateOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Updating job status: %v", err)
|
t.Fatalf("Updating job status: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go manager.Run(context.TODO(), 0)
|
// Since Pod was not correctly tracked, the the backup code
|
||||||
|
// should remove the finalizer. (backupRemovePodFinalizers)
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), wait.ForeverTestTimeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
if err := wait.PollUntilContextTimeout(ctx, 100*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
|
if err := wait.PollUntilContextTimeout(ctx, 100*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
|
||||||
p, err := clientset.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{})
|
p, err := clientset.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -5227,7 +5223,6 @@ func TestBackupFinalizerRemoval(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return !hasJobTrackingFinalizer(p), nil
|
return !hasJobTrackingFinalizer(p), nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
// Check for context deadline exceeded, which would mean the operation timed out
|
|
||||||
if errors.Is(err, context.DeadlineExceeded) {
|
if errors.Is(err, context.DeadlineExceeded) {
|
||||||
t.Errorf("Timed out waiting for Pod to get the finalizer removed")
|
t.Errorf("Timed out waiting for Pod to get the finalizer removed")
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user