如何在ceph中新增google unit test总结

来源:互联网 发布:淘宝五金冠 编辑:程序博客网 时间:2024/05/21 17:34

针对get torrent的feature,社区要求添加相关的unit test。自己摸索完成代码的添加,总结如下:

1、需要生成一个可执行的脚本文件,我的脚本文件名字是unittest_rgw_bencode

2、如何生成这个可执行的脚本文件呢?这个需要在相关的makefile中进行配置

1)首先需要自己写一个unit test的代码,我的单元测试的功能就是比较生成的字符串和预想的字符串是否相等。

我的unit test文件名字为test_rgw_bencode.cc大体如下:

// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-

// vim: ts=8 sw=2 smarttab

#include "gtest/gtest.h"

 

#include "rgw/rgw_torrent.h"

 

TEST(Bencode, String)

{

  TorrentBencode decode;

  bufferlist bl;

 

  decode.bencode("foo", bl);

  decode.bencode("bar", bl);

  decode.bencode("baz", bl);

 

  const char *p = "3:foo3:bar3:baz";

  ASSERT_EQ(0, strcmp(p, bl.c_str()));

}

 

TEST(Bencode, Integers)

{

  TorrentBencode decode;

  bufferlist bl;

 

  decode.bencode(0, bl);

  decode.bencode(-3, bl);

  decode.bencode(7, bl);

 

  const char *p = "i0ei-3ei7e";

  ASSERT_EQ(0, strcmp(p, bl.c_str()));

}

 

TEST(Bencode, Dict)

{

  TorrentBencode decode;  

  bufferlist bl;

 

  decode.bencode_dict(bl);

  decode.bencode("foo", 5, bl);

  decode.bencode("bar", "baz", bl);

  decode.bencode_end(bl);

 

  const char *p = "d3:fooi5e3:bar3:baze";

  ASSERT_EQ(0, strcmp(p, bl.c_str()));

}

 

TEST(Bencode, List)

{

  TorrentBencode decode;

  bufferlist bl;

 

  decode.bencode_list(bl);

  decode.bencode("foo", 5, bl);

  decode.bencode("bar", "baz", bl);

  decode.bencode_end(bl);

 

  const char *p = "l3:fooi5e3:bar3:baze";

  ASSERT_EQ(0, strcmp(p, bl.c_str()));

}

2)将上面的test_rgw_bencode.cc生成对应的可执行文件,这就需要在makefile中配置相关的依赖,这里涉及到两个makefile

/src/test/rgw/CMakeLists.txt 和 /src/test/Makefile-client.am

CMakeLists.txt添加的内容如下:

请注意命名规则,社区要求unit test可执行文件都必须以unittest开头, rgw_a是链接库

#unittest_rgw_bencode
add_executable(unittest_rgw_bencode test_rgw_bencode.cc)
add_ceph_unittest(unittest_rgw_bencode ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest_rgw_bencode)
target_link_libraries(unittest_rgw_bencode rgw_a)

/Makefile-client.am添加的内容如下:

unittest_rgw_bencode_SOURCES = test/rgw/test_rgw_bencode.cc
unittest_rgw_bencode_LDADD = \
    $(LIBRADOS) $(LIBRGW) $(LIBRGW_DEPS) $(CEPH_GLOBAL) \
    $(UNITTEST_LDADD) $(CRYPTO_LIBS) -lcurl -lexpat
unittest_rgw_bencode_CXXFLAGS = $(UNITTEST_CXXFLAGS)
check_TESTPROGRAMS += unittest_rgw_bencode

3、为了使Makefile-client.am在编译的时候生效,需要打开相关开关,因为在/src/test/Makefile.am中有下面的判断

if ENABLE_CLIENT
include test/Makefile-client.am
endif

if ENABLE_SERVER
include test/Makefile-server.am
endif

以上的两个宏在配置文件/src/acconfig.h中添加

/* define if radosgw enabled */

#define WITH_RADOSGW 1

#define ENABLE_CLIENT 1     ------------ 新添加宏


4、完成以上工作后,执行make -j4 check, 会生成相关的可执行文件,然后就开始执行每个可执行文件,这里我看生成了 unittest_rgw_bencode,就暂停make -j4 check

     然后收到执行./unittest_rgw_bencode

 

 

0 0
原创粉丝点击