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