• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / go-lib-micro / 1380073550

19 Jul 2024 09:07AM UTC coverage: 100.0% (+18.2%) from 81.785%
1380073550

push

gitlab-ci

web-flow
Merge pull request #246 from kjaskiewiczz/custom-reg

feat: make it possible to set custom registry in the test mongo client

2 of 7 new or added lines in 1 file covered. (28.57%)

0 of 0 relevant lines covered (NaN%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.0
/mongo/testing/db.go
1
// Copyright 2024 Northern.tech AS
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 testing
16

17
import (
18
        "context"
19
        "io/ioutil"
20
        "os"
21

22
        "go.mongodb.org/mongo-driver/bson/bsoncodec"
23
        "go.mongodb.org/mongo-driver/mongo"
24
        "go.mongodb.org/mongo-driver/mongo/options"
25

26
        "github.com/mendersoftware/go-lib-micro/mongo/dbtest"
27
)
28

29
// TestDBRunner exports selected calls of dbtest.DBServer API, just the ones
30
// that are useful in tests.
31
type TestDBRunner interface {
32
        Client() *mongo.Client
33
        Wipe()
34
        CTX() context.Context
35
}
36

37
// WithDB will set up a test DB instance and pass it to `f` callback as
38
// `dbtest`. Once `f()` is finished, the DB will be cleaned up. Value returned
39
// from `f()` is obtained as return status of a call to WithDB().
40
// reg is optional custom registry which can be set up for the test client.
41
func WithDB(f func(dbtest TestDBRunner) int, reg *bsoncodec.Registry) int {
1✔
42
        var runner TestDBRunner
1✔
43
        if url, ok := os.LookupEnv("TEST_MONGO_URL"); ok {
2✔
44
                clientOpts := options.Client().
1✔
45
                        ApplyURI(url)
1✔
46
                if reg != nil {
1✔
NEW
47
                        clientOpts.SetRegistry(reg)
×
NEW
48
                }
×
49
                client, err := mongo.Connect(context.Background(), clientOpts)
1✔
50
                if err != nil {
1✔
51
                        panic(err)
×
52
                }
53
                runner = (*dbClientFromEnv)(client)
1✔
54
        } else {
×
55
                // Fallback to running mongod on host
×
56
                dbdir, _ := ioutil.TempDir("", "dbsetup-test")
×
57
                db := &dbtest.DBServer{}
×
58
                db.SetPath(dbdir)
×
NEW
59
                if reg != nil {
×
NEW
60
                        db.SetRegistry(reg)
×
NEW
61
                }
×
62
                runner = db
×
63

×
64
                defer os.RemoveAll(dbdir)
×
65
                defer db.Stop()
×
66

67
        }
68

69
        return f(runner)
1✔
70
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc