Motr  M0
cache.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-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 "conf/cache.h"
24 #include "conf/obj_ops.h" /* m0_conf_obj_create */
25 #include "conf/preload.h" /* m0_confstr_parse, m0_confx_free */
26 #include "conf/onwire.h" /* m0_confx_obj, m0_confx */
27 #include "conf/dir.h" /* m0_conf_dir_add */
28 #include "conf/ut/common.h"
29 #include "lib/buf.h" /* m0_buf, M0_BUF_INITS */
30 #include "lib/errno.h" /* ENOENT */
31 #include "lib/fs.h" /* m0_file_read */
32 #include "lib/memory.h" /* m0_free0 */
33 #include "ut/misc.h" /* M0_UT_PATH */
34 #include "ut/ut.h"
35 
36 static void test_obj_xtors(void)
37 {
38  struct m0_conf_obj *obj;
39  const struct m0_conf_obj_type *t = NULL;
40 
41  while ((t = m0_conf_obj_type_next(t)) != NULL) {
42  const struct m0_fid fid = M0_FID_TINIT(t->cot_ftype.ft_id,
43  1, 0);
46  M0_UT_ASSERT(obj != NULL);
47 
51  }
52 }
53 
54 static void
55 ut_conf_obj_create(const struct m0_fid *fid, struct m0_conf_obj **result)
56 {
57  struct m0_conf_obj *obj;
58  int rc;
59 
61  M0_UT_ASSERT(obj == NULL);
62 
64  M0_UT_ASSERT(obj != NULL);
66 
70  M0_UT_ASSERT(rc == 0);
71 
73  *result = obj;
74 }
75 
76 static void ut_conf_obj_delete(struct m0_conf_obj *obj)
77 {
81 
83  NULL);
84 }
85 
86 static void test_dir_add_del(void)
87 {
88  struct m0_conf_obj *dir;
89  struct m0_conf_obj *obj;
90  const struct m0_fid objid = M0_FID_TINIT('a', 8, 1);
91 
92  ut_conf_obj_create(&M0_FID_TINIT('D', 8, 1), &dir);
93  M0_CONF_CAST(dir, m0_conf_dir)->cd_relfid = M0_FID_TINIT('/', 0, 999);
94  M0_CONF_CAST(dir, m0_conf_dir)->cd_item_type = m0_conf_fid_type(&objid);
95 
96  ut_conf_obj_create(&objid, &obj);
99 
102 }
103 
104 static void test_cache(void)
105 {
106  int rc;
107  struct m0_conf_obj *obj;
108  struct m0_fid samples[] = {
109  M0_FID_TINIT('p', ~0, 0),
110  M0_FID_TINIT('D', 7, 3)
111  };
112 
113  m0_forall(i, ARRAY_SIZE(samples),
114  ut_conf_obj_create(&samples[i], &obj), true);
115  ut_conf_obj_delete(obj); /* delete the last object */
116 
117  /* Duplicated identity. */
118  obj = m0_conf_obj_create(&samples[0], &m0_conf_ut_cache);
119  M0_UT_ASSERT(obj != NULL);
120 
124  M0_UT_ASSERT(rc == -EEXIST);
125 
129 }
130 
131 static void test_obj_find(void)
132 {
133  int rc;
134  const struct m0_fid id = M0_FID_TINIT('p', 1, 0);
135  struct m0_conf_obj *p = NULL;
136  struct m0_conf_obj *q = NULL;
137 
139 
141  M0_UT_ASSERT(rc == 0);
142  M0_UT_ASSERT(p != NULL);
143 
145  M0_UT_ASSERT(rc == 0);
146  M0_UT_ASSERT(q == p);
147 
149  M0_UT_ASSERT(rc == 0);
150  M0_UT_ASSERT(q != p);
151 
153 }
154 
155 static void test_obj_fill(void)
156 {
157  struct m0_confx *enc;
158  struct m0_conf_obj *obj;
159  char *confstr = NULL;
160  int i;
161  int rc;
162 
163  m0_confx_free(NULL); /* to make sure this can be done */
164 
165  rc = m0_file_read(M0_UT_PATH("conf.xc"), &confstr);
166  M0_UT_ASSERT(rc == 0);
167  rc = m0_confstr_parse(confstr, &enc);
168  M0_UT_ASSERT(rc == 0);
169  M0_UT_ASSERT(enc->cx_nr == M0_UT_CONF_NR_OBJS);
170  m0_free0(&confstr);
171 
173  for (i = 0; i < enc->cx_nr; ++i) {
174  struct m0_confx_obj *xobj = M0_CONFX_AT(enc, i);
175 
177  &obj) ?:
178  m0_conf_obj_fill(obj, xobj);
179  M0_UT_ASSERT(rc == 0);
180  }
182 
183  m0_confx_free(enc);
184 }
185 
187  .ts_name = "conf-ut",
188  .ts_init = m0_conf_ut_cache_init,
189  .ts_fini = m0_conf_ut_cache_fini,
190  .ts_tests = {
191  { "obj-xtors", test_obj_xtors },
192  { "cache", test_cache },
193  { "obj-find", test_obj_find },
194  { "obj-fill", test_obj_fill },
195  { "dir-add-del", test_dir_add_del },
196  { NULL, NULL }
197  }
198 };
struct m0_fid co_id
Definition: obj.h:208
uint64_t id
Definition: cob.h:2380
static struct m0_addb2_philter p
Definition: consumer.c:40
M0_INTERNAL int m0_conf_ut_cache_init(void)
Definition: common.c:107
M0_INTERNAL void m0_confx_free(struct m0_confx *enc)
Definition: preload.c:33
static struct m0_semaphore q
Definition: rwlock.c:55
#define NULL
Definition: misc.h:38
static void test_obj_xtors(void)
Definition: cache.c:36
static void test_obj_fill(void)
Definition: cache.c:155
M0_INTERNAL struct m0_conf_obj * m0_conf_cache_lookup(const struct m0_conf_cache *cache, const struct m0_fid *id)
Definition: cache.c:106
M0_INTERNAL void m0_conf_dir_add(struct m0_conf_dir *dir, struct m0_conf_obj *obj)
Definition: dir.c:43
const struct m0_conf_obj_type * m0_conf_fid_type(const struct m0_fid *fid)
Definition: obj.c:368
M0_INTERNAL int m0_file_read(const char *path, char **out)
Definition: fs.c:61
#define M0_CONFX_AT(cx, idx)
Definition: onwire.h:271
Definition: ut.h:77
static struct foo * obj
Definition: tlist.c:302
struct m0_fid fid
Definition: di.c:46
M0_INTERNAL int m0_conf_obj_fill(struct m0_conf_obj *dest, const struct m0_confx_obj *src)
Definition: obj_ops.c:232
static void ut_conf_obj_delete(struct m0_conf_obj *obj)
Definition: cache.c:76
int i
Definition: dir.c:1033
struct m0_conf_cache m0_conf_ut_cache
Definition: common.c:31
#define M0_FID_TINIT(type, container, key)
Definition: fid.h:90
uint32_t cx_nr
Definition: onwire.h:259
#define m0_free0(pptr)
Definition: memory.h:77
#define M0_ASSERT(cond)
static struct m0_thread t[8]
Definition: service_ut.c:1230
M0_INTERNAL int m0_conf_ut_cache_fini(void)
Definition: common.c:114
static void ut_conf_obj_create(const struct m0_fid *fid, struct m0_conf_obj **result)
Definition: cache.c:55
static void test_obj_find(void)
Definition: cache.c:131
M0_INTERNAL int m0_conf_obj_find(struct m0_conf_cache *cache, const struct m0_fid *id, struct m0_conf_obj **out)
Definition: obj_ops.c:136
#define M0_CONF_CAST(ptr, type)
Definition: obj.h:780
M0_INTERNAL int m0_conf_cache_add(struct m0_conf_cache *cache, struct m0_conf_obj *obj)
Definition: cache.c:79
M0_INTERNAL const struct m0_conf_obj_type * m0_conf_obj_type_next(const struct m0_conf_obj_type *otype)
Definition: obj.c:351
bool m0_conf_fid_is_valid(const struct m0_fid *fid)
Definition: obj.c:378
M0_INTERNAL bool m0_fid_eq(const struct m0_fid *fid0, const struct m0_fid *fid1)
Definition: fid.c:164
#define m0_forall(var, nr,...)
Definition: misc.h:112
const char * ts_name
Definition: ut.h:99
M0_INTERNAL struct m0_conf_obj * m0_conf_obj_create(const struct m0_fid *id, struct m0_conf_cache *cache)
Definition: obj_ops.c:80
M0_INTERNAL int m0_confstr_parse(const char *str, struct m0_confx **out)
Definition: preload.c:41
Definition: fid.h:38
M0_INTERNAL void m0_conf_cache_del(const struct m0_conf_cache *cache, struct m0_conf_obj *obj)
Definition: cache.c:124
M0_INTERNAL void m0_conf_obj_delete(struct m0_conf_obj *obj)
Definition: obj_ops.c:167
struct inode * dir
Definition: dir.c:1028
M0_INTERNAL void m0_conf_dir_del(struct m0_conf_dir *dir, struct m0_conf_obj *obj)
Definition: dir.c:53
#define M0_UT_PATH(name)
Definition: misc.h:41
M0_INTERNAL void m0_conf_cache_lock(struct m0_conf_cache *cache)
Definition: cache.c:50
struct m0_ut_suite conf_ut
Definition: cache.c:186
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
static void test_cache(void)
Definition: cache.c:104
#define M0_UT_ASSERT(a)
Definition: ut.h:46
M0_INTERNAL void m0_conf_cache_unlock(struct m0_conf_cache *cache)
Definition: cache.c:55
static void test_dir_add_del(void)
Definition: cache.c:86
const struct m0_fid * m0_conf_objx_fid(const struct m0_confx_obj *obj)
Definition: obj_ops.c:266