gdb 调试ceph参见上篇文章。本篇主要讲解ceph中的googletest使用和ceph的makefile.am修改。
一,googletest简单介绍
googletest是谷歌开源的测试c++的框架,它是跨平台的,可应用在windows、linux、Mac等OS平台上。
参考http://bbs.51testing.com/thread-1161627-1-1.html
二、ceph中的googletest应用
在ceph中用到了gmock和gtest,ceph源码src中有gtest和gmock相关的文件。
1、Makefile.am方式编译ceph
编译流程:
进入ceph源码目录,依次执行如下命令
./install-deps.sh #安装ceph需要的依赖文件
./autogen.sh #产生ceph的configure配置文件
./configure --prefix=/usr --libexecdir=/usr/lib --sysconfdir=/etc --with-radosgw --with-rados --with-rbd --with-cephfs --with-nss --without-libcryptopp #产生Makefile文件,加上--
with-debug选项,make后会编译出bin_DEBUGPROGRAMS测试程序。
make -j48 #用48核编译
make check #编译ceph的测试文件,在执行make check过程中会执行src/test下的脚本,搭建集群并测试相关的功能,是否正常,这一步执行完后也会产生google unittest 文件,生成的google单元测试文件以unittest开头
make install #安装ceph
Makefile.am调用流程
ceph源码根目录下的Makefile.am文件包含子目录下的文件
4 # the "." here makes sure check-local builds gtest and gmock before they are used
5 SUBDIRS = . src man doc systemd selinux #SUBDIRS表示在处理目录之前,要递归处理哪些子目录,
要注意处理的顺序。
接下来可以继续看一下src下面的Makefile.am
1 include Makefile-env.am #Makefile-env.am中定义了Makefile.am文件中用到的变量
30 # subdirs
31 #包含src子目录下的Makefile.am
32 include arch/Makefile.am
33 include auth/Makefile.am
42 include osd/Makefile.am
313 if ENABLE_CLIENT #检测是否定义了ENABLE_CLIENT,ENABLE_CLIENT来自src/acconfig.h
314 include Makefile-client.am
315 endif
316
317 if ENABLE_SERVER
318 include Makefile-server.am #在Makefile-server.am文件中提到了具体编译出那些可执行的文件
319 endif
Makefile-env.am文件定义了变量
196 LIBGLOBAL = libglobal.la #定义了LIBGLOBAL变量,la为libtool自动生成的一些共享库,
#vi编辑查看,主要记录了一些配置信息,la文件下文中会讲到。是生成的该变量在Makefile-server.am文件中用到,下同
197 LIBCOMMON = libcommon.la
198 LIBSECRET = libsecret.la
199 LIBARCH = libarch.la
200 LIBPERFGLUE = libperfglue.la
201 LIBAUTH = libauth.la
202 LIBMSG = libmsg.la
Makefile-server.am文件定义了要编译的那些可执行文件
23 if WITH_MON
24
25 ceph_mon_SOURCES = ceph_mon.cc #ceph-mon需要的源文件
26 ceph_mon_LDADD = $(LIBMON) $(LIBOS) $(CEPH_GLOBAL) $(LIBCOMMON) $(LIBAUTH) $(LIBCOMMON) $(LIBMON_TYPES) #这些变量在Makefile-env.am文件中定义
27 bin_PROGRAMS += ceph-mon
28
29 endif # WITH_MON
la文件在src的子目录下生成,如src/common文件下的Makefile.am
176 libcommon_la_SOURCES = common/buffer.cc
177 libcommon_la_LIBADD = $(LIBCOMMON_DEPS) #libcommon_la依赖的库
178 noinst_LTLIBRARIES += libcommon.la #只编译不安装
修改Makefile.am添加自己的测试文件
了解了ceph Makefile.am调用流程后,添加自己的测试文件,mytestencode.cc,该文件主要测试ceph的decode和encode功能。
#include "include/buffer.h"
#include "include/encoding.h"
#include <iostream>
using namespace std;
template < typename T >
static void test_encode_and_decode(const T& src)
{
//bufferlist bl(1000000);
bufferlist bl;
encode(src, bl);
T dst;
bufferlist::iterator i(bl.begin());
decode(dst, i);
cout << " orig=" << src << ";" << " new=" << dst << endl;
}
void test_StringSimple()
{
string my_str("this is a test");
test_encode_and_decode < std::string >(my_str);
}
void test_StringEmpty()
{
string my_str("");
test_encode_and_decode < std::string >(my_str);
}
void test_StringNewline()
{
string my_str("foo bar baz\n");
test_encode_and_decode < std::string >(my_str);
}
int main()
{
test_StringSimple();
test_StringEmpty();
test_StringNewline();
return 0;
}
把该测试文件放到src/下,测试文件需要用到common/buffer.cc等一些cc文件,修改Makefile-server.am文件,添加
19 mytestmain_SOURCES = mytestencode.cc
20 mytestmain_LDADD = $(CEPH_GLOBAL) $(LIBCOMMON)
21 bin_PROGRAMS += mytestmain
在ceph根目录下make,执行完后生成mytestmain可执行文件,
root@bogon:/home/mycephdir/ceph/src# ./mytestmain
orig=I am the very model of a modern major general; new=I am the very model of a modern major general
orig=; new=
orig=foo bar baz
; new=foo bar baz
参考:http://blog.csdn.net/zrs19800702/article/details/53081682