测试工具,使您的测试系统变得悲观
项目描述
什么是Cynic?
如今,几乎任何应用程序都有几个集成点,如数据库、支付网关或它通过HTTP消耗的某些Web服务。
与远程系统的所有通信都通过网络进行,而网络和这些系统经常出现异常。
如果我们不测试系统在远程端操作超出规范并且变得混乱时的行为,唯一的测试场所就是生产环境,而众所周知,对于某些系统来说,这是不可接受的。
由于调用远程系统使用网络,套接字连接可以有不同的失败场景,例如
远程端通过发送TCP RST数据包重置连接
连接可能已建立,但响应永远不会发送回来,连接也不会关闭(如果您在应用程序中没有使用套接字超时,您可能在某个时候会遇到麻烦)。
远程端可以发送垃圾数据作为响应
服务可以在HTTP上发送HTML而不是预期的JSON响应
HTTP服务可以每30秒发送一个字节的响应数据
远程HTTP服务只发送头信息而不发送正文
服务可以发送兆字节的数据而不是预期的千字节
等等。
能够测试我们的应用程序在这些条件发生时的行为会很好。
Cynic试图帮助进行这种测试。基本上,它是一个测试工具(测试替身),可以用来从您的命令行模拟狡猾和诡异的远程系统。
Cynic会尽力伤害您的系统。
它的目标是使您的测试系统变得悲观。
阅读格式化的文档,请访问http://cynic.readthedocs.org
安装
$ [sudo] pip install cynic
或者从git master分支获取最新的版本
$ [sudo] pip install git+https://github.com/rspivak/cynic.git#egg=cynic
快速入门
启动Cynic,它会启动多个服务,分别在不同的端口上
$ cynic INFO [2012-06-03 23:44:35,603] server: Starting 'HTTPHtmlResponse' on port 2000 INFO [2012-06-03 23:44:35,603] server: Starting 'HTTPJsonResponse' on port 2001 INFO [2012-06-03 23:44:35,604] server: Starting 'HTTPNoBodyResponse' on port 2002 INFO [2012-06-03 23:44:35,604] server: Starting 'HTTPSlowResponse' on port 2003 INFO [2012-06-03 23:44:35,604] server: Starting 'RSTResponse' on port 2020 INFO [2012-06-03 23:44:35,604] server: Starting 'RandomDataResponse' on port 2021 INFO [2012-06-03 23:44:35,604] server: Starting 'NoResponse' on port 2022 INFO [2012-06-03 23:44:35,604] server: Starting 'LogRecordHandler' on port /tmp/_cynic.sock
测试不同的服务
连接到端口2020上的服务,立即收到一个TCP RST包,导致命令行上显示“由对方重置连接”的消息
$ curl https://:2020 curl: (56) Recv failure: Connection reset by peer
连接到端口2021上的服务,返回7字节的随机数据
$ telnet localhost 2021 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. #6 Connection closed by foreign host.
连接到端口2001上的服务以获取HTTP JSON响应
$ curl https://:2001 {"message": "Hello, World!"}
这样你就有了基本的概念。将你的应用程序的集成点指向Cynic的服务,看看你的应用程序如何响应。
让我们看看Cynic的帮助选项
$ cynic -h Usage: cynic [options] Test harness to make your system under test cynical Options: -h, --help show this help message and exit -c CONFIG_PATH, --config=CONFIG_PATH Path to an INI configuration file. If no configuration file is provided then default configuration is applied. To see the default configuration use -d option described below. -d, --dump Dump default configuration to STDOUT
如你所见,如果我们不使用-c选项启动Cynic,则使用默认配置,使用-d选项将配置输出到标准输出。
让我们看看默认配置中包含什么
默认配置
$ cynic -d ############################################################ # HTTP protocol specific # ############################################################ [handler:httphtml] # sends simple 'hello world!' HTML page over HTTP as a response # and terminates class = cynic.handlers.httphtml.HTTPHtmlResponse #args = ('/tmp/test.html', ) host = 0.0.0.0 port = 2000 [handler:httpjson] # sends simple 'hello world!' JSON over HTTP as a response # and terminates class = cynic.handlers.httpjson.HTTPJsonResponse #args = ('/tmp/test.json', ) host = 0.0.0.0 port = 2001 [handler:httpnone] # sends headers, but not the response body and terminates class = cynic.handlers.httpnone.HTTPNoBodyResponse host = 0.0.0.0 port = 2002 [handler:httpslow] # sends one byte of the response every 30 seconds. # when the data to be sent is exhausted - terminates class = cynic.handlers.httpslow.HTTPSlowResponse #args = ('/tmp/test.json', 'application/json', 1) host = 0.0.0.0 port = 2003 ############################################################ # Any TCP socket protocol # ############################################################ [handler:reset] # accepts a connection, sends an RST packet right away # and terminates class = cynic.handlers.reset.RSTResponse host = 0.0.0.0 port = 2020 [handler:random] # accepts a connection, sends 7 bytes from the /dev/urandom device # and terminates class = cynic.handlers.rnd.RandomDataResponse host = 0.0.0.0 port = 2021 [handler:noresp] # accepts a connection, but doesn't send any response back. # sleeps for 24 hours and exits class = cynic.handlers.noresp.NoResponse host = 0.0.0.0 port = 2022 ############################################################ # System handlers used internally by the Cynic server # ############################################################ [handler:unixlog] # a logging server that accepts connections over Unix socket # from multiple local processes to output passed log records class = cynic.handlers.log.LogRecordHandler host = /tmp/_cynic.sock port = 0 family = unix
基本上有两种类型的处理器
处理任何TCP套接字协议的处理器
特定的HTTP处理器
让我们更详细地看看其中的一些。
cynic.handlers.httpslow.HTTPSlowResponse
此处理器每30秒发送HTTP响应的一个字节。配置部分如下
[handler:httpslow] # sends one byte of the response every 30 seconds. # when the data to be sent is exhausted - terminates class = cynic.handlers.httpslow.HTTPSlowResponse #args = ('/tmp/test.json', 'application/json', 1) host = 0.0.0.0 port = 2003
其中
class - 处理器类的完全限定的点分Python名称
args - 一个元组,包含要传递给处理器构造函数的Python值。
第一个参数指定了从文件中读取数据的绝对路径,而不是使用默认的字符串“Hello, world!”
第二个参数指定了HTTP响应头中Content-Type的值
第三个参数指定了在向客户端发送额外字节之前,以秒为单位的间隔时间,默认为30
host - 绑定服务的IP地址(对于Unix套接字,是文件路径)
port - 监听的端口(不适用于Unix套接字)
即使仅使用此服务,你也能发挥创意,设计出几个测试场景,让你的测试系统难以承受
在args中指定包含兆字节数据的文件,看看你的系统如何处理如此大的响应
你可以更改文件路径和内容类型参数,以发送HTML、JSON、XML、纯文本等
你可以发送HTML数据,但将Content-Type头值设置为application/json
你可以更改时间间隔来测试你的套接字读取超时过期或不存在的情况。
你可以将所有上述内容作为不同端口上的独立服务。只需将[handler:httpslow1]、[handler:httpslow2]等部分添加到INI文件中,并调整args。
使用自定义处理器扩展Cynic
向Cynic添加自己的处理器非常简单。
要添加新的TCP处理器,从cynic.handlers.base.BaseHandler继承并实现handle方法,该方法直接与TCP套接字交互。
要添加新的HTTP处理器,从cynic.handlers.base.BaseHTTPHandler继承并实现你的自定义do_GET、do_POST、do_PUT等方法。有关处理器的更多信息,请参阅BaseHTTPRequestHandler
在INI配置文件中添加一个[handler:my_new_name]部分,并添加相应的配置参数。
XXX: 完整示例?
致谢
许多想法来自Release It!
许可
版权(c)2012 Ruslan Spivak
在MIT许可下发布,请参阅LICENSE
变更历史
1.0 (2012-06-04)
公共发布
项目详情
cynic-1.0.zip的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b3f2f22036bd5fa2217e46ff5635aaafcc044746216232bc2ff8e58c0dde8a6f |
|
MD5 | 83f70207b6aa2864881e95afea24c71a |
|
BLAKE2b-256 | fce6ff61c7443817f5460c89ed00f85d5a2611ca1f2cdb9352a18aa1621f1b7c |