github.com/kubeflow/training-operator@v1.7.0/test_job/test_util/v1/pod.go (about)

     1  // Copyright 2018 The Kubeflow Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package v1
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	v1 "k8s.io/api/core/v1"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/client-go/tools/cache"
    24  
    25  	testjobv1 "github.com/kubeflow/training-operator/test_job/apis/test_job/v1"
    26  )
    27  
    28  const (
    29  	// labels for pods and servers.
    30  	testReplicaTypeLabel  = "test-replica-type"
    31  	testReplicaIndexLabel = "test-replica-index"
    32  )
    33  
    34  var (
    35  	controllerKind = testjobv1.SchemeGroupVersionKind
    36  )
    37  
    38  func NewBasePod(name string, testJob *testjobv1.TestJob, t *testing.T) *v1.Pod {
    39  	return &v1.Pod{
    40  		ObjectMeta: metav1.ObjectMeta{
    41  			Name:            name,
    42  			Labels:          GenLabels(testJob.Name),
    43  			Namespace:       testJob.Namespace,
    44  			OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(testJob, controllerKind)},
    45  		},
    46  	}
    47  }
    48  
    49  func NewPod(testJob *testjobv1.TestJob, typ string, index int, t *testing.T) *v1.Pod {
    50  	pod := NewBasePod(fmt.Sprintf("%s-%d", typ, index), testJob, t)
    51  	pod.Labels[testReplicaTypeLabel] = typ
    52  	pod.Labels[testReplicaIndexLabel] = fmt.Sprintf("%d", index)
    53  	return pod
    54  }
    55  
    56  // create count pods with the given phase for the given testjob
    57  func NewPodList(count int32, status v1.PodPhase, testJob *testjobv1.TestJob, typ string, start int32, t *testing.T) []*v1.Pod {
    58  	pods := []*v1.Pod{}
    59  	for i := int32(0); i < count; i++ {
    60  		newPod := NewPod(testJob, typ, int(start+i), t)
    61  		newPod.Status = v1.PodStatus{Phase: status}
    62  		pods = append(pods, newPod)
    63  	}
    64  	return pods
    65  }
    66  
    67  func SetPodsStatuses(podIndexer cache.Indexer, testJob *testjobv1.TestJob, typ string, pendingPods, activePods, succeededPods, failedPods int32, restartCounts []int32, t *testing.T) {
    68  	var index int32
    69  	for _, pod := range NewPodList(pendingPods, v1.PodPending, testJob, typ, index, t) {
    70  		if err := podIndexer.Add(pod); err != nil {
    71  			t.Errorf("%s: unexpected error when adding pod %v", testJob.Name, err)
    72  		}
    73  	}
    74  	index += pendingPods
    75  	for i, pod := range NewPodList(activePods, v1.PodRunning, testJob, typ, index, t) {
    76  		if restartCounts != nil {
    77  			pod.Status.ContainerStatuses = []v1.ContainerStatus{{RestartCount: restartCounts[i]}}
    78  		}
    79  		if err := podIndexer.Add(pod); err != nil {
    80  			t.Errorf("%s: unexpected error when adding pod %v", testJob.Name, err)
    81  		}
    82  	}
    83  	index += activePods
    84  	for _, pod := range NewPodList(succeededPods, v1.PodSucceeded, testJob, typ, index, t) {
    85  		if err := podIndexer.Add(pod); err != nil {
    86  			t.Errorf("%s: unexpected error when adding pod %v", testJob.Name, err)
    87  		}
    88  	}
    89  	index += succeededPods
    90  	for _, pod := range NewPodList(failedPods, v1.PodFailed, testJob, typ, index, t) {
    91  		if err := podIndexer.Add(pod); err != nil {
    92  			t.Errorf("%s: unexpected error when adding pod %v", testJob.Name, err)
    93  		}
    94  	}
    95  }