Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / erasure-code / TestErasureCodePlugin.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph distributed storage system
5  *
6  * Copyright (C) 2013,2014 Cloudwatt <libre.licensing@cloudwatt.com>
7  * Copyright (C) 2014 Red Hat <contact@redhat.com>
8  *
9  * Author: Loic Dachary <loic@dachary.org>
10  *
11  *  This library is free software; you can redistribute it and/or
12  *  modify it under the terms of the GNU Lesser General Public
13  *  License as published by the Free Software Foundation; either
14  *  version 2.1 of the License, or (at your option) any later version.
15  * 
16  */
17
18 #include <errno.h>
19 #include <signal.h>
20 #include <stdlib.h>
21 #include "common/Thread.h"
22 #include "erasure-code/ErasureCodePlugin.h"
23 #include "global/global_context.h"
24 #include "common/config.h"
25 #include "gtest/gtest.h"
26
27
28 class ErasureCodePluginRegistryTest : public ::testing::Test {
29 protected:
30
31   class Thread_factory : public Thread {
32   public:
33     static void cleanup(void *arg) {
34       ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
35       if (instance.lock.is_locked())
36         instance.lock.Unlock();
37     }
38
39     void *entry() override {
40       ErasureCodeProfile profile;
41       ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
42       ErasureCodeInterfaceRef erasure_code;
43       pthread_cleanup_push(cleanup, NULL);
44       instance.factory("hangs",
45                        g_conf->get_val<std::string>("erasure_code_dir"),
46                        profile, &erasure_code, &cerr);
47       pthread_cleanup_pop(0);
48       return NULL;
49     }
50   };
51
52 };
53
54 TEST_F(ErasureCodePluginRegistryTest, factory_mutex) {
55   ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
56
57   EXPECT_TRUE(instance.lock.TryLock());
58   instance.lock.Unlock();
59
60   // 
61   // Test that the loading of a plugin is protected by a mutex.
62   //
63   useconds_t delay = 0;
64   const useconds_t DELAY_MAX = 20 * 1000 * 1000;
65   Thread_factory sleep_forever;
66   sleep_forever.create("sleep_forever");
67   do {
68     cout << "Trying (1) with delay " << delay << "us\n";
69     if (delay > 0)
70       usleep(delay);
71     if (!instance.loading)
72       delay = ( delay + 1 ) * 2;
73   } while(!instance.loading && delay < DELAY_MAX);
74   ASSERT_TRUE(delay < DELAY_MAX);
75
76   EXPECT_FALSE(instance.lock.TryLock());
77
78   EXPECT_EQ(0, pthread_cancel(sleep_forever.get_thread_id()));
79   EXPECT_EQ(0, sleep_forever.join());
80 }
81
82 TEST_F(ErasureCodePluginRegistryTest, all)
83 {
84   ErasureCodeProfile profile;
85   const char* env = getenv("CEPH_LIB");
86   string directory(env ? env : ".libs");
87   ErasureCodeInterfaceRef erasure_code;
88   ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
89   EXPECT_FALSE(erasure_code);
90   EXPECT_EQ(-EIO, instance.factory("invalid",
91                                    g_conf->get_val<std::string>("erasure_code_dir"),
92                                    profile, &erasure_code, &cerr));
93   EXPECT_FALSE(erasure_code);
94   EXPECT_EQ(-EXDEV, instance.factory("missing_version",
95                                      g_conf->get_val<std::string>("erasure_code_dir"),
96                                      profile,
97                                      &erasure_code, &cerr));
98   EXPECT_FALSE(erasure_code);
99   EXPECT_EQ(-ENOENT, instance.factory("missing_entry_point",
100                                       g_conf->get_val<std::string>("erasure_code_dir"),
101                                       profile,
102                                       &erasure_code, &cerr));
103   EXPECT_FALSE(erasure_code);
104   EXPECT_EQ(-ESRCH, instance.factory("fail_to_initialize",
105                                      g_conf->get_val<std::string>("erasure_code_dir"),
106                                      profile,
107                                      &erasure_code, &cerr));
108   EXPECT_FALSE(erasure_code);
109   EXPECT_EQ(-EBADF, instance.factory("fail_to_register",
110                                      g_conf->get_val<std::string>("erasure_code_dir"),
111                                      profile,
112                                      &erasure_code, &cerr));
113   EXPECT_FALSE(erasure_code);
114   EXPECT_EQ(0, instance.factory("example",
115                                 g_conf->get_val<std::string>("erasure_code_dir"),
116                                 profile, &erasure_code, &cerr));
117   EXPECT_TRUE(erasure_code.get());
118   ErasureCodePlugin *plugin = 0;
119   {
120     Mutex::Locker l(instance.lock);
121     EXPECT_EQ(-EEXIST, instance.load("example", directory, &plugin, &cerr));
122     EXPECT_EQ(-ENOENT, instance.remove("does not exist"));
123     EXPECT_EQ(0, instance.remove("example"));
124     EXPECT_EQ(0, instance.load("example", directory, &plugin, &cerr));
125   }
126 }
127
128 /*
129  * Local Variables:
130  * compile-command: "cd ../.. ; make -j4 && 
131  *   make unittest_erasure_code_plugin && 
132  *   valgrind --tool=memcheck \
133  *      ./unittest_erasure_code_plugin \
134  *      --gtest_filter=*.* --log-to-stderr=true --debug-osd=20"
135  * End:
136  */