Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / common / test_blkdev.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <string.h>
5 #include <errno.h>
6 #include <stdlib.h>
7
8 #include "include/types.h"
9 #include "common/blkdev.h"
10
11 #include "gtest/gtest.h"
12 #include <iostream>
13
14   using namespace std;
15
16 TEST(blkdev, get_block_device_base) {
17   char buf[PATH_MAX*2];
18   char buf3[PATH_MAX*2];
19
20   ASSERT_EQ(-EINVAL, get_block_device_base("/etc/notindev", buf, 100));
21
22   for (int i=0; i<2; ++i) {
23     string root;
24     if (i == 0) {
25       const char* env = getenv("CEPH_ROOT");
26       ASSERT_NE(env, nullptr) << "Environment Variable CEPH_ROOT not found!";
27       root = string(env) + "/src/test/common/test_blkdev_sys_block";
28     }
29     set_block_device_sandbox_dir(root.c_str());
30
31     // work backwards
32     sprintf(buf, "%s/sys/block", root.c_str());
33     DIR *dir = opendir(buf);
34     ASSERT_NE(dir, nullptr);
35     struct dirent *de = nullptr;
36     while ((de = ::readdir(dir))) {
37       if (de->d_name[0] == '.')
38         continue;
39
40       char base[PATH_MAX];
41       sprintf(base, "/dev/%s", de->d_name);
42       printf("base %s (%s)\n", base, de->d_name);
43       for (char *p = base; *p; ++p)
44         if (*p == '!')
45           *p = '/';
46
47       ASSERT_EQ(-ERANGE, get_block_device_base(base, buf3, 1));
48       ASSERT_EQ(0, get_block_device_base(base, buf3, sizeof(buf3)));
49       printf("  got '%s' expected '%s'\n", buf3, de->d_name);
50       ASSERT_EQ(0, strcmp(de->d_name, buf3));
51       printf("  discard granularity = %lld .. supported = %d\n",
52              (long long)get_block_device_int_property(base, "queue/discard_granularity"),
53              (int)block_device_support_discard(base));
54
55       char subdirfn[PATH_MAX];
56       sprintf(subdirfn, "%s/sys/block/%s", root.c_str(), de->d_name);
57       DIR *subdir = opendir(subdirfn);
58       ASSERT_TRUE(subdir);
59       struct dirent *de2 = nullptr;
60       while ((de2 = ::readdir(subdir))) {
61         if (de2->d_name[0] == '.')
62           continue;
63         // partiions will be prefixed with the base name
64         if (strncmp(de2->d_name, de->d_name, strlen(de->d_name))) {
65           //printf("skipping %s\n", de2->d_name);
66           continue;
67         }
68         char part[PATH_MAX];
69         sprintf(part, "/dev/%s", de2->d_name);
70         for (char *p = part; *p; ++p)
71           if (*p == '!')
72             *p = '/';
73         printf(" part %s (%s %s)\n", part, de->d_name, de2->d_name);
74
75         ASSERT_EQ(0, get_block_device_base(part, buf3, sizeof(buf3)));
76         printf("  got '%s' expected '%s'\n", buf3, de->d_name);
77         ASSERT_EQ(0, strcmp(buf3, de->d_name));
78         printf("  discard granularity = %lld .. supported = %d\n",
79                (long long)get_block_device_int_property(part, "queue/discard_granularity"),
80                (int)block_device_support_discard(part));
81       }
82
83       closedir(subdir);
84     }
85     closedir(dir);
86   }
87 }
88
89 TEST(blkdev, device_model)
90 {
91   const char* env = getenv("CEPH_ROOT");
92   ASSERT_NE(env, nullptr) << "Environment Variable CEPH_ROOT not found!";
93   string root = string(env) + "/src/test/common/test_blkdev_sys_block";
94   set_block_device_sandbox_dir(root.c_str());
95
96   char model[1000] = {0};
97   int rc = block_device_model("sda", model, sizeof(model));
98   ASSERT_EQ(0, rc);
99
100   printf("model '%s'\n", model);
101   ASSERT_EQ(strcmp(model, "myfancymodel"), 0);
102 }
103
104 TEST(blkdev, get_block_device_string_property)
105 {
106   const char* env = getenv("CEPH_ROOT");
107   ASSERT_NE(env, nullptr) << "Environment Variable CEPH_ROOT not found!";
108   string root = string(env) + "/src/test/common/test_blkdev_sys_block";
109   set_block_device_sandbox_dir(root.c_str());
110
111   char val[1000] = {0};
112   int rc = get_block_device_string_property("sda", "device/model",
113                                             val, sizeof(val));
114   ASSERT_EQ(0, rc);
115   printf("val '%s'\n", val);
116   ASSERT_EQ(strcmp(val, "myfancymodel"), 0);
117 }
118
119 TEST(blkdev, is_rotational)
120 {
121   const char* env = getenv("CEPH_ROOT");
122   ASSERT_NE(env, nullptr) << "Environment Variable CEPH_ROOT not found!";
123   string root = string(env) + "/src/test/common/test_blkdev_sys_block";
124   set_block_device_sandbox_dir(root.c_str());
125
126   ASSERT_FALSE(block_device_is_rotational("sda"));
127   ASSERT_TRUE(block_device_is_rotational("sdb"));
128 }
129
130