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

mendersoftware / mender-server / 1494786857

14 Oct 2024 10:01AM UTC coverage: 72.891% (-1.2%) from 74.094%
1494786857

Pull #100

gitlab-ci

tranchitella
chore: centralize the mock generator across the different services

Changelog: None
Ticket: None

Signed-off-by: Fabio Tranchitella <fabio@tranchitella.eu>
Pull Request #100: chore: centralize the mock generator across the different services

4399 of 6347 branches covered (69.31%)

Branch coverage included in aggregate %.

722 of 2034 new or added lines in 44 files covered. (35.5%)

197 existing lines in 36 files now uncovered.

42530 of 58035 relevant lines covered (73.28%)

28.15 hits per line

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

57.61
/backend/services/deviceauth/store/mongo/migration_1_1_0.go
1
// Copyright 2023 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
package mongo
15

16
import (
17
        "context"
18
        "time"
19

20
        "github.com/pkg/errors"
21
        "go.mongodb.org/mongo-driver/bson"
22
        "go.mongodb.org/mongo-driver/mongo"
23
        mopts "go.mongodb.org/mongo-driver/mongo/options"
24

25
        "github.com/mendersoftware/mender-server/pkg/mongo/migrate"
26
        ctxstore "github.com/mendersoftware/mender-server/pkg/store"
27

28
        "github.com/mendersoftware/mender-server/services/deviceauth/model"
29
)
30

31
type migration_1_1_0 struct {
32
        ms  *DataStoreMongo
33
        ctx context.Context
34
}
35

36
type device_0_1_0 struct {
37
        Id          string    `bson:"_id,omitempty"`
38
        TenantToken string    `bson:"tenant_token,omitempty"`
39
        PubKey      string    `bson:"pubkey,omitempty"`
40
        IdData      string    `bson:"id_data,omitempty"`
41
        Status      string    `bson:"status,omitempty"`
42
        CreatedTs   time.Time `bson:"created_ts,omitempty"`
43
        UpdatedTs   time.Time `bson:"updated_ts,omitempty"`
44
}
45

46
type token_0_1_0 struct {
47
        Id    string `bson:"_id,omitempty"`
48
        DevId string `bson:"dev_id,omitempty"`
49
        Token string `bson:"token,omitempty"`
50
}
51

52
func (m *migration_1_1_0) Up(from migrate.Version) error {
3✔
53
        devColl := m.ms.client.Database(ctxstore.DbFromContext(m.ctx, DbName)).Collection(DbDevicesColl)
3✔
54
        asColl := m.ms.client.Database(ctxstore.DbFromContext(m.ctx, DbName)).Collection(DbAuthSetColl)
3✔
55
        tColl := m.ms.client.Database(ctxstore.DbFromContext(m.ctx, DbName)).Collection(DbTokensColl)
3✔
56

3✔
57
        if err := m.ensureIndexes(m.ctx); err != nil {
3✔
UNCOV
58
                return errors.Wrap(err, "database indexing failed")
×
UNCOV
59
        }
×
60

61
        cursor, err := devColl.Find(m.ctx, bson.M{})
3✔
62
        if err != nil {
3✔
63
                return err
×
64
        }
×
65

66
        var olddev device_0_1_0
3✔
67

3✔
68
        for cursor.Next(m.ctx) {
3✔
69
                if err = cursor.Decode(&olddev); err != nil {
×
70
                        continue
×
71
                }
72
                // first prepare an auth set
73

74
                // reuse device ID as auth set ID
75
                asetId := olddev.Id
×
76

×
77
                aset := model.AuthSet{
×
78
                        Id:        asetId,
×
79
                        IdData:    olddev.IdData,
×
80
                        PubKey:    olddev.PubKey,
×
81
                        DeviceId:  olddev.Id,
×
82
                        Status:    olddev.Status,
×
83
                        Timestamp: &olddev.UpdatedTs,
×
84
                }
×
85

×
86
                if _, err := asColl.InsertOne(m.ctx, aset); err != nil {
×
87
                        return errors.Wrapf(err, "failed to insert auth set for device %v",
×
88
                                olddev.Id)
×
89
                }
×
90

91
                // update tokens
92

93
                filter := token_0_1_0{
×
94
                        DevId: olddev.Id,
×
95
                }
×
96

×
97
                update := bson.M{
×
98
                        "$set": bson.M{
×
99
                                // see model.Token for field naming
×
100
                                "auth_id": asetId,
×
101
                        },
×
102
                }
×
103

×
104
                if _, err = tColl.UpdateMany(m.ctx, filter, update); err != nil {
×
105
                        return errors.Wrapf(err, "failed to update tokens of device %v", olddev.Id)
×
106
                }
×
107
        }
108

109
        if err := cursor.Close(m.ctx); err != nil {
3✔
110
                return errors.Wrap(err, "failed to close DB iterator")
×
111
        }
×
112

113
        return nil
3✔
114
}
115

116
func (m *migration_1_1_0) Version() migrate.Version {
3✔
117
        return migrate.MakeVersion(1, 1, 0)
3✔
118
}
3✔
119

120
func (m *migration_1_1_0) ensureIndexes(ctx context.Context) error {
3✔
121
        _false := false
3✔
122
        _true := true
3✔
123

3✔
124
        devIdDataUniqueIndex := mongo.IndexModel{
3✔
125
                Keys: bson.D{
3✔
126
                        {Key: model.DevKeyIdData, Value: 1},
3✔
127
                },
3✔
128
                Options: &mopts.IndexOptions{
3✔
129
                        Background: &_false,
3✔
130
                        Name:       &indexDevices_IdentityData,
3✔
131
                        Unique:     &_true,
3✔
132
                },
3✔
133
        }
3✔
134

3✔
135
        authSetUniqueIndex := mongo.IndexModel{
3✔
136
                Keys: bson.D{
3✔
137
                        {Key: model.AuthSetKeyDeviceId, Value: 1},
3✔
138
                        {Key: model.AuthSetKeyIdData, Value: 1},
3✔
139
                        {Key: model.AuthSetKeyPubKey, Value: 1},
3✔
140
                },
3✔
141
                Options: &mopts.IndexOptions{
3✔
142
                        Background: &_false,
3✔
143
                        Name:       &indexAuthSet_DeviceId_IdentityData_PubKey,
3✔
144
                        Unique:     &_true,
3✔
145
                },
3✔
146
        }
3✔
147

3✔
148
        cDevs := m.ms.client.Database(ctxstore.DbFromContext(ctx, DbName)).Collection(DbDevicesColl)
3✔
149
        devIndexes := cDevs.Indexes()
3✔
150
        _, err := devIndexes.CreateOne(ctx, devIdDataUniqueIndex)
3✔
151
        if err != nil {
3✔
152
                return err
×
153
        }
×
154

155
        cAuthSets := m.ms.client.Database(ctxstore.DbFromContext(ctx, DbName)).Collection(DbAuthSetColl)
3✔
156
        authSetIndexes := cAuthSets.Indexes()
3✔
157
        _, err = authSetIndexes.CreateOne(ctx, authSetUniqueIndex)
3✔
158

3✔
159
        return err
3✔
160
}
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