azure.pm 4.9 KB

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