summaryrefslogtreecommitdiff
path: root/tests/azure.pm
blob: 4f64622b0d9f1a84f862e4d7a48c8dfda225037e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
# Copyright (C) 2020, Marc Hoersken, <info@marc-hoersken.de>
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
###########################################################################

use strict;
use warnings;

use POSIX qw(strftime);

sub azure_check_environment {
    if(defined $ENV{'AZURE_ACCESS_TOKEN'} && $ENV{'AZURE_ACCESS_TOKEN'} &&
       defined $ENV{'AGENT_JOBNAME'} && $ENV{'BUILD_BUILDID'} &&
       defined $ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'} &&
       defined $ENV{'SYSTEM_TEAMPROJECTID'}) {
        return 1;
    }
    return 0;
}

sub azure_create_test_run {
    my ($curl)=@_;
    my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
    my $azure_run=`$curl --silent --noproxy "*" \\
    --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
    --header "Content-Type: application/json" \\
    --data "
        {
            'name': '$ENV{'AGENT_JOBNAME'}',
            'automated': true,
            'build': {'id': '$ENV{'BUILD_BUILDID'}'}
        }
    " \\
    "$azure_baseurl/_apis/test/runs?api-version=5.1"`;
    if($azure_run =~ /"id":(\d+)/) {
        return $1;
    }
    return "";
}

sub azure_create_test_result {
    my ($curl, $azure_run_id, $testnum, $testname)=@_;
    $testname =~ s/\\/\\\\/g;
    $testname =~ s/\'/\\\'/g;
    $testname =~ s/\"/\\\"/g;
    my $title_testnum=sprintf("%04d", $testnum);
    my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
    my $azure_result=`$curl --silent --noproxy "*" \\
    --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
    --header "Content-Type: application/json" \\
    --data "
        [
            {
                'build': {'id': '$ENV{'BUILD_BUILDID'}'},
                'testCase': {'id': $testnum},
                'testCaseTitle': '$title_testnum: $testname',
                'testCaseRevision': 2,
                'automatedTestName': 'curl.tests.$testnum',
                'outcome': 'InProgress'
            }
        ]
    " \\
    "$azure_baseurl/_apis/test/runs/$azure_run_id/results?api-version=5.1"`;
    if($azure_result =~ /\[\{"id":(\d+)/) {
        return $1;
    }
    return "";
}

sub azure_update_test_result {
    my ($curl, $azure_run_id, $azure_result_id, $testnum, $error, $start, $stop)=@_;
    if(!defined $stop) {
        $stop = $start;
    }
    my $azure_start = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime $start;
    my $azure_complete = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime $stop;
    my $azure_duration = sprintf("%.0f", ($stop-$start)*1000);
    my $azure_outcome;
    if($error == 2) {
        $azure_outcome = 'NotApplicable';
    }
    elsif($error < 0) {
        $azure_outcome = 'NotExecuted';
    }
    elsif(!$error) {
        $azure_outcome = 'Passed';
    }
    else {
        $azure_outcome = 'Failed';
    }
    my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
    my $azure_result=`$curl --silent --noproxy "*" --request PATCH \\
    --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
    --header "Content-Type: application/json" \\
    --data "
        [
            {
                'id': $azure_result_id,
                'outcome': '$azure_outcome',
                'startedDate': '$azure_start',
                'completedDate': '$azure_complete',
                'durationInMs': $azure_duration
            }
        ]
    " \\
    "$azure_baseurl/_apis/test/runs/$azure_run_id/results?api-version=5.1"`;
    if($azure_result =~ /\[\{"id":(\d+)/) {
        return $1;
    }
    return "";
}

sub azure_update_test_run {
    my ($curl, $azure_run_id)=@_;
    my $azure_baseurl="$ENV{'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI'}$ENV{'SYSTEM_TEAMPROJECTID'}";
    my $azure_run=`$curl --silent --noproxy "*" --request PATCH \\
    --header "Authorization: Bearer $ENV{'AZURE_ACCESS_TOKEN'}" \\
    --header "Content-Type: application/json" \\
    --data "
        {
            'state': 'Completed'
        }
    " \\
    "$azure_baseurl/_apis/test/runs/$azure_run_id?api-version=5.1"`;
    if($azure_run =~ /"id":(\d+)/) {
        return $1;
    }
    return "";
}

1;