github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/utils/aws/ec2_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 aws 18 19 import ( 20 "testing" 21 22 "github.com/google/uuid" 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestIsEC2NodeID(t *testing.T) { 27 // EC2 Node IDs are {AWS account ID}-{EC2 resource ID} eg: 28 // 123456789012-i-1234567890abcdef0 29 // AWS account ID is always a 12 digit number, see 30 // https://docs.aws.amazon.com/general/latest/gr/acct-identifiers.html 31 // EC2 resource ID is i-{8 or 17 hex digits}, see 32 // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/resource-ids.html 33 testCases := []struct { 34 name string 35 id string 36 expected bool 37 }{ 38 { 39 name: "8 digit", 40 id: "123456789012-i-12345678", 41 expected: true, 42 }, 43 { 44 name: "17 digit", 45 id: "123456789012-i-1234567890abcdef0", 46 expected: true, 47 }, 48 { 49 name: "foo", 50 id: "foo", 51 expected: false, 52 }, 53 { 54 name: "uuid", 55 id: uuid.NewString(), 56 expected: false, 57 }, 58 } 59 for _, tc := range testCases { 60 t.Run(tc.name, func(t *testing.T) { 61 assert.Equal(t, tc.expected, IsEC2NodeID(tc.id)) 62 }) 63 } 64 }