顯示具有 Aws 標籤的文章。 顯示所有文章
顯示具有 Aws 標籤的文章。 顯示所有文章

2023年9月21日 星期四

Postman+Newman+AWS CI/CD Testing


在Postman 測試之前需要了解的東西


  • 使用 Enviroment 來變更 domain 或 測試資料


    {{變數名稱}} 這裡的變數名稱則是Enviroment 或是 global用的變數名稱 postman 會自動幫你帶入


  • 撰寫測試Script


    在 postman 的頁面中,你可以看到Tests 點擊進去就可以寫test script了


  • 匯出相關資訊


匯出 postman 的 collection.json 跟 Environmant.json


pm.test()

為了遵循postman的規範,所有測試程式碼必須寫在 pm.test()的function裡面。第一個引數是該測試的名稱,第二個引數是一個返回 boolean值的函式。也就是說這個函式如果返回true,表示測試通過,否則測試失敗。


pm.expect()

這個斷言函式是基於一個JavaScript測試庫ChaiJS BDD構建的。通過類似的語法,你可以很方便的對響應裡的資料或者一些變數進行斷言。

(expect:預期)


pm.response.to.be.*


這個物件可以提供對響應常見的檢查。如狀態碼是否符合預期等。


tv4.validate()

第一個引數是被檢驗的資料,第二個引數是 json schema。通過則返回true,失敗則返回false。如果要快速學習json的schema規範,可以參考https://www.jianshu.com/p/8278eb2458c4?winzoom=1 

tv4是postman一個內建的json validator。它基於json-schema draft v4,通過豐富的校驗語法來校驗簡單的 值或者複雜的json物件。

常用api詳情請檢視https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference


***如果真的想快速產生Test Code程式碼的話可以用Postman的新功能Postbot。怎麼用Postbot請參考我寫的這一篇文章。

Postman之AI助理 Postbot



使用介面測試


  • 寫完測試CODE,可以發送請求。可以發現Test Results裡面測試結果


  • 使用Runner功能批量執行測試請求



  • 使用命令列進行測試

  • 安裝newman

    1. npm install -g newman

    2. brew install newman


  • newman執行指令


newman run "collection path" -e "environment path"


例:newman run /Users/hello/Documents/PostmanTesting/Postman_testing.postman_collection.json

-e /Users/hello/Documents/PostmanTesting/Demo100_Records.postman_environment.json


官方說明提供幾種格式cli, json, junit, progress, emojitran,並且在使用 -r 後面帶的格式名稱不可以有空白。預設沒有Html報表......


執行過程可以下-r 的時候附加 cli,就可以看到執行過程。


例: 

newman run /Users/hello/Documents/PostmanTesting/Postman_testing_collection.json -r json,cli


下 -e 就可以用環境變數檔

newman run "collection path" -e "environment path"

 例:

newman run /Users/hello/Documents/PostmanTesting/Postman_testing_collection.json -e

/Users/hello/Documents/PostmanTesting/Demo100_Records.postman_environment.json -r json,cli


 產生html報表需安裝套件
陽春版:npm install -g newman-reporter-html
彩色版:npm install -g newman-reporter-htmlextra

           例: (彩色版)

            newman run /Users/hello/Documents/PostmanTesting/Postman_testing_collection.json -e  /Users/hello/Documents/PostmanTesting/Demo100_Records.postman_environment.json -r htmlextra --reporter-htmlextra-export /Users/hello/Documents/PostmanTesting/TestResult/result.html




  1. API測試

    API測試(接口測試)是軟體測試的一種,它包括兩種測試類型:

      • 狹義上指的是直接針對應用程式接口的功能進行的測試;
      • 廣義上指集成測試中,通過調用 API測試整體的功能完成度、可靠性、安全性與性能等指標。

API測試流程


    1. 編寫API測試計畫
    2. 編寫API測試案例
    3. 執行測試
    4. 持續整合API自動化測試


API測試概念圖







