写点什么

hoverfly 学习笔记

作者:陈磊@Criss
  • 2023-03-13
    北京
  • 本文字数:4644 字

    阅读完需:约 15 分钟

hoverfly 学习笔记

在微服务测试中,我们既需要测试的 provider 服务是否能够正常提供服务,也需要测试 consumer 服务是否能够正常的调用服务,但是如果仅仅是这样这中间缺少了一层被测件并没有被测试,那就是网关层。如果要测试网关就需要运行其后面的生产者服务,也就是 provider 服务。如果 provider 还需要数据持久化层的支持,那么同样需要建设持久化层。从网关、provider 服务、数据持久化层之间还需要网络的连接,这就为测试过程引入了一大堆影响测试结果的因素,这些因素如果出现了不确定的情况导致测试失效就会出发一系列的缺陷流程。在测试失效后,测试工程师会判断是不是被测件也就是网关的缺陷,如果不是就需要确定是误报,那么需要建立技术任务卡,寻求研发帮忙解决对应的问题,从而引起了大量的额外工作,这也是一种极大的浪费。同时,我们要测试网关,需要一系列的额外服务支持,这样也违反了单一职责原则,服务应该只知道如何部署本身即可,不必关心它所依赖的服务。服务虚拟化就是为了解决上述情况而生的。

什么是服务虚拟化

服务虚拟化技术能够用来模拟服务依赖项的行为。它除了可以帮助我们解决外部服务级别依赖所导致一些问题以外,还可以帮助我们测试不受控的服务以及能够解决引起不稳定的外部因素。例如无法与外部服务器通信、外部依赖服务出现了一些问题、公共 API 访问次数限制、公共 APi 访问速度限制等等。

服务虚拟化和之前的 Mock 有什么区别呢

服务虚拟化和常说的 mock 有什么区别呢?mock 乃至 stub 都是为了能够跳过无效的系统服务组件而使用的技术,服务虚拟化是通过环境模拟外部依赖的服务(这个服务是正在修改的、或者暂时不可用的、难以访问和配置的)而支持测试活动。


要是说的直白的,服务虚拟化就是 mock、fake、spy 等的一个集合体,入门简单操作灵活,技术小白都可以很好掌握。

服务虚拟化工具:Hoverfly

Hoverfly 就是服务虚拟化的一种解决方案,它提供了两种模式,一种是回放模式,一种是捕获模式。回放模式就是手工创建服务响应,捕获模式就是真实拦截与服务之间的交互,已被后用。


Hoverfly 除了上面说的两种模式之外,还提供:


  • 监听(Spy)模式::如果模拟数据匹配请求时,则模拟外部 API,否则,把该请求转发给实际的 API。

  • 合成(Synthesize)模式:将请求转发给事先定义好的中间处理服务,中间处理服务负责消费掉对应的请求后直接返回。

  • 修改(modify):中间有一层处理机制,在消息住那发给对应的 provider 之前需要先发给中间处理服务先行处理,然后再发给对应的 provider。对应的请求转发给中间处理服务处理,然后再发给消息的 consumer。

  • 比对差异(diff)模式:请求直接发给真是的 provider,得到 response 后将响应与当前所存储的模拟进行比较,并保存比对结果。

Hoverfly 的应用命令

Hoverfly 有 hoverfly java 和 hoverpy 支持 java 和 python 语言的开发库存,但是对于测试工程师而言,我更加推荐 shell 的命令工具 hoverfly 和 hoverctl,其中 hoverctl 是一个后台驻守进程,方便在 shell 端使用,我们可以通过 hoverctl --help 显示它的帮助文档内容。


hoverctl is the command line tool for Hoverfly
Usage: hoverctl [command]
Available Commands: completion Create Bash completion file for hoverctl config Show hoverctl configuration information delete Delete Hoverfly simulation destination Get and set Hoverfly destination diff Manage the diffs for Hoverfly export Export a simulation from Hoverfly flush Flush the internal cache in Hoverfly import Import a simulation into Hoverfly login Login to Hoverfly logs Get the logs from Hoverfly middleware Get and set Hoverfly middleware mode Get and set the Hoverfly mode simulation Manage the simulation for Hoverfly start Start Hoverfly state Manage the state for Hoverfly status Get the current status of Hoverfly stop Stop Hoverfly targets Get the current targets registered with hoverctl version Get the version of hoverctl
Flags: -f, --force Bypass any confirmation when using hoverctl -h, --help help for hoverctl --set-default Sets the current target as the default target for hoverctl -t, --target string A name for an instance of Hoverfly you are trying to communicate with. Overrides the default target (default) -v, --verbose Verbose logging from hoverctl
Use "hoverctl [command] --help" for more information about a command.
复制代码


