volcano.sh/volcano@v1.9.0/pkg/cli/job/util_test.go (about)

     1  /*
     2  Copyright 2019 The Volcano 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 job
    18  
    19  import (
    20  	"testing"
    21  
    22  	"time"
    23  )
    24  
    25  func TestJobUtil(t *testing.T) {
    26  	testCases := []struct {
    27  		Name        string
    28  		Duration    time.Duration
    29  		ExpectValue string
    30  	}{
    31  		{
    32  			Name:        "InvalidTime",
    33  			Duration:    -time.Minute,
    34  			ExpectValue: "<invalid>",
    35  		},
    36  		{
    37  			Name:        "SmallInvalieTime",
    38  			Duration:    -time.Millisecond,
    39  			ExpectValue: "0s",
    40  		},
    41  		{
    42  			Name:        "NormalSeconds",
    43  			Duration:    62 * time.Second,
    44  			ExpectValue: "62s",
    45  		},
    46  		{
    47  			Name:        "NormalMinutes",
    48  			Duration:    180 * time.Second,
    49  			ExpectValue: "3m",
    50  		},
    51  		{
    52  			Name:        "NormalMinutesWithSecond",
    53  			Duration:    190 * time.Second,
    54  			ExpectValue: "3m10s",
    55  		},
    56  		{
    57  			Name:        "BiggerMinutesWithoutSecond",
    58  			Duration:    121*time.Minute + 56*time.Second,
    59  			ExpectValue: "121m",
    60  		},
    61  		{
    62  			Name:        "NormalHours",
    63  			Duration:    5*time.Hour + 9*time.Second,
    64  			ExpectValue: "5h",
    65  		},
    66  		{
    67  			Name:        "NormalHoursWithMinute",
    68  			Duration:    5*time.Hour + 7*time.Minute + 9*time.Second,
    69  			ExpectValue: "5h7m",
    70  		},
    71  		{
    72  			Name:        "BiggerHoursWithoutMinute",
    73  			Duration:    12*time.Hour + 7*time.Minute + 9*time.Second,
    74  			ExpectValue: "12h",
    75  		},
    76  		{
    77  			Name:        "NormalDays",
    78  			Duration:    5*24*time.Hour + 7*time.Minute + 9*time.Second,
    79  			ExpectValue: "5d",
    80  		},
    81  		{
    82  			Name:        "NormalDaysWithHours",
    83  			Duration:    5*24*time.Hour + 9*time.Hour,
    84  			ExpectValue: "5d9h",
    85  		},
    86  		{
    87  			Name:        "BiggerDayWithoutHours",
    88  			Duration:    531*24*time.Hour + 7*time.Minute + 9*time.Second,
    89  			ExpectValue: "531d",
    90  		},
    91  		{
    92  			Name:        "NormalYears",
    93  			Duration:    (365*5+89)*24*time.Hour + 7*time.Minute + 9*time.Second,
    94  			ExpectValue: "5y89d",
    95  		},
    96  		{
    97  			Name:        "BiggerYears",
    98  			Duration:    (365*12+15)*24*time.Hour + 7*time.Minute + 9*time.Second,
    99  			ExpectValue: "12y",
   100  		},
   101  	}
   102  
   103  	for i, testcase := range testCases {
   104  		answer := HumanDuration(testcase.Duration)
   105  		if answer != testcase.ExpectValue {
   106  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, answer)
   107  		}
   108  	}
   109  }