自動化測試工具架構與流程(AWS)

        
  1.  利用Postman GUI編寫API與測試項目
  2.  匯出測試項目成JSON檔與環境變數檔
  3.  將匯出測試項目成JSON檔與環境變數檔提交到AWS CodeCommit上去
  4.  建一個Buildspec檔(buildspec.yml)
  5.  建一個S3 桶子來儲存測試報表
  6.  創建一個AWS CodeBuild 專案
  7.  創建一個AWS CodePipeline在CodeCommit有更新時來驅動CodeBuild
  8.  最後的結果是在S3的桶子裡有此次的NewmanTesting報表



 使用AWS CodeBuild、AWS CodePipeline 和 Postman 自動化您的API 測試


參考此篇文章:


https://aws.amazon.com/tw/blogs/devops/automating-your-api-testing-with-a ws-codebuild-aws-codepipeline-and-postman/


Step 1. Fork the Github repository


Step 2. Clone the forked repository

將分支的存儲庫Clone到本地開發環境中


Step 3. Create an Amazon S3 bucket

此存儲桶包含與此項目相關的資源。
Using the AWS CLI: aws s3 mb s3://<UNIQUE_BUCKET_NAME>


Step 4. Edit the buildspec file

  • 將 petstore-api-buildspec.yml 中的文本內的 REPLACE_ME_WITH_UNIQUE_BUCKET_NAME 替換為上面第 3 步中創建的存儲桶名稱。
  • commit更改到github上

Step 5. Store Postman collection and environment files in S3

  1. 導航到 02postman資料夾。對於這個項目,我們包含了一個 Postman 集合文件

    PetStoreAPI.postman_collection.json


  2. 使用 AWS CLI 將 Postman 集合文件保存在 S3 中

    aws s3 cp PetStoreAPI.postman_collection.json \ s3://<REPLACE_ME_WITH_UNIQUE_BUCKET_NAME>/postman-env-files/PetStoreAPI.postman_collection.jso n


  3. 使用 AWS CLI 將 Postman 環境文件保存到 S3 中

    aws s3 cp PetStoreAPIEnvironment.postman_environment.json \ s3://<REPLACE_ME_WITH_UNIQUE_BUCKET_NAME>/postman-env-files/PetStoreAPIEnvironment.postman_env ironment.json


  4. 使用 AWS CLI 將 update-postman-env-file.sh 保存到 S3 中

aws s3 cp update-postman-env-file.sh \

s3://<REPLACE_ME_WITH_UNIQUE_BUCKET_NAME>/postman-env-files/newman/update-postman-env-file.sh


Step 6. Create the PetStore API pipeline

        我們現在創建 AWS CodePipeline PetStoreAPI 管道,它將部署和測試我們的 API。我們使用 AWS CloudFormation 模板 (petstore-api-pipeline.yaml) 來定義管道和所需的階段,
使用 AWS CLI 部署 AWS CloudFormation 模板:
aws cloudformation create-stack --stack-name petstore-api-pipeline \
--template-body file://./petstore-api-pipeline.yaml \
--parameters \
ParameterKey=BucketRoot,ParameterValue=<第3步驟建立的存儲桶名稱(aws-with-postman)> \ ParameterKey=GitHubBranch,ParameterValue=master \ ParameterKey=GitHubRepositoryName,ParameterValue=aws-codepipeline-codebuild-with-postman \ ParameterKey=GitHubToken,ParameterValue=<此處要去github建立要用的token(建立連結)> \ ParameterKey=GitHubUser,ParameterValue=<your github username> \
--capabilities CAPABILITY_NAMED_IAM
此命令創建一個 CodePipeline 管道和所需的階段,以使用 CodeBuild 和 Newman 部署和測試我們的 API。打開 CodePipeline 控制台以觀察您的管道執行並監控不同的階段,



             

執行結果






參考資訊

https://dotblogs.com.tw/im_sqz777/2018/08/16/002431

https://www.itread01.com/content/1545094295.html

https://www.itread01.com/content/1546579836.html

https://www.jishuwen.com/d/2Ecj/zh-tw

https://dev.to/charanrajgolla/json-schema-validation-in-postman-using-external-json-files-1jnc

https://medium.com/cubemail88/newman-%E4%B8%80%E5%80%8B%E8%AE%93-postman-api-testing-%E8%87%AA%E5%8B%95%E5%8C%96%E7%9A%84%E5%A5%BD%E5%B9%AB%E6%89%8B-8e12a6956a25 
(newman 一個讓 postman api testing 自動化的好幫手)

