github.com/facebookincubator/ttpforge@v1.0.13-0.20240405153150-5ae801628835/pkg/platforms/spec_test.go (about)

     1  /*
     2  Copyright © 2023-present, Meta Platforms, Inc. and affiliates
     3  Permission is hereby granted, free of charge, to any person obtaining a copy
     4  of this software and associated documentation files (the "Software"), to deal
     5  in the Software without restriction, including without limitation the rights
     6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  copies of the Software, and to permit persons to whom the Software is
     8  furnished to do so, subject to the following conditions:
     9  The above copyright notice and this permission notice shall be included in
    10  all copies or substantial portions of the Software.
    11  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    12  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    13  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    14  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    15  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    16  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    17  THE SOFTWARE.
    18  */
    19  
    20  package platforms
    21  
    22  import (
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestCompatibleWith(t *testing.T) {
    30  
    31  	testCases := []struct {
    32  		name                string
    33  		spec                Spec
    34  		otherSpec           Spec
    35  		expectValidateError bool
    36  		desiredResult       bool
    37  		correctString       string
    38  	}{
    39  		{
    40  			name: "Empty OS and Architecture",
    41  			spec: Spec{},
    42  			otherSpec: Spec{
    43  				OS:   "linux",
    44  				Arch: "amd64",
    45  			},
    46  			expectValidateError: true,
    47  		},
    48  		{
    49  			name: "Invalid OS",
    50  			spec: Spec{
    51  				OS: "invalid",
    52  			},
    53  			otherSpec: Spec{
    54  				OS:   "linux",
    55  				Arch: "amd64",
    56  			},
    57  			expectValidateError: true,
    58  		},
    59  		{
    60  			name: "Invalid Arch",
    61  			spec: Spec{
    62  				Arch: "invalid",
    63  			},
    64  			otherSpec: Spec{
    65  				OS:   "linux",
    66  				Arch: "amd64",
    67  			},
    68  			expectValidateError: true,
    69  		},
    70  		{
    71  			name: "Matching OS",
    72  			spec: Spec{
    73  				OS: "linux",
    74  			},
    75  			otherSpec: Spec{
    76  				OS:   "linux",
    77  				Arch: "amd64",
    78  			},
    79  			desiredResult: true,
    80  			correctString: "linux/[any architecture]",
    81  		},
    82  		{
    83  			name: "Matching OS but Not Architecture",
    84  			spec: Spec{
    85  				OS:   "linux",
    86  				Arch: "arm64",
    87  			},
    88  			otherSpec: Spec{
    89  				OS:   "linux",
    90  				Arch: "amd64",
    91  			},
    92  			desiredResult: false,
    93  			correctString: "linux/arm64",
    94  		},
    95  		{
    96  			name: "Matching Architecture but not OS",
    97  			spec: Spec{
    98  				OS:   "darwin",
    99  				Arch: "amd64",
   100  			},
   101  			otherSpec: Spec{
   102  				OS:   "linux",
   103  				Arch: "amd64",
   104  			},
   105  			desiredResult: false,
   106  			correctString: "darwin/amd64",
   107  		},
   108  		{
   109  			name: "Matching Architecture",
   110  			spec: Spec{
   111  				Arch: "amd64",
   112  			},
   113  			otherSpec: Spec{
   114  				OS:   "linux",
   115  				Arch: "amd64",
   116  			},
   117  			desiredResult: true,
   118  			correctString: "[any OS]/amd64",
   119  		},
   120  	}
   121  
   122  	for _, tc := range testCases {
   123  		t.Run(tc.name, func(t *testing.T) {
   124  			err := tc.spec.Validate()
   125  			if tc.expectValidateError {
   126  				require.Error(t, err)
   127  				return
   128  			}
   129  			require.NoError(t, err)
   130  			result := tc.spec.IsCompatibleWith(tc.otherSpec)
   131  			assert.Equal(t, tc.desiredResult, result)
   132  			assert.Equal(t, tc.correctString, tc.spec.String())
   133  		})
   134  	}
   135  }