github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/clone_test.go (about) 1 /* 2 Copyright 2023 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 "fmt" 21 "testing" 22 23 "github.com/stretchr/testify/require" 24 "google.golang.org/protobuf/protoadapt" 25 26 "github.com/gravitational/teleport/api/utils" 27 ) 28 29 type protoResource interface { 30 Resource 31 protoadapt.MessageV1 32 } 33 34 func TestCloning(t *testing.T) { 35 // Test that cloning some of our messages produces the same type 36 // with the same contents. When CheckAndSetDefaults sets an empty 37 // slice or map instead of a nil one, set it to nil so the 38 // equality check below won't fail. 39 var resources []protoResource 40 41 a, err := NewAccessRequest("foo", "bar", "role") 42 require.NoError(t, err) 43 accessRequest := a.(*AccessRequestV3) 44 accessRequest.Spec.SuggestedReviewers = nil 45 accessRequest.Spec.RequestedResourceIDs = nil 46 resources = append(resources, accessRequest) 47 48 user, err := NewUser("foo") 49 require.NoError(t, err) 50 resources = append(resources, user.(*UserV2)) 51 52 s, err := NewServer("foo", KindNode, ServerSpecV2{}) 53 require.NoError(t, err) 54 server := s.(*ServerV2) 55 server.Metadata.Labels = nil 56 resources = append(resources, server) 57 58 remCluster, err := NewRemoteCluster("foo") 59 require.NoError(t, err) 60 resources = append(resources, remCluster.(*RemoteClusterV3)) 61 62 for _, r := range resources { 63 t.Run(fmt.Sprintf("%T", r), func(t *testing.T) { 64 rCopy := utils.CloneProtoMsg(r) 65 require.Equal(t, r, rCopy) 66 require.IsType(t, r, rCopy) 67 }) 68 } 69 }