PT-1822 Fixed for CR

This commit is contained in:
Carlos Salguero
2020-05-25 22:35:35 -03:00
parent 40f28d977a
commit 1f33cb97e6
4 changed files with 23 additions and 17 deletions
+6 -5
View File
@@ -13,11 +13,12 @@ import (
)
const (
shardingNotEnabledError = 203
shardingNotEnabledErrorCode = 203
)
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)")
ShardingNotEnabledError = errors.New("sharding not enabled")
)
func GetReplicasetMembers(ctx context.Context, clientOptions *options.ClientOptions) ([]proto.Members, error) {
@@ -95,7 +96,7 @@ func GetReplicasetMembers(ctx context.Context, clientOptions *options.ClientOpti
membersMap[m.Name] = m
}
client.Disconnect(ctx)
client.Disconnect(ctx) //nolint
}
for _, member := range membersMap {
@@ -122,8 +123,8 @@ func GetHostnames(ctx context.Context, client *mongo.Client) ([]string, error) {
var shardsMap proto.ShardsMap
smRes := client.Database("admin").RunCommand(ctx, primitive.M{"getShardMap": 1})
if smRes.Err() != nil {
if e, ok := smRes.Err().(mongo.CommandError); ok && e.Code == shardingNotEnabledError {
return nil, nil // standalone instance
if e, ok := smRes.Err().(mongo.CommandError); ok && e.Code == shardingNotEnabledErrorCode {
return nil, ShardingNotEnabledError // standalone instance
}
return nil, errors.Wrap(smRes.Err(), "cannot getShardMap for GetHostnames")
}
+1 -1
View File
@@ -162,7 +162,7 @@ func TestGetShardedHosts(t *testing.T) {
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))
t.Fatalf("mongodb client is nil i: %d, uri: %s\n", i, test.uri)
}
if err := client.Connect(ctx); err != nil {
+13 -8
View File
@@ -38,13 +38,19 @@ const (
DefaultRunningOpsSamples = 5
DefaultOutputFormat = "text"
typeMongos = "mongos"
// Exit Codes
cannotFormatResults = 1
cannotParseCommandLineParameters = 2
cannotGetHostInfo = 3
cannotGetReplicasetMembers = 4
)
var (
Build string = "2020-04-23" // nolint
GoVersion string = "1.14.1" // nolint
Version string = "3.2.0" // nolint
Commit string // nolint
Version string = "3.2.0"
Commit string
)
type TimedStats struct {
@@ -158,7 +164,7 @@ func main() {
opts, err := parseFlags()
if err != nil {
log.Errorf("cannot get parameters: %s", err.Error())
os.Exit(2)
os.Exit(cannotParseCommandLineParameters)
}
if opts == nil && err == nil {
return
@@ -206,7 +212,7 @@ func main() {
defer client.Disconnect(ctx) // nolint
hostnames, err := util.GetHostnames(ctx, client)
if err != nil {
if err != nil && errors.Is(err, util.ShardingNotEnabledError) {
log.Errorf("Cannot get hostnames: %s", err)
}
log.Debugf("hostnames: %v", hostnames)
@@ -217,12 +223,12 @@ func main() {
if err != nil {
message := fmt.Sprintf("Cannot get host info for %q: %s", opts.Host, err.Error())
log.Errorf(message)
os.Exit(2)
os.Exit(cannotGetHostInfo)
}
if ci.ReplicaMembers, err = util.GetReplicasetMembers(ctx, clientOptions); err != nil {
log.Warnf("[Error] cannot get replicaset members: %v\n", err)
os.Exit(2)
os.Exit(cannotGetReplicasetMembers)
}
log.Debugf("replicaMembers:\n%+v\n", ci.ReplicaMembers)
@@ -270,10 +276,9 @@ func main() {
out, err := formatResults(ci, opts.OutputFormat)
if err != nil {
log.Errorf("Cannot format the results: %s", err.Error())
os.Exit(1)
os.Exit(cannotFormatResults)
}
fmt.Println(string(out))
}
func formatResults(ci *collectedInfo, format string) ([]byte, error) {