azure.pm 4.8 KB

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