System test for Yangtools
[integration/test.git] / csit / libraries / NexusKeywords.robot
1 *** Settings ***
2 Documentation     Nexus repository access keywords, and supporting Java and Maven handling.
3 ...
4 ...               Copyright (c) 2015,2016 Cisco Systems, Inc. and others. All rights reserved.
5 ...
6 ...               This program and the accompanying materials are made available under the
7 ...               terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 ...               and is available at http://www.eclipse.org/legal/epl-v10.html
9 ...
10 ...
11 ...               This library encapsulates a bunch of somewhat complex and commonly used
12 ...               Nexus operations into reusable keywords to make writing test suites easier.
13 ...
14 ...               Currently, Java version detection is incorporated so that Java tools can be run reliably.
15 ...               Also, suport for installing and running Maven is added, as that needs the Java detection.
16 ...               TODO: Move Java detection and Maven to a separate Resource, or rename this Resource.
17 Library           OperatingSystem
18 Library           SSHLibrary
19 Library           String
20 Resource          ${CURDIR}/SSHKeywords.robot
21 Resource          ${CURDIR}/Utils.robot
22
23 *** Variables ***
24 ${JDKVERSION}     None
25 ${JAVA_7_HOME_CENTOS}    /usr/lib/jvm/java-1.7.0
26 ${JAVA_7_HOME_UBUNTU}    /usr/lib/jvm/java-7-openjdk-amd64
27 ${JAVA_8_HOME_CENTOS}    /usr/lib/jvm/java-1.8.0
28 ${JAVA_8_HOME_UBUNTU}    /usr/lib/jvm/java-8-openjdk-amd64
29 ${JAVA_OPTIONS}    -Xmx2560m    # Note that '-Xmx=3g' is wrong syntax. Also 3GB heap may not fit in 4GB RAM.
30 ${JAVA_7_OPTIONS}    -Xmx2048m -XX:MaxPermSize=512m
31 ${MAVEN_DEFAULT_OUTPUT_FILENAME}    default_maven.log
32 ${MAVEN_OPTIONS}    -Pq -Djenkins
33 ${MAVEN_REPOSITORY_PATH}    /tmp/r
34 ${MAVEN_SETTINGS_URL}    https://raw.githubusercontent.com/opendaylight/odlparent/master/settings.xml
35 ${MAVEN_VERSION}    3.3.9
36 ${NEXUS_FALLBACK_URL}    ${NEXUSURL_PREFIX}/content/repositories/opendaylight.snapshot
37
38 *** Keywords ***
39 Initialize_Artifact_Deployment_And_Usage
40     [Arguments]    ${tools_system_connect}=True
41     [Documentation]    Places search utility to ODL system, which will be needed for version detection.
42     ...    By default also initialize a SSH connection to Tools system,
43     ...    as following Keywords assume a working connection towards target system.
44     # Connect to the ODL machine.
45     ${odl} =    SSHKeywords.Open_Connection_To_ODL_System
46     # Deploy the search tool.
47     SSHLibrary.Put_File    ${CURDIR}/../../tools/deployment/search.sh
48     SSHLibrary.Close_Connection
49     # Optionally connect to the Tools System machine.
50     BuiltIn.Return_From_Keyword_If    not (${tools_system_connect})    # the argument may be a convoluted Python expression
51     SSHKeywords.Open_Connection_To_Tools_System
52
53 NexusKeywords__Get_Items_To_Look_At
54     [Arguments]    ${component}
55     [Documentation]    Get a list of items that might contain the version number that we are looking for.
56     BuiltIn.Return_From_Keyword_If    '${component}' == 'bgpcep'    pcep-impl
57     BuiltIn.Return_From_Keyword_If    '${component}' == 'yangtools'    yang-data-impl
58     [Return]    ${component}-impl
59
60 NexusKeywords__Detect_Version_To_Pull
61     [Arguments]    ${component}
62     [Documentation]    Determine the exact Nexus directory to be used as a source for a particular test tool
63     ...    Figure out what version of the tool needs to be pulled out of the
64     ...    Nexus by looking at the version directory of the subsystem from
65     ...    which the tool is being pulled. This code is REALLY UGLY but there
66     ...    is no way around it until the bug
67     ...    https://bugs.opendaylight.org/show_bug.cgi?id=5206 gets fixed.
68     ...    I also don't want to depend on maven-metadata-local.xml and other
69     ...    bits and pieces of ODL distribution which are not required for ODL
70     ...    to function properly.
71     ${itemlist} =    NexusKeywords__Get_Items_To_Look_At    ${component}
72     ${current_ssh_connection} =    SSHLibrary.Get Connection
73     SSHKeywords.Open_Connection_To_ODL_System
74     ${version}    ${result} =    SSHLibrary.Execute_Command    sh search.sh ${WORKSPACE}/${BUNDLEFOLDER}/system ${itemlist}    return_rc=True
75     SSHLibrary.Close_Connection
76     SSHKeywords.Restore Current SSH Connection From Index    ${current_ssh_connection.index}
77     BuiltIn.Log    ${version}
78     BuiltIn.Run_Keyword_If    ${result}!=0    BuiltIn.Fail    Component "${component}" not found, cannot locate test tool
79     ${version}    ${location} =    String.Split_String    ${version}    max_split=1
80     [Return]    ${version}    ${location}
81
82 Deploy_From_Url
83     [Arguments]    ${url}
84     [Documentation]    On active SSH conenction execute download ${url} command, log output, check RC and return file name.
85     ${filename} =    String.Fetch_From_Right    ${url}    /
86     ${response}    ${result} =    SSHLibrary.Execute_Command    wget -q -N '${url}' 2>&1    return_rc=True
87     BuiltIn.Log    ${response}
88     BuiltIn.Run_Keyword_If    ${result} != 0    BuiltIn.Fail    File ${filename} could not be downloaded from ${url}
89     [Return]    ${filename}
90
91 Deploy_Artifact
92     [Arguments]    ${component}    ${artifact}    ${name_prefix}    ${name_suffix}=-executable.jar    ${fallback_url}=${NEXUS_FALLBACK_URL}    ${explicit_url}=${EMPTY}
93     [Documentation]    Deploy the specified artifact from Nexus to the cwd of the machine to which the active SSHLibrary connection points.
94     ...    Must have ${BUNDLE_URL} variable set to the URL from which the
95     ...    tested ODL distribution was downloaded and this place must be
96     ...    inside a repository created by a standard distribution
97     ...    construction job. If this is detected to ne be the case, fallback URL is used.
98     ...    If ${explicit_url} is non-empty, Deploy_From_Utrl is called instead.
99     BuiltIn.Run_Keyword_And_Return_If    """${explicit_url}""" != ""    Deploy_From_Url    ${explicit_url}
100     ${urlbase} =    String.Fetch_From_Left    ${BUNDLE_URL}    /org/opendaylight
101     # If the BUNDLE_URL points somewhere else (perhaps *patch-test* job in Jenkins),
102     # ${urlbase} is the whole ${BUNDLE_URL}, in which case we use the ${fallback_url}
103     ${urlbase} =    BuiltIn.Set_Variable_If    '${urlbase}' != '${BUNDLE_URL}'    ${urlbase}    ${fallback_url}
104     ${version}    ${location} =    NexusKeywords__Detect_Version_To_Pull    ${component}
105     # TODO: Use RequestsLibrary and String instead of curl and bash utilities?
106     ${url} =    BuiltIn.Set_Variable    ${urlbase}/${location}/${artifact}/${version}
107     ${metadata} =    SSHKeywords.Execute_Command_Should_Pass    curl -L ${url}/maven-metadata.xml
108     ${namepart} =    SSHKeywords.Execute_Command_Should_Pass    echo "${metadata}" | grep value | head -n 1 | cut -d '>' -f 2 | cut -d '<' -f 1    stderr_must_be_empty=${True}
109     ${length} =    BuiltIn.Get_Length    ${namepart}
110     ${namepart} =    BuiltIn.Set_Variable_If    ${length} == 0    ${version}    ${namepart}
111     ${filename} =    BuiltIn.Set_Variable    ${name_prefix}${namepart}${name_suffix}
112     BuiltIn.Log    ${filename}
113     ${url} =    BuiltIn.Set_Variable    ${url}/${filename}
114     ${response}    ${result} =    SSHLibrary.Execute_Command    wget -q -N '${url}' 2>&1    return_rc=True
115     BuiltIn.Log    ${response}
116     BuiltIn.Run_Keyword_If    ${result} != 0    BuiltIn.Fail    Artifact "${artifact}" in component "${component}" could not be downloaded from ${url}
117     [Return]    ${filename}
118
119 Deploy_Test_Tool
120     [Arguments]    ${component}    ${artifact}    ${suffix}=executable    ${fallback_url}=${NEXUS_FALLBACK_URL}    ${explicit_url}=${EMPTY}
121     [Documentation]    Deploy a test tool.
122     ...    The test tools have naming convention of the form
123     ...    "<repository_url>/some/dir/somewhere/<tool-name>/<tool-name>-<version-tag>-${suffix}.jar"
124     ...    where "<tool-name>" is the name of the tool and "<version-tag>" is
125     ...    the version tag that is digged out of the maven metadata. This
126     ...    keyword calculates ${name_prefix} and ${name_suffix} for
127     ...    "Deploy_Artifact" and then calls "Deploy_Artifact" to do the real
128     ...    work of deploying the artifact.
129     ${name_prefix} =    BuiltIn.Set_Variable    ${artifact}-
130     ${name_suffix} =    BuiltIn.Set_Variable_If    "${suffix}" != ""    -${suffix}.jar    .jar
131     ${filename} =    Deploy_Artifact    ${component}    ${artifact}    ${name_prefix}    ${name_suffix}    ${fallback_url}
132     ...    ${explicit_url}
133     [Return]    ${filename}
134
135 Install_And_Start_Java_Artifact
136     [Arguments]    ${component}    ${artifact}    ${suffix}=executable    ${tool_options}=${EMPTY}    ${java_options}=${EMPTY}    ${openjdk}=${JDKVERSION}
137     ...    ${fallback_url}=${NEXUS_FALLBACK_URL}    ${explicit_url}=${EMPTY}
138     [Documentation]    Deploy the artifact, assign name for log file, figure out java command, write the command to active SSH connection and return the log name.
139     ...    This keyword does not examine whether the artifact was started successfully or whether is still running upon return.
140     # TODO: Unify this keyword with what NexusKeywords.Install_And_Start_Testtool does.
141     ${default_java_options} =    Default_Java_Options    ${openjdk}
142     ${actual_java_options} =    BuiltIn.Set_Variable_If    """${java_options}""" != ""    ${java_options}    ${default_java_options}
143     ${filename} =    Deploy_Test_Tool    ${component}    ${artifact}    ${suffix}    ${fallback_url}    ${explicit_url}
144     ${command} =    Compose_Full_Java_Command    ${actual_java_options} -jar ${filename} ${tool_options}
145     ${logfile} =    Utils.Get_Log_File_Name    ${artifact}
146     SSHLibrary.Write    ${command} >${logfile} 2>&1
147     [Return]    ${logfile}
148
149 Compose_Dilemma_Filepath
150     [Arguments]    ${default_path}    ${specific_path}
151     [Documentation]    Query active SSH connection, return specific path if it exists else default path.
152     ${out}    ${rc} =    SSHLibrary.Execute_Command    ls -lA ${specific_path} 2>&1    return_rc=True
153     BuiltIn.Return_From_Keyword_If    ${rc} == 0    ${specific_path}
154     BuiltIn.Return_From_Keyword    ${default_path}
155
156 Compose_Base_Java_Command
157     [Arguments]    ${openjdk}=${JDKVERSION}
158     [Documentation]    Return string suitable for launching Java programs over SSHLibrary, depending on JRE version needed.
159     ...    This requires that the SSH connection on which the command is going to be used is active as it is needed for querying files.
160     ...    Commands composed for one SSH connection shall not be reused on other SSH connections as the two connections may have different Java setups.
161     ...    Not directly related to Nexus, but versioned Java tools may need this.
162     # Check whether the user set the override and return it if yes.
163     BuiltIn.Run_Keyword_And_Return_If    """${openjdk}""" == "openjdk8"    Compose_Dilemma_Filepath    ${JAVA_8_HOME_CENTOS}/bin/java    ${JAVA_8_HOME_UBUNTU}/bin/java
164     BuiltIn.Run_Keyword_And_Return_If    """${openjdk}""" == "openjdk7"    Compose_Dilemma_Filepath    ${JAVA_7_HOME_CENTOS}/bin/java    ${JAVA_7_HOME_UBUNTU}/bin/java
165     # Attempt to call plain "java" command directly. If it works, return it.
166     ${out}    ${rc} =    SSHLibrary.Execute_Command    java -version 2>&1    return_rc=True
167     BuiltIn.Return_From_Keyword_If    ${rc} == 0    java
168     # Query the virtual machine for the JAVA_HOME environment variable and
169     # use it to assemble a (hopefully) working command. If that worked out,
170     # return the result.
171     ${java} =    SSHLibrary.Execute_Command    echo \$JAVA_HOME/bin/java 2>&1
172     ${out}    ${rc} =    SSHLibrary.Execute_Command    ${java} -version 2>&1    return_rc=True
173     BuiltIn.Return_From_Keyword_If    ${rc} == 0    ${java}
174     # There are bizzare test environment setups where the (correct) JAVA_HOME
175     # is set in the VM where Robot is running but not in the VM where the
176     # tools are supposed to run (usually because these two are really one
177     # and the same system and idiosyncracies of BASH prevent easy injection
178     # of the JAVA_HOME environment variable into a place where connections
179     # made by SSHLibrary would pick it up). So try to use that value to
180     # create a java command and check that it works.
181     ${JAVA_HOME} =    OperatingSystem.Get_Environment_Variable    JAVA_HOME    ${EMPTY}
182     ${java} =    BuiltIn.Set_Variable_If    """${JAVA_HOME}"""!=""    ${JAVA_HOME}/bin/java    false
183     ${out}    ${rc} =    SSHLibrary.Execute_Command    ${java} -version 2>&1    return_rc=True
184     BuiltIn.Return_From_Keyword_If    ${rc} == 0    ${java}
185     # Nothing works, most likely java is not installed at all on the target
186     # machine or it is hopelesly lost. Bail out with a helpful message
187     # telling the user how to make it accessible for the script.
188     BuiltIn.Fail    Unable to find Java; specify \${JDKVERSION}, put it to your PATH or set JAVA_HOME environment variable.
189
190 Compose_Full_Java_Command
191     [Arguments]    ${options}    ${openjdk}=${JDKVERSION}
192     [Documentation]    Return full Bash command to run Java with given options.
193     ...    This requires that the SSH connection on which the command is going to be used is active as it is needed for querying files.
194     ...    The options may include JVM options, application command line arguments, Bash redirects and other constructs.
195     ${base_command} =    Compose_Base_Java_Command    openjdk=${openjdk}
196     ${full_command} =    BuiltIn.Set_Variable    ${base_command} ${options}
197     BuiltIn.Log    ${full_command}
198     [Return]    ${full_command}
199
200 Compose_Java_Home
201     [Arguments]    ${openjdk}=${JDKVERSION}
202     [Documentation]    Compose base java command and strip trailing "/bin/java".
203     ${java_command} =    Compose_Base_Java_Command
204     ${java_home}    ${bin}    ${java} =    String.Split_String_From_Right    ${java_command}    separator=/    max_split=2
205     [Return]    ${java_home}
206
207 Default_Java_Options
208     [Arguments]    ${openjdk}=${JDKVERSION}
209     [Documentation]    Return Java options suitable for current JDK version.
210     ${java_actual_options} =    BuiltIn.Set_Variable_If    """${openjdk}""" == "openjdk7"    ${JAVA_7_OPTIONS}    ${JAVA_OPTIONS}
211     [Return]    ${java_actual_options}
212
213 Install_Maven_Bare
214     [Arguments]    ${maven_version}=3.3.9    ${openjdk}=${JDKVERSION}
215     [Documentation]    Download and unpack Maven, prepare launch command with proper Java version and download settings file.
216     ...    This Keyword requires an active SSH connection to target machine.
217     ...    This Keyword sets global variables, so that suites can reuse existing installation.
218     ...    This Keyword can only place Maven (and settings) to remote current working directory.
219     ...    This Keyword does not perform any initial or final cleanup.
220     # Avoid multiple initialization by several downstream libraries.
221     ${installed_version} =    BuiltIn.Get_Variable_Value    \${Maven__installed_version}    None
222     BuiltIn.Return_From_Keyword_If    """${installed_version}""" == """${maven_version}"""
223     BuiltIn.Set_Global_Variable    \${Maven__installed_version}    ${maven_version}
224     BuiltIn.Set_Global_Variable    \${maven_directory}    apache-maven-${maven_version}
225     SSHKeywords.Execute_Command_At_Cwd_Should_Pass    rm -rf '${maven_directory}'
226     ${maven_archive_filename} =    BuiltIn.Set_Variable    ${maven_directory}-bin.tar.gz
227     ${maven_download_url} =    BuiltIn.Set_Variable    http://www-us.apache.org/dist/maven/maven-3/${maven_version}/binaries/${maven_archive_filename}
228     SSHKeywords.Execute_Command_At_Cwd_Should_Pass    wget -N '${maven_download_url}'    stderr_must_be_empty=False
229     SSHKeywords.Execute_Command_At_Cwd_Should_Pass    tar xvf '${maven_archive_filename}'
230     ${java_home} =    NexusKeywords.Compose_Java_Home    openjdk=${openjdk}
231     ${java_actual_options} =    Default_Java_Options    ${openjdk}
232     BuiltIn.Set_Global_Variable    \${maven_bash_command}    export JAVA_HOME='${java_home}' && export MAVEN_OPTS='${java_actual_options}' && ./${maven_directory}/bin/mvn
233     # TODO: Get settings files from Jenkins settings provider, somehow.
234     SSHKeywords.Execute_Command_At_Cwd_Should_Pass    wget '${MAVEN_SETTINGS_URL}' -O settings.xml    stderr_must_be_empty=False
235
236 Install_Maven
237     [Arguments]    ${maven_version}=3.3.9    ${openjdk}=${JDKVERSION}    ${branch}=${EMPTY}    ${patches}=${EMPTY}
238     [Documentation]    Install Maven.
239     ...    Depending on arguments, perform a multipatch build to populate local Maven repository with patched artifacts.
240     Install_Maven_Bare    maven_version=${maven_version}    openjdk=${openjdk}
241     BuiltIn.Return_From_Keyword_If    """${patches}""" == ""    No post-install build requested.
242     BuiltIn.Run_Keyword_If    """${branch}""" == ""    BuiltIn.Fail    BRANCH needs to be specified for multipatch builds.
243     ${script_name} =    BuiltIn.Set_Variable    include-raw-integration-multipatch-distribution-test.sh
244     ${script_url} =    BuiltIn.Set_Variable    https://raw.githubusercontent.com/opendaylight/releng-builder/master/jjb/integration/${script_name}
245     SSHKeywords.Execute_Command_At_Cwd_Should_Pass    wget -N '${script_url}'    stderr_must_be_empty=False
246     SSHKeywords.Execute_Command_At_Cwd_Should_Pass    export WORKSPACE='${WORKSPACE}' && export BRANCH='${branch}' && export PATCHES_TO_BUILD='${patches}' && bash '${script_name}'    stderr_must_be_empty=False
247     Run_Maven    pom_file=${WORKSPACE}/patch_tester/pom.xml
248
249 Run_Maven
250     [Arguments]    ${pom_file}=pom.xml    ${log_file}=${MAVEN_DEFAULT_OUTPUT_FILENAME}
251     [Documentation]    Determine arguments to use and call mvn command against given pom file.
252     SSHKeywords.Execute_Command_At_Cwd_Should_Pass    mkdir -p '${MAVEN_REPOSITORY_PATH}'
253     ${maven_repository_options} =    BuiltIn.Set_Variable    -Dmaven.repo.local=${MAVEN_REPOSITORY_PATH} -Dorg.ops4j.pax.url.mvn.localRepository=${MAVEN_REPOSITORY_PATH}
254     SSHKeywords.Execute_Command_At_Cwd_Should_Pass    ${maven_bash_command} clean install dependency:tree -V -B -DoutputFile=dependency_tree.log -s './settings.xml' -f '${pom_file}' ${MAVEN_OPTIONS} ${maven_repository_options} > '${log_file}'