PT-1822 Fixed get hostnames for standalone

This commit is contained in:
Carlos Salguero
2020-05-14 23:53:01 -03:00
parent 8e7113d457
commit 55502267d6
4 changed files with 26 additions and 9 deletions

View File

@@ -4,13 +4,10 @@ services:
standalone: standalone:
network_mode: host network_mode: host
image: ${TEST_MONGODB_FLAVOR}:${TEST_PSMDB_VERSION} image: ${TEST_MONGODB_FLAVOR}:${TEST_PSMDB_VERSION}
environment:
MONGO_INITDB_ROOT_USERNAME: ${TEST_MONGODB_ADMIN_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${TEST_MONGODB_ADMIN_PASSWORD}
command: --port=27017 command: --port=27017
volumes:
- ./docker/test/entrypoint-mongod.sh:/entrypoint.sh:ro
- ./docker/test/entrypoint-mongod.sh:/usr/local/bin/docker-entrypoint.sh:ro
- ./docker/test/mongod.key:/mongod.key:ro
- ./docker/test/ssl/rootCA.crt:/rootCA.crt:ro
- ./docker/test/ssl/mongodb.pem:/mongod.pem:ro
s1-mongo1: s1-mongo1:
network_mode: host network_mode: host
image: ${TEST_MONGODB_FLAVOR}:${TEST_PSMDB_VERSION} image: ${TEST_MONGODB_FLAVOR}:${TEST_PSMDB_VERSION}

View File

@@ -30,6 +30,8 @@ const (
envMongoDBConfigsvr3Port = "TEST_MONGODB_CONFIGSVR3_PORT" envMongoDBConfigsvr3Port = "TEST_MONGODB_CONFIGSVR3_PORT"
// //
envMongoDBMongosPort = "TEST_MONGODB_MONGOS_PORT" envMongoDBMongosPort = "TEST_MONGODB_MONGOS_PORT"
envMongoDBStandalonePort = "TEST_MONGODB_STANDALONE_PORT"
// //
envMongoDBUser = "TEST_MONGODB_ADMIN_USERNAME" envMongoDBUser = "TEST_MONGODB_ADMIN_USERNAME"
envMongoDBPassword = "TEST_MONGODB_ADMIN_PASSWORD" envMongoDBPassword = "TEST_MONGODB_ADMIN_PASSWORD"
@@ -39,6 +41,9 @@ var (
// MongoDBHost is the hostname. Since it runs locally, it is localhost // MongoDBHost is the hostname. Since it runs locally, it is localhost
MongoDBHost = "127.0.0.1" MongoDBHost = "127.0.0.1"
// Port for standalone instance
MongoDBStandalonePort = os.Getenv(envMongoDBStandalonePort)
// MongoDBShard1ReplsetName Replicaset name for shard 1 // MongoDBShard1ReplsetName Replicaset name for shard 1
MongoDBShard1ReplsetName = os.Getenv(envMongoDBShard1ReplsetName) MongoDBShard1ReplsetName = os.Getenv(envMongoDBShard1ReplsetName)
// MongoDBShard1PrimaryPort is the port for the primary instance of shard 1 // MongoDBShard1PrimaryPort is the port for the primary instance of shard 1

View File

@@ -2,7 +2,6 @@ package util
import ( import (
"context" "context"
"fmt"
"sort" "sort"
"strings" "strings"
@@ -13,6 +12,10 @@ import (
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
const (
shardingNotEnabledError = 203
)
var ( var (
CANNOT_GET_QUERY_ERROR = errors.New("cannot get query field from the profile document (it is not a map)") CANNOT_GET_QUERY_ERROR = errors.New("cannot get query field from the profile document (it is not a map)")
) )
@@ -119,6 +122,9 @@ func GetHostnames(ctx context.Context, client *mongo.Client) ([]string, error) {
var shardsMap proto.ShardsMap var shardsMap proto.ShardsMap
smRes := client.Database("admin").RunCommand(ctx, primitive.M{"getShardMap": 1}) smRes := client.Database("admin").RunCommand(ctx, primitive.M{"getShardMap": 1})
if smRes.Err() != nil { if smRes.Err() != nil {
if e, ok := smRes.Err().(mongo.CommandError); ok && e.Code == shardingNotEnabledError {
return nil, nil // standalone instance
}
return nil, errors.Wrap(smRes.Err(), "cannot getShardMap for GetHostnames") return nil, errors.Wrap(smRes.Err(), "cannot getShardMap for GetHostnames")
} }
if err := smRes.Decode(&shardsMap); err != nil { if err := smRes.Decode(&shardsMap); err != nil {
@@ -134,7 +140,7 @@ func GetHostnames(ctx context.Context, client *mongo.Client) ([]string, error) {
} }
} }
return nil, fmt.Errorf("cannot get shards map") return nil, nil // standalone instance
} }
func buildHostsListFromReplStatus(replStatus proto.ReplicaSetStatus) []string { func buildHostsListFromReplStatus(replStatus proto.ReplicaSetStatus) []string {

View File

@@ -100,6 +100,11 @@ func TestGetReplicasetMembers(t *testing.T) {
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBShard3PrimaryPort), uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBShard3PrimaryPort),
want: 3, want: 3,
}, },
{
name: "from_standalone",
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBStandalonePort),
want: 0,
},
} }
for _, test := range testCases { for _, test := range testCases {
@@ -146,7 +151,7 @@ func TestGetShardedHosts(t *testing.T) {
}, },
} }
for _, test := range testCases { for i, test := range testCases {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
clientOptions := options.Client().ApplyURI(test.uri) clientOptions := options.Client().ApplyURI(test.uri)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
@@ -156,6 +161,10 @@ func TestGetShardedHosts(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("Cannot get a new client for host %s: %s", test.uri, err) t.Errorf("Cannot get a new client for host %s: %s", test.uri, err)
} }
if client == nil {
panic(fmt.Sprintf("i: %d, uri: %s\n", i, test.uri))
}
if err := client.Connect(ctx); err != nil { if err := client.Connect(ctx); err != nil {
t.Errorf("Cannot connect to host %s: %s", test.uri, err) t.Errorf("Cannot connect to host %s: %s", test.uri, err)
} }