Motr  M0
filterc_ut.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2017-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_FDMI
24 #include "lib/trace.h"
25 
26 #include "ut/ut.h"
27 #include "fdmi/fdmi.h"
28 #include "fdmi/ut/sd_common.h" /* M0_FDMI_UT_PATH */
29 
30 #include "rpc/rpclib.h"
31 #include "ut/cs_service.h" /* m0_cs_default_stypes */
32 #include "lib/string.h" /* m0_strdup */
33 #include "lib/finject.h"
34 #include "fdmi/service.h" /* m0_reqh_fdmi_svc_params */
35 #include "conf/ut/rpc_helpers.h"
36 #include "ut/misc.h"
37 #include <errno.h>
38 
39 static char g_fdmi_data[] = "hello, FDMI";
40 static struct m0_fdmi_src_rec g_src_rec;
41 
42 static struct m0_filterc_ops *ufc_fco;
44 
45 /* ----------------------------------------------------------------
46  * Tests
47  * ---------------------------------------------------------------- */
48 #define CLIENT_ENDPOINT_ADDR "0@lo:12345:34:*"
49 #define SERVER_ENDPOINT_ADDR "0@lo:12345:34:1"
50 #define SERVER_ENDPOINT M0_NET_XPRT_PREFIX_DEFAULT":"SERVER_ENDPOINT_ADDR
51 
52 #define SERVER_DB_NAME "fdmi_filterc_ut.db"
53 #define SERVER_STOB_NAME "fdmi_filterc_ut.stob"
54 #define SERVER_ADDB_STOB_NAME "linuxstob:fdmi_filterc_ut.addb_stob"
55 #define SERVER_LOG_NAME "fdmi_filterc_ut.log"
56 #define SERVER_ENDPOINT_ADDR "0@lo:12345:34:1"
57 
58 #define CLIENT_DB_NAME "fdmi_filterc_ut.db"
59 
61 
62 static char *server_argv[] = {
63  "fdmi_filterc_ut", "-T", "AD", "-D", SERVER_DB_NAME,
65  "-e", SERVER_ENDPOINT, "-H", SERVER_ENDPOINT_ADDR, "-w", "10",
66  "-c", M0_SRC_PATH("fdmi/ut/conf.xc")
67 };
68 
69 enum {
71 };
72 
73 static struct m0_rpc_server_ctx sctx = {
74  .rsx_xprts_nr = 1,
75  .rsx_argv = server_argv,
76  .rsx_argc = ARRAY_SIZE(server_argv),
77  .rsx_log_file_name = SERVER_LOG_NAME,
78 };
79 
80 static struct m0_rpc_client_ctx cctx = {
82  .rcx_local_addr = CLIENT_ENDPOINT_ADDR,
83  .rcx_remote_addr = SERVER_ENDPOINT_ADDR,
84  .rcx_max_rpcs_in_flight = MAX_RPCS_IN_FLIGHT,
85  .rcx_fid = &g_process_fid,
86 };
87 
88 /* ----------------------------------------------------------------
89  * FilterC setup/teardown
90  * ---------------------------------------------------------------- */
91 
92 static struct m0_reqh *motr2reqh(struct m0_motr *motr)
93 {
94  return &motr->cc_reqh_ctx.rc_reqh;
95 }
96 
97 static int ut_filterc_start()
98 {
100  struct m0_reqh_service_type *stype;
101  bool start_service = false;
102  int rc = 0;
103 
104  stype = m0_reqh_service_type_find("M0_CST_FDMI");
105  if (stype == NULL) {
106  M0_LOG(M0_ERROR, "FDMI service type is not found.");
107  return M0_ERR_INFO(-EINVAL, "Unknown reqh service type: fdmi");
108  }
109 
111  if (ufc_fdmi_service == NULL) {
113  M0_UT_ASSERT(rc == 0);
115  start_service = true;
116  }
117 
118  /* Patch filterc instance used by source dock FOM */
119  if (ufc_fco != NULL) {
120  struct m0_reqh_fdmi_svc_params *fdms_start_params;
121  M0_ALLOC_PTR(fdms_start_params);
122  M0_ASSERT(fdms_start_params != NULL);
123  fdms_start_params->filterc_ops = ufc_fco;
124 
126  fdms_start_params,
127  sizeof(*fdms_start_params));
128  }
129 
130  if (start_service) {
132  M0_UT_ASSERT(rc == 0);
133  }
134  return M0_RC(rc);
135 }
136 
137 /* ----------------------------------------------------------------
138  * FilterC Operation replacement
139  * ---------------------------------------------------------------- */
140 
142  struct m0_reqh *reqh)
143 {
144  int rc;
145 
147  M0_UT_ASSERT(rc == 0);
148  return rc;
149 }
150 
152 {
153  int rc;
156  M0_UT_ASSERT(rc == 0);
160  M0_UT_ASSERT(rc == 0);
162  M0_UT_ASSERT(rc == 0);
163 }
164 
165 static void rpc_client_and_server_stop(void)
166 {
167  int rc;
169  M0_UT_ASSERT(rc == 0);
174 }
175 
176 static struct m0_cond match_cond;
177 static struct m0_mutex cond_mutex;
178 
179 static int test_fs_node_eval(
180  struct m0_fdmi_src_rec *src_rec,
181  struct m0_fdmi_flt_var_node *value_desc,
182  struct m0_fdmi_flt_operand *value)
183 {
184  bool matched;
185 
186  M0_UT_ASSERT(src_rec == &g_src_rec);
187  M0_UT_ASSERT(src_rec->fsr_data == &g_fdmi_data);
188 
196  matched = value->ffo_type == M0_FF_OPND_BOOL &&
197  value->ffo_data.fpl_type == M0_FF_OPND_PLD_BOOL &&
198  value->ffo_data.fpl_pld.fpl_boolean;
200  src_rec->fsr_matched = matched;
201  return 0;
202 }
203 
204 static int test_fs_encode(struct m0_fdmi_src_rec *src_rec,
205  struct m0_buf *buf)
206 {
207  return 0;
208 }
209 
210 static void test_fs_get(struct m0_fdmi_src_rec *src_rec)
211 {
212  M0_UT_ASSERT(src_rec != NULL);
213  M0_UT_ASSERT(src_rec == &g_src_rec);
214 }
215 
216 static void test_fs_put(struct m0_fdmi_src_rec *src_rec)
217 {
218  M0_UT_ASSERT(src_rec != NULL);
219  M0_UT_ASSERT(src_rec == &g_src_rec);
220 }
221 
222 static void test_fs_begin(struct m0_fdmi_src_rec *src_rec)
223 {
224  M0_UT_ASSERT(src_rec != NULL);
225  M0_UT_ASSERT(src_rec == &g_src_rec);
226 }
227 
228 static void test_fs_end(struct m0_fdmi_src_rec *src_rec)
229 {
230  M0_UT_ASSERT(src_rec != NULL);
231  M0_UT_ASSERT(src_rec == &g_src_rec);
235 }
236 
237 static struct m0_fdmi_src *src_alloc()
238 {
239  struct m0_fdmi_src *src;
240  int rc;
241 
243  M0_UT_ASSERT(rc == 0);
244 
245  src->fs_node_eval = test_fs_node_eval;
246  src->fs_get = test_fs_get;
247  src->fs_put = test_fs_put;
248  src->fs_begin = test_fs_begin;
249  src->fs_end = test_fs_end;
250  src->fs_encode = test_fs_encode;
251  return src;
252 }
253 
254 static void filterc_connect_to_confd(void)
255 {
256  struct m0_filterc_ops fc_ops = filterc_def_ops;
257  struct m0_fdmi_src *src = src_alloc();
258  int rc;
259  M0_ENTRY();
260 
263 
264  ufc_fco = &fc_ops;
266 
268 
269  rc = ut_filterc_start();
270  M0_UT_ASSERT(rc == 0);
271 
273  M0_UT_ASSERT(rc == 0);
274  g_src_rec = (struct m0_fdmi_src_rec) {
275  /* Don't send to remote ep. */
276  .fsr_dryrun = true,
277  .fsr_matched = false,
278  .fsr_src = src,
279  .fsr_data = g_fdmi_data,
280  };
281 
283 
288 
291 
295 
296  M0_LEAVE();
297 }
298 
300  .ts_name = "fdmi-filterc-ut",
301  .ts_tests = {
302  { "filterc-connect-to-confd", filterc_connect_to_confd},
303  { NULL, NULL },
304  },
305 };
306 
307 #undef M0_TRACE_SUBSYSTEM
308 
309 /*
310  * Local variables:
311  * c-indentation-style: "K&R"
312  * c-basic-offset: 8
313  * tab-width: 8
314  * fill-column: 80
315  * scroll-step: 1
316  * End:
317  */
318 /*
319  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
320  */
M0_EXTERN struct m0_reqh_service_type m0_fdmi_service_type
Definition: fdmi.h:194
Definition: cond.h:99
static struct m0_net_domain client_net_dom
Definition: filterc_ut.c:60
static void rpc_client_and_server_stop(void)
Definition: filterc_ut.c:165
M0_INTERNAL int m0_reqh_service_start(struct m0_reqh_service *service)
Definition: reqh_service.c:343
const struct m0_filterc_ops * filterc_ops
Definition: service.h:49
static void test_fs_get(struct m0_fdmi_src_rec *src_rec)
Definition: filterc_ut.c:210
M0_INTERNAL void m0_mutex_unlock(struct m0_mutex *mutex)
Definition: mutex.c:66
void m0_net_domain_fini(struct m0_net_domain *dom)
Definition: domain.c:71
static struct m0_mutex cond_mutex
Definition: filterc_ut.c:177
const struct m0_filterc_ops filterc_def_ops
Definition: filterc.c:41
#define NULL
Definition: misc.h:38
bool fsr_dryrun
Definition: src_rec.h:106
M0_INTERNAL void m0_fdmi_flt_bool_opnd_fill(struct m0_fdmi_flt_operand *opnd, bool value)
Definition: filter.c:125
#define M0_LOG(level,...)
Definition: trace.h:167
static struct m0_reqh * motr2reqh(struct m0_motr *motr)
Definition: filterc_ut.c:92
M0_LEAVE()
static int ut_filterc_fco_start(struct m0_filterc_ctx *ctx, struct m0_reqh *reqh)
Definition: filterc_ut.c:141
static void rpc_client_and_server_start(void)
Definition: filterc_ut.c:151
int m0_rpc_server_start(struct m0_rpc_server_ctx *sctx)
Definition: rpclib.c:50
M0_INTERNAL void m0_buf_init(struct m0_buf *buf, void *data, uint32_t nob)
Definition: buf.c:37
int const char const void * value
Definition: dir.c:325
bool fsr_matched
Definition: src_rec.h:101
M0_INTERNAL void m0_fdmi_source_free(struct m0_fdmi_src *src)
Definition: source_dock.c:301
#define M0_SRC_PATH(name)
Definition: misc.h:48
#define M0_SET0(obj)
Definition: misc.h:64
M0_INTERNAL void m0_mutex_lock(struct m0_mutex *mutex)
Definition: mutex.c:49
Definition: ut.h:77
#define SERVER_STOB_NAME
Definition: filterc_ut.c:53
M0_INTERNAL void m0_cond_init(struct m0_cond *cond, struct m0_mutex *mutex)
Definition: cond.c:40
Definition: sock.c:887
M0_INTERNAL struct m0_reqh_service_type * m0_reqh_service_type_find(const char *sname)
Definition: reqh_service.c:168
static void test_fs_end(struct m0_fdmi_src_rec *src_rec)
Definition: filterc_ut.c:228
#define SERVER_DB_NAME
Definition: filterc_ut.c:52
static char g_fdmi_data[]
Definition: filterc_ut.c:39
return M0_RC(rc)
static struct m0_filterc_ops * ufc_fco
Definition: filterc_ut.c:42
struct m0_buf rs_ss_param
Definition: reqh_service.h:281
#define M0_ENTRY(...)
Definition: trace.h:170
Definition: buf.h:37
M0_INTERNAL int m0_fdmi_source_alloc(enum m0_fdmi_rec_type_id type_id, struct m0_fdmi_src **src)
Definition: source_dock.c:285
static struct m0_rpc_client_ctx cctx
Definition: filterc_ut.c:80
#define M0_ERR_INFO(rc, fmt,...)
Definition: trace.h:215
#define SERVER_LOG_NAME
Definition: filterc_ut.c:55
static int ut_filterc_start()
Definition: filterc_ut.c:97
static const struct socktype stype[]
Definition: sock.c:1156
#define M0_ASSERT(cond)
M0_INTERNAL void m0_cond_fini(struct m0_cond *cond)
Definition: cond.c:46
M0_INTERNAL void m0_fdmi_source_deregister(struct m0_fdmi_src *src)
Definition: source_dock.c:337
static struct m0_cond match_cond
Definition: filterc_ut.c:176
void * fsr_data
Definition: src_rec.h:75
static char * server_argv[]
Definition: filterc_ut.c:62
struct m0_reqh rc_reqh
Definition: setup.h:312
int m0_net_xprt_nr(void)
Definition: net.c:168
int m0_rpc_client_stop(struct m0_rpc_client_ctx *cctx)
Definition: rpclib.c:217
M0_INTERNAL int m0_fdmi_source_register(struct m0_fdmi_src *src)
Definition: source_dock.c:311
struct m0_net_xprt * m0_net_xprt_default_get(void)
Definition: net.c:151
int m0_rpc_client_start(struct m0_rpc_client_ctx *cctx)
Definition: rpclib.c:160
struct m0_net_xprt ** rsx_xprts
Definition: rpclib.h:69
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
M0_INTERNAL int m0_reqh_service_allocate(struct m0_reqh_service **out, const struct m0_reqh_service_type *stype, struct m0_reqh_context *rctx)
Definition: reqh_service.c:185
#define SERVER_ADDB_STOB_NAME
Definition: filterc_ut.c:54
Definition: reqh.h:94
static int test_fs_encode(struct m0_fdmi_src_rec *src_rec, struct m0_buf *buf)
Definition: filterc_ut.c:204
M0_INTERNAL void m0_reqh_service_init(struct m0_reqh_service *service, struct m0_reqh *reqh, const struct m0_fid *fid)
Definition: reqh_service.c:428
static void filterc_connect_to_confd(void)
Definition: filterc_ut.c:254
struct m0_net_domain * rcx_net_dom
Definition: rpclib.h:128
static struct m0_fdmi_src_rec g_src_rec
Definition: filterc_ut.c:40
#define SERVER_ENDPOINT
Definition: filterc_ut.c:50
static struct m0_reqh_service * ufc_fdmi_service
Definition: filterc_ut.c:43
const char * ts_name
Definition: ut.h:99
M0_INTERNAL void m0_cond_wait(struct m0_cond *cond)
Definition: cond.c:52
M0_INTERNAL struct m0_reqh_service * m0_reqh_service_find(const struct m0_reqh_service_type *st, const struct m0_reqh *reqh)
Definition: reqh_service.c:538
Definition: setup.h:354
struct m0_reqh reqh
Definition: rm_foms.c:48
static struct m0_fdmi_src * src_alloc()
Definition: filterc_ut.c:237
int m0_net_domain_init(struct m0_net_domain *dom, const struct m0_net_xprt *xprt)
Definition: domain.c:36
static struct m0_rpc_server_ctx sctx
Definition: filterc_ut.c:73
int rsx_xprts_nr
Definition: rpclib.h:71
struct m0_reqh_context cc_reqh_ctx
Definition: setup.h:361
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
struct m0_motr motr
#define M0_FDMI_SOURCE_POST_RECORD(_src_rec_ptr)
Definition: source_dock.h:148
struct m0_net_xprt ** m0_net_all_xprt_get(void)
Definition: net.c:161
static int test_fs_node_eval(struct m0_fdmi_src_rec *src_rec, struct m0_fdmi_flt_var_node *value_desc, struct m0_fdmi_flt_operand *value)
Definition: filterc_ut.c:179
M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
Definition: mutex.c:42
int(* fco_start)(struct m0_filterc_ctx *ctx, struct m0_reqh *reqh)
Definition: filterc.h:56
M0_INTERNAL void m0_cond_broadcast(struct m0_cond *cond)
Definition: cond.c:100
void m0_rpc_server_stop(struct m0_rpc_server_ctx *sctx)
Definition: rpclib.c:85
static void test_fs_put(struct m0_fdmi_src_rec *src_rec)
Definition: filterc_ut.c:216
Definition: nucleus.c:42
def start_service(self, service, idx)
#define SERVER_ENDPOINT_ADDR
Definition: filterc_ut.c:56
Definition: mutex.h:47
struct m0_pdclust_src_addr src
Definition: fd.c:108
int32_t rc
Definition: trigger_fop.h:47
static void test_fs_begin(struct m0_fdmi_src_rec *src_rec)
Definition: filterc_ut.c:222
#define ARRAY_SIZE(a)
Definition: misc.h:45
struct m0_fid g_process_fid
Definition: ut.c:689
struct m0_ut_suite fdmi_filterc_ut
Definition: filterc_ut.c:299
#define M0_UT_ASSERT(a)
Definition: ut.h:46
struct m0_motr rsx_motr_ctx
Definition: rpclib.h:84
#define CLIENT_ENDPOINT_ADDR
Definition: filterc_ut.c:48