除去 hoverctl 以外,hoverfly 就不是一个后端进程了,它通过前台显示调用,如果我们在 shell 端退出了这个命令,那么服务虚拟化的能力也就没有了。hoverfly --help 运行后,显示如下。


Usage of hoverfly:  -add        Add new user '-add -username hfadmin -password hfpass'  -admin        Supply '-admin=false' to make this non admin user (default true)  -ap string        Admin port - run admin interface on another port (i.e. '-ap 1234' to run admin UI on port 1234)  -auth        Enable authentication  -cache-size int        Set the size of request/response cache (default 1000)  -capture        Start Hoverfly in capture mode - transparently intercepts and saves requests/response  -cert string        CA certificate used to sign MITM certificates  -cert-name string        Cert name (default "hoverfly.proxy")  -cert-org string        Organisation name for new cert (default "Hoverfly Authority")  -client-authentication-ca-cert string        Path to the ca cert file used for authentication  -client-authentication-client-cert string        Path to the client certification file used for authentication  -client-authentication-client-key string        Path to the client key file used for authentication  -client-authentication-destination string        Regular expression of destination with client authentication  -cors        Enable CORS support  -db string        Storage to use - 'boltdb' or 'memory' which will not write anything to disk (DEPRECATED) (default "memory")  -db-path string        A path to a BoltDB file with persisted user and token data for authentication (DEPRECATED)  -dest value        Specify which hosts to process (i.e. '-dest fooservice.org -dest barservice.org -dest catservice.org') - other hosts will be ignored will passthrough'  -destination string        Control which URLs Hoverfly should intercept and process, it can be string or regex (default ".")  -dev        Enable CORS headers to allow Hoverfly Admin UI development  -dev-cors-origin string        Custom CORS origin for dev mode (default "http://localhost:4200")  -diff        Start Hoverfly in diff mode - calls real server and compares the actual response with the expected simulation config if present  -disable-cache        Disable the request/response cache (the cache that sits in front of matching)  -generate-ca-cert        Generate CA certificate and private key for MITM  -https-only        Allow only secure secure requests to be proxied by hoverfly  -import value        Import from file or from URL (i.e. '-import my_service.json' or '-import http://mypage.com/service_x.json'  -journal-size int        Set the size of request/response journal (default 1000)  -key string        Private key of the CA used to sign MITM certificates  -listen-on-host string        Specify which network interface to bind to, eg. 0.0.0.0 will bind to all interfaces. By default hoverfly will only bind ports to loopback interface  -log-level string        Set log level (panic, fatal, error, warn, info or debug) (default "info")  -log-no-color        Disable colors for logging  -logs string        Specify format for logs, options are "plaintext" and "json" (default "plaintext")  -logs-file string        Specify log file name for output logs (default "hoverfly.log")  -logs-output value        Specify locations for output logs, options are "console" and "file" (default "console")  -logs-size int        Set the amount of logs to be stored in memory (default 1000)  -metrics        Enable metrics logging to stdout  -middleware string        Set middleware by passing the name of the binary and the path of the middleware script separated by space. (i.e. '-middleware "python script.py"')  -modify        Start Hoverfly in modify mode - applies middleware (required) to both outgoing and incoming HTTP traffic  -no-import-check        Skip duplicate request check when importing simulations  -password string        Password for new user  -password-hash string        Password hash for new user instead of password  -plain-http-tunneling        Use plain http tunneling to host with non-443 port  -pp string        Proxy port - run proxy on another port (i.e. '-pp 9999' to run proxy on port 9999)  -proxy-auth Proxy-Authorization        Switch the Proxy-Authorization header from proxy-auth Proxy-Authorization to header-auth `X-HOVERFLY-AUTHORIZATION`. Switching to header-auth will auto enable -https-only (default "proxy-auth")  -response-body-files-allow-origin value        When a response contains a url in bodyFile, it will be loaded only if the origin is allowed  -response-body-files-path string        When a response contains a relative bodyFile, it will be resolved against this path (default is CWD)  -spy        Start Hoverfly in spy mode, similar to simulate but calls real server when cache miss  -synthesize        Start Hoverfly in synthesize mode (middleware is required)  -tls-verification        Turn on/off tls verification for outgoing requests (will not try to verify certificates) (default true)  -upstream-proxy string        Specify an upstream proxy for hoverfly to route traffic through  -username string        Username for new user  -v    Should every proxy request be logged to stdout  -version        Get the version of hoverfly  -webserver        Start Hoverfly in webserver mode (simulate mode)
复制代码


发布于: 刚刚阅读数: 3
用户头像

陈磊@Criss

关注

测者观天下bugs 2018-03-11 加入

华为云MVP,阿里云MVP

评论

发布
暂无评论
hoverfly 学习笔记_陈磊@Criss_InfoQ写作社区