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