azure.pm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. # Copyright (C) Marc Hoersken, <info@marc-hoersken.de>
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. # SPDX-License-Identifier: curl
  23. #
  24. ###########################################################################
  25. package azure;
  26. use strict;
  27. use warnings;
  28. BEGIN {
  29. use base qw(Exporter);
  30. our @EXPORT = qw(
  31. azure_check_environment
  32. azure_create_test_run
  33. azure_create_test_result
  34. azure_update_test_result
  35. azure_update_test_run
  36. );
  37. }
  38. use POSIX qw(strftime);
  39. sub azure_check_environment {
  40. if(defined $ENV{'AZURE_ACCESS_TOKEN'} && $ENV{'AZURE_ACCESS_TOKEN'} &&
  41. defined $ENV{'AGENT_JOBNAME'} && $ENV{'BUILD_BUILDID'} &&
  42. defined $ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'} &&
  43. defined $ENV{'SYSTEM_TEAMPROJECTID'}) {
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. sub azure_create_test_run {
  49. my ($curl)=@_;
  50. my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
  51. my $azure_run=`$curl --silent --noproxy "*" \\
  52. --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
  53. --header "Content-Type: application/json" \\
  54. --data "
  55. {
  56. 'name': '$ENV{'AGENT_JOBNAME'}',
  57. 'automated': true,
  58. 'build': {'id': '$ENV{'BUILD_BUILDID'}'}
  59. }
  60. " \\
  61. "$azure_baseurl/_apis/test/runs?api-version=5.1"`;
  62. if($azure_run =~ /"id":(\d+)/) {
  63. return $1;
  64. }
  65. return "";
  66. }
  67. sub azure_create_test_result {
  68. my ($curl, $azure_run_id, $testnum, $testname)=@_;
  69. $testname =~ s/\\/\\\\/g;
  70. $testname =~ s/\"/\\\"/g;
  71. $testname =~ s/\'/'"'"'/g;
  72. my $title_testnum=sprintf("%04d", $testnum);
  73. my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
  74. my $azure_result=`$curl --silent --noproxy '*' \\
  75. --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
  76. --header 'Content-Type: application/json' \\
  77. --data '
  78. [
  79. {
  80. "build": {"id": "$ENV{'BUILD_BUILDID'}"},
  81. "testCase": {"id": $testnum},
  82. "testCaseTitle": "$title_testnum: $testname",
  83. "testCaseRevision": 2,
  84. "automatedTestName": "curl.tests.$testnum",
  85. "outcome": "InProgress"
  86. }
  87. ]
  88. ' \\
  89. '$azure_baseurl/_apis/test/runs/$azure_run_id/results?api-version=5.1'`;
  90. if($azure_result =~ /\[\{"id":(\d+)/) {
  91. return $1;
  92. }
  93. return "";
  94. }
  95. sub azure_update_test_result {
  96. my ($curl, $azure_run_id, $azure_result_id, $testnum, $error, $start, $stop)=@_;
  97. if(!defined $stop) {
  98. $stop = $start;
  99. }
  100. my $azure_start = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime $start;
  101. my $azure_complete = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime $stop;
  102. my $azure_duration = sprintf("%.0f", ($stop-$start)*1000);
  103. my $azure_outcome;
  104. if($error == 2) {
  105. $azure_outcome = 'NotApplicable';
  106. }
  107. elsif($error < 0) {
  108. $azure_outcome = 'NotExecuted';
  109. }
  110. elsif(!$error) {
  111. $azure_outcome = 'Passed';
  112. }
  113. else {
  114. $azure_outcome = 'Failed';
  115. }
  116. my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
  117. my $azure_result=`$curl --silent --noproxy '*' --request PATCH \\
  118. --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
  119. --header "Content-Type: application/json" \\
  120. --data '
  121. [
  122. {
  123. "id": $azure_result_id,
  124. "outcome": "$azure_outcome",
  125. "startedDate": "$azure_start",
  126. "completedDate": "$azure_complete",
  127. "durationInMs": $azure_duration
  128. }
  129. ]
  130. ' \\
  131. '$azure_baseurl/_apis/test/runs/$azure_run_id/results?api-version=5.1'`;
  132. if($azure_result =~ /\[\{"id":(\d+)/) {
  133. return $1;
  134. }
  135. return "";
  136. }
  137. sub azure_update_test_run {
  138. my ($curl, $azure_run_id)=@_;
  139. my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
  140. my $azure_run=`$curl --silent --noproxy '*' --request PATCH \\
  141. --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
  142. --header 'Content-Type: application/json' \\
  143. --data '
  144. {
  145. "state": "Completed"
  146. }
  147. ' \\
  148. '$azure_baseurl/_apis/test/runs/$azure_run_id?api-version=5.1'`;
  149. if($azure_run =~ /"id":(\d+)/) {
  150. return $1;
  151. }
  152. return "";
  153. }
  154. 1;