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

mendersoftware / iot-manager / 1336988003

18 Jun 2024 09:38AM UTC coverage: 87.602%. Remained the same
1336988003

Pull #288

gitlab-ci

alfrunes
test(accpetance): Infer Docker compose service name from host

Remove hard-coded host name from config and actually use the `--host`
pytest config.

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #288: test(accpetance): Infer Docker compose service name from host

3229 of 3686 relevant lines covered (87.6%)

11.46 hits per line

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

69.64
/client/iothub/model.go
1
// Copyright 2022 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 iothub
16

17
import (
18
        "bytes"
19
        "crypto/rand"
20
        "encoding/base64"
21
        "io"
22
        "reflect"
23

24
        "github.com/mendersoftware/iot-manager/model"
25

26
        validation "github.com/go-ozzo/ozzo-validation/v4"
27
)
28

29
type Key []byte
30

31
func (k Key) MarshalText() ([]byte, error) {
12✔
32
        n := base64.StdEncoding.EncodedLen(len(k))
12✔
33
        ret := make([]byte, n)
12✔
34
        base64.StdEncoding.Encode(ret, k)
12✔
35
        return ret, nil
12✔
36
}
12✔
37

38
type SymmetricKey struct {
39
        Primary   Key `json:"primaryKey"`
40
        Secondary Key `json:"secondaryKey"`
41
}
42

43
type AuthType string
44

45
const (
46
        AuthTypeSymmetric   AuthType = "sas"
47
        AuthTypeCertificate AuthType = "certificate"
48
        AuthTypeNone        AuthType = "none"
49
        AuthTypeAuthority   AuthType = "Authority"
50
        AuthTypeSelfSigned  AuthType = "selfSigned"
51
)
52

53
type Auth struct {
54
        Type          AuthType `json:"type"`
55
        *SymmetricKey `json:"symmetricKey,omitempty"`
56
}
57

58
func NewSymmetricAuth() (*Auth, error) {
×
59
        var primKey, secKey [48]byte
×
60
        _, err := io.ReadFull(rand.Reader, primKey[:])
×
61
        if err != nil {
×
62
                return nil, err
×
63
        }
×
64
        _, err = io.ReadFull(rand.Reader, secKey[:])
×
65
        if err != nil {
×
66
                return nil, err
×
67
        }
×
68
        return &Auth{
×
69
                Type: AuthTypeSymmetric,
×
70
                SymmetricKey: &SymmetricKey{
×
71
                        Primary:   Key(primKey[:]),
×
72
                        Secondary: Key(secKey[:]),
×
73
                },
×
74
        }, nil
×
75
}
76

77
type Status string
78

79
const (
80
        StatusEnabled  Status = "enabled"
81
        StatusDisabled Status = "disabled"
82
)
83

84
func NewStatusFromMenderStatus(status model.Status) Status {
28✔
85
        switch status {
28✔
86
        case model.StatusAccepted, model.StatusPreauthorized:
16✔
87
                return StatusEnabled
16✔
88
        default:
12✔
89
                return StatusDisabled
12✔
90
        }
91
}
92

93
func (s *Status) UnmarshalText(b []byte) error {
34✔
94
        *s = Status(bytes.ToLower(b))
34✔
95
        return s.Validate()
34✔
96
}
34✔
97

98
var validateStatus = validation.In(
99
        StatusEnabled,
100
        StatusDisabled,
101
)
102

103
func (s Status) Validate() error {
34✔
104
        return validateStatus.Validate(s)
34✔
105
}
34✔
106

107
type DeviceCapabilities struct {
108
        IOTEdge bool `json:"iotEdge"`
109
}
110

111
type TwinProperties struct {
112
        Desired  map[string]interface{} `json:"desired"`
113
        Reported map[string]interface{} `json:"reported"`
114
}
115

116
type X509ThumbPrint struct {
117
        Primary   string `json:"primaryThumbprint"`
118
        Secondary string `json:"secondaryThumbprint"`
119
}
120

121
type Device struct {
122
        *Auth                  `json:"authentication,omitempty"`
123
        *DeviceCapabilities    `json:"capabilities,omitempty"`
124
        C2DMessageCount        int    `json:"cloudToDeviceMessageCount,omitempty"`
125
        ConnectionState        string `json:"connectionState,omitempty"`
126
        ConnectionStateUpdated string `json:"connectionStateUpdatedTime,omitempty"`
127

128
        DeviceID         string `json:"deviceId"`
129
        DeviceScope      string `json:"deviceScope,omitempty"`
130
        ETag             string `json:"etag,omitempty"`
131
        GenerationID     string `json:"generationId,omitempty"`
132
        LastActivityTime string `json:"lastActivityTime,omitempty"`
133
        Status           Status `json:"status,omitempty"`
134
        StatusReason     string `json:"statusReason,omitempty"`
135
        StatusUpdateTime string `json:"statusUpdateTime,omitempty"`
136
}
137

138
func mergeDevices(devices ...*Device) *Device {
19✔
139
        var device *Device
19✔
140
        for _, device = range devices {
28✔
141
                if device != nil {
18✔
142
                        break
9✔
143
                }
144
        }
145
        if device == nil {
29✔
146
                return new(Device)
10✔
147
        }
10✔
148
        rDevice := reflect.ValueOf(device).Elem()
9✔
149
        for _, dev := range devices {
24✔
150
                if dev == nil {
21✔
151
                        continue
6✔
152
                }
153
                rDev := reflect.ValueOf(*dev)
9✔
154
                for i := 0; i < rDev.NumField(); i++ {
126✔
155
                        fDev := rDev.Field(i)
117✔
156
                        if fDev.IsZero() {
216✔
157
                                continue
99✔
158
                        }
159
                        fDevice := rDevice.Field(i)
18✔
160
                        fDevice.Set(fDev)
18✔
161
                }
162
        }
163
        return device
9✔
164
}
165

166
type DeviceTwin struct {
167
        AuthenticationType string              `json:"authenticationType,omitempty"`
168
        Capabilities       *DeviceCapabilities `json:"capabilities,omitempty"`
169

170
        CloudToDeviceMessageCount int64 `json:"cloudToDeviceMessageCount,omitempty"`
171

172
        ConnectionState  string                 `json:"connectionState,omitempty"`
173
        DeviceEtag       string                 `json:"deviceEtag,omitempty"`
174
        DeviceID         string                 `json:"deviceId,omitempty"`
175
        DeviceScope      string                 `json:"deviceScope,omitempty"`
176
        ETag             string                 `json:"etag,omitempty"`
177
        LastActivityTime string                 `json:"lastActivityTime,omitempty"`
178
        ModuleID         string                 `json:"moduleId,omitempty"`
179
        Properties       TwinProperties         `json:"properties,omitempty"`
180
        Status           Status                 `json:"status,omitempty"`
181
        StatusReason     string                 `json:"statusReason,omitempty"`
182
        StatusUpdateTime string                 `json:"statusUpdateTime,omitempty"`
183
        Tags             map[string]interface{} `json:"tags,omitempty"`
184
        Version          int32                  `json:"version,omitempty"`
185
        X509ThumbPrint   X509ThumbPrint         `json:"x509Thumbprint,omitempty"`
186
}
187

188
type UpdateProperties struct {
189
        Desired map[string]interface{} `json:"desired"`
190
}
191

192
type DeviceTwinUpdate struct {
193
        Properties UpdateProperties       `json:"properties,omitempty"`
194
        Tags       map[string]interface{} `json:"tags,omitempty"`
195
        ETag       string                 `json:"-"`
196
        Replace    bool                   `json:"-"`
197
}
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