Mercurial > irccd
comparison tests/src/libirccd/command-plugin-load/main.cpp @ 611:9fbd1700435b
Tests: reoganize hierarchy
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 15 Dec 2017 15:37:58 +0100 |
parents | tests/src/plugin-load-command/main.cpp@22b3cd6f991f |
children | f69b1faf812f |
comparison
equal
deleted
inserted
replaced
610:22b3cd6f991f | 611:9fbd1700435b |
---|---|
1 /* | |
2 * main.cpp -- test plugin-load remote command | |
3 * | |
4 * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #define BOOST_TEST_MODULE "plugin-load" | |
20 #include <boost/test/unit_test.hpp> | |
21 | |
22 #include <irccd/daemon/command.hpp> | |
23 #include <irccd/daemon/plugin_service.hpp> | |
24 | |
25 #include <irccd/test/command_test.hpp> | |
26 | |
27 namespace irccd { | |
28 | |
29 namespace { | |
30 | |
31 class custom_loader : public plugin_loader { | |
32 public: | |
33 custom_loader() | |
34 : plugin_loader({}, {".none"}) | |
35 { | |
36 } | |
37 | |
38 std::shared_ptr<plugin> open(const std::string&, | |
39 const std::string&) noexcept override | |
40 { | |
41 return nullptr; | |
42 } | |
43 | |
44 std::shared_ptr<plugin> find(const std::string& id) noexcept override | |
45 { | |
46 class broken : public plugin { | |
47 public: | |
48 using plugin::plugin; | |
49 | |
50 void on_load(irccd&) override | |
51 { | |
52 throw std::runtime_error("broken"); | |
53 } | |
54 }; | |
55 | |
56 /* | |
57 * The 'magic' plugin will be created for the unit tests, all other | |
58 * plugins will return null. | |
59 */ | |
60 if (id == "magic") | |
61 return std::make_unique<plugin>(id, ""); | |
62 if (id == "broken") | |
63 return std::make_unique<broken>(id, ""); | |
64 | |
65 return nullptr; | |
66 } | |
67 }; | |
68 | |
69 class plugin_load_test : public command_test<plugin_load_command> { | |
70 public: | |
71 plugin_load_test() | |
72 { | |
73 daemon_->plugins().add_loader(std::make_unique<custom_loader>()); | |
74 daemon_->plugins().add(std::make_unique<plugin>("already", "")); | |
75 } | |
76 }; | |
77 | |
78 } // !namespace | |
79 | |
80 BOOST_FIXTURE_TEST_SUITE(plugin_load_test_suite, plugin_load_test) | |
81 | |
82 BOOST_AUTO_TEST_CASE(basic) | |
83 { | |
84 ctl_->send({ | |
85 { "command", "plugin-load" }, | |
86 { "plugin", "magic" } | |
87 }); | |
88 | |
89 wait_for([&] () { | |
90 return daemon_->plugins().has("magic"); | |
91 }); | |
92 | |
93 BOOST_TEST(!daemon_->plugins().list().empty()); | |
94 BOOST_TEST(daemon_->plugins().has("magic")); | |
95 } | |
96 | |
97 BOOST_AUTO_TEST_SUITE(errors) | |
98 | |
99 BOOST_AUTO_TEST_CASE(not_found) | |
100 { | |
101 boost::system::error_code result; | |
102 | |
103 ctl_->send({ | |
104 { "command", "plugin-load" }, | |
105 { "plugin", "unknown" } | |
106 }); | |
107 ctl_->recv([&] (auto code, auto) { | |
108 result = code; | |
109 }); | |
110 | |
111 wait_for([&] { | |
112 return result; | |
113 }); | |
114 | |
115 BOOST_TEST(result == plugin_error::not_found); | |
116 } | |
117 | |
118 BOOST_AUTO_TEST_CASE(already_exists) | |
119 { | |
120 boost::system::error_code result; | |
121 nlohmann::json message; | |
122 | |
123 ctl_->send({ | |
124 { "command", "plugin-load" }, | |
125 { "plugin", "already" } | |
126 }); | |
127 ctl_->recv([&] (auto rresult, auto rmessage) { | |
128 result = rresult; | |
129 message = rmessage; | |
130 }); | |
131 | |
132 wait_for([&] { | |
133 return result; | |
134 }); | |
135 | |
136 BOOST_TEST(result == plugin_error::already_exists); | |
137 BOOST_TEST(message["error"].template get<int>() == plugin_error::already_exists); | |
138 BOOST_TEST(message["errorCategory"].template get<std::string>() == "plugin"); | |
139 } | |
140 | |
141 BOOST_AUTO_TEST_CASE(exec_error) | |
142 { | |
143 boost::system::error_code result; | |
144 nlohmann::json message; | |
145 | |
146 ctl_->send({ | |
147 { "command", "plugin-load" }, | |
148 { "plugin", "broken" } | |
149 }); | |
150 ctl_->recv([&] (auto rresult, auto rmessage) { | |
151 result = rresult; | |
152 message = rmessage; | |
153 }); | |
154 | |
155 wait_for([&] { | |
156 return result; | |
157 }); | |
158 | |
159 BOOST_TEST(result == plugin_error::exec_error); | |
160 BOOST_TEST(message["error"].template get<int>() == plugin_error::exec_error); | |
161 BOOST_TEST(message["errorCategory"].template get<std::string>() == "plugin"); | |
162 } | |
163 | |
164 BOOST_AUTO_TEST_SUITE_END() | |
165 | |
166 BOOST_AUTO_TEST_SUITE_END() | |
167 | |
168 } // !irccd |