Motr  M0
service.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-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 #include "ut/ut.h" /* M0_UT_ASSERT */
24 #include "lib/arith.h" /* m0_rnd */
25 #include "lib/misc.h" /* M0_SET0 */
26 #include "lib/errno.h" /* ENOENT */
27 
28 #include "net/test/service.h"
29 
30 enum {
32 };
33 
34 static struct m0_net_test_service svc;
41 static int service_cmd_errno;
42 
43 static int *service_func_called[] = {
47 };
48 
49 static int service_ut_cmd(void *ctx,
50  const struct m0_net_test_cmd *cmd,
51  struct m0_net_test_cmd *reply,
52  enum m0_net_test_cmd_type cmd_type)
53 {
54  service_cmd_called[cmd_type] = true;
55  M0_UT_ASSERT(cmd == &service_cmd);
57  return service_cmd_errno;
58 }
59 
60 static int service_ut_cmd_init(void *ctx_,
61  const struct m0_net_test_cmd *cmd,
62  struct m0_net_test_cmd *reply)
63 {
64  return service_ut_cmd(ctx_, cmd, reply, M0_NET_TEST_CMD_INIT);
65 }
66 
67 static int service_ut_cmd_start(void *ctx_,
68  const struct m0_net_test_cmd *cmd,
69  struct m0_net_test_cmd *reply)
70 {
71  return service_ut_cmd(ctx_, cmd, reply, M0_NET_TEST_CMD_START);
72 }
73 
74 static int service_ut_cmd_stop(void *ctx_,
75  const struct m0_net_test_cmd *cmd,
76  struct m0_net_test_cmd *reply)
77 {
78  return service_ut_cmd(ctx_, cmd, reply, M0_NET_TEST_CMD_STOP);
79 }
80 
81 static int service_ut_cmd_status(void *ctx_,
82  const struct m0_net_test_cmd *cmd,
83  struct m0_net_test_cmd *reply)
84 {
85  return service_ut_cmd(ctx_, cmd, reply, M0_NET_TEST_CMD_STATUS);
86 }
87 
88 static void *service_ut_init(struct m0_net_test_service *svc_)
89 {
90  M0_UT_ASSERT(svc_ == &svc);
91  service_init_called = true;
92  return &svc;
93 }
94 
95 static void service_ut_fini(void *ctx)
96 {
97  service_fini_called = true;
98 }
99 
100 static int service_ut_step(void *ctx)
101 {
102  service_step_called = true;
103  return 0;
104 }
105 
107  {
109  .ntsch_handler = service_ut_cmd_init,
110  },
111  {
112  .ntsch_type = M0_NET_TEST_CMD_START,
113  .ntsch_handler = service_ut_cmd_start,
114  },
115  {
116  .ntsch_type = M0_NET_TEST_CMD_STOP,
117  .ntsch_handler = service_ut_cmd_stop,
118  },
119  {
120  .ntsch_type = M0_NET_TEST_CMD_STATUS,
121  .ntsch_handler = service_ut_cmd_status,
122  },
123 };
124 
127  .ntso_fini = service_ut_fini,
128  .ntso_step = service_ut_step,
129  .ntso_cmd_handler = service_ut_cmd_handler,
130  .ntso_cmd_handler_nr = ARRAY_SIZE(service_ut_cmd_handler),
131 };
132 
134  enum m0_net_test_service_state state)
135 {
136  enum m0_net_test_service_state svc_state;
137  bool rc_bool;
138 
140  M0_UT_ASSERT(rc_bool);
141  svc_state = m0_net_test_service_state_get(svc);
142  M0_UT_ASSERT(svc_state == state);
143 }
144 
145 static void service_ut_check_reset(void)
146 {
147  int i;
148  for (i = 0; i < ARRAY_SIZE(service_func_called); ++i)
149  *service_func_called[i] = false;
151 }
152 
153 static void service_ut_check_called(int *func_bool)
154 {
155  size_t func_nr = ARRAY_SIZE(service_func_called);
156  size_t cmd_nr = ARRAY_SIZE(service_cmd_called);
157  int called_nr = 0;
158  int i;
159 
160  M0_PRE(func_bool != NULL);
161 
162  for (i = 0; i < func_nr; ++i) {
163  M0_UT_ASSERT(equi(func_bool == service_func_called[i],
165  called_nr += *service_func_called[i];
166  }
167  for (i = 0; i < cmd_nr; ++i)
168  called_nr += service_cmd_called[i];
169  M0_UT_ASSERT(called_nr == 1);
170 }
171 
172 static bool service_can_handle(enum m0_net_test_cmd_type cmd_type)
173 {
174  int i;
175 
176  for (i = 0; i < ARRAY_SIZE(service_ut_cmd_handler); ++i) {
177  if (service_ut_cmd_handler[i].ntsch_type == cmd_type)
178  return true;
179  }
180  return false;
181 }
182 
184 {
185  enum m0_net_test_cmd_type cmd_type;
186  int rc;
187  uint64_t seed = 42;
188  int i;
189  int cmd_max;
190  int cmd_index;
191 
192  /* test m0_net_test_service_init() */
195  M0_UT_ASSERT(rc == 0);
198 
199  /* test m0_net_test_service_step()/m0_net_test_service_cmd_handle() */
200  cmd_max = ARRAY_SIZE(service_cmd_called) + 1;
201  for (i = 0; i < SERVICE_ITERATIONS_NR; ++i) {
202  cmd_index = m0_rnd(cmd_max, &seed);
203  if (cmd_index == cmd_max - 1) {
204  /* step */
207  M0_UT_ASSERT(rc == 0);
210  } else {
211  /* command */
212  cmd_type = cmd_index;
214  -m0_rnd(64, &seed) : -ENOENT;
216  service_cmd.ntc_type = cmd_type;
218  &service_reply);
221  }
222  }
223 
224  /* test M0_NET_TEST_SERVICE_FAILED state */
227 
228  /* test m0_net_test_service_fini() */
232 }
233 
234 /*
235  * Local variables:
236  * c-indentation-style: "K&R"
237  * c-basic-offset: 8
238  * tab-width: 8
239  * fill-column: 79
240  * scroll-step: 1
241  * End:
242  */
enum m0_net_test_cmd_type ntsch_type
Definition: service.h:52
#define M0_PRE(cond)
static void * service_ut_init(struct m0_net_test_service *svc_)
Definition: service.c:88
static bool service_can_handle(enum m0_net_test_cmd_type cmd_type)
Definition: service.c:172
#define NULL
Definition: misc.h:38
bool m0_net_test_service_invariant(struct m0_net_test_service *svc)
Definition: service.c:98
enum m0_net_test_service_state m0_net_test_service_state_get(struct m0_net_test_service *svc)
Definition: service.c:159
void m0_net_test_service_state_change(struct m0_net_test_service *svc, enum m0_net_test_service_state state)
Definition: service.c:147
m0_net_test_cmd_type
Definition: commands.h:78
static int service_step_called
Definition: service.c:39
static int * service_func_called[]
Definition: service.c:43
static void service_ut_fini(void *ctx)
Definition: service.c:95
static int service_fini_called
Definition: service.c:38
static int service_ut_cmd_status(void *ctx_, const struct m0_net_test_cmd *cmd, struct m0_net_test_cmd *reply)
Definition: service.c:81
#define equi(a, b)
Definition: misc.h:297
int i
Definition: dir.c:1033
#define M0_SET_ARR0(arr)
Definition: misc.h:72
static void service_ut_check_reset(void)
Definition: service.c:145
static int service_cmd_errno
Definition: service.c:41
M0_INTERNAL uint64_t m0_rnd(uint64_t max, uint64_t *seed)
Definition: misc.c:115
int m0_net_test_service_step(struct m0_net_test_service *svc)
Definition: service.c:107
static int service_init_called
Definition: service.c:37
static int service_ut_cmd(void *ctx, const struct m0_net_test_cmd *cmd, struct m0_net_test_cmd *reply, enum m0_net_test_cmd_type cmd_type)
Definition: service.c:49
static int service_ut_step(void *ctx)
Definition: service.c:100
static int service_cmd_called[M0_NET_TEST_CMD_NR]
Definition: service.c:40
static struct m0_net_test_cmd service_reply
Definition: service.c:36
void m0_net_test_service_ut(void)
Definition: service.c:183
static void service_ut_checks(struct m0_net_test_service *svc, enum m0_net_test_service_state state)
Definition: service.c:133
static int service_ut_cmd_start(void *ctx_, const struct m0_net_test_cmd *cmd, struct m0_net_test_cmd *reply)
Definition: service.c:67
static void service_ut_check_called(int *func_bool)
Definition: service.c:153
static int service_ut_cmd_init(void *ctx_, const struct m0_net_test_cmd *cmd, struct m0_net_test_cmd *reply)
Definition: service.c:60
static struct m0_net_test_service_cmd_handler service_ut_cmd_handler[]
Definition: service.c:106
void m0_net_test_service_fini(struct m0_net_test_service *svc)
Definition: service.c:88
int m0_net_test_service_cmd_handle(struct m0_net_test_service *svc, struct m0_net_test_cmd *cmd, struct m0_net_test_cmd *reply)
Definition: service.c:121
static struct m0_net_test_service_ops service_ut_ops
Definition: service.c:125
static int service_ut_cmd_stop(void *ctx_, const struct m0_net_test_cmd *cmd, struct m0_net_test_cmd *reply)
Definition: service.c:74
m0_net_test_service_state
Definition: service.h:60
static struct m0_net_test_service svc
Definition: service.c:34
enum m0_net_test_cmd_type ntc_type
Definition: commands.h:204
static struct m0_net_test_cmd service_cmd
Definition: service.c:35
Definition: nucleus.c:42
void *(* ntso_init)(struct m0_net_test_service *svc)
Definition: service.h:76
static struct m0_dtm_oper_descr reply
Definition: transmit.c:94
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46
int m0_net_test_service_init(struct m0_net_test_service *svc, struct m0_net_test_service_ops *ops)
Definition: service.c:68