istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/version/version_test.go (about)

     1  // Copyright Istio 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 version
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"gopkg.in/yaml.v2"
    22  
    23  	"istio.io/istio/pkg/test/util/assert"
    24  )
    25  
    26  func TestVersion(t *testing.T) {
    27  	tests := []struct {
    28  		desc    string
    29  		yamlStr string
    30  		want    Version
    31  		wantErr string
    32  	}{
    33  		{
    34  			desc: "nil success",
    35  		},
    36  		{
    37  			desc:    "major success",
    38  			yamlStr: "1",
    39  			want:    NewVersion(1, 0, 0, ""),
    40  		},
    41  		{
    42  			desc:    "major fail",
    43  			yamlStr: "1..",
    44  			wantErr: `Malformed version: 1..`,
    45  		},
    46  		{
    47  			desc:    "major fail prefix",
    48  			yamlStr: ".1",
    49  			wantErr: `Malformed version: .1`,
    50  		},
    51  		{
    52  			desc:    "minor success",
    53  			yamlStr: "1.2",
    54  			want:    NewVersion(1, 2, 0, ""),
    55  		},
    56  		{
    57  			desc:    "minor fail",
    58  			yamlStr: "1.1..",
    59  			wantErr: `Malformed version: 1.1..`,
    60  		},
    61  		{
    62  			desc:    "patch success",
    63  			yamlStr: "1.2.3",
    64  			want:    NewVersion(1, 2, 3, ""),
    65  		},
    66  		{
    67  			desc:    "patch fail",
    68  			yamlStr: "1.1.-1",
    69  			wantErr: `Malformed version: 1.1.-1`,
    70  		},
    71  		{
    72  			desc:    "suffix success",
    73  			yamlStr: "1.2.3-istio-test",
    74  			want:    NewVersion(1, 2, 3, "istio-test"),
    75  		},
    76  		{
    77  			desc:    "suffix fail",
    78  			yamlStr: ".1.1.1-something",
    79  			wantErr: `Malformed version: .1.1.1-something`,
    80  		},
    81  		{
    82  			desc:    "Malformed version fail",
    83  			yamlStr: "istio-testing-distroless",
    84  			wantErr: `Malformed version: istio-testing-distroless`,
    85  		},
    86  	}
    87  
    88  	for _, tt := range tests {
    89  		t.Run(tt.desc, func(t *testing.T) {
    90  			got := Version{}
    91  			err := yaml.Unmarshal([]byte(tt.yamlStr), &got)
    92  			if gotErr, wantErr := errToString(err), tt.wantErr; gotErr != wantErr {
    93  				t.Fatalf("yaml.Unmarshal(%s): got error: %s, want error: %s", tt.desc, gotErr, wantErr)
    94  			}
    95  			if tt.wantErr == "" {
    96  				assert.Equal(t, got, tt.want)
    97  			}
    98  		})
    99  	}
   100  }
   101  
   102  // errToString returns the string representation of err and the empty string if
   103  // err is nil.
   104  func errToString(err error) string {
   105  	if err == nil {
   106  		return ""
   107  	}
   108  	return err.Error()
   109  }
   110  
   111  func TestIsVersionString(t *testing.T) {
   112  	tests := []struct {
   113  		name string
   114  		ver  string
   115  		want bool
   116  	}{
   117  		{
   118  			name: "empty",
   119  			ver:  "",
   120  			want: false,
   121  		},
   122  		{
   123  			name: "unknown",
   124  			ver:  "unknown",
   125  			want: false,
   126  		},
   127  		{
   128  			name: "release branch dev",
   129  			ver:  "1.4-dev",
   130  			want: true,
   131  		},
   132  		{
   133  			name: "release",
   134  			ver:  "1.4.5",
   135  			want: true,
   136  		},
   137  		{
   138  			name: "incorrect",
   139  			ver:  "1.4.xxx",
   140  			want: false,
   141  		},
   142  		{
   143  			name: "dev sha",
   144  			ver:  "a3703b76cf4745f3d56bf653ed751509be116351",
   145  			want: false,
   146  		},
   147  		{
   148  			name: "dev sha digit prefix",
   149  			ver:  "60023b76cf4745f3d56bf653ed751509be116351",
   150  			want: false,
   151  		},
   152  	}
   153  	for _, tt := range tests {
   154  		t.Run(tt.name, func(t *testing.T) {
   155  			if got := IsVersionString(tt.ver); got != tt.want {
   156  				t.Errorf("IsVersionString() = %v, want %v", got, tt.want)
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  func TestTagToVersionString(t *testing.T) {
   163  	//type args struct {
   164  	//	path string
   165  	//}
   166  	tests := []struct {
   167  		name string
   168  		// args    args
   169  		want    string
   170  		wantErr bool
   171  	}{
   172  		{
   173  			name:    "1.4.3",
   174  			want:    "1.4.3",
   175  			wantErr: false,
   176  		},
   177  		{
   178  			name:    "1.4.3-distroless",
   179  			want:    "1.4.3",
   180  			wantErr: false,
   181  		},
   182  		{
   183  			name:    "1.5.0-alpha.0",
   184  			want:    "1.5.0",
   185  			wantErr: false,
   186  		},
   187  		{
   188  			name:    "1.5.0-alpha.0-distroless",
   189  			want:    "1.5.0",
   190  			wantErr: false,
   191  		},
   192  		{
   193  			name:    "1.2.10",
   194  			want:    "1.2.10",
   195  			wantErr: false,
   196  		},
   197  		{
   198  			name:    "1.4.0-beta.5",
   199  			want:    "1.4.0",
   200  			wantErr: false,
   201  		},
   202  		{
   203  			name:    "1.3.0-rc.3",
   204  			want:    "1.3.0",
   205  			wantErr: false,
   206  		},
   207  		{
   208  			name:    "1.3.0-rc.3-distroless",
   209  			want:    "1.3.0",
   210  			wantErr: false,
   211  		},
   212  		{
   213  			name:    "1.5-dev",
   214  			want:    "1.5.0",
   215  			wantErr: false,
   216  		},
   217  		{
   218  			name:    "1.5-dev-distroless",
   219  			want:    "1.5.0",
   220  			wantErr: false,
   221  		},
   222  		{
   223  			name:    "1.5-alpha.f850909d7ac95501bbb2ae91f57df218bcf7c630",
   224  			want:    "1.5.0",
   225  			wantErr: false,
   226  		},
   227  		{
   228  			name:    "1.5-alpha.f850909d7ac95501bbb2ae91f57df218bcf7c630-distroless",
   229  			want:    "1.5.0",
   230  			wantErr: false,
   231  		},
   232  		{
   233  			name:    "release-1.3-20200108-10-15",
   234  			want:    "1.3.0",
   235  			wantErr: false,
   236  		},
   237  		{
   238  			name:    "release-1.3-latest-daily",
   239  			want:    "1.3.0",
   240  			wantErr: false,
   241  		},
   242  		{
   243  			name:    "release-1.3-20200108-10-15-distroless",
   244  			want:    "1.3.0",
   245  			wantErr: false,
   246  		},
   247  		{
   248  			name:    "release-1.3-latest-daily-distroless",
   249  			want:    "1.3.0",
   250  			wantErr: false,
   251  		},
   252  		{
   253  			name:    "latest",
   254  			want:    "",
   255  			wantErr: true,
   256  		},
   257  		{
   258  			name:    "latest-distroless",
   259  			want:    "",
   260  			wantErr: true,
   261  		},
   262  		{
   263  			name:    "999450fd4add69e26ba04d001b811863cba8175b",
   264  			want:    "",
   265  			wantErr: true,
   266  		},
   267  	}
   268  	for _, tt := range tests {
   269  		t.Run(tt.name, func(t *testing.T) {
   270  			got, err := TagToVersionString(tt.name)
   271  			if (err != nil) != tt.wantErr {
   272  				t.Errorf("TagToVersionString() error = %v, wantErr %v", err, tt.wantErr)
   273  				return
   274  			}
   275  			if got != tt.want {
   276  				t.Errorf("TagToVersionString() got = %v, want %v", got, tt.want)
   277  			}
   278  		})
   279  	}
   280  }
   281  
   282  func TestVersionString(t *testing.T) {
   283  	tests := map[string]struct {
   284  		version Version
   285  		want    string
   286  	}{
   287  		"with suffix": {
   288  			version: NewVersion(1, 2, 3, "xyz"),
   289  			want:    "1.2.3-xyz",
   290  		},
   291  		"without suffix": {
   292  			version: NewVersion(1, 5, 0, ""),
   293  			want:    "1.5.0",
   294  		},
   295  	}
   296  	for name, tt := range tests {
   297  		t.Run(name, func(t *testing.T) {
   298  			got := tt.version.String()
   299  			if got != tt.want {
   300  				t.Errorf("Version.String(): got: %s, want: %s", got, tt.want)
   301  			}
   302  		})
   303  	}
   304  }
   305  
   306  func TestUnmarshalYAML(t *testing.T) {
   307  	v := &Version{}
   308  	expectedErr := fmt.Errorf("test error")
   309  	errReturn := func(any) error { return expectedErr }
   310  	gotErr := v.UnmarshalYAML(errReturn)
   311  	if gotErr == nil {
   312  		t.Errorf("expected error but got nil")
   313  	}
   314  	if gotErr != expectedErr {
   315  		t.Errorf("error mismatch")
   316  	}
   317  }