github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/utils/atlas/endpoints_test.go (about) 1 // Copyright 2023 Gravitational, Inc 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package atlas 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestIsAtlasEndpoint(t *testing.T) { 24 for _, tc := range []struct { 25 desc string 26 endpoint string 27 result bool 28 }{ 29 // Valid 30 {"public endpoint host only", "test.xxxxxxx.mongodb.net", true}, 31 {"public endpoint", "mongodb://test.xxxxxxx.mongodb.net", true}, 32 {"public endpoint with srv", "mongodb+srv://test.xxxxxxx.mongodb.net", true}, 33 {"private link", "mongodb://pl-0-us-east-1-xxxxx.mongodb.net:1024,pl-0-us-east-1-xxxxx.mongodb.net:1025,pl-0-us-east-1-xxxxx.mongodb.net:1026/?ssl=true&authSource=admin&replicaSet=Cluster0-shard-0-shard-0", true}, 34 {"private link with srv", "mongodb+srv://cluster0-pl-0-xxxxx.mongodb.net", true}, 35 {"azure private link", "mongodb://cluster0-pl-0.xxxxx.azure.mongodb.net", true}, 36 {"azure private link with srv", "mongodb://pl-0-eastus2.xxxxx.azure.mongodb.net:1024,pl-0-eastus2.xxxxx.azure.mongodb.net:1025,pl-0-eastus2.xxxxx.azure.mongodb.net:1026/?ssl=truereplicaSet=atlas-xxxxxx-shard-0 ", true}, 37 // Invalid 38 {"internal name", "mongodb://mongodb", false}, 39 {"domain name with mongodb", "mongodb://mongodb.company.com", false}, 40 } { 41 t.Run(tc.desc, func(t *testing.T) { 42 require.Equal(t, tc.result, IsAtlasEndpoint(tc.endpoint)) 43 }) 44 } 45 } 46 47 func TestParseAtlasEndpoint(t *testing.T) { 48 for _, tc := range []struct { 49 desc string 50 endpoint string 51 result string 52 errAssert require.ErrorAssertionFunc 53 }{ 54 // Valid 55 {"public endpoint host only", "test.xxxxxxx.mongodb.net", "test", require.NoError}, 56 {"public endpoint", "mongodb://test.xxxxxxx.mongodb.net", "test", require.NoError}, 57 {"public endpoint with srv", "mongodb+srv://test.xxxxxxx.mongodb.net", "test", require.NoError}, 58 { 59 "private link", 60 "mongodb://pl-0-us-east-1-xxxxx.mongodb.net:1024,pl-0-us-east-1-xxxxx.mongodb.net:1025,pl-0-us-east-1-xxxxx.mongodb.net:1026/?ssl=true&authSource=admin&replicaSet=Cluster0-shard-0-shard-0", 61 "pl-0-us-east-1-xxxxx", 62 require.NoError, 63 }, 64 {"private link with srv", "mongodb+srv://cluster0-pl-0-xxxxx.mongodb.net", "cluster0-pl-0-xxxxx", require.NoError}, 65 {"azure private link", "mongodb://cluster0-pl-0.xxxxx.azure.mongodb.net", "cluster0-pl-0", require.NoError}, 66 { 67 "azure private link with srv", 68 "mongodb://pl-0-eastus2.xxxxx.azure.mongodb.net:1024,pl-0-eastus2.xxxxx.azure.mongodb.net:1025,pl-0-eastus2.xxxxx.azure.mongodb.net:1026/?ssl=truereplicaSet=atlas-xxxxxx-shard-0 ", 69 "pl-0-eastus2", 70 require.NoError, 71 }, 72 // Invalid 73 {"internal name", "mongodb://mongodb", "", require.Error}, 74 {"domain name with mongodb", "mongodb://mongodb.company.com", "", require.Error}, 75 } { 76 t.Run(tc.desc, func(t *testing.T) { 77 res, err := ParseAtlasEndpoint(tc.endpoint) 78 tc.errAssert(t, err) 79 require.Equal(t, tc.result, res) 80 }) 81 } 82 }