github.com/kubeflow/training-operator@v1.7.0/pkg/util/labels/labels_test.go (about)

     1  /*
     2  Copyright 2023 The Kubeflow 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 labels
    18  
    19  import (
    20  	"testing"
    21  
    22  	v1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
    23  )
    24  
    25  func TestReplicaIndex(t *testing.T) {
    26  	cases := map[string]struct {
    27  		labels  map[string]string
    28  		want    int
    29  		wantErr bool
    30  	}{
    31  		"new": {
    32  			labels: map[string]string{
    33  				v1.ReplicaIndexLabel: "2",
    34  			},
    35  			want: 2,
    36  		},
    37  		"old": {
    38  			labels: map[string]string{
    39  				v1.ReplicaIndexLabel: "3",
    40  			},
    41  			want: 3,
    42  		},
    43  		"none": {
    44  			labels:  map[string]string{},
    45  			wantErr: true,
    46  		},
    47  		"both": {
    48  			labels: map[string]string{
    49  				v1.ReplicaIndexLabel: "4",
    50  			},
    51  			want: 4,
    52  		},
    53  	}
    54  	for name, tc := range cases {
    55  		t.Run(name, func(t *testing.T) {
    56  			got, err := ReplicaIndex(tc.labels)
    57  			if gotErr := err != nil; tc.wantErr != gotErr {
    58  				t.Errorf("ReplicaIndex returned error (%t) want (%t)", gotErr, tc.wantErr)
    59  			}
    60  			if got != tc.want {
    61  				t.Errorf("ReplicaIndex returned %d, want %d", got, tc.want)
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func TestReplicaType(t *testing.T) {
    68  	cases := map[string]struct {
    69  		labels  map[string]string
    70  		want    v1.ReplicaType
    71  		wantErr bool
    72  	}{
    73  		"new": {
    74  			labels: map[string]string{
    75  				v1.ReplicaTypeLabel: "Foo",
    76  			},
    77  			want: "Foo",
    78  		},
    79  		"old": {
    80  			labels: map[string]string{
    81  				v1.ReplicaTypeLabel: "Bar",
    82  			},
    83  			want: "Bar",
    84  		},
    85  		"none": {
    86  			labels:  map[string]string{},
    87  			wantErr: true,
    88  		},
    89  		"both": {
    90  			labels: map[string]string{
    91  				v1.ReplicaTypeLabel: "Baz",
    92  			},
    93  			want: "Baz",
    94  		},
    95  	}
    96  	for name, tc := range cases {
    97  		t.Run(name, func(t *testing.T) {
    98  			got, err := ReplicaType(tc.labels)
    99  			if gotErr := err != nil; tc.wantErr != gotErr {
   100  				t.Errorf("ReplicaType returned error (%t) want (%t)", gotErr, tc.wantErr)
   101  			}
   102  			if got != tc.want {
   103  				t.Errorf("ReplicaType returned %v, want %v", got, tc.want)
   104  			}
   105  		})
   106  	}
   107  }