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

mendersoftware / mender / 939977254

pending completion
939977254

push

gitlab-ci

kacf
chore: Move database key values to more central location.

There will also be more keys later, further motivating this move.

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

9 of 9 new or added lines in 1 file covered. (100.0%)

4174 of 5691 relevant lines covered (73.34%)

154.25 hits per line

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

90.0
/mender-update/update_module/v3/platform/c++17/update_module_call.cpp
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

15
#include <mender-update/update_module/v3/update_module.hpp>
16

17
#include <iostream>
18
#include <sstream>
19

20
#include <filesystem>
21

22
#include <common/common.hpp>
23
#include <common/events.hpp>
24
#include <common/log.hpp>
25
#include <common/processes.hpp>
26

27
namespace mender {
28
namespace update {
29
namespace update_module {
30
namespace v3 {
31

32
namespace error = mender::common::error;
33
namespace events = mender::common::events;
34
namespace log = mender::common::log;
35
namespace fs = std::filesystem;
36

37
UpdateModule::StateRunner::StateRunner(
162✔
38
        events::EventLoop &loop,
39
        State state,
40
        const string &module_path,
41
        const string &module_work_path) :
162✔
42
        loop(loop),
43
        module_work_path(module_work_path),
44
        proc({module_path, StateToString(state), module_work_path}),
45
        timeout(loop) {
648✔
46
        proc.SetWorkDir(module_work_path);
162✔
47
}
162✔
48

49
error::Error UpdateModule::StateRunner::AsyncCallState(
162✔
50
        State state, bool procOut, chrono::seconds timeout_seconds, HandlerFunction handler) {
51
        this->handler = handler;
162✔
52

53
        string state_string = StateToString(state);
324✔
54
        if (!fs::is_directory(module_work_path)) {
162✔
55
                if (state == State::Cleanup) {
1✔
56
                        return error::NoError;
×
57
                } else {
58
                        return error::Error(
59
                                make_error_condition(errc::no_such_file_or_directory),
1✔
60
                                state_string + ": File tree does not exist: " + module_work_path);
3✔
61
                }
62
        }
63

64
        class OutputHandler {
65
        public:
66
                void operator()(const char *data, size_t size) {
9✔
67
                        if (size == 0) {
9✔
68
                                return;
×
69
                        }
70
                        // Get rid of exactly one trailing newline, if there is one. This is because
71
                        // we unconditionally print one at the end of every log line. If the string
72
                        // does not contain a trailing newline, add a "{...}" instead, since we
73
                        // cannot avoid breaking the line apart then.
74
                        string content(data, size);
18✔
75
                        if (content.back() == '\n') {
9✔
76
                                content.pop_back();
9✔
77
                        } else {
78
                                content.append("{...}");
×
79
                        }
80
                        auto lines = mender::common::SplitString(content, "\n");
27✔
81
                        for (auto line : lines) {
18✔
82
                                log::Info(prefix + line);
9✔
83
                        }
84
                }
85
                string prefix;
86
        } stderr_handler {"Update Module output (stderr): "};
322✔
87

88
        error::Error processStart;
322✔
89
        if (procOut) {
161✔
90
                // Provide string to put content in.
91
                output.emplace(string());
65✔
92
                processStart = proc.Start(
65✔
93
                        [this](const char *data, size_t size) {
81✔
94
                                // At the moment, no state that queries output accepts more than one line,
95
                                // so reject multiple lines here. This would have been rejected anyway due
96
                                // to matching, but by doing it here, we also prevent using excessive memory
97
                                // if the process dumps a large log on us.
98
                                if (!first_line_captured) {
27✔
99
                                        auto lines = mender::common::SplitString(string(data, size), "\n");
104✔
100
                                        if (lines.size() >= 1) {
26✔
101
                                                *output = lines[0];
26✔
102
                                                first_line_captured = true;
26✔
103
                                        }
104
                                        if (lines.size() > 2 || (lines.size() == 2 && lines[1] != "")) {
26✔
105
                                                too_many_lines = true;
1✔
106
                                        }
107
                                } else {
108
                                        too_many_lines = true;
1✔
109
                                }
110
                        },
27✔
111
                        stderr_handler);
130✔
112
        } else {
113
                processStart =
114
                        proc.Start(OutputHandler {"Update Module output (stdout): "}, stderr_handler);
96✔
115
        }
116
        if (processStart != error::NoError) {
161✔
117
                return GetProcessError(processStart).WithContext(state_string);
×
118
        }
119

120
        error::Error err;
322✔
121
        err = proc.AsyncWait(loop, [this, state, handler](error::Error process_err) {
161✔
122
                timeout.Cancel();
160✔
123
                auto err = process_err.WithContext(StateToString(state));
160✔
124
                ProcessFinishedHandler(state, err);
160✔
125
        });
482✔
126

127
        timeout.AsyncWait(timeout_seconds, [this, state, handler](error::Error inner_err) {
161✔
128
                proc.EnsureTerminated();
1✔
129
                auto err = error::Error(
130
                        make_error_condition(errc::timed_out),
2✔
131
                        StateToString(state) + ": Timed out while waiting for Update Module to complete");
3✔
132
                ProcessFinishedHandler(state, err);
1✔
133
        });
323✔
134

135
        return err;
161✔
136
}
137

138
void UpdateModule::StateRunner::ProcessFinishedHandler(State state, error::Error err) {
161✔
139
        if (state == State::Cleanup) {
161✔
140
                std::error_code ec;
28✔
141
                if (!fs::remove_all(module_work_path, ec)) {
28✔
142
                        err = error::Error(
×
143
                                ec.default_error_condition(),
×
144
                                StateToString(state) + ": Error removing directory: " + module_work_path);
×
145
                }
146
        }
147

148
        if (err == error::NoError && too_many_lines) {
161✔
149
                err = error::Error(
2✔
150
                        make_error_condition(errc::protocol_error),
2✔
151
                        "Too many lines when querying " + StateToString(state));
6✔
152
        }
153

154
        if (err != error::NoError) {
161✔
155
                handler(expected::unexpected(err));
13✔
156
        } else {
157
                handler(output);
148✔
158
        }
159
}
161✔
160

161
} // namespace v3
162
} // namespace update_module
163
} // namespace update
164
} // namespace mender
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