azure.nim 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #
  2. #
  3. # The Nim Tester
  4. # (c) Copyright 2019 Leorize
  5. #
  6. # Look at license.txt for more info.
  7. # All rights reserved.
  8. import base64, json, httpclient, os, strutils
  9. import specs
  10. const
  11. ApiRuns = "/_apis/test/runs"
  12. ApiVersion = "?api-version=5.0"
  13. ApiResults = ApiRuns & "/$1/results"
  14. var runId* = -1
  15. proc getAzureEnv(env: string): string =
  16. # Conversion rule at:
  17. # https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables#set-variables-in-pipeline
  18. env.toUpperAscii().replace('.', '_').getEnv
  19. proc invokeRest(httpMethod: HttpMethod; api: string; body = ""): Response =
  20. let http = newHttpClient()
  21. defer: close http
  22. result = http.request(getAzureEnv("System.TeamFoundationCollectionUri") &
  23. getAzureEnv("System.TeamProjectId") & api & ApiVersion,
  24. httpMethod,
  25. $body,
  26. newHttpHeaders {
  27. "Accept": "application/json",
  28. "Authorization": "Basic " & encode(':' & getAzureEnv("System.AccessToken")),
  29. "Content-Type": "application/json"
  30. })
  31. if not result.code.is2xx:
  32. raise newException(HttpRequestError, "Server returned: " & result.body)
  33. proc finish*() {.noconv.} =
  34. if not isAzure or runId < 0:
  35. return
  36. try:
  37. discard invokeRest(HttpPatch,
  38. ApiRuns & "/" & $runId,
  39. $ %* { "state": "Completed" })
  40. except:
  41. stderr.writeLine "##vso[task.logissue type=warning;]Unable to finalize Azure backend"
  42. stderr.writeLine getCurrentExceptionMsg()
  43. runId = -1
  44. # TODO: Only obtain a run id if tests are run
  45. # NOTE: We can't delete test runs with Azure's access token
  46. proc start*() =
  47. if not isAzure:
  48. return
  49. try:
  50. if runId < 0:
  51. runId = invokeRest(HttpPost,
  52. ApiRuns,
  53. $ %* {
  54. "automated": true,
  55. "build": { "id": getAzureEnv("Build.BuildId") },
  56. "buildPlatform": hostCPU,
  57. "controller": "nim-testament",
  58. "name": getAzureEnv("Agent.JobName")
  59. }).body.parseJson["id"].getInt(-1)
  60. except:
  61. stderr.writeLine "##vso[task.logissue type=warning;]Unable to initialize Azure backend"
  62. stderr.writeLine getCurrentExceptionMsg()
  63. proc addTestResult*(name, category: string; durationInMs: int; errorMsg: string;
  64. outcome: TResultEnum) =
  65. if not isAzure or runId < 0:
  66. return
  67. let outcome = case outcome
  68. of reSuccess: "Passed"
  69. of reDisabled, reJoined: "NotExecuted"
  70. else: "Failed"
  71. try:
  72. discard invokeRest(HttpPost,
  73. ApiResults % [$runId],
  74. $ %* [{
  75. "automatedTestName": name,
  76. "automatedTestStorage": category,
  77. "durationInMs": durationInMs,
  78. "errorMessage": errorMsg,
  79. "outcome": outcome,
  80. "testCaseTitle": name
  81. }])
  82. except:
  83. stderr.writeLine "##vso[task.logissue type=warning;]Unable to log test case: ",
  84. name, ", outcome: ", outcome
  85. stderr.writeLine getCurrentExceptionMsg()