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