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

mendersoftware / mender-server / 1841158960

28 May 2025 02:31PM UTC coverage: 65.865% (+0.08%) from 65.783%
1841158960

Pull #696

gitlab-ci

alfrunes
test(deviceconnect): Add error context to failing test

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #696: ci: Debug mongodb in backend unit tests

32569 of 49448 relevant lines covered (65.87%)

1.39 hits per line

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

79.37
/backend/services/inventory/server.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 main
15

16
import (
17
        "context"
18
        "net/http"
19
        "os"
20
        "os/signal"
21
        "time"
22

23
        "github.com/pkg/errors"
24
        "golang.org/x/sys/unix"
25

26
        "github.com/mendersoftware/mender-server/pkg/log"
27

28
        api_http "github.com/mendersoftware/mender-server/services/inventory/api/http"
29
        "github.com/mendersoftware/mender-server/services/inventory/client/devicemonitor"
30
        "github.com/mendersoftware/mender-server/services/inventory/client/workflows"
31
        "github.com/mendersoftware/mender-server/services/inventory/config"
32
        inventory "github.com/mendersoftware/mender-server/services/inventory/inv"
33
        "github.com/mendersoftware/mender-server/services/inventory/store/mongo"
34
)
35

36
func RunServer(c config.Reader) error {
2✔
37

2✔
38
        l := log.New(log.Ctx{})
2✔
39

2✔
40
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
2✔
41
        if err != nil {
2✔
42
                return errors.Wrap(err, "database connection failed")
×
43
        }
×
44

45
        limitAttributes := c.GetInt(SettingLimitAttributes)
2✔
46
        limitTags := c.GetInt(SettingLimitTags)
2✔
47

2✔
48
        inv := inventory.NewInventory(db).WithLimits(limitAttributes, limitTags)
2✔
49

2✔
50
        devicemonitorAddr := c.GetString(SettingDevicemonitorAddr)
2✔
51
        if devicemonitorAddr != "" {
4✔
52
                c := devicemonitor.NewClient(devicemonitorAddr)
2✔
53
                inv = inv.WithDevicemonitor(c)
2✔
54
        }
2✔
55

56
        if inv, err = maybeWithInventory(inv, c); err != nil {
2✔
57
                return err
×
58
        }
×
59

60
        invapi := api_http.NewInventoryApiHandlers(inv)
2✔
61
        handler, err := invapi.Build()
2✔
62
        if err != nil {
2✔
63
                return errors.Wrap(err, "inventory API handlers setup failed")
×
64
        }
×
65
        addr := c.GetString(SettingListen)
2✔
66
        l.Printf("listening on %s", addr)
2✔
67

2✔
68
        srv := &http.Server{
2✔
69
                Addr:    addr,
2✔
70
                Handler: handler,
2✔
71
        }
2✔
72

2✔
73
        errChan := make(chan error, 1)
2✔
74
        go func() {
4✔
75
                if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
2✔
76
                        errChan <- err
×
77
                }
×
78
        }()
79
        quit := make(chan os.Signal, 1)
2✔
80
        signal.Notify(quit, unix.SIGINT, unix.SIGTERM)
2✔
81
        select {
2✔
82
        case sig := <-quit:
2✔
83
                l.Infof("received signal %s: terminating", sig)
2✔
84
        case err := <-errChan:
×
85
                l.Errorf("server terminated unexpectedly: %s", err.Error())
×
86
                return err
×
87
        }
88
        l.Info("server shutdown")
2✔
89
        ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
2✔
90
        defer cancel()
2✔
91
        if err := srv.Shutdown(ctxWithTimeout); err != nil {
2✔
92
                l.Error("error when shutting down the server ", err)
×
93
        }
×
94
        return nil
2✔
95
}
96

97
func maybeWithInventory(
98
        inv inventory.InventoryApp,
99
        c config.Reader,
100
) (inventory.InventoryApp, error) {
3✔
101
        if reporting := c.GetBool(SettingEnableReporting); reporting {
4✔
102
                orchestrator := c.GetString(SettingOrchestratorAddr)
1✔
103
                if orchestrator == "" {
2✔
104
                        return inv, errors.New("reporting integration needs orchestrator address")
1✔
105
                }
1✔
106

107
                c := workflows.NewClient(orchestrator)
1✔
108
                inv = inv.WithReporting(c)
1✔
109
        }
110
        return inv, nil
3✔
111
}
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