2016-06-17 6 views
0

私はRESTサービスの自動展開にAWS CodeDeployプラットフォームを使用しています。デプロイスクリプトには、他のスタッフをコピー/設定/実行するための多くの手順があります。いずれかの手順が失敗した場合、このサーバーの展開全体が失敗し、そのサーバーに関する明確な通知があります。したがって、必要な最後のステップは、ヘルスチェックです。構成が適切であり、すべてが稼動していることの検証です。 原因として、いくつかのカールPOSTを作成し、その結果を解析し、より多くのカールPOST内でいくつかの抽出された値を使用していくつかのサニティカバレッジを得ることができます。 私のプロダクションサーバのそれぞれに巨大なテストをインストールすることなく、簡単に "パック"してスクリプトで呼び出すことができる、納得のいくテストフレームワーク/ツールはありますか?自動展開時のRESTサービスヘルスチェック

答えて

0

私は私が探していた素敵な何かを見つけた:としてfrisby.js +テスト・ランタイムとしてjasmine-nodeを検証スクリプトツール。 それは、スクリプトの両面で本当にポータブル(私は、展開時にインストールNPM実行)と本当に便利です例えば(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(); 
0

あなたがRESTをやっているとすれば、身体を解析するのではなく、おそらく状態コードに頼ることができます。あなたが2xxにないコードを取得した場合、何かが間違っています。

もっと精巧なチェックが必要な場合は、いくつかのDBクエリを実行する特殊なエンドポイントを追加して、無害なクエリをその統合に送信することができます。

最も複雑なオプションは、いくつかのワークフロー手順に従ったスマートな導入後の手順を実装することです。複雑な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 
+0

さて、あなたは「私はドンの場所に正確になるだろう自分のスクリプトに基づいて独自のテストフレームワークを作成する。また、200で十分ではありません:私の最初の呼び出しはいくつかの事前定義されたユーザーとのログインでなければなりません、私は応答の "SessionId"値を取って、次の呼び出しに置く必要があります。 –

関連する問題