github.com/kubeflow/training-operator@v1.7.0/pkg/util/train/train_util_test.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 train
    16  
    17  import (
    18  	"testing"
    19  
    20  	"k8s.io/utils/pointer"
    21  
    22  	kubeflowv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
    23  )
    24  
    25  func TestIsRetryableExitCode(t *testing.T) {
    26  	tcs := []struct {
    27  		ExitCode int32
    28  		Expected bool
    29  	}{
    30  		{
    31  			ExitCode: 1,
    32  			Expected: false,
    33  		},
    34  		{
    35  			ExitCode: 2,
    36  			Expected: false,
    37  		},
    38  		{
    39  			ExitCode: 3,
    40  			Expected: false,
    41  		},
    42  		{
    43  			ExitCode: 130,
    44  			Expected: true,
    45  		},
    46  		{
    47  			ExitCode: 138,
    48  			Expected: true,
    49  		},
    50  	}
    51  
    52  	for _, tc := range tcs {
    53  		actual := IsRetryableExitCode(tc.ExitCode)
    54  		if actual != tc.Expected {
    55  			t.Errorf("ExitCode %d: Expected %t, got %t", tc.ExitCode, tc.Expected, actual)
    56  		}
    57  	}
    58  }
    59  
    60  func TestIsJobSuspended(t *testing.T) {
    61  	cases := map[string]struct {
    62  		runPolicy *kubeflowv1.RunPolicy
    63  		want      bool
    64  	}{
    65  		"runPolicy is nil": {
    66  			runPolicy: nil,
    67  			want:      false,
    68  		},
    69  		"suspend is nil": {
    70  			runPolicy: &kubeflowv1.RunPolicy{
    71  				Suspend: nil,
    72  			},
    73  			want: false,
    74  		},
    75  		"suspend is false": {
    76  			runPolicy: &kubeflowv1.RunPolicy{
    77  				Suspend: pointer.Bool(false),
    78  			},
    79  			want: false,
    80  		},
    81  		"suspend is true": {
    82  			runPolicy: &kubeflowv1.RunPolicy{
    83  				Suspend: pointer.Bool(true),
    84  			},
    85  			want: true,
    86  		},
    87  	}
    88  	for name, tc := range cases {
    89  		t.Run(name, func(t *testing.T) {
    90  			got := IsJobSuspended(tc.runPolicy)
    91  			if tc.want != got {
    92  				t.Errorf("Unexpected suspended from IsJobSuspended \nwant: %v\n, \ngot: %v\n", tc.want, got)
    93  			}
    94  		})
    95  	}
    96  }