github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/utils/version/version_test.go (about)

     1  // Copyright © 2022 Alibaba Group Holding Ltd.
     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 version
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/sirupsen/logrus"
    22  )
    23  
    24  func TestVersion_Compare(t *testing.T) {
    25  	tests := []struct {
    26  		name         string
    27  		givenVersion Version
    28  		oldVersion   Version
    29  		wantRes      bool
    30  	}{
    31  		{
    32  			name:         "test v > v1",
    33  			givenVersion: "v1.20.4",
    34  			oldVersion:   "v1.19.8",
    35  			wantRes:      true,
    36  		},
    37  		{
    38  			name:         "test v = v1",
    39  			givenVersion: "v1.19.8",
    40  			oldVersion:   "v1.19.8",
    41  			wantRes:      true,
    42  		},
    43  		{
    44  			name:         "test v < v1",
    45  			givenVersion: "v1.19.8",
    46  			oldVersion:   "v1.20.4",
    47  			wantRes:      false,
    48  		},
    49  		{
    50  			name:         "test1 old Version illegal",
    51  			givenVersion: "v1.19.8",
    52  			oldVersion:   "",
    53  			wantRes:      false,
    54  		},
    55  		{
    56  			name:         "test2 give Version illegal",
    57  			givenVersion: "",
    58  			oldVersion:   "v1.19.8",
    59  			wantRes:      false,
    60  		},
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			v := tt.givenVersion
    65  			res, err := v.GreaterThan(tt.oldVersion)
    66  			if err != nil {
    67  				logrus.Errorf("compare kubernetes version failed: %s", err)
    68  			}
    69  			if !reflect.DeepEqual(res, tt.wantRes) {
    70  				t.Errorf("Version compare failed! result: %v, want: %v", res, tt.wantRes)
    71  			}
    72  		})
    73  	}
    74  }