k8s.io/kubernetes@v1.29.3/test/e2e/upgrades/apps/job.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package apps 18 19 import ( 20 "context" 21 "fmt" 22 "strings" 23 24 batchv1 "k8s.io/api/batch/v1" 25 v1 "k8s.io/api/core/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/labels" 28 clientset "k8s.io/client-go/kubernetes" 29 "k8s.io/kubernetes/test/e2e/framework" 30 e2ejob "k8s.io/kubernetes/test/e2e/framework/job" 31 "k8s.io/kubernetes/test/e2e/upgrades" 32 33 "github.com/onsi/ginkgo/v2" 34 ) 35 36 // JobUpgradeTest is a test harness for batch Jobs. 37 type JobUpgradeTest struct { 38 job *batchv1.Job 39 namespace string 40 } 41 42 // Name returns the tracking name of the test. 43 func (JobUpgradeTest) Name() string { return "[sig-apps] job-upgrade" } 44 45 // Setup starts a Job with a parallelism of 2 and 2 completions running. 46 func (t *JobUpgradeTest) Setup(ctx context.Context, f *framework.Framework) { 47 t.namespace = f.Namespace.Name 48 49 ginkgo.By("Creating a job") 50 t.job = e2ejob.NewTestJob("notTerminate", "foo", v1.RestartPolicyOnFailure, 2, 2, nil, 6) 51 job, err := e2ejob.CreateJob(ctx, f.ClientSet, t.namespace, t.job) 52 t.job = job 53 framework.ExpectNoError(err) 54 55 ginkgo.By("Ensuring active pods == parallelism") 56 err = e2ejob.WaitForJobPodsRunning(ctx, f.ClientSet, t.namespace, job.Name, 2) 57 framework.ExpectNoError(err) 58 } 59 60 // Test verifies that the Jobs Pods are running after the an upgrade 61 func (t *JobUpgradeTest) Test(ctx context.Context, f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) { 62 <-done 63 ginkgo.By("Ensuring active pods == parallelism") 64 err := ensureAllJobPodsRunning(ctx, f.ClientSet, t.namespace, t.job.Name, 2) 65 framework.ExpectNoError(err) 66 } 67 68 // Teardown cleans up any remaining resources. 69 func (t *JobUpgradeTest) Teardown(ctx context.Context, f *framework.Framework) { 70 // rely on the namespace deletion to clean up everything 71 } 72 73 // ensureAllJobPodsRunning uses c to check in the Job named jobName in ns 74 // is running, returning an error if the expected parallelism is not 75 // satisfied. 76 func ensureAllJobPodsRunning(ctx context.Context, c clientset.Interface, ns, jobName string, parallelism int32) error { 77 label := labels.SelectorFromSet(labels.Set(map[string]string{e2ejob.JobSelectorKey: jobName})) 78 options := metav1.ListOptions{LabelSelector: label.String()} 79 pods, err := c.CoreV1().Pods(ns).List(ctx, options) 80 if err != nil { 81 return err 82 } 83 podsSummary := make([]string, 0, parallelism) 84 count := int32(0) 85 for _, p := range pods.Items { 86 if p.Status.Phase == v1.PodRunning { 87 count++ 88 } 89 podsSummary = append(podsSummary, fmt.Sprintf("%s (%s: %s)", p.ObjectMeta.Name, p.Status.Phase, p.Status.Message)) 90 } 91 if count != parallelism { 92 return fmt.Errorf("job has %d of %d expected running pods: %s", count, parallelism, strings.Join(podsSummary, ", ")) 93 } 94 return nil 95 }