github.com/vmware/govmomi@v0.37.2/vim25/types/hardware_version_test.go (about)

     1  /*
     2  Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
     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 types
    18  
    19  import (
    20  	"fmt"
    21  	"sort"
    22  	"strconv"
    23  	"testing"
    24  )
    25  
    26  func TestParseHardwareVersion(t *testing.T) {
    27  	testCases := []struct {
    28  		name            string
    29  		in              string
    30  		expectedIsValid bool
    31  		expectedVersion HardwareVersion
    32  		expectedString  string
    33  	}{
    34  		{
    35  			name: "EmptyString",
    36  			in:   "",
    37  		},
    38  		{
    39  			name: "PrefixSansNumber",
    40  			in:   "vmx-",
    41  		},
    42  		{
    43  			name:            "NumberSansPrefix",
    44  			in:              "13",
    45  			expectedIsValid: true,
    46  			expectedVersion: VMX13,
    47  			expectedString:  "vmx-13",
    48  		},
    49  		{
    50  			name:            "vmx-13",
    51  			in:              "vmx-13",
    52  			expectedIsValid: true,
    53  			expectedVersion: VMX13,
    54  			expectedString:  "vmx-13",
    55  		},
    56  		{
    57  			name:            "VMX-18",
    58  			in:              "VMX-18",
    59  			expectedIsValid: true,
    60  			expectedVersion: VMX18,
    61  			expectedString:  "vmx-18",
    62  		},
    63  	}
    64  
    65  	t.Run("ParseHardwareVersion", func(t *testing.T) {
    66  		for i := range testCases {
    67  			tc := testCases[i]
    68  			t.Run(tc.name, func(t *testing.T) {
    69  				v, err := ParseHardwareVersion(tc.in)
    70  				if err != nil && tc.expectedIsValid {
    71  					t.Fatalf("unexpected error: %v", err)
    72  				}
    73  				if a, e := v.IsValid(), tc.expectedIsValid; a != e {
    74  					t.Errorf("unexpected invalid value: a=%v, e=%v", a, e)
    75  				}
    76  				if v.IsValid() {
    77  					if a, e := v, tc.expectedVersion; a != e {
    78  						t.Errorf("unexpected value: a=%v, e=%v", a, e)
    79  					}
    80  					if a, e := v.String(), tc.expectedString; a != e {
    81  						t.Errorf("unexpected string: a=%v, e=%v", a, e)
    82  					}
    83  				}
    84  			})
    85  		}
    86  	})
    87  
    88  }
    89  
    90  func TestHardwareVersion(t *testing.T) {
    91  
    92  	var uniqueExpectedVersions []HardwareVersion
    93  
    94  	type testCase struct {
    95  		name            string
    96  		in              string
    97  		expectedIsValid bool
    98  		expectedVersion HardwareVersion
    99  		expectedString  string
   100  	}
   101  
   102  	testCasesForVersion := func(
   103  		version int,
   104  		expectedVersion HardwareVersion,
   105  		expectedString string) []testCase {
   106  
   107  		uniqueExpectedVersions = append(uniqueExpectedVersions, MustParseHardwareVersion(expectedString))
   108  
   109  		szVersion := strconv.Itoa(version)
   110  		return []testCase{
   111  			{
   112  				name:            szVersion,
   113  				in:              szVersion,
   114  				expectedIsValid: true,
   115  				expectedVersion: expectedVersion,
   116  				expectedString:  expectedString,
   117  			},
   118  			{
   119  				name:            "vmx-" + szVersion,
   120  				in:              "vmx-" + szVersion,
   121  				expectedIsValid: true,
   122  				expectedVersion: expectedVersion,
   123  				expectedString:  expectedString,
   124  			},
   125  			{
   126  				name:            "VMX-" + szVersion,
   127  				in:              "VMX-" + szVersion,
   128  				expectedIsValid: true,
   129  				expectedVersion: expectedVersion,
   130  				expectedString:  expectedString,
   131  			},
   132  		}
   133  	}
   134  
   135  	testCases := []testCase{
   136  		{
   137  			name: "EmptyString",
   138  			in:   "",
   139  		},
   140  		{
   141  			name: "PrefixSansVersion",
   142  			in:   "vmx-",
   143  		},
   144  		{
   145  			name: "PrefixAndInvalidVersion",
   146  			in:   "vmx-0",
   147  		},
   148  		{
   149  			name: "InvalidVersion",
   150  			in:   "1",
   151  		},
   152  	}
   153  
   154  	testCases = append(testCases, testCasesForVersion(3, VMX3, "vmx-3")...)
   155  	testCases = append(testCases, testCasesForVersion(4, VMX4, "vmx-4")...)
   156  	testCases = append(testCases, testCasesForVersion(6, VMX6, "vmx-6")...)
   157  	testCases = append(testCases, testCasesForVersion(7, VMX7, "vmx-7")...)
   158  	testCases = append(testCases, testCasesForVersion(8, VMX8, "vmx-8")...)
   159  	testCases = append(testCases, testCasesForVersion(9, VMX9, "vmx-9")...)
   160  	testCases = append(testCases, testCasesForVersion(10, VMX10, "vmx-10")...)
   161  	testCases = append(testCases, testCasesForVersion(11, VMX11, "vmx-11")...)
   162  	testCases = append(testCases, testCasesForVersion(12, VMX12, "vmx-12")...)
   163  	testCases = append(testCases, testCasesForVersion(13, VMX13, "vmx-13")...)
   164  	testCases = append(testCases, testCasesForVersion(14, VMX14, "vmx-14")...)
   165  	testCases = append(testCases, testCasesForVersion(15, VMX15, "vmx-15")...)
   166  	testCases = append(testCases, testCasesForVersion(16, VMX16, "vmx-16")...)
   167  	testCases = append(testCases, testCasesForVersion(17, VMX17, "vmx-17")...)
   168  	testCases = append(testCases, testCasesForVersion(18, VMX18, "vmx-18")...)
   169  	testCases = append(testCases, testCasesForVersion(19, VMX19, "vmx-19")...)
   170  	testCases = append(testCases, testCasesForVersion(20, VMX20, "vmx-20")...)
   171  	testCases = append(testCases, testCasesForVersion(21, VMX21, "vmx-21")...)
   172  
   173  	t.Run("GetHardwareVersions", func(t *testing.T) {
   174  		a, e := uniqueExpectedVersions, GetHardwareVersions()
   175  		sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
   176  		sort.Slice(e, func(i, j int) bool { return e[i] < e[j] })
   177  		if a, e := len(a), len(e); a != e {
   178  			t.Errorf("unexpected number of versions: a=%d, e=%d", a, e)
   179  		}
   180  		for i := range a {
   181  			if a[i] != e[i] {
   182  				t.Errorf("unexpected version: i=%d, a=%s, e=%s", i, a, e)
   183  			}
   184  		}
   185  	})
   186  
   187  	t.Run("ParseHardwareVersion", func(t *testing.T) {
   188  		for i := range testCases {
   189  			tc := testCases[i]
   190  			t.Run(tc.name, func(t *testing.T) {
   191  				v, err := ParseHardwareVersion(tc.in)
   192  				if err != nil && tc.expectedIsValid {
   193  					t.Fatalf("unexpected error: %v", err)
   194  				}
   195  				if a, e := v.IsValid(), tc.expectedIsValid; a != e {
   196  					t.Errorf("unexpected invalid value: a=%v, e=%v", a, e)
   197  				}
   198  				if tc.expectedIsValid {
   199  					if a, e := v, tc.expectedVersion; a != e {
   200  						t.Errorf("unexpected value: a=%v, e=%v", a, e)
   201  					}
   202  					if a, e := v.String(), tc.expectedString; a != e {
   203  						t.Errorf("unexpected string: a=%v, e=%v", a, e)
   204  					}
   205  				}
   206  			})
   207  		}
   208  	})
   209  	t.Run("MarshalText", func(t *testing.T) {
   210  		for i := range testCases {
   211  			tc := testCases[i]
   212  			t.Run(tc.name, func(t *testing.T) {
   213  				data, err := tc.expectedVersion.MarshalText()
   214  				if err != nil {
   215  					t.Fatalf("unexpected error marshaling text: %v", err)
   216  				}
   217  				if a, e := string(data), tc.expectedString; a != e {
   218  					t.Errorf("unexpected data marshaling text: %s", a)
   219  				}
   220  			})
   221  		}
   222  	})
   223  
   224  	t.Run("UnmarshalText", func(t *testing.T) {
   225  		for i := range testCases {
   226  			tc := testCases[i]
   227  			t.Run(tc.name, func(t *testing.T) {
   228  				var (
   229  					data = []byte(tc.in)
   230  					v    HardwareVersion
   231  				)
   232  				if err := v.UnmarshalText(data); err != nil {
   233  					if !tc.expectedIsValid {
   234  						if a, e := err.Error(), fmt.Sprintf("invalid version: %q", tc.in); a != e {
   235  							t.Errorf("unexpected error unmarshaling text: %q", a)
   236  						}
   237  					} else {
   238  						t.Errorf("unexpected error unmarshaling text: %v", err)
   239  					}
   240  				} else if a, e := v, v; a != e {
   241  					t.Errorf("unexpected data unmarshaling text: %s", a)
   242  				}
   243  			})
   244  		}
   245  	})
   246  }