github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/cmp_test.go (about)

     1  /*
     2  Copyright 2022 Gravitational, Inc.
     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  	"testing"
    21  
    22  	"github.com/gogo/protobuf/proto"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestProtoKnownFieldsEqual(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	tests := []struct {
    30  		name   string
    31  		inputA proto.Message
    32  		inputB proto.Message
    33  		assert require.BoolAssertionFunc
    34  	}{
    35  		{
    36  			name: "true",
    37  			inputA: &AWS{
    38  				Region: "us-west-1",
    39  				RedshiftServerless: RedshiftServerless{
    40  					WorkgroupID: "id",
    41  				},
    42  			},
    43  			inputB: &AWS{
    44  				Region: "us-west-1",
    45  				RedshiftServerless: RedshiftServerless{
    46  					WorkgroupID: "id",
    47  				},
    48  			},
    49  			assert: require.True,
    50  		},
    51  		{
    52  			name: "true ignoring XXX_unrecognized",
    53  			inputA: &AWS{
    54  				Region:           "us-west-1",
    55  				XXX_unrecognized: []byte{66, 0},
    56  			},
    57  			inputB: &AWS{
    58  				Region: "us-west-1",
    59  			},
    60  			assert: require.True,
    61  		},
    62  		{
    63  			name: "true ignoring nested XXX_unrecognized",
    64  			inputA: &AWS{
    65  				Region: "us-west-1",
    66  				MemoryDB: MemoryDB{
    67  					XXX_unrecognized: []byte{99, 0},
    68  				},
    69  			},
    70  			inputB: &AWS{
    71  				Region: "us-west-1",
    72  			},
    73  			assert: require.True,
    74  		},
    75  		{
    76  			name: "false differrent values",
    77  			inputA: &AWS{
    78  				Region: "us-west-1",
    79  				RedshiftServerless: RedshiftServerless{
    80  					WorkgroupID: "id",
    81  				},
    82  			},
    83  			inputB: &AWS{
    84  				Region: "us-west-1",
    85  				RedshiftServerless: RedshiftServerless{
    86  					WorkgroupID: "differrent-id",
    87  				},
    88  			},
    89  			assert: require.False,
    90  		},
    91  		{
    92  			name: "false different types",
    93  			inputA: &AWS{
    94  				Region: "us-west-1",
    95  			},
    96  			inputB: &KubeAWS{
    97  				Region: "us-west-1",
    98  			},
    99  			assert: require.False,
   100  		},
   101  	}
   102  
   103  	for _, test := range tests {
   104  		t.Run(test.name, func(t *testing.T) {
   105  			test.assert(t, protoKnownFieldsEqual(test.inputA, test.inputB))
   106  		})
   107  	}
   108  }