github.com/vmware/govmomi@v0.43.0/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  		expectedIsSupported bool
    32  		expectedVersion     HardwareVersion
    33  		expectedString      string
    34  	}{
    35  		{
    36  			name:                "EmptyString",
    37  			in:                  "",
    38  			expectedIsValid:     false,
    39  			expectedIsSupported: false,
    40  			expectedVersion:     0,
    41  			expectedString:      "",
    42  		},
    43  		{
    44  			name:                "PrefixSansNumber",
    45  			in:                  "vmx-",
    46  			expectedIsValid:     false,
    47  			expectedIsSupported: false,
    48  			expectedVersion:     0,
    49  			expectedString:      "",
    50  		},
    51  		{
    52  			name:                "NumberSansPrefix",
    53  			in:                  "13",
    54  			expectedIsValid:     true,
    55  			expectedIsSupported: true,
    56  			expectedVersion:     VMX13,
    57  			expectedString:      "vmx-13",
    58  		},
    59  		{
    60  			name:                "PrefixAndNumber",
    61  			in:                  "vmx-13",
    62  			expectedIsValid:     true,
    63  			expectedIsSupported: true,
    64  			expectedVersion:     VMX13,
    65  			expectedString:      "vmx-13",
    66  		},
    67  		{
    68  			name:                "UpperPrefixAndNumber",
    69  			in:                  "VMX-18",
    70  			expectedIsValid:     true,
    71  			expectedIsSupported: true,
    72  			expectedVersion:     VMX18,
    73  			expectedString:      "vmx-18",
    74  		},
    75  		{
    76  			name:                "vmx-99",
    77  			in:                  "vmx-99",
    78  			expectedIsValid:     true,
    79  			expectedIsSupported: false,
    80  			expectedVersion:     HardwareVersion(99),
    81  			expectedString:      "vmx-99",
    82  		},
    83  		{
    84  			name:                "ETooLarge",
    85  			in:                  "vmx-512",
    86  			expectedIsValid:     false,
    87  			expectedIsSupported: false,
    88  			expectedVersion:     0,
    89  			expectedString:      "",
    90  		},
    91  		{
    92  			name:                "ETooLarge2",
    93  			in:                  "512",
    94  			expectedIsValid:     false,
    95  			expectedIsSupported: false,
    96  			expectedVersion:     0,
    97  			expectedString:      "",
    98  		},
    99  	}
   100  
   101  	t.Run("ParseHardwareVersion", func(t *testing.T) {
   102  		for i := range testCases {
   103  			tc := testCases[i]
   104  			t.Run(tc.name, func(t *testing.T) {
   105  				v, err := ParseHardwareVersion(tc.in)
   106  				if err != nil && tc.expectedIsValid {
   107  					t.Fatalf("unexpected error: %v", err)
   108  				}
   109  				if a, e := v.IsValid(), tc.expectedIsValid; a != e {
   110  					t.Errorf("unexpected invalid value: a=%v, e=%v", a, e)
   111  				}
   112  				if a, e := v.IsSupported(), tc.expectedIsSupported; a != e {
   113  					t.Errorf("unexpected supported value: a=%v, e=%v", a, e)
   114  				}
   115  				if a, e := v, tc.expectedVersion; a != e {
   116  					t.Errorf("unexpected value: a=%v, e=%v", a, e)
   117  				}
   118  				if a, e := v.String(), tc.expectedString; a != e {
   119  					t.Errorf("unexpected string: a=%v, e=%v", a, e)
   120  				}
   121  			})
   122  		}
   123  	})
   124  
   125  }
   126  
   127  func TestHardwareVersion(t *testing.T) {
   128  
   129  	var uniqueExpectedVersions []HardwareVersion
   130  
   131  	type testCase struct {
   132  		name                string
   133  		in                  string
   134  		expectedIsValid     bool
   135  		expectedIsSupported bool
   136  		expectedVersion     HardwareVersion
   137  		expectedString      string
   138  	}
   139  
   140  	testCasesForVersion := func(
   141  		version int,
   142  		expectedVersion HardwareVersion,
   143  		expectedString string) []testCase {
   144  
   145  		uniqueExpectedVersions = append(uniqueExpectedVersions, MustParseHardwareVersion(expectedString))
   146  
   147  		szVersion := strconv.Itoa(version)
   148  		return []testCase{
   149  			{
   150  				name:                szVersion,
   151  				in:                  szVersion,
   152  				expectedIsValid:     true,
   153  				expectedIsSupported: true,
   154  				expectedVersion:     expectedVersion,
   155  				expectedString:      expectedString,
   156  			},
   157  			{
   158  				name:                "vmx-" + szVersion,
   159  				in:                  "vmx-" + szVersion,
   160  				expectedIsValid:     true,
   161  				expectedIsSupported: true,
   162  				expectedVersion:     expectedVersion,
   163  				expectedString:      expectedString,
   164  			},
   165  			{
   166  				name:                "VMX-" + szVersion,
   167  				in:                  "VMX-" + szVersion,
   168  				expectedIsValid:     true,
   169  				expectedIsSupported: true,
   170  				expectedVersion:     expectedVersion,
   171  				expectedString:      expectedString,
   172  			},
   173  		}
   174  	}
   175  
   176  	testCases := []testCase{
   177  		{
   178  			name:                "EmptyString",
   179  			in:                  "",
   180  			expectedIsValid:     false,
   181  			expectedIsSupported: false,
   182  			expectedVersion:     0,
   183  			expectedString:      "",
   184  		},
   185  		{
   186  			name:                "PrefixSansVersion",
   187  			in:                  "vmx-",
   188  			expectedIsValid:     false,
   189  			expectedIsSupported: false,
   190  			expectedVersion:     0,
   191  			expectedString:      "",
   192  		},
   193  		{
   194  			name:                "PrefixAndInvalidVersion",
   195  			in:                  "vmx-0",
   196  			expectedIsValid:     false,
   197  			expectedIsSupported: false,
   198  			expectedVersion:     0,
   199  			expectedString:      "",
   200  		},
   201  		{
   202  			name:                "UnsupportedVersion",
   203  			in:                  "1",
   204  			expectedIsValid:     true,
   205  			expectedIsSupported: false,
   206  			expectedVersion:     HardwareVersion(1),
   207  			expectedString:      "vmx-1",
   208  		},
   209  	}
   210  
   211  	testCases = append(testCases, testCasesForVersion(3, VMX3, "vmx-3")...)
   212  	testCases = append(testCases, testCasesForVersion(4, VMX4, "vmx-4")...)
   213  	testCases = append(testCases, testCasesForVersion(6, VMX6, "vmx-6")...)
   214  	testCases = append(testCases, testCasesForVersion(7, VMX7, "vmx-7")...)
   215  	testCases = append(testCases, testCasesForVersion(8, VMX8, "vmx-8")...)
   216  	testCases = append(testCases, testCasesForVersion(9, VMX9, "vmx-9")...)
   217  	testCases = append(testCases, testCasesForVersion(10, VMX10, "vmx-10")...)
   218  	testCases = append(testCases, testCasesForVersion(11, VMX11, "vmx-11")...)
   219  	testCases = append(testCases, testCasesForVersion(12, VMX12, "vmx-12")...)
   220  	testCases = append(testCases, testCasesForVersion(13, VMX13, "vmx-13")...)
   221  	testCases = append(testCases, testCasesForVersion(14, VMX14, "vmx-14")...)
   222  	testCases = append(testCases, testCasesForVersion(15, VMX15, "vmx-15")...)
   223  	testCases = append(testCases, testCasesForVersion(16, VMX16, "vmx-16")...)
   224  	testCases = append(testCases, testCasesForVersion(17, VMX17, "vmx-17")...)
   225  	testCases = append(testCases, testCasesForVersion(18, VMX18, "vmx-18")...)
   226  	testCases = append(testCases, testCasesForVersion(19, VMX19, "vmx-19")...)
   227  	testCases = append(testCases, testCasesForVersion(20, VMX20, "vmx-20")...)
   228  	testCases = append(testCases, testCasesForVersion(21, VMX21, "vmx-21")...)
   229  
   230  	t.Run("GetHardwareVersions", func(t *testing.T) {
   231  		a, e := uniqueExpectedVersions, GetHardwareVersions()
   232  		sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
   233  		sort.Slice(e, func(i, j int) bool { return e[i] < e[j] })
   234  		if a, e := len(a), len(e); a != e {
   235  			t.Errorf("unexpected number of versions: a=%d, e=%d", a, e)
   236  		}
   237  		for i := range a {
   238  			if a[i] != e[i] {
   239  				t.Errorf("unexpected version: i=%d, a=%s, e=%s", i, a, e)
   240  			}
   241  		}
   242  	})
   243  
   244  	t.Run("ParseHardwareVersion", func(t *testing.T) {
   245  		for i := range testCases {
   246  			tc := testCases[i]
   247  			t.Run(tc.name, func(t *testing.T) {
   248  				v, err := ParseHardwareVersion(tc.in)
   249  				if err != nil && tc.expectedIsValid {
   250  					t.Fatalf("unexpected error: %v", err)
   251  				}
   252  				if a, e := v.IsValid(), tc.expectedIsValid; a != e {
   253  					t.Errorf("unexpected invalid value: a=%v, e=%v", a, e)
   254  				}
   255  				if tc.expectedIsValid {
   256  					if a, e := v, tc.expectedVersion; a != e {
   257  						t.Errorf("unexpected value: a=%v, e=%v", a, e)
   258  					}
   259  					if a, e := v.String(), tc.expectedString; a != e {
   260  						t.Errorf("unexpected string: a=%v, e=%v", a, e)
   261  					}
   262  				}
   263  			})
   264  		}
   265  	})
   266  	t.Run("MarshalText", func(t *testing.T) {
   267  		for i := range testCases {
   268  			tc := testCases[i]
   269  			t.Run(tc.name, func(t *testing.T) {
   270  				data, err := tc.expectedVersion.MarshalText()
   271  				if err != nil {
   272  					t.Fatalf("unexpected error marshaling text: %v", err)
   273  				}
   274  				if a, e := string(data), tc.expectedString; a != e {
   275  					t.Errorf("unexpected data marshaling text: %s", a)
   276  				}
   277  			})
   278  		}
   279  	})
   280  
   281  	t.Run("UnmarshalText", func(t *testing.T) {
   282  		for i := range testCases {
   283  			tc := testCases[i]
   284  			t.Run(tc.name, func(t *testing.T) {
   285  				var (
   286  					data = []byte(tc.in)
   287  					v    HardwareVersion
   288  				)
   289  				if err := v.UnmarshalText(data); err != nil {
   290  					if !tc.expectedIsValid {
   291  						if a, e := err.Error(), fmt.Sprintf("invalid version: %q", tc.in); a != e {
   292  							t.Errorf("unexpected error unmarshaling text: %q", a)
   293  						}
   294  					} else {
   295  						t.Errorf("unexpected error unmarshaling text: %v", err)
   296  					}
   297  				} else if a, e := v, v; a != e {
   298  					t.Errorf("unexpected data unmarshaling text: %s", a)
   299  				}
   300  			})
   301  		}
   302  	})
   303  }