https://json-schema.org/

https://malagege.github.io/blog/2018/11/05/postman%E9%A9%97%E8%AD%89JSON-Schema%E6%96%B9%E6%B3%95/ (postman驗證JSON Schema方法)

https://www.npmjs.com/package/newman-reporter-htmlextra (製作報表) https://www.npmjs.com/package/newman-reporter-html (製作報表)

https://unmesh.me/2017/06/10/api-testing-with-postman-collections-in-aws-c odepipeline/ (API Testing with Postman Collections in AWS CodePipeline )

https://learning.postman.com/docs/running-collections/using-newman-cli/co mmand-line-integration-with-newman/#using-newman-with-cicd (newman 指令)

2017年5月23日 星期二

Laravel5+Docker Windows開發環境安裝

開發環境:WINDOWS 10 ,Ecplise(Neon.3 Release 4.6.3),PHP 5.6,Laravel 5.3
執行環境:WAMP,Docker, AWS EC2,AWS RDS


1.先在windows下載安裝composer(可以參考這裡)

2.接著安裝Laravel5

composer global require "laravel/installer"
安裝完後要記得確定把 ~/.composer/vendor/bin 路徑放置於你的 PATH 或是 開發專案 裡,這樣 laravel 執行檔就會存在於你的系統之中。

3.開啟一個新專案
     3.1 進入命令提示字元( 開始→執行  cmd )
     3.2 進入專案上層路徑( 例如 cd  c:/wamp64/www )
     3.3 輸入 composer create-project laravel/laravel laravel_test 按下enter 系統就會去建立專案所需的檔案。最後看到Application key [base64:~~~~~~~~~~~] set successfully.就表示專案已經建立完成了!

      
     3.4 測試一下local 連結http://localhost/laravel_test/public/ 會出現下圖


     
4.將此開發環境放到Docker裡去
   因為公司之後要上線的環境會以Docker的方式在AWS ECS上執行,所以就先將此專案開發環境變成一個Docker Container。
   4.1 進入Windows PowerShell只要下這個指令:
       docker run -idt --name 專案名稱 -p 8100:80 -v /你的.aws位置/.aws/:/root/.aws/ -v /你的local專案路徑/:/var/www/html/專案名稱/資料卷名稱
如 docker run -idt --name laravel_test -p 8080:80 -v C:\Users\Asus\.aws:/root/.aws/ -v  c:/wamp64/www/laravel_test/:/var/www/html/laravel_test php:5.6.30-apache
就會將此環境對應到docker container裡去。
    
     4.2 測試一下輸入local docker網址:http://localhost:8100/laravel_test/public/ 會出現跟之前一樣的畫面


     
這樣就完成了Laravel5環境的安裝了!!!

2015年11月11日 星期三

Drupal 上傳檔案到AWS S3上的方法(Upload File to AWS S3 From Drupal)

1.先在Drupal 安裝兩個Modules

     1.S3 File System (https://www.drupal.org/project/s3fs)
     2.Libraries API (https://www.drupal.org/project/libraries)

2.安裝AWS SDK for PHP 2.x library

     1.下載AWS SDK for PHP 2.x library的zip檔 (不能下載3.x版本會有問題)
     下載參考網址:https://github.com/aws/aws-sdk-php/releases
     目前是下載Version 2.8.23版的zip檔(aws.zip)

     2.解壓縮aws.zip檔到drupal的sites/all/libraries/awssdk2的路徑下。
          *可以先在local環境解壓縮再上傳。也可以將zip檔上傳後用命令模式(如ssh)去解壓縮(使用tar) 。例如 tar xvf filename.tar

3.設定S3 File System


     1.到「設定(setting)」/ 「媒體(media)」/ S3 File System來設定與AWS S3連線的設定。如圖:


    2.進入S3 File System後先設定 「S3 Bucket Name」與「S3 Region」。然後展開「 顯示AMAZON WEB SERVICES CREDENTIALS」來設定 「Amazon Web Services Access Key」,「Amazon Web Services Secret Key」。如圖:


4.設定drupal內容類型的圖片或是檔案欄位 上載目的地為s3 file system

如下圖所示
















設定完後就可以測試啦!!!

測試結果:





* Drupal版本:7.37