github.com/kubeflow/training-operator@v1.7.0/pkg/controller.v1/common/util_test.go (about)

     1  // Copyright 2019 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 common
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  
    22  	apiv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
    23  )
    24  
    25  func TestGenGeneralName(t *testing.T) {
    26  	tcs := []struct {
    27  		index        string
    28  		key          string
    29  		replicaType  apiv1.ReplicaType
    30  		expectedName string
    31  	}{
    32  		{
    33  			index:        "1",
    34  			key:          "1/2/3/4/5",
    35  			replicaType:  "worker",
    36  			expectedName: "1-2-3-4-5-worker-1",
    37  		},
    38  		{
    39  			index:        "1",
    40  			key:          "1/2/3/4/5",
    41  			replicaType:  "WORKER",
    42  			expectedName: "1-2-3-4-5-worker-1",
    43  		},
    44  	}
    45  
    46  	for _, tc := range tcs {
    47  		actual := GenGeneralName(tc.key, string(tc.replicaType), tc.index)
    48  		if actual != tc.expectedName {
    49  			t.Errorf("Expected name %s, got %s", tc.expectedName, actual)
    50  		}
    51  	}
    52  }
    53  
    54  func TestMaxInt(t *testing.T) {
    55  	type testCase struct {
    56  		x           int
    57  		y           int
    58  		expectedMax int
    59  	}
    60  	var testCases = []testCase{
    61  		{
    62  			x:           10,
    63  			y:           20,
    64  			expectedMax: 20,
    65  		},
    66  		{
    67  			x:           20,
    68  			y:           10,
    69  			expectedMax: 20,
    70  		},
    71  		{
    72  			x:           5,
    73  			y:           5,
    74  			expectedMax: 5,
    75  		},
    76  	}
    77  
    78  	for _, tc := range testCases {
    79  		result := MaxInt(tc.x, tc.y)
    80  		assert.Equal(t, tc.expectedMax, result)
    81  	}
    82  }