|
6 | 6 | # Look at license.txt for more info. |
7 | 7 | # All rights reserved. |
8 | 8 |
|
9 | | -import base64, json, httpclient, os, strutils |
| 9 | +import base64, json, httpclient, os, strutils, uri |
10 | 10 | import specs |
11 | 11 |
|
12 | 12 | const |
13 | | - ApiRuns = "/_apis/test/runs" |
14 | | - ApiVersion = "?api-version=5.0" |
15 | | - ApiResults = ApiRuns & "/$1/results" |
16 | | - |
17 | | -var runId* = -1 |
| 13 | + RunIdEnv = "TESTAMENT_AZURE_RUN_ID" |
| 14 | + CacheSize = 8 # How many results should be cached before uploading to |
| 15 | + # Azure Pipelines. This prevents throttling that might arise. |
18 | 16 |
|
19 | 17 | proc getAzureEnv(env: string): string = |
20 | 18 | # Conversion rule at: |
21 | 19 | # https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables#set-variables-in-pipeline |
22 | 20 | env.toUpperAscii().replace('.', '_').getEnv |
23 | 21 |
|
24 | | -proc invokeRest(httpMethod: HttpMethod; api: string; body = ""): Response = |
25 | | - let http = newHttpClient() |
26 | | - defer: close http |
27 | | - result = http.request(getAzureEnv("System.TeamFoundationCollectionUri") & |
28 | | - getAzureEnv("System.TeamProjectId") & api & ApiVersion, |
29 | | - httpMethod, |
30 | | - $body, |
31 | | - newHttpHeaders { |
32 | | - "Accept": "application/json", |
33 | | - "Authorization": "Basic " & encode(':' & getAzureEnv("System.AccessToken")), |
34 | | - "Content-Type": "application/json" |
35 | | - }) |
36 | | - if not result.code.is2xx: |
37 | | - raise newException(HttpRequestError, "Server returned: " & result.body) |
38 | | - |
39 | | -proc finish*() {.noconv.} = |
40 | | - if not isAzure or runId < 0: |
41 | | - return |
| 22 | +template getRun(): string = |
| 23 | + ## Get the test run attached to this instance |
| 24 | + getEnv(RunIdEnv) |
42 | 25 |
|
43 | | - try: |
44 | | - discard invokeRest(HttpPatch, |
45 | | - ApiRuns & "/" & $runId, |
46 | | - $ %* { "state": "Completed" }) |
47 | | - except: |
48 | | - stderr.writeLine "##vso[task.logissue type=warning;]Unable to finalize Azure backend" |
49 | | - stderr.writeLine getCurrentExceptionMsg() |
| 26 | +template setRun(id: string) = |
| 27 | + ## Attach a test run to this instance and its future children |
| 28 | + putEnv(RunIdEnv, id) |
50 | 29 |
|
51 | | - runId = -1 |
| 30 | +template delRun() = |
| 31 | + ## Unattach the test run associtated with this instance and its future children |
| 32 | + delEnv(RunIdEnv) |
52 | 33 |
|
53 | | -# TODO: Only obtain a run id if tests are run |
54 | | -# NOTE: We can't delete test runs with Azure's access token |
55 | | -proc start*() = |
56 | | - if not isAzure: |
57 | | - return |
58 | | - try: |
59 | | - if runId < 0: |
60 | | - runId = invokeRest(HttpPost, |
61 | | - ApiRuns, |
62 | | - $ %* { |
63 | | - "automated": true, |
64 | | - "build": { "id": getAzureEnv("Build.BuildId") }, |
65 | | - "buildPlatform": hostCPU, |
66 | | - "controller": "nim-testament", |
67 | | - "name": getAzureEnv("Agent.JobName") |
68 | | - }).body.parseJson["id"].getInt(-1) |
69 | | - except: |
70 | | - stderr.writeLine "##vso[task.logissue type=warning;]Unable to initialize Azure backend" |
71 | | - stderr.writeLine getCurrentExceptionMsg() |
| 34 | +template warning(args: varargs[untyped]) = |
| 35 | + ## Add a warning to the current task |
| 36 | + stderr.writeLine "##vso[task.logissue type=warning;]", args |
| 37 | + |
| 38 | +let |
| 39 | + ownRun = not existsEnv RunIdEnv |
| 40 | + ## Whether the test run is owned by this instance |
| 41 | + accessToken = getAzureEnv("System.AccessToken") |
| 42 | + ## Access token to Azure Pipelines |
| 43 | + |
| 44 | +var |
| 45 | + active = false ## Whether the backend should be activated |
| 46 | + requestBase: Uri ## Base URI for all API requests |
| 47 | + requestHeaders: HttpHeaders ## Headers required for all API requests |
| 48 | + results: JsonNode ## A cache for test results before uploading |
| 49 | + |
| 50 | +proc request(api: string, httpMethod: HttpMethod, body = ""): Response {.inline.} = |
| 51 | + let client = newHttpClient(timeout = 3000) |
| 52 | + defer: close client |
| 53 | + result = client.request($(requestBase / api), httpMethod, body, requestHeaders) |
| 54 | + if result.code != Http200: |
| 55 | + raise newException(CatchableError, "Request failed") |
| 56 | + |
| 57 | +proc init*() = |
| 58 | + ## Initialize the Azure Pipelines backend. |
| 59 | + ## |
| 60 | + ## If an access token is provided and no test run is associated with the |
| 61 | + ## current instance, this proc will create a test run named after the current |
| 62 | + ## Azure Pipelines' job name, then associate it to the current testament |
| 63 | + ## instance and its future children. Should this fail, the backend will be |
| 64 | + ## disabled. |
| 65 | + if isAzure and accessToken.len > 0: |
| 66 | + active = true |
| 67 | + requestBase = parseUri(getAzureEnv("System.TeamFoundationCollectionUri")) / |
| 68 | + getAzureEnv("System.TeamProjectId") / "_apis" ? {"api-version": "5.0"} |
| 69 | + requestHeaders = newHttpHeaders { |
| 70 | + "Accept": "application/json", |
| 71 | + "Authorization": "Basic " & encode(':' & accessToken), |
| 72 | + "Content-Type": "application/json" |
| 73 | + } |
| 74 | + results = newJArray() |
| 75 | + if ownRun: |
| 76 | + try: |
| 77 | + let resp = request( |
| 78 | + "test/runs", |
| 79 | + HttpPost, |
| 80 | + $ %* { |
| 81 | + "automated": true, |
| 82 | + "build": { "id": getAzureEnv("Build.BuildId") }, |
| 83 | + "buildPlatform": hostCPU, |
| 84 | + "controller": "nim-testament", |
| 85 | + "name": getAzureEnv("Agent.JobName") |
| 86 | + } |
| 87 | + ) |
| 88 | + setRun $resp.body.parseJson["id"].getInt |
| 89 | + except: |
| 90 | + warning "Couldn't create test run for Azure Pipelines integration" |
| 91 | + # Set run id to empty to prevent child processes from trying to request |
| 92 | + # for yet another test run id, which wouldn't be shared with other |
| 93 | + # instances. |
| 94 | + setRun "" |
| 95 | + active = false |
| 96 | + elif getRun().len == 0: |
| 97 | + # Disable integration if there aren't any valid test run id |
| 98 | + active = false |
| 99 | + |
| 100 | +proc uploadAndClear() = |
| 101 | + ## Upload test results from cache to Azure Pipelines. Then clear the cache |
| 102 | + ## after. |
| 103 | + if results.len > 0: |
| 104 | + try: |
| 105 | + discard request("test/runs/" & getRun() & "/results", HttpPost, $results) |
| 106 | + except: |
| 107 | + for i in results: |
| 108 | + warning "Couldn't log test result to Azure Pipelines: ", |
| 109 | + i["automatedTestName"], ", outcome: ", i["outcome"] |
| 110 | + results = newJArray() |
| 111 | + |
| 112 | +proc finalize*() {.noconv.} = |
| 113 | + ## Finalize the Azure Pipelines backend. |
| 114 | + ## |
| 115 | + ## If a test run has been associated and is owned by this instance, it will |
| 116 | + ## be marked as complete. |
| 117 | + if active: |
| 118 | + if ownRun: |
| 119 | + uploadAndClear() |
| 120 | + try: |
| 121 | + discard request("test/runs/" & getRun(), HttpPatch, |
| 122 | + $ %* {"state": "Completed"}) |
| 123 | + except: |
| 124 | + warning "Couldn't update test run ", getRun(), " on Azure Pipelines" |
| 125 | + delRun() |
72 | 126 |
|
73 | 127 | proc addTestResult*(name, category: string; durationInMs: int; errorMsg: string; |
74 | 128 | outcome: TResultEnum) = |
75 | | - if not isAzure or runId < 0: |
| 129 | + if not active: |
76 | 130 | return |
| 131 | + |
77 | 132 | let outcome = case outcome |
78 | 133 | of reSuccess: "Passed" |
79 | 134 | of reDisabled, reJoined: "NotExecuted" |
80 | 135 | else: "Failed" |
81 | | - try: |
82 | | - discard invokeRest(HttpPost, |
83 | | - ApiResults % [$runId], |
84 | | - $ %* [{ |
85 | | - "automatedTestName": name, |
86 | | - "automatedTestStorage": category, |
87 | | - "durationInMs": durationInMs, |
88 | | - "errorMessage": errorMsg, |
89 | | - "outcome": outcome, |
90 | | - "testCaseTitle": name |
91 | | - }]) |
92 | | - except: |
93 | | - stderr.writeLine "##vso[task.logissue type=warning;]Unable to log test case: ", |
94 | | - name, ", outcome: ", outcome |
95 | | - stderr.writeLine getCurrentExceptionMsg() |
| 136 | + |
| 137 | + results.add(%* { |
| 138 | + "automatedTestName": name, |
| 139 | + "automatedTestStorage": category, |
| 140 | + "durationInMs": durationInMs, |
| 141 | + "errorMessage": errorMsg, |
| 142 | + "outcome": outcome, |
| 143 | + "testCaseTitle": name |
| 144 | + }) |
| 145 | + |
| 146 | + if results.len > CacheSize: |
| 147 | + uploadAndClear() |
0 commit comments