azure.pm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. use strict;
  26. use warnings;
  27. use POSIX qw(strftime);
  28. sub azure_check_environment {
  29. if(defined $ENV{'AZURE_ACCESS_TOKEN'} && $ENV{'AZURE_ACCESS_TOKEN'} &&
  30. defined $ENV{'AGENT_JOBNAME'} && $ENV{'BUILD_BUILDID'} &&
  31. defined $ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'} &&
  32. defined $ENV{'SYSTEM_TEAMPROJECTID'}) {
  33. return 1;
  34. }
  35. return 0;
  36. }
  37. sub azure_create_test_run {
  38. my ($curl)=@_;
  39. my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
  40. my $azure_run=`$curl --silent --noproxy "*" \\
  41. --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
  42. --header "Content-Type: application/json" \\
  43. --data "
  44. {
  45. 'name': '$ENV{'AGENT_JOBNAME'}',
  46. 'automated': true,
  47. 'build': {'id': '$ENV{'BUILD_BUILDID'}'}
  48. }
  49. " \\
  50. "$azure_baseurl/_apis/test/runs?api-version=5.1"`;
  51. if($azure_run =~ /"id":(\d+)/) {
  52. return $1;
  53. }
  54. return "";
  55. }
  56. sub azure_create_test_result {
  57. my ($curl, $azure_run_id, $testnum, $testname)=@_;
  58. $testname =~ s/\\/\\\\/g;
  59. $testname =~ s/\'/\\\'/g;
  60. $testname =~ s/\"/\\\"/g;
  61. my $title_testnum=sprintf("%04d", $testnum);
  62. my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
  63. my $azure_result=`$curl --silent --noproxy "*" \\
  64. --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
  65. --header "Content-Type: application/json" \\
  66. --data "
  67. [
  68. {
  69. 'build': {'id': '$ENV{'BUILD_BUILDID'}'},
  70. 'testCase': {'id': $testnum},
  71. 'testCaseTitle': '$title_testnum: $testname',
  72. 'testCaseRevision': 2,
  73. 'automatedTestName': 'curl.tests.$testnum',
  74. 'outcome': 'InProgress'
  75. }
  76. ]
  77. " \\
  78. "$azure_baseurl/_apis/test/runs/$azure_run_id/results?api-version=5.1"`;
  79. if($azure_result =~ /\[\{"id":(\d+)/) {
  80. return $1;
  81. }
  82. return "";
  83. }
  84. sub azure_update_test_result {
  85. my ($curl, $azure_run_id, $azure_result_id, $testnum, $error, $start, $stop)=@_;
  86. if(!defined $stop) {
  87. $stop = $start;
  88. }
  89. my $azure_start = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime $start;
  90. my $azure_complete = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime $stop;
  91. my $azure_duration = sprintf("%.0f", ($stop-$start)*1000);
  92. my $azure_outcome;
  93. if($error == 2) {
  94. $azure_outcome = 'NotApplicable';
  95. }
  96. elsif($error < 0) {
  97. $azure_outcome = 'NotExecuted';
  98. }
  99. elsif(!$error) {
  100. $azure_outcome = 'Passed';
  101. }
  102. else {
  103. $azure_outcome = 'Failed';
  104. }
  105. my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
  106. my $azure_result=`$curl --silent --noproxy "*" --request PATCH \\
  107. --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
  108. --header "Content-Type: application/json" \\
  109. --data "
  110. [
  111. {
  112. 'id': $azure_result_id,
  113. 'outcome': '$azure_outcome',
  114. 'startedDate': '$azure_start',
  115. 'completedDate': '$azure_complete',
  116. 'durationInMs': $azure_duration
  117. }
  118. ]
  119. " \\
  120. "$azure_baseurl/_apis/test/runs/$azure_run_id/results?api-version=5.1"`;
  121. if($azure_result =~ /\[\{"id":(\d+)/) {
  122. return $1;
  123. }
  124. return "";
  125. }
  126. sub azure_update_test_run {
  127. my ($curl, $azure_run_id)=@_;
  128. my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
  129. my $azure_run=`$curl --silent --noproxy "*" --request PATCH \\
  130. --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
  131. --header "Content-Type: application/json" \\
  132. --data "
  133. {
  134. 'state': 'Completed'
  135. }
  136. " \\
  137. "$azure_baseurl/_apis/test/runs/$azure_run_id?api-version=5.1"`;
  138. if($azure_run =~ /"id":(\d+)/) {
  139. return $1;
  140. }
  141. return "";
  142. }
  143. 1;