~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to tests/test_tools/randgen/lib/GenTest/XML/Transporter.pm

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2013-10-29 15:43:40 UTC
  • mfrom: (1.2.12) (2.1.19 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20131029154340-2gp39el6cv8bwf2o
Tags: 1:7.2.3-2ubuntu1
* Merge from debian, remaining changes:
  - Link against boost_system because of boost_thread.
  - Add required libs to message/include.am
  - Add upstart job and adjust init script to be upstart compatible.
  - Disable -floop-parallelize-all due to gcc-4.8/4.9 compiler ICE
    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57732

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2010,2011 Oracle and/or its affiliates. All rights
 
2
# reserved.
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; version 2 of the License.
 
7
#
 
8
# This program is distributed in the hope that it will be useful, but
 
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
11
# General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 
16
# USA
 
17
 
 
18
package GenTest::XML::Transporter;
 
19
 
 
20
require Exporter;
 
21
@ISA = qw(GenTest);
 
22
 
 
23
#@EXPORT = ('XMLTRANSPORT_TYPE_SCP', 'XMLTRANSPORT_TYPE_MYSQL');
 
24
 
 
25
use strict;
 
26
use GenTest;
 
27
use GenTest::Constants;
 
28
use GenTest::Properties;
 
29
 
 
30
 
 
31
use constant XMLTRANSPORT_TYPE              => 0;  # which transport type to use
 
32
use constant XMLTRANSPORT_TYPE_NONE         => 1;  # Noop. Does nothing
 
33
use constant XMLTRANSPORT_TYPE_MYSQL        => 2;  # db connections
 
34
use constant XMLTRANSPORT_TYPE_SCP          => 3;  # secure copy
 
35
use constant XMLTRANSPORT_TYPES             => 4;  # collection of types
 
36
 
 
37
# Defaults:
 
38
use constant XML_DEFAULT_TRANSPORT_TYPE     => XMLTRANSPORT_TYPE_SCP;
 
39
use constant XML_MYSQL_DEFAULT_DSN          => 
 
40
    'dbi:mysql:host=myhost:port=3306:user=xmldrop:password=test;database=test';
 
41
use constant XML_SCP_DEFAULT_USER           => undef;
 
42
use constant XML_SCP_DEFAULT_HOST           => 'regin.norway.sun.com';
 
43
use constant XML_SCP_DEFAULT_DEST_PATH      => '/raid/xml_results/TestTool/xml/';
 
44
 
 
45
1;  # so the require or use succeeds
 
46
 
 
47
#
 
48
# Use this class for transporting XML reports to a given destination.
 
49
#
 
50
# Usage example (using default settings):
 
51
#
 
52
#   use GenTest::XML::Transporter;
 
53
#   my $xml_transporter = GenTest::XML::Transporter->new(
 
54
#       type => undef)
 
55
#   );
 
56
#   my $result = $xml_transporter->sendXML($xmlFileName);
 
57
#   if ($result != STATUS_OK) {
 
58
#       croak("Error from XML Transporter: $result");
 
59
#   }
 
60
#
 
61
#
 
62
sub new {
 
63
        my $class = shift;
 
64
 
 
65
        my $self = $class->SUPER::new({
 
66
        type        => XMLTRANSPORT_TYPE
 
67
        }, @_);
 
68
 
 
69
    # Figure out transport type, which may be set as string value on
 
70
    # command-line, or elsewhere. Use default if not set.
 
71
    if (not defined $self->[XMLTRANSPORT_TYPE]) {
 
72
        $self->[XMLTRANSPORT_TYPE] = XML_DEFAULT_TRANSPORT_TYPE;
 
73
        say('XML Transport: Using default settings');
 
74
    } elsif ($self->[XMLTRANSPORT_TYPE] =~ m{scp}io) {
 
75
        # string match for "scp" (case insensitive)
 
76
        $self->[XMLTRANSPORT_TYPE] = XMLTRANSPORT_TYPE_SCP;
 
77
    } elsif ($self->[XMLTRANSPORT_TYPE] =~ m{mysql}io) {
 
78
        # string match for "mysql" (case insensitive)
 
79
        $self->[XMLTRANSPORT_TYPE] = XMLTRANSPORT_TYPE_MYSQL;
 
80
    } elsif ($self->[XMLTRANSPORT_TYPE] =~ m{none}io) {
 
81
        $self->[XMLTRANSPORT_TYPE] = XMLTRANSPORT_TYPE_NONE;
 
82
    }
 
83
 
 
84
    #${$self}[XMLTRANSPORT_TYPES] = ();
 
85
 
 
86
    return $self;
 
87
}
 
88
 
 
89
 
 
90
#
 
91
# Returns the type of transport mechanism this object represents.
 
92
#
 
93
sub type {
 
94
    my $self = shift;
 
95
    if (defined $self->[XMLTRANSPORT_TYPE]) {
 
96
        return $self->[XMLTRANSPORT_TYPE];
 
97
    } else {
 
98
        return XML_DEFAULT_TRANSPORT_TYPE;
 
99
    }
 
100
}
 
101
 
 
102
#
 
103
# Constructs a default destination for the SCP transport type.
 
104
# Suitable for use in an scp command-line such as:
 
105
#    scp myfile <defaultScpDestination>
 
106
# where <defaultScpDestination> is <user>@<host>:<path>.
 
107
#
 
108
sub defaultScpDestination {
 
109
    my $self = shift;
 
110
    my $dest = XML_SCP_DEFAULT_HOST.':'.XML_SCP_DEFAULT_DEST_PATH;
 
111
    $dest = XML_SCP_DEFAULT_USER.'@'.$dest if defined XML_SCP_DEFAULT_USER;
 
112
    return $dest;
 
113
}
 
114
 
 
115
 
 
116
#
 
117
# Sends XML data to a destination.
 
118
# The transport mechanism to use (e.g. file copy, database insert, ftp, etc.)
 
119
# and destination is determined by the "type" argument to the object's
 
120
# constructor.
 
121
#
 
122
# Arguments:
 
123
#   arg1: xml  - The xml data file name. TODO: Support XML as string?
 
124
#   arg2: dest - Destination for xml report. Defaults are used if omitted.
 
125
#
 
126
sub sendXML {
 
127
    my ($self, $xml, $dest) = @_;
 
128
 
 
129
    if ($self->type == XMLTRANSPORT_TYPE_NONE) {
 
130
        say("XML Transport type: NONE");
 
131
        return STATUS_OK;
 
132
    } elsif ($self->type == XMLTRANSPORT_TYPE_MYSQL) {
 
133
        say("XML Transport type: MySQL database connection");
 
134
        $dest = XML_MYSQL_DEFAULT_DSN if not defined $dest;
 
135
        return $self->mysql($xml, $dest);
 
136
    } elsif ($self->type == XMLTRANSPORT_TYPE_SCP) {
 
137
        say("XML Transport type: SCP");
 
138
        $dest = $self->defaultScpDestination if not defined $dest;
 
139
        return $self->scp($xml, $dest);
 
140
    } else {
 
141
        say("[ERROR] XML transport type '".$self->type."' not supported.");
 
142
        return STATUS_ENVIRONMENT_FAILURE;
 
143
    }
 
144
 
 
145
 
 
146
    
 
147
}
 
148
 
 
149
#
 
150
# Sends the XML contents of file $xml to $dest.
 
151
# If $dest is not defined, a default MySQL dsn will be used.
 
152
#
 
153
# TODO: - Support argument as string (real XML contents) instead of file name.
 
154
#       - Support non-default destination.
 
155
#
 
156
sub mysql() {
 
157
    my ($self, $xml, $dest) = @_;
 
158
 
 
159
    # TODO:
 
160
    # 1. Establish dbh / connect
 
161
    # 2. Execute query
 
162
    # 3. Check for errors
 
163
    # 4. Return appropriate status.
 
164
    say("MySQL XML transport not implemented yet");
 
165
    return STATUS_WONT_HANDLE;
 
166
}
 
167
 
 
168
#
 
169
# Sends the file $xml by SCP (secure file copy) to $dest.
 
170
#
 
171
sub scp {
 
172
    my ($self, $xml, $dest) = @_;
 
173
 
 
174
    # For now, we assume $xml is a file name
 
175
    # TODO: Support XML as string as well? Create temporary file?
 
176
    my $xmlfile = $xml;
 
177
 
 
178
    my $cmd;
 
179
    if (osWindows()) {
 
180
        # We currently support only pscp.exe from Putty on native Windows.
 
181
        #
 
182
        # NOTE: Using pscp without specifying private key (-i <keyfile>)
 
183
        #       requires that Putty's pageant tool is running and set up with
 
184
        #       the correct ssh key on the test host.
 
185
        #       If support for options is needed, add it below.
 
186
        $cmd = 'pscp.exe -q '.$xmlfile.' '.$dest;
 
187
    } else {
 
188
        $cmd = 'scp '.$xmlfile.' '.$dest;
 
189
    }
 
190
 
 
191
    say("SCP command is: ".$cmd);
 
192
 
 
193
    # TODO: The scp command is interactive if keys and hosts are not set up.
 
194
    #       This may cause hangs in automated environments. Find a way to
 
195
    #       always run non-interactively, or kill the command after a timeout.
 
196
    my $result = system($cmd);
 
197
    if ($result != STATUS_OK) {
 
198
        warn('XML Transport: scp failed. Command was: '.$cmd);
 
199
    }
 
200
 
 
201
    return $result >> 8;
 
202
}