View
532
Download
12
Category
Preview:
Citation preview
軟體品質靜態分析 –
SonarQube 實務
授課講師:周彥江
上課日期:2017/04/10
1
課程設計
2
https://insights.sei.cmu.edu/devops/2015/01/continuous-integration-in-devops-1.html
版本控制與自動化持續整合實務3/27, 3/28
軟體變更衝擊分析實務 4/17軟體品質靜態分析-SonarQube實務 4/10
大綱
軟體品質 What & Why & When?
技術債與其管理策略
SonarQube 簡介
靜態程式碼掃描與改善
單元測試與涵蓋度
SonarQube 管理
3
軟體品質 WHAT &
WHY & WHEN?
4
什麼是「軟體品質」?
不易量化的質性指標
Reliability
Efficiency
Security
Maintainability
量化指標
Size
5
Reliability 可靠性
6
Efficiency 效率
7
Security 安全性
8
Maintainability 可維護性
9
http://www.nownews.com/n/2017/03/27/2458686
Size
雖然是量化指標,但明顯影響 Maintainability
不只是程式碼,連 Data Schema 也應納入
10
Measurable vs. Desirable
11
https://en.wikipedia.org/wiki/Software_quality#/media/File:SoftwareQualityCharacteristicAttributeRelationship.png
為何要「軟體品質」?
如果不想在上線期間沒日沒夜
如果不想進入保固後電話不停
如果不想以後被人笑話/抱怨
12
如何提升(確保)軟體品質?
Programmer Training
Code Review
每位 Reviewer 的程度不同
Reviewer 與 Programmer 的交情不同
Reviewer 每天的心情不同
13
人為因素削弱了軟體品質的穩定性
如何更好地提升(確保)軟體品質?
Coding Rule Checklist
Propose by Committee
Check by Reviewer
Feedback by Reviewer / Committee
14
有問題嗎?
人力成本過高
Committee
Reviewer
Programmer
各方角力結果
放棄品質
15
https://tensix.com/2014/10/management-and-the-triple-constraints/
何時應重視軟體品質?
16
Source: IBM Systems Sciences Institute
經驗談:近年來要求零中高資安風險
17
技術債與其管理策略
18
何謂 Technical Debt 技術債?
針對 Maintainability 而來
為了加速開發,捨棄最佳實務,留下 Code
Smells 程式異味。
短期得利,未來需附利息才能償還。
19
技術債象限
20
https://martinfowler.com/bliki/TechnicalDebtQuadrant.html
由成因看技術債管理策略
積極
不容許技術債二次存在
…
僅容許 Prudent + Deliberate
…
中庸
…
僅處理 Reckless + Inadvertent
…
無視技術債
消極
21
積極
消極
由後果看技術債管理策略
積極
不容許技術債二次存在
…
僅容許低風險
…
中庸
…
僅處理高風險
…
無視技術債
消極
22
積極
消極
近似的概念
Remediation Cost 矯正成本
Reliability
Security
23
更多客觀量化指標
Complexity 複雜度
Reliability
Maintainability
Coverage 涵蓋度
Reliability
Duplications 重複度 / Similarity 相似度
Maintainability
24
Efficiency?
不易找到量化指標
與 Reliability 正相關
25
空有策略是不夠的
如何落實?
Assign Owner
定期 Review + Threshold + 自己的 Bug 自己救
…
還是落空?
列入績效評比
不得 Release
…
26
SONARQUBE 簡介
27
SonarQube 網站有兩家
SonarQube.org
Official Site
自行安裝管理主機
SonarQube.com
Continuous Code Quality as a Service
雲端試用觀摩學習
28
SonarQube.org
29
SonarQube.com
30
由 SonarQube 上的 Project 開始
Project
(源自於軟體專案)一群檔案的集合
Rule
針對某種程式語言,判定品質好壞的規則
依性質可分三大類(詳次頁)
Project 會經許多 Rule 許多次的檢驗
是否含某種程式語言的檔案,由附檔名判斷
31
Issues 問題
在某專案的某時點(某次檢驗),可能有零到多個違反 Rule 之處,每處稱為一個 Issue。
依 Rule 所屬不同,有更精確的名稱:
Reliability – Bug
Security – Vulnerability
Maintainability – Code Smell
32
SonarQube 支援的程式語言
33
Dashboard – 跨專案比較
34
跨專案如何比較?
每次評分中,都有很多裁判在評量,最後再由裁判長彙總。
總評僅揭露通過/不通過,決定是否能 Release
,沒有高低分數的差別。
各專案最後一次總評的結果並列,可供比較。
也可僅針對某些評量比較。
35
各種評量有哪些?
Reliability Rating
Security Rating
Maintainability Rating
Coverage
Duplication
Size
36
裁判長 Quality Gates
裁判長不負實質評判之責,僅決定是否採計某裁判的評量,並忠實彙總。
採計何者的決策應保長期穩定。
為保持多裁判長以適用於不同類型專案的彈性,可為各裁判長命名以便識別。
預設唯一的裁判長名為 SonarQube way。
37
SonarQube way 採計四項 Conditions
38
彙總判定標準
39
Condition 的組成
40
Measure 評量
Issue
Reliability
Security
Maintainability
Complexity
Duplication
Documentation
Test
41
https://docs.sonarqube.org/display/SONAR/Metric+Definitions
Project Overview
42
各類 Rule 的精確度要求與估值
Violation False Positive Estimation
Reliability Bug Not Expected Remediation Cost
Security Vulnerability Not Expected Remediation Cost
Maintainability Code Smell Acceptable Technical Debt
43
矯正成本 Remediation Cost
以下三選一
Constant/issue
a = 0, y = b
Linear
隨 Issue 所在檔案大小線性變化
a > 0, b = 0, y = ax
Linear with offset
線性變化+偏移量
a > 0, b != 0, y = ax + b
Technical Debt 亦共用此算法
利率為 0%
44
技術債比率 Technical Debt Ratio, TDR
技術債比率 =
矯正成本 / 開發成本
開發成本 =
開發一行程式碼的時間 * 程式碼行數
開發一行程式碼的時間 =
0.06 日 (in SonarQube)
45
Rule Default Severity 嚴重度
錢要花在刀口上,刀口在這找…
46
Rating 分級
A B C D E
Reliability Bug = 0 Minor > 0 Major > 0 Critical > 0 Blocker > 0
Security Vulnerability
= 0
Minor > 0 Major > 0 Critical > 0 Blocker > 0
Maintaina-
bility
TDR <= 5% TDR <= 10% TDR <= 20% TDR <= 50% TDR > 50%
47
Leak 新問題
常見三種選擇
Diff(Latest, Previous Version)
與 Sprint 切齊
固定日期(Milestone)
48
Complexity
49
Duplication
50
共用環境上專案太多?左右護法來也
51
左有「我的最愛」
右有「搜尋條件」
靜態程式碼掃描與改善
52
靜態程式碼掃描與改善
專案原始碼
掃描
Console Mode
IDE
Continuous Integration
目標
改善
53
專案原始碼
dynaTrace easyTravel src 2.0.0.2542
Unzip to easytravel-src
54
dynaTrace easyTravel src 2.0.0.2542
https://community.dynatrace.com/community/display/DL/Demo
+Applications+-+easyTravel#DemoApplications-easyTravel-
Download(Installer+License)
https://s3.amazonaws.com/easytravel/dT65/dynatrace-
easytravel-src-2.0.0.2542.zip
55
easyTravel architecture
56
easyTravel - Architecture and Deployments.pptx
Console Mode
Setup
Java 8
SonarQube 6.x
Sonar Scanner
dynaTrace easyTravel src 2.0.0.2542
Scan
Result
57
Java 8
Download
https://java.com/download
Install
Set Path
C:\Program Files\Java\jdk1.8.0_111\bin
Verify
58
SonarQube 6.x
Download
https://www.sonarqube.org
Unzip
Run
Verify
Console
Browser
59
Run SonarQube
60
也可以變身「服務」
61
Verify SonarQube (Console)
62
Verify SonarQube (Browser)
http://localhost:9000
63
Sonar Scanner
Download
http://docs.sonarqube.org/display/SCAN/Analyzin
g+with+SonarQube+Scanner
Unzip to ScannerHome
Set Path
ScannerHome\bin
Verify
64
Unzip Sonar Scanner
65
Set Path for Sonar Scanner
66
Verify Sonar Scanner
67
Unzip & Scan
cd to easytravel-src\BusinessBackend
Scan arguments
-Dsonar.projectKey=BusinessBackend
-Dsonar.sources=.
68
Scan
69
Scan Successful
70
也試試 .NET Project
cd to easytravel-src\dotNET45
Scan arguments
-Dsonar.projectKey=dotNET45
-Dsonar.sources=.
71
比較不順利
72
.NET Framework 4.5.2
https://www.microsoft.com/en-
us/download/details.aspx?id=42643
僅在掃描 .NET 程式時需要
73
Result
74
版本未提供,長遠來看不太理想
75
登入 admin / admin 可事後修改
76
也可在掃描前指定
-Dsonar.projectVersion=2
77
與前次比較,有版號才清楚
78
IDE
Java + Spring Tool Suite / Eclipse
Download / Install SonarLint Plugin
Add SonarLint View
Scan On-The-Fly
Visual Studio Community
Download / Install SonarLint for Visual Studio
Add SonarLint View
Scan On-The-Fly
Calculate Code Metrics for Solution
79
Download / Install SonarLint Plugin
80
Add SonarLint View – SonarLint On-The-
Fly
81
程式右側標記與程式 Issues 清單
82
Download / Install SonarLint for Visual
Studio
83
Add SonarLint View – Code Metrics
Results
84
程式右側標記與方案 Issues 清單
85
IDE Scan 可為輔助,但不足夠
各開發人員的版本並不保證一致
各開發人員的自律並不保證一致
掃描僅限於 RuleSet (Quality Profile) 而不包含Duplication、Complexity、Coverage 等。
專案團隊無法掌握全貌
86
Continuous Integration
Setup
SonarQube 6.x
GitLab
dynaTrace easyTravel src 2.0.0.2542
Jenkins
BusinessBackend
dotNET45
Scan
View
87
Why CI? 實務上較完整的架構
88
https://lasithapetthawadu.wordpress.com/2014/05/03/configure-jenkins-with-sonarqube-for-static-code-analysis-and-integration/
Push source to GitLab
89
原始碼已上 GitLab
90
Jenkins New Project
Login
Create
Naming & Selection
Get Source Code from Git
Execute SonarQube Scanner
Scanning Details
91
Login Jenkins
帳密 root / password
92
Create Jenkins Project
93
Naming & Selection
94
Get Source Code from Git
95
Execute SonarQube Scanner
96
Scanning Details
97
Scan – Jenkins Build Now
98
View - Jenkins SonarQube 專案名、版號一致
99
一行 C# 程式都找不到?
100
Log 也沒什麼頭緒
101
-X 進入 Debug
102
終於找到線索
103
專案外移到 Windows Jenkins Node
104
似曾相識的錯誤訊息
105
Windows Jenkins Node + .NET 4.5.2 即可
106
偶爾可能面臨系統資源窘迫
107
在 SonarQube 處理
108
在 Jenkins 處理
109
總結 CI Static Code Scan 系統需求
GitLab
SonarQube
Jenkins
Java 8
GitLab Plugin
SonarQube Scanner Plugin
Windows +.NET Framework 4.5.2*
110
目標
幾個範例(可複選)
Quailty Gate: Pass
Leak Issues = 0
Bug = 0, Reliability Rating: A
Vulnerability = 0, Security Rating: A
Maintainability Rating: A
Blocker + Critical Issues = 0
Complexity / File < 20
Duplication < 10%
Coverage > 80%
111
改善
Issue
Duplication
Complexity
Coverage
112
未登入也可針對問題找參考解答
113
不一定要改程式
114
https://docs.sonarqube.org/display/SONAR/Issue+Lifecycle
Resolved 還可分三種
115
針對誤判或 won’t fix 提出說明
116
Won’t fix 可能是大宗
如果第三方元件有 Issues,要修嗎?
如果要修,要貢獻回去嗎? 如果貢獻回去,要等下次釋出嗎?
sonar.exclusions=**/jquery*.js,**/modernizr.js
117
看例子比較容易理解
118
若改程式就要驗證
驗證「藥效」
透過 SonarQube 重新 Scan -> Issues 數量減少
驗證軟體專案仍能建置
透過 Build Tool (Jenkins / Ant / Visual Studio…)
驗證原本功能仍在
透過單元測試
驗證其他功能未受破壞
透過單元測試
119
Refactoring 重構
To improve the design and quality of existing
source code without changing its external
behavior ~by Martin Fowler
Not a “value added” activity
透過 Unit test 來確保不會改變原先的行為
既然如此,為什麼要 refactoring?
需求會改變,程式必須跟著改
程式在一直改變的過程中品質也會跟著下降(如果沒有 refactor)
http://www.designsmells.com/resources
120
重複程式碼的改善
Refactoring methods
Extract method:將重複的程式碼抽離成獨立方法
Extract superclass:將多個相同性質的類別共通方法抽離成一個父類別
Extract class:將多個性質不同的類別共通方法抽離出一個獨立的共用類別 (utility class),透過委託(delegate)方式使用被前述類別使用
參考書
Martin Fowler, Refactoring: Improving the Design
of Existing Code, 1999
121
Cyclomatic Complexity
呈現程式判斷分支與迴圈的多寡,越高表示複雜度越高
122
Cyclomatic Complexity 指標
判斷分支、迴圈、Throw、提早的 return 等敘述會增加 complexity
範例
資料來源: G. Campbell and P. Papapetrou, SonarQube in Action, 2013
程式複雜度的改善
Refactoring-Extract Method
範例
124
資料來源: G. Campbell and P. Papapetrou, SonarQube in Action, 2013
單元測試與涵蓋度
125
單元測試與涵蓋度
正式程式
撰寫 -> 編譯 -> 產出 -> 執行
單元測試程式
撰寫 -> 編譯 -> 產出 -> 合併正式程式產出 -> 測試
涵蓋度
修改編譯腳本
植入涵蓋度計算工具
結合 SonarQube
測試
126
正式程式 3-1(JDK 與版控位址)
127
正式程式 3-2(稀疏簽出)
128
正式程式 3-3(編譯選項)
129
正式程式通過編譯
130
單元測試撰寫前先另開分支
131
單元測試程式
132
單元測試編譯腳本
133
改抓 Test Branch
134
改跑新的 test Target
135
測試通過
136
副產品:單元測試報表 – 設定
137
副產品:單元測試報表 – 呈現
138
為涵蓋度修改編譯腳本前先另開分支
139
修改編譯腳本
140
改抓 Coverage Branch
141
涵蓋度計算工具 JaCoCo
142
植入涵蓋度計算工具
143
正規做法(需要 Jenkins 主機帳戶權限)
下載 JaCoCo、解壓
將 jacocoant.jar 複製到 Ant lib 目錄下
取巧做法(需要 Jenkins 專案組態權限)
下載 JaCoCo、解壓
將 jacocoant.jar 上雲端硬碟
取得雲端硬碟的「直接下載」連結
在 Jenkins Invoke Ant 之前,檢查工具目錄,若無jacocoant.jar 則下載補上。
植入涵蓋度計算工具 – 取巧做法
144
結合 SonarQube
145
測試通過,涵蓋率破蛋
146
挑戰提高涵蓋率
147
在 Jenkins 計算 .NET 涵蓋率需要什麼?
程式
easyTravel dotNET45
已含測試程式
工具
Jenkins (node) on Windows
MSBuild Plugin
SonarQube Scanner for MSBuild
MSTest Plugin*
Visual Studio Community
OpenCover
148
MSBuild Plugin
149
SonarQube Scanner for MSBuild
150
OpenCover
151
Jenkins 上的專案組態設定
Code Base
SonarQube + MSBuild 四部曲
副產品:單元測試報表
152
Code Base
153
SonarQube + MSBuild 四部曲
154
副產品:單元測試報表
155
日
SONARQUBE 管理
158
Administration
SonarQube
Add Supported Language
Programming / Natural Language
Change Rule
Change Quality Profile
Change Quality Gate
申訴機制
159
Admin Login
160
Add Supported Language
161
列出現有 Rules(左側可再篩選)
162
Rule 可改 Severity、Description、Activate
163
Quality Profile 規則集
164
Quality Gate
165
申訴機制
Code Review Committee
Won’t Fix
False Positive
166
Recommended