自动部署的REST服务运行状况检查

时间:2016-06-17 15:14:33

标签: rest testing deployment automation

我使用AWS CodeDeploy平台自动部署我的REST服务。部署脚本有很多步骤可以复制/配置/执行其他人员。如果任何步骤失败 - 此服务器的整个部署失败,我收到了关于它的明确通知。因此,我需要的最后一步是运行状况检查 - 验证配置是否合适且所有运行正常。 当然,我可以做一些卷曲POST,解析他们的结果,并在更多的卷曲POST中使用一些提取的值来获得一些理智的覆盖,但所有这些解析听起来像一个轮子发明。 是否有任何方便的测试框架/工具可以轻松地“打包”#34;并在脚本中调用而不在我的每个生产服务器上安装大量的测试工具?

2 个答案:

答案 0 :(得分:0)

鉴于您正在执行REST,您可能可以依赖状态代码而不是解析正文。如果你得到的代码不在2xx中,那就出问题了。

如果您需要更详细的检查,可以添加一个特殊的端点来执行某些数据库查询,并可能向其集成发送一些无害的查询。

最复杂的选择是实现遵循某些工作流程的智能部署后步骤。您需要使用精心设计的bash脚本,或使用更高级的编程语言和框架(如Java中的RestAssured或Groovy中的RestClient)。

不要忘记引入一个带有一些超时的循环来执行运行状况检查,因为在应用程序仍处于部署状态时,您的第一个请求可能会过早发送。

以下是一个简单的bash脚本示例,用于检查应用的状态和版本:

#!/usr/bin/env bash

# Helps to define whether application deployment was successful by checking
# connection to HTTP resource. If the page is loaded and the response is 200
# or 201, then the script finishes successfully. In case connection refused
# is or Gateway Timeout (503) the script is trying to connect again within
# timeout period. Otherwise script finishes with fail.
# Needs required parameter url to application and optional parameters timeout
# (by default equals to 180) and artifact version. If artifact version
# parameter is given and the response is 200 or 201, then script also checks
# that # deployed version (gets from $url/version) equals to the passed
# version. If not, the script finishes with fail. Example of usage in bash 
# script:
# sh post_deployment_test.sh http://blah.com/version 100 1.0.102-20160404.101644-5
# result=$?
#
# If $result value equals to 0, then connection is successfully established,
# otherwise, it is not established.

url=$1
timeout=$2
version=$3

if [ -z "$timeout" ]; then
    timeout=180
fi

counter=0
delay=3
while [ $counter -le $timeout ]; do
    command="curl -L -s -o /dev/null -w %{http_code} $url"
    echo "Executing: $command"
    status_code=$($command)
    curl_code=$?

    # Curl error code CURLE_COULDNT_CONNECT (7) means fail to connect to host or proxy.
    # It occurs, in particular, in case when connection refused.
    if [ $curl_code -ne 0 ] && [ $curl_code -ne 7 ]; then
        echo "Connection is not established"
        exit 1
    fi

    if [ $curl_code = 7 ] || [ $status_code = 503 ]; then
        echo "Connection has not been established yet, because connection refused or service unavailable. Trying to connect again"
        sleep $delay
        let counter=$counter+$delay
        continue
    elif [ $status_code = 200 ] || [ $status_code = 201 ]; then
        if [ -z "$version" ]; then
            echo "Connection is successfully established"
            exit 0
        else
            grep_result=`curl -L -s $url | grep $version`
            if [ -z "$grep_result" ]; then
                echo `curl -L -s $url`
                echo "Deployed version doesn't equal to expected"
                exit 1
            else
                echo "Connection is successfully established"
                exit 0
            fi
        fi
    else
        echo "Connection is not established"
        exit 1
    fi
done

echo "Connection is not established"
exit 1

答案 1 :(得分:0)

我找到了一些不错的东西:jasmine-node作为测试运行时+ frisby.js作为验证脚本工具。 它非常便携(我只是在部署期间运行npm install)并且在脚本方面非常方便,例如(来自frisby的官方示例):

var frisby = require('frisby');
  .get('https://api.twitter.com/1/statuses/user_timeline.json?screen_name=brightbit')
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON('0', {
place: function(val) { expect(val).toMatchOrBeNull("Oklahoma City, OK");  }, // Custom matcher callback
user: {
  verified: false,
  location: "Oklahoma City, OK",
  url: "http://brightb.it"
}
})
 .expectJSONTypes('0', {
  id_str: String,
  retweeted: Boolean,
  in_reply_to_screen_name: function(val) {    expect(val).toBeTypeOrNull(String); }, // Custom matcher callback
user: {
  verified: Boolean,
  location: String,
  url: String
}
})
.toss();