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

mendersoftware / mender / 951615729

pending completion
951615729

push

gitlab-ci

larsewi
fix: Add 'build/' to '.gitignore'

Ticket: None
Changelog: None
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>

4199 of 5286 relevant lines covered (79.44%)

166.43 hits per line

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

71.43
/common/testing.hpp
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
#ifndef MENDER_COMMON_TESTING
16
#define MENDER_COMMON_TESTING
17

18
#include <string>
19

20
#include <gtest/gtest.h>
21

22
#include <common/events.hpp>
23
#include <common/http.hpp>
24

25
namespace mender {
26
namespace common {
27
namespace testing {
28

29
using namespace std;
30

31
namespace http = mender::http;
32

33
class TemporaryDirectory {
34
public:
35
        TemporaryDirectory();
36
        ~TemporaryDirectory();
37

38
        std::string Path();
39

40
private:
41
        std::string path_;
42
};
43

44
// An event loop which automatically times out after a given amount of time.
45
class TestEventLoop : public mender::common::events::EventLoop {
46
public:
47
        TestEventLoop(chrono::seconds seconds = chrono::seconds(5)) :
2✔
48
                timer_(*this) {
2✔
49
                timer_.AsyncWait(seconds, [this](error::Error err) {
2✔
50
                        Stop();
×
51
                        // Better to throw exception than FAIL(), since we want to escape the caller
52
                        // as well.
53
                        throw runtime_error("Test timed out");
×
54
                });
4✔
55
        }
2✔
56

57
private:
58
        mender::common::events::Timer timer_;
59
};
60

61
::testing::AssertionResult FileContains(const string &filename, const string &expected_content);
62
::testing::AssertionResult FileJsonEquals(const string &filename, const string &expected_content);
63
::testing::AssertionResult FilesEqual(const string &filename1, const string &filename2);
64

65
class RedirectStreamOutputs {
66
public:
67
        RedirectStreamOutputs() {
68
                cout_stream_ = cout.rdbuf(cout_string_.rdbuf());
69
                cerr_stream_ = cerr.rdbuf(cerr_string_.rdbuf());
70
        }
71
        ~RedirectStreamOutputs() {
72
                cout.rdbuf(cout_stream_);
73
                cerr.rdbuf(cerr_stream_);
74
        }
75

76
        string GetCout() const {
77
                return cout_string_.str();
78
        }
79

80
        string GetCerr() const {
81
                return cerr_string_.str();
82
        }
83

84
private:
85
        streambuf *cout_stream_;
86
        streambuf *cerr_stream_;
87
        stringstream cout_string_;
88
        stringstream cerr_string_;
89
};
90

91
class HttpFileServer {
92
public:
93
        HttpFileServer(const string &dir);
94
        ~HttpFileServer();
95

96
        const string &GetBaseUrl() const {
97
                return serve_address_;
98
        }
99

100
        void Serve(http::ExpectedIncomingRequestPtr req);
101

102
private:
103
        static const string serve_address_;
104

105
        string dir_;
106
        thread thread_;
107
        events::EventLoop loop_;
108
        http::Server server_;
109
};
110

111
} // namespace testing
112
} // namespace common
113
} // namespace mender
114

115
#endif // MENDER_COMMON_TESTING
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