Skip to content

Commit ad71f83

Browse files
PBM-1414 add unit test and add some small refactor for easier troubleshooting
1 parent 179c819 commit ad71f83

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

cmd/pbm/restore.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,12 @@ func nsIsTaken(
358358
ns string,
359359
) error {
360360
ns = strings.TrimSpace(ns)
361-
db, coll, ok := strings.Cut(ns, ".")
361+
dbName, coll, ok := strings.Cut(ns, ".")
362362
if !ok {
363363
return errors.Wrap(ErrInvalidNamespace, ns)
364364
}
365365

366-
collNames, err := conn.MongoClient().Database(db).ListCollectionNames(ctx, bson.D{{"name", coll}})
366+
collNames, err := conn.MongoClient().Database(dbName).ListCollectionNames(ctx, bson.D{{"name", coll}})
367367
if err != nil {
368368
return errors.Wrap(err, "list collection names for cloning target validation")
369369
}

cmd/pbm/restore_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"errors"
5+
"reflect"
56
"testing"
67
)
78

@@ -110,3 +111,56 @@ func TestCloningValidation(t *testing.T) {
110111
})
111112
}
112113
}
114+
115+
func Test_parseCLINumInsertionWorkersOption(t *testing.T) {
116+
117+
type args struct {
118+
value int32
119+
}
120+
121+
var num int32 = 1
122+
123+
tests := []struct {
124+
name string
125+
args args
126+
want *int32
127+
wantErr bool
128+
}{
129+
{
130+
name: "valid number - no error",
131+
args: args{
132+
value: 1,
133+
},
134+
want: &num,
135+
wantErr: false,
136+
},
137+
{
138+
name: "zero - no error, but return nil",
139+
args: args{
140+
value: 0,
141+
},
142+
want: nil,
143+
wantErr: false,
144+
},
145+
{
146+
name: "negative value - error",
147+
args: args{
148+
value: -1,
149+
},
150+
want: nil,
151+
wantErr: true,
152+
},
153+
}
154+
for _, tt := range tests {
155+
t.Run(tt.name, func(t *testing.T) {
156+
got, err := parseCLINumInsertionWorkersOption(tt.args.value)
157+
if (err != nil) != tt.wantErr {
158+
t.Errorf("parseCLINumInsertionWorkersOption() error = %v, wantErr %v", err, tt.wantErr)
159+
return
160+
}
161+
if !reflect.DeepEqual(got, tt.want) {
162+
t.Errorf("parseCLINumInsertionWorkersOption() got = %v, want %v", got, tt.want)
163+
}
164+
})
165+
}
166+
}

pbm/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ func confSetPITR(ctx context.Context, m connect.Client, value bool) error {
554554
// GetConfigVar returns value of given config vaiable
555555
func GetConfigVar(ctx context.Context, m connect.Client, key string) (interface{}, error) {
556556
if !validateConfigKey(key) {
557-
return nil, errors.New("invalid config key")
557+
return nil, errors.Errorf("invalid config key: %s", key)
558558
}
559559

560560
bts, err := m.ConfigCollection().

pbm/topo/cluster.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package topo
22

33
import (
44
"context"
5+
"encoding/json"
6+
"fmt"
57
"strings"
68

79
"go.mongodb.org/mongo-driver/bson"
@@ -58,7 +60,13 @@ func GetClusterTime(ctx context.Context, m connect.Client) (primitive.Timestamp,
5860

5961
func ClusterTimeFromNodeInfo(info *NodeInfo) (primitive.Timestamp, error) {
6062
if info.ClusterTime == nil {
61-
return primitive.Timestamp{}, errors.New("no clusterTime in response")
63+
jsonResponse, err := json.Marshal(info)
64+
errDetails := string(jsonResponse)
65+
if err != nil {
66+
fmt.Println(err)
67+
errDetails = "Unable to parse response."
68+
}
69+
return primitive.Timestamp{}, errors.Errorf("No clusterTime in response. Received: %s", errDetails)
6270
}
6371

6472
return info.ClusterTime.ClusterTime, nil

0 commit comments

Comments
 